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

Annotation of /trunk/libTES3FileFormat/PlugingClothing.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 17 - (hide annotations) (download)
Fri Jul 29 16:09:53 2011 UTC (8 years, 6 months ago) by william
File size: 8435 byte(s)
*** overhaul of code:
    +++ forget trying to parse the entire TES3 ESM/ESP plugin format
        just parse the clothing section

1 william 17 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    
13    
14     public interface IPlugingClothing : IParse, ICommit, IAsBytes, ISizeOffset
15     {
16    
17     }
18     public class PlugingClothing : IPlugingClothing, ICommitParseAsBytes
19     {
20     private ConsoleLog Log;
21     private const string CLOTHING_MAGIC = "CLOT";
22     private const string CLOTHING_NAME_MAGIC = "NAME";
23     MemoryStream reader = new MemoryStream();
24     public PlugingClothing()
25     {
26     this.reader = new MemoryStream();
27     this.Offset = 0;
28     this.Size = 0;
29     }
30     public PlugingClothing(BinaryReader br, int offset, int size, bool ReadOnly, ConsoleLog Log)
31     : this()
32     {
33     this.Log = Log;
34     br.BaseStream.Seek(offset, SeekOrigin.Begin);
35     this.Offset = offset;
36     this.Size = size;
37     this.reader = new MemoryStream(br.ReadBytes((int)br.BaseStream.Length), 0, size);
38     this.Parse(this.reader);
39     }
40    
41     #region IParse Members
42    
43     public bool Parse(MemoryStream stream)
44     {
45     BinaryReader br = new BinaryReader(stream);
46     this.Log.WriteLine();
47     // read header section magic
48    
49    
50     //bool NoMoreClothingEntries = false;
51    
52     //while (NoMoreClothingEntries != true)
53     //{
54     // // read the MAST tag
55     // byte[] magic = br.ReadBytes(4);
56     // string str_magic = ASCIIEncoding.ASCII.GetString(magic);
57     // StringBuilder magic_info_message = new StringBuilder();
58     // magic_info_message.Append("\tDetected Master Section: ");
59     // magic_info_message.Append(magic.ToStr());
60     // magic_info_message.Append(" : " + str_magic);
61    
62     // if (str_magic != PlugingClothing.CLOTHING_MAGIC)
63     // {
64     // NoMoreClothingEntries = true;
65     // br.BaseStream.Seek(br.BaseStream.Position - 4, SeekOrigin.Begin); // seek back 4 bytes in the stream
66     // break;
67     // }
68     // // throw assert if not true
69     // Debug.Assert(str_magic == PlugingClothing.CLOTHING_MAGIC, string.Format("Excepted: {0} found: {1}", PlugingClothing.CLOTHING_MAGIC, str_magic));
70    
71     // this.Log.WriteLine(magic_info_message.ToString());
72     // int MasterFileNameLength = br.ReadInt32();
73     // byte[] MasterFileName = br.ReadBytes(MasterFileNameLength);
74     // this.Log.WriteLine("\t\tFileName: " + MasterFileName.ToAscii());
75     // // read this master's data section
76     // magic = br.ReadBytes(4);
77     // str_magic = ASCIIEncoding.ASCII.GetString(magic);
78     // Debug.Assert(str_magic == PlugingClothing.CLOTHING_NAME_MAGIC, string.Format("Excepted: {0} found: {1}", PlugingClothing.CLOTHING_NAME_MAGIC, str_magic));
79     // int DataSectionLength = br.ReadInt32();
80     // byte[] Data = br.ReadBytes(DataSectionLength);
81     // this.Log.WriteLine("\t\tData: " + Data.ToStr());
82     //}
83    
84    
85     return true;
86     }
87    
88     #endregion
89    
90     #region ICommit Members
91    
92     public bool Commit(BinaryWriter writer)
93     {
94     throw new NotImplementedException();
95     }
96    
97     #endregion
98    
99     #region IAsBytes Members
100    
101     public byte[] AsBytes
102     {
103     get
104     {
105     this.reader.Seek(0, SeekOrigin.Begin);
106     BinaryReader br = new BinaryReader(this.reader);
107     return br.ReadBytes(this.Size);
108     }
109     }
110    
111     #endregion
112    
113     #region ISizeOffset Members
114    
115     private int _Size;
116     public int Size { get { return _Size; } private set { _Size = value; } } // headersize
117    
118     private int _Offset;
119     public int Offset { get { return _Offset; } private set { _Offset = value; } }
120     public int OffsetToNext { get { return this.Size + this.Offset; } } // force this to 16 bytes
121     #endregion
122    
123     #region Sub-Classes
124     public class SectionLocater : IParse, IOffset, ISize
125     {
126     private ConsoleLog Log;
127     MemoryStream reader = new MemoryStream();
128     public SectionLocater()
129     {
130     this.reader = new MemoryStream();
131     this.Log = new ConsoleLog();
132     }
133     public SectionLocater(BinaryReader br, int CurrentOffset, ConsoleLog Log) : this()
134     {
135     this.Log = Log;
136     br.BaseStream.Seek(0, SeekOrigin.Begin);
137     this.Offset = CurrentOffset;
138     this.reader = new MemoryStream(br.ReadBytes((int)br.BaseStream.Length), CurrentOffset, (int)br.BaseStream.Length - CurrentOffset);
139     this.Parse(this.reader);
140     }
141    
142     #region IParse Members
143    
144     public bool Parse(MemoryStream stream)
145     {
146     BinaryReader br = new BinaryReader(stream);
147     this.Log.WriteLine();
148    
149     byte[] data = br.ReadBytes((int)br.BaseStream.Length);
150    
151     bool FirstEntry = true;
152    
153     this.Offset = 0;
154    
155     int LastOffset = 0;
156     int ClothingSectionCount = 0;
157    
158     foreach (var position in data.Locate(ASCIIEncoding.ASCII.GetBytes(PlugingClothing.CLOTHING_MAGIC)))
159     {
160     if (FirstEntry)
161     {
162     this.Offset = position;
163     FirstEntry = false;
164     }
165     LastOffset = position;
166     ClothingSectionCount++;
167     }
168    
169     if (this.Offset != 0)
170     {
171     this.Log.WriteLine("\t\tFound First Clothing Section at offset: 0x" + this.Offset.ToString("x8"));
172     }
173     else
174     {
175     this.Log.WriteLine("\t\tNo Clothing Section Could be Found!");
176     }
177    
178     if (LastOffset != 0)
179     {
180     this.Log.WriteLine("\t\tFound Last Clothing Section at offset: 0x" + LastOffset.ToString("x8"));
181     br.BaseStream.Seek(LastOffset, SeekOrigin.Begin);
182    
183     byte[] magic = br.ReadBytes(4);
184     string str_magic = ASCIIEncoding.ASCII.GetString(magic);
185     Debug.Assert(str_magic == PlugingClothing.CLOTHING_MAGIC, string.Format("Excepted: {0} found: {1}", PlugingClothing.CLOTHING_MAGIC, str_magic));
186     int CLOTH_LENGTH = BitConverter.ToInt32(br.ReadBytes(12), 0);
187    
188     magic = br.ReadBytes(4);
189     str_magic = ASCIIEncoding.ASCII.GetString(magic);
190     Debug.Assert(str_magic == PlugingClothing.CLOTHING_NAME_MAGIC, string.Format("Excepted: {0} found: {1}", PlugingClothing.CLOTHING_NAME_MAGIC, str_magic));
191    
192     int b = (CLOTH_LENGTH + (int)br.BaseStream.Position) - PlugingClothing.CLOTHING_NAME_MAGIC.Length;
193    
194     this.Size = b - this.Offset;
195     this.Log.WriteLine("\t\tSize of All Clothing Sections (from first entry): 0x" + this.Size.ToString("x8"));
196     this.Log.WriteLine("\t\tCloting Section: Start=0x" + this.Offset.ToString("x8") + " End=0x" + (this.Offset + this.Size).ToString("x8"));
197     this.Log.WriteLine("\t\tTotal Clothing Sections: " + ClothingSectionCount.ToString());
198     }
199    
200     return true;
201     }
202    
203     #endregion
204    
205    
206    
207     #region IOffset Members
208     private int _Offset;
209     public int Offset
210     {
211     get { return _Offset; }
212     private set { _Offset = value; }
213     }
214     private int _Size;
215     public int Size
216     {
217     get { return _Size; }
218     private set { _Size = value; }
219     }
220     #endregion
221     }
222     #endregion
223     }
224     }

  ViewVC Help
Powered by ViewVC 1.1.22