/[MorrowindCustomClothingSlotCreator]/trunk/libTES3FileFormat/PlugingClothing.cs
ViewVC logotype

Contents of /trunk/libTES3FileFormat/PlugingClothing.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 18 - (show annotations) (download)
Fri Jul 29 16:54:40 2011 UTC (8 years, 7 months ago) by william
File size: 10983 byte(s)
*** parse CLOT section down to CTDT (clothing data)

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO;
6 using System.Diagnostics;
7 using System.Windows.Forms;
8
9 namespace libTES3FileFormat
10 {
11
12 public enum ClothingType
13 {
14 Pants,
15 Shoes,
16 Shirt,
17 Belt,
18 Robe,
19 RightGlove,
20 LeftGlove,
21 Skirt,
22 Ring,
23 Amulet,
24 }
25
26 public interface IPlugingClothing : IParse, ICommit, IAsBytes, ISizeOffset
27 {
28
29 }
30 public class PlugingClothing : IPlugingClothing, ICommitParseAsBytes
31 {
32 private ConsoleLog Log;
33 private const string CLOTHING_MAGIC = "CLOT";
34 private const string CLOTHING_NAME_MAGIC = "NAME";
35 MemoryStream reader = new MemoryStream();
36 private int SectionCount = 0;
37 public PlugingClothing()
38 {
39 this.reader = new MemoryStream();
40 this.Offset = 0;
41 this.Size = 0;
42 }
43 public PlugingClothing(BinaryReader br, int offset, int size, int SectionCount, bool ReadOnly, ConsoleLog Log)
44 : this()
45 {
46 this.Log = Log;
47 br.BaseStream.Seek(offset, SeekOrigin.Begin);
48 this.Offset = offset;
49 this.Size = size;
50 this.SectionCount = SectionCount;
51 this.reader = new MemoryStream(br.ReadBytes((int)br.BaseStream.Length), 0, size);
52 this.Parse(this.reader);
53 }
54
55 #region IParse Members
56
57 public bool Parse(MemoryStream stream)
58 {
59 BinaryReader br = new BinaryReader(stream);
60 this.Log.WriteLine();
61 // read header section magic
62
63 for (int i = 0; i < this.SectionCount; i++)
64 {
65
66 this.Log.WriteLine(string.Format("Clothing Section: Index=0x{0:x8} Offset=0x{1:x8}", i,(int)br.BaseStream.Position));
67
68 byte[] magic = br.ReadBytes(4);
69 string str_magic = ASCIIEncoding.ASCII.GetString(magic);
70 Debug.Assert(str_magic == "CLOT", string.Format("Excepted: {0} found: {1}", "CLOT", str_magic));
71
72 // Clothing Name
73 int section_size = BitConverter.ToInt32(br.ReadBytes(12), 0);
74 magic = br.ReadBytes(4);
75 str_magic = ASCIIEncoding.ASCII.GetString(magic);
76 Debug.Assert(str_magic == "NAME", string.Format("Excepted: {0} found: {1}", "NAME", str_magic));
77
78 int name_size = br.ReadInt32();
79 this.Log.WriteLine("\t\tClothing Name Size: " + name_size.ToString());
80 byte[] name_bytes = br.ReadBytes(name_size);
81 string ClothingName = ASCIIEncoding.ASCII.GetString(name_bytes);
82 this.Log.WriteLine("\t\tClothing Name : " + ClothingName);
83
84
85 // Clothing Model
86 magic = br.ReadBytes(4);
87 str_magic = ASCIIEncoding.ASCII.GetString(magic);
88 Debug.Assert(str_magic == "MODL", string.Format("Excepted: {0} found: {1}", "MODL", str_magic));
89
90 int modl_name_size = br.ReadInt32();
91 this.Log.WriteLine("\t\tClothing Model Name Size: " + modl_name_size.ToString());
92 byte[] modl_name_bytes = br.ReadBytes(modl_name_size);
93 string ClothingModelName = ASCIIEncoding.ASCII.GetString(modl_name_bytes);
94 this.Log.WriteLine("\t\tClothing Model Name: " + ClothingModelName);
95
96 // Clothing Full Name
97 magic = br.ReadBytes(4);
98 str_magic = ASCIIEncoding.ASCII.GetString(magic);
99 Debug.Assert(str_magic == "FNAM", string.Format("Excepted: {0} found: {1}", "FNAM", str_magic));
100
101 int fname_size = br.ReadInt32();
102 this.Log.WriteLine("\t\tClothing FullName Size: " + fname_size.ToString());
103 byte[] fname_bytes = br.ReadBytes(fname_size);
104 string ClothingFullName = ASCIIEncoding.ASCII.GetString(fname_bytes);
105 this.Log.WriteLine("\t\tClothing FullName: " + ClothingFullName);
106
107 // Clothing Data
108 magic = br.ReadBytes(4);
109 str_magic = ASCIIEncoding.ASCII.GetString(magic);
110 Debug.Assert(str_magic == "CTDT", string.Format("Excepted: {0} found: {1}", "CTDT", str_magic));
111
112 int ctdt_size = br.ReadInt32();
113
114 int ctdt_type = br.ReadInt32();
115 string ctdt_type_string = "";
116
117 try
118 {
119 ctdt_type_string = Enum.GetName(typeof(ClothingType), ctdt_type);
120 }
121 catch { ctdt_type_string = "unknown"; }
122
123
124 float ctdt_weight = br.ReadSingle();
125 short ctdt_value = br.ReadInt16();
126 short ctdt_echant_pts = br.ReadInt16();
127
128 this.Log.WriteLine("\t\tClothing Data: ");
129 this.Log.WriteLine("\t\t\tType: " + ctdt_type + " (" + ctdt_type_string +")");
130 this.Log.WriteLine("\t\t\tWeight: " + ctdt_weight);
131 this.Log.WriteLine("\t\t\tValue: " + ctdt_value);
132 this.Log.WriteLine("\t\t\tEnchant Points: " + ctdt_echant_pts);
133
134 magic = br.ReadBytes(4);
135 str_magic = ASCIIEncoding.ASCII.GetString(magic);
136 Debug.Assert(str_magic == "SCRI", string.Format("Excepted: {0} found: {1}", "SCRI", str_magic));
137
138 break;
139
140 }
141
142
143 return true;
144 }
145
146 #endregion
147
148 #region ICommit Members
149
150 public bool Commit(BinaryWriter writer)
151 {
152 throw new NotImplementedException();
153 }
154
155 #endregion
156
157 #region IAsBytes Members
158
159 public byte[] AsBytes
160 {
161 get
162 {
163 this.reader.Seek(0, SeekOrigin.Begin);
164 BinaryReader br = new BinaryReader(this.reader);
165 return br.ReadBytes(this.Size);
166 }
167 }
168
169 #endregion
170
171 #region ISizeOffset Members
172
173 private int _Size;
174 public int Size { get { return _Size; } private set { _Size = value; } } // headersize
175
176 private int _Offset;
177 public int Offset { get { return _Offset; } private set { _Offset = value; } }
178 public int OffsetToNext { get { return this.Size + this.Offset; } } // force this to 16 bytes
179 #endregion
180
181 #region Sub-Classes
182 public class SectionLocater : IParse, IOffset, ISize
183 {
184 private ConsoleLog Log;
185 MemoryStream reader = new MemoryStream();
186 public SectionLocater()
187 {
188 this.reader = new MemoryStream();
189 this.Log = new ConsoleLog();
190 }
191 public SectionLocater(BinaryReader br, int CurrentOffset, ConsoleLog Log) : this()
192 {
193 this.Log = Log;
194 br.BaseStream.Seek(0, SeekOrigin.Begin);
195 this.Offset = CurrentOffset;
196 this.reader = new MemoryStream(br.ReadBytes((int)br.BaseStream.Length), CurrentOffset, (int)br.BaseStream.Length - CurrentOffset);
197 this.Parse(this.reader);
198 }
199
200 #region IParse Members
201
202 public bool Parse(MemoryStream stream)
203 {
204 BinaryReader br = new BinaryReader(stream);
205 this.Log.WriteLine();
206
207 byte[] data = br.ReadBytes((int)br.BaseStream.Length);
208
209 bool FirstEntry = true;
210
211 this.Offset = 0;
212
213 int LastOffset = 0;
214 int ClothingSectionCount = 0;
215
216 foreach (var position in data.Locate(ASCIIEncoding.ASCII.GetBytes(PlugingClothing.CLOTHING_MAGIC)))
217 {
218 if (FirstEntry)
219 {
220 this.Offset = position;
221 FirstEntry = false;
222 }
223 LastOffset = position;
224 ClothingSectionCount++;
225 }
226
227 if (this.Offset != 0)
228 {
229 this.Log.WriteLine("\t\tFound First Clothing Section at offset: 0x" + this.Offset.ToString("x8"));
230 }
231 else
232 {
233 this.Log.WriteLine("\t\tNo Clothing Section Could be Found!");
234 }
235
236 if (LastOffset != 0)
237 {
238 this.Log.WriteLine("\t\tFound Last Clothing Section at offset: 0x" + LastOffset.ToString("x8"));
239 br.BaseStream.Seek(LastOffset, SeekOrigin.Begin);
240
241 byte[] magic = br.ReadBytes(4);
242 string str_magic = ASCIIEncoding.ASCII.GetString(magic);
243 Debug.Assert(str_magic == PlugingClothing.CLOTHING_MAGIC, string.Format("Excepted: {0} found: {1}", PlugingClothing.CLOTHING_MAGIC, str_magic));
244 int CLOTH_LENGTH = BitConverter.ToInt32(br.ReadBytes(12), 0);
245
246 magic = br.ReadBytes(4);
247 str_magic = ASCIIEncoding.ASCII.GetString(magic);
248 Debug.Assert(str_magic == PlugingClothing.CLOTHING_NAME_MAGIC, string.Format("Excepted: {0} found: {1}", PlugingClothing.CLOTHING_NAME_MAGIC, str_magic));
249
250 int b = (CLOTH_LENGTH + (int)br.BaseStream.Position) - PlugingClothing.CLOTHING_NAME_MAGIC.Length;
251
252 this.Size = b - this.Offset;
253 this.SectionCount = ClothingSectionCount;
254 this.Log.WriteLine("\t\tSize of All Clothing Sections (from first entry): 0x" + this.Size.ToString("x8"));
255 this.Log.WriteLine("\t\tCloting Section: Start=0x" + this.Offset.ToString("x8") + " End=0x" + (this.Offset + this.Size).ToString("x8"));
256 this.Log.WriteLine("\t\tTotal Clothing Sections: " + this.SectionCount.ToString());
257 }
258
259 return true;
260 }
261
262 #endregion
263
264
265
266 #region IOffset Members
267 private int _Offset;
268 public int Offset
269 {
270 get { return _Offset; }
271 private set { _Offset = value; }
272 }
273 private int _Size;
274 public int Size
275 {
276 get { return _Size; }
277 private set { _Size = value; }
278 }
279 private int _SectionCount;
280 public int SectionCount
281 {
282 get { return _SectionCount; }
283 private set { _SectionCount = value; }
284 }
285 #endregion
286 }
287 #endregion
288 }
289 }

  ViewVC Help
Powered by ViewVC 1.1.22