/[IPackageUtils]/trunk/IPackageUtils/IPackageValidator.cs
ViewVC logotype

Contents of /trunk/IPackageUtils/IPackageValidator.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 30 - (show annotations) (download)
Thu Dec 24 11:22:13 2015 UTC (3 years, 11 months ago) by william
File size: 6528 byte(s)
+ update to support Sims4 package format (V2.1)
1 using System;
2 using System.Collections.Generic;
3 using System.Collections;
4 using System.Text;
5
6 namespace IPackageUtils
7 {
8 public interface IIVersion
9 {
10 int MAJOR { get; }
11 int MINOR { get; }
12 int REVSION { get; }
13 int BUILD { get; }
14 string ToString();
15 }
16 public class IVersion : IIVersion
17 {
18 public IVersion()
19 {
20 this.m_MAJOR = 0;
21 this.m_MINOR = 0;
22 this.m_REVSION = 0;
23 this.m_BUILD = 0;
24 }
25 public IVersion(ArrayList Version)
26 {
27 if (!(Version.Count == 4))
28 {
29 throw new ArgumentOutOfRangeException("Input: Version requires excatly 4 elements.");
30 }
31 else
32 {
33 this.m_MAJOR = Convert.ToInt32(Version[0]);
34 this.m_MINOR = Convert.ToInt32(Version[1]);
35 this.m_REVSION = Convert.ToInt32(Version[2]);
36 this.m_BUILD = Convert.ToInt32(Version[3]);
37 }
38 }
39 public IVersion(string Version)
40 {
41 string[] Version_Array = Version.Split('.');
42 if (!(Version_Array.Length == 4))
43 {
44 throw new ArgumentOutOfRangeException("Input: Version requires a string informat MAJOR.MINOR.REVISION.BUILD (0.0.0.0) .");
45 }
46 else
47 {
48 this.m_MAJOR = Convert.ToInt32(Version_Array[0]);
49 this.m_MINOR = Convert.ToInt32(Version_Array[1]);
50 this.m_REVSION = Convert.ToInt32(Version_Array[2]);
51 this.m_BUILD = Convert.ToInt32(Version_Array[3]);
52 }
53 }
54
55 #region IIVersion Members
56 private int m_MAJOR;
57 public int MAJOR
58 {
59 get { return m_MAJOR; }
60 }
61 private int m_MINOR;
62 public int MINOR
63 {
64 get { return m_MINOR; }
65 }
66 private int m_REVSION;
67 public int REVSION
68 {
69 get { return m_REVSION; }
70 }
71 private int m_BUILD;
72 public int BUILD
73 {
74 get { return m_BUILD; }
75 }
76 public override string ToString()
77 {
78 return String.Format("{0}.{1}.{2}.{3}", MAJOR, MINOR, BUILD, REVSION);
79 }
80 #endregion
81
82 }
83
84 public interface IIPackageValidator
85 {
86 bool WasPackageLoaded { get; }
87
88 IVersion PackageVersion { get; }
89 bool IsValidPackage { get; }
90 bool IsEncryptedPackage { get; }
91
92 void Dispose();
93 }
94
95 public class IPackageValidator : IIPackageValidator
96 {
97 private FileMagic.MagicValidator MyMagicValidator;
98 private string PackageFile;
99
100 public IPackageValidator()
101 {
102 this.m_PackageVersion = new IVersion();
103 this.m_IsValidPackage = false;
104 this.m_IsEncryptedPackage = false;
105 this.PackageFile = "";
106 this.m_WasPackageLoaded = false;
107 }
108
109 public IPackageValidator(string filename) : this()
110 {
111 try
112 {
113 if (System.IO.File.Exists(filename))
114 {
115 this.PackageFile = filename;
116 this.ValidatePackage();
117
118 if (this.IsValidPackage && !this.IsEncryptedPackage)
119 {
120 this.m_PackageVersion = new IVersion(this.GetPackageVersion());
121 this.m_WasPackageLoaded = true;
122 }
123 else
124 {
125 this.m_PackageVersion = new IVersion();
126 this.m_WasPackageLoaded = false;
127 }
128 this.Dispose();
129 }
130 else
131 {
132 this.m_WasPackageLoaded = false;
133 this.Dispose();
134 //throw new System.IO.FileNotFoundException(filename);
135 }
136 }
137 catch
138 {
139 this.m_PackageVersion = new IVersion();
140 this.m_IsValidPackage = false;
141 this.m_IsEncryptedPackage = false;
142 this.PackageFile = "";
143 this.m_WasPackageLoaded = false;
144 this.Dispose();
145 }
146
147 }
148
149
150 #region IIPackageValidator Members
151 private bool m_WasPackageLoaded;
152
153 public bool WasPackageLoaded
154 {
155 get { return m_WasPackageLoaded; }
156 }
157
158 private IVersion m_PackageVersion;
159 public IVersion PackageVersion
160 {
161 get { return m_PackageVersion; }
162 }
163
164 private bool m_IsValidPackage;
165 public bool IsValidPackage
166 {
167 get { return m_IsValidPackage; }
168 }
169
170 private bool m_IsEncryptedPackage;
171 public bool IsEncryptedPackage
172 {
173 get { return m_IsEncryptedPackage; }
174 }
175
176 public void Dispose()
177 {
178 try
179 {
180 if (this.MyMagicValidator != null)
181 {
182 this.MyMagicValidator.dispose();
183 }
184 this.MyMagicValidator = null;
185 }
186 catch (NullReferenceException)
187 {
188 // ignore
189 }
190 catch (Exception)
191 {
192 // ignore
193 }
194 }
195
196
197 #endregion
198
199 #region Package Validation Methods
200 private void ValidatePackage()
201 {
202 this.MyMagicValidator = new FileMagic.MagicValidator(this.PackageFile, "DBPP");
203 this.m_IsEncryptedPackage = MyMagicValidator.ValidateMagic();
204
205 if (this.m_IsEncryptedPackage)
206 {
207 this.m_IsValidPackage = false;
208 return;
209 }
210
211 this.MyMagicValidator = new FileMagic.MagicValidator(this.PackageFile, "DBPF");
212 this.m_IsValidPackage = MyMagicValidator.ValidateMagic();
213 }
214 private ArrayList GetPackageVersion()
215 {
216 if (this.MyMagicValidator == null) { return new ArrayList() { "0", "0", "0", "0" }; }
217 return this.MyMagicValidator.GetFileVersion(sizeof(int) * 1, sizeof(int) * 1);
218 }
219 #endregion
220 }
221 }

  ViewVC Help
Powered by ViewVC 1.1.22