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

Contents of /trunk/libTES3FileFormat/PlugingClothing.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 19 - (show annotations) (download)
Fri Jul 29 17:39:39 2011 UTC (8 years, 6 months ago) by william
File size: 10084 byte(s)
*** implement parsing of all clothing entries ... they might not be all in one section

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 = "\0CLOT";
34 private const string CLOTHING_NAME_MAGIC = "NAME";
35 MemoryStream reader = new MemoryStream();
36 private List<int> SectionOffsets = new List<int>();
37 public PlugingClothing()
38 {
39 this.reader = new MemoryStream();
40 this.Offset = 0;
41 this.Size = 0;
42 this.SectionOffsets = new List<int>();
43 }
44 public PlugingClothing(BinaryReader br, List<int> SectionOffsets, bool ReadOnly, ConsoleLog Log)
45 : this()
46 {
47 this.Log = Log;
48 br.BaseStream.Seek(0, SeekOrigin.Begin);
49 //this.Offset = offset;
50 //this.Size = size;
51 this.SectionOffsets = SectionOffsets;
52 this.reader = new MemoryStream(br.ReadBytes((int)br.BaseStream.Length), 0, (int)br.BaseStream.Length);
53 this.Parse(this.reader);
54 }
55
56 #region IParse Members
57
58 public bool Parse(MemoryStream stream)
59 {
60 BinaryReader br = new BinaryReader(stream);
61 this.Log.WriteLine();
62 // read header section magic
63
64 for (int i = 0; i < this.SectionOffsets.Count; i++)
65 {
66 br.BaseStream.Seek(this.SectionOffsets[i], SeekOrigin.Begin);
67 this.Log.WriteLine(string.Format("Clothing Section: Index=0x{0:x8} Offset=0x{1:x8}", i,(int)br.BaseStream.Position));
68
69 byte[] magic = br.ReadBytes(4);
70 string str_magic = ASCIIEncoding.ASCII.GetString(magic);
71 Debug.Assert(str_magic == "CLOT", string.Format("Excepted: {0} found: {1}", "CLOT", str_magic));
72
73 // Clothing Name
74 int section_size = BitConverter.ToInt32(br.ReadBytes(12), 0);
75 int name_offset = (int)br.BaseStream.Position;
76 magic = br.ReadBytes(4);
77 str_magic = ASCIIEncoding.ASCII.GetString(magic);
78 Debug.Assert(str_magic == "NAME", string.Format("Excepted: {0} found: {1}", "NAME", str_magic));
79
80 int name_size = br.ReadInt32();
81 this.Log.WriteLine("\t\tClothing Name Size: " + name_size.ToString());
82 byte[] name_bytes = br.ReadBytes(name_size);
83 string ClothingName = ASCIIEncoding.ASCII.GetString(name_bytes);
84 this.Log.WriteLine("\t\tClothing Name : " + ClothingName);
85
86
87 // Clothing Model
88 magic = br.ReadBytes(4);
89 str_magic = ASCIIEncoding.ASCII.GetString(magic);
90 Debug.Assert(str_magic == "MODL", string.Format("Excepted: {0} found: {1}", "MODL", str_magic));
91
92 int modl_name_size = br.ReadInt32();
93 this.Log.WriteLine("\t\tClothing Model Name Size: " + modl_name_size.ToString());
94 byte[] modl_name_bytes = br.ReadBytes(modl_name_size);
95 string ClothingModelName = ASCIIEncoding.ASCII.GetString(modl_name_bytes);
96 this.Log.WriteLine("\t\tClothing Model Name: " + ClothingModelName);
97
98 // Clothing Full Name
99 magic = br.ReadBytes(4);
100 str_magic = ASCIIEncoding.ASCII.GetString(magic);
101 Debug.Assert(str_magic == "FNAM", string.Format("Excepted: {0} found: {1}", "FNAM", str_magic));
102
103 int fname_size = br.ReadInt32();
104 this.Log.WriteLine("\t\tClothing FullName Size: " + fname_size.ToString());
105 byte[] fname_bytes = br.ReadBytes(fname_size);
106 string ClothingFullName = ASCIIEncoding.ASCII.GetString(fname_bytes);
107 this.Log.WriteLine("\t\tClothing FullName: " + ClothingFullName);
108
109 // Clothing Data
110 magic = br.ReadBytes(4);
111 str_magic = ASCIIEncoding.ASCII.GetString(magic);
112 Debug.Assert(str_magic == "CTDT", string.Format("Excepted: {0} found: {1}", "CTDT", str_magic));
113
114 int ctdt_size = br.ReadInt32();
115
116 int ctdt_type = br.ReadInt32();
117 string ctdt_type_string = "";
118
119 try
120 {
121 ctdt_type_string = Enum.GetName(typeof(ClothingType), ctdt_type);
122 }
123 catch { ctdt_type_string = "unknown"; }
124
125
126 float ctdt_weight = br.ReadSingle();
127 short ctdt_value = br.ReadInt16();
128 short ctdt_echant_pts = br.ReadInt16();
129
130 this.Log.WriteLine("\t\tClothing Data: ");
131 this.Log.WriteLine("\t\t\tType: " + ctdt_type + " (" + ctdt_type_string +")");
132 this.Log.WriteLine("\t\t\tWeight: " + ctdt_weight);
133 this.Log.WriteLine("\t\t\tValue: " + ctdt_value);
134 this.Log.WriteLine("\t\t\tEnchant Points: " + ctdt_echant_pts);
135
136
137
138 //magic = br.ReadBytes(4);
139 //str_magic = ASCIIEncoding.ASCII.GetString(magic);
140 //Debug.Assert(str_magic == "SCRI", string.Format("Excepted: {0} found: {1}", "SCRI", str_magic));
141 //br.BaseStream.Position = br.BaseStream.Position - 4; // back up 4 bytes
142 //int length_till_end_of_section = (name_offset + section_size) + 0x10;
143 int length_till_end_of_section = (name_offset + section_size) - (int)br.BaseStream.Position;
144
145
146 try
147 {
148 byte[] extra_clothing_data = br.ReadBytes(length_till_end_of_section);
149 }
150 catch { break; }
151
152 //break;
153
154 }
155
156
157 return true;
158 }
159
160 #endregion
161
162 #region ICommit Members
163
164 public bool Commit(BinaryWriter writer)
165 {
166 throw new NotImplementedException();
167 }
168
169 #endregion
170
171 #region IAsBytes Members
172
173 public byte[] AsBytes
174 {
175 get
176 {
177 this.reader.Seek(0, SeekOrigin.Begin);
178 BinaryReader br = new BinaryReader(this.reader);
179 return br.ReadBytes(this.Size);
180 }
181 }
182
183 #endregion
184
185 #region ISizeOffset Members
186
187 private int _Size;
188 public int Size { get { return _Size; } private set { _Size = value; } } // headersize
189
190 private int _Offset;
191 public int Offset { get { return _Offset; } private set { _Offset = value; } }
192 public int OffsetToNext { get { return this.Size + this.Offset; } } // force this to 16 bytes
193 #endregion
194
195 #region Sub-Classes
196 public class SectionLocater : IParse, IOffset, ISize
197 {
198 private ConsoleLog Log;
199 MemoryStream reader = new MemoryStream();
200 public SectionLocater()
201 {
202 this.reader = new MemoryStream();
203 this.Log = new ConsoleLog();
204 this.ClothingSectionOffsets = new List<int>();
205 }
206 public SectionLocater(BinaryReader br, int CurrentOffset, ConsoleLog Log) : this()
207 {
208 this.Log = Log;
209 br.BaseStream.Seek(0, SeekOrigin.Begin);
210 this.Offset = CurrentOffset;
211 this.reader = new MemoryStream(br.ReadBytes((int)br.BaseStream.Length), CurrentOffset, (int)br.BaseStream.Length - CurrentOffset);
212 this.Parse(this.reader);
213 }
214
215 #region IParse Members
216
217 public bool Parse(MemoryStream stream)
218 {
219 BinaryReader br = new BinaryReader(stream);
220 this.Log.WriteLine();
221
222 byte[] data = br.ReadBytes((int)br.BaseStream.Length);
223
224
225 this.ClothingSectionOffsets = new List<int>();
226
227 foreach (var position in data.Locate(ASCIIEncoding.ASCII.GetBytes(PlugingClothing.CLOTHING_MAGIC)))
228 {
229 this.ClothingSectionOffsets.Add(position + 0x1); // add 1 byte to remove the \0,
230 }
231
232 if (this.ClothingSectionOffsets.Count == 0)
233 {
234 this.Log.WriteLine("\t\tNo Clothing Section Could be Found!");
235 }
236 else
237 {
238 this.Log.WriteLine("\t\tFound 0x" + this.ClothingSectionOffsets.Count.ToString("x2") + "clothing sections");
239 }
240
241 return true;
242 }
243
244 #endregion
245
246
247
248 #region IOffset Members
249 private int _Offset;
250 public int Offset
251 {
252 get { return _Offset; }
253 private set { _Offset = value; }
254 }
255 private int _Size;
256 public int Size
257 {
258 get { return _Size; }
259 private set { _Size = value; }
260 }
261 private List<int> _ClothingSectionOffsets;
262 public List<int> ClothingSectionOffsets
263 {
264 get { return _ClothingSectionOffsets; }
265 private set { _ClothingSectionOffsets = value; }
266 }
267 #endregion
268 }
269 #endregion
270 }
271 }

  ViewVC Help
Powered by ViewVC 1.1.22