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

Diff of /trunk/libTES3FileFormat/PlugingClothing.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/libTES3FileFormat/PlugingClothing.cs	2011/07/29 16:54:40	18
+++ trunk/libTES3FileFormat/PlugingClothing.cs	2011/07/29 17:39:39	19
@@ -30,25 +30,26 @@
     public class PlugingClothing : IPlugingClothing, ICommitParseAsBytes
     {
         private ConsoleLog Log;
-        private const string CLOTHING_MAGIC = "CLOT";
+        private const string CLOTHING_MAGIC = "\0CLOT";
         private const string CLOTHING_NAME_MAGIC = "NAME";
         MemoryStream reader = new MemoryStream();
-        private int SectionCount = 0;
+        private List<int> SectionOffsets = new List<int>();
         public PlugingClothing()
         {
             this.reader = new MemoryStream();
             this.Offset = 0;
             this.Size = 0;
+            this.SectionOffsets = new List<int>();
         }
-        public PlugingClothing(BinaryReader br, int offset, int size, int SectionCount, bool ReadOnly, ConsoleLog Log)
+        public PlugingClothing(BinaryReader br, List<int> SectionOffsets, bool ReadOnly, ConsoleLog Log)
             : this()
         {
             this.Log = Log;
-            br.BaseStream.Seek(offset, SeekOrigin.Begin);
-            this.Offset = offset;
-            this.Size = size;
-            this.SectionCount = SectionCount;
-            this.reader = new MemoryStream(br.ReadBytes((int)br.BaseStream.Length), 0, size);
+            br.BaseStream.Seek(0, SeekOrigin.Begin);
+            //this.Offset = offset;
+            //this.Size = size;
+            this.SectionOffsets = SectionOffsets;
+            this.reader = new MemoryStream(br.ReadBytes((int)br.BaseStream.Length), 0, (int)br.BaseStream.Length);
             this.Parse(this.reader);
         }
 
@@ -60,9 +61,9 @@
             this.Log.WriteLine();
             // read header section magic
 
-            for (int i = 0; i < this.SectionCount; i++)
+            for (int i = 0; i < this.SectionOffsets.Count; i++)
             {
-
+                br.BaseStream.Seek(this.SectionOffsets[i], SeekOrigin.Begin);
                 this.Log.WriteLine(string.Format("Clothing Section: Index=0x{0:x8} Offset=0x{1:x8}", i,(int)br.BaseStream.Position));
 
                 byte[] magic = br.ReadBytes(4);
@@ -71,6 +72,7 @@
 
                 // Clothing Name
                 int section_size = BitConverter.ToInt32(br.ReadBytes(12), 0);
+                int name_offset = (int)br.BaseStream.Position;
                 magic = br.ReadBytes(4);
                 str_magic = ASCIIEncoding.ASCII.GetString(magic);
                 Debug.Assert(str_magic == "NAME", string.Format("Excepted: {0} found: {1}", "NAME", str_magic));
@@ -131,11 +133,23 @@
                 this.Log.WriteLine("\t\t\tValue: " + ctdt_value);
                 this.Log.WriteLine("\t\t\tEnchant Points: " + ctdt_echant_pts);
 
-                magic = br.ReadBytes(4);
-                str_magic = ASCIIEncoding.ASCII.GetString(magic);
-                Debug.Assert(str_magic == "SCRI", string.Format("Excepted: {0} found: {1}", "SCRI", str_magic));
+                
+
+                //magic = br.ReadBytes(4);
+                //str_magic = ASCIIEncoding.ASCII.GetString(magic);
+                //Debug.Assert(str_magic == "SCRI", string.Format("Excepted: {0} found: {1}", "SCRI", str_magic));
+                //br.BaseStream.Position = br.BaseStream.Position - 4; // back up 4 bytes
+                //int length_till_end_of_section = (name_offset + section_size) + 0x10;
+                int length_till_end_of_section = (name_offset + section_size) - (int)br.BaseStream.Position;
+
+
+                try
+                {
+                    byte[] extra_clothing_data = br.ReadBytes(length_till_end_of_section);
+                }
+                catch { break; }
 
-                break;
+                //break;
 
             }
                   
@@ -187,6 +201,7 @@
             {
                 this.reader = new MemoryStream();
                 this.Log = new ConsoleLog();
+                this.ClothingSectionOffsets = new List<int>();
             }
             public SectionLocater(BinaryReader br, int CurrentOffset, ConsoleLog Log) : this()
             {
@@ -205,56 +220,23 @@
                 this.Log.WriteLine();
 
                 byte[] data = br.ReadBytes((int)br.BaseStream.Length);
+                              
 
-                bool FirstEntry = true;
-
-                this.Offset = 0;
-
-                int LastOffset = 0;
-                int ClothingSectionCount = 0;
+                this.ClothingSectionOffsets = new List<int>();
 
                 foreach (var position in data.Locate(ASCIIEncoding.ASCII.GetBytes(PlugingClothing.CLOTHING_MAGIC)))
                 {
-                    if (FirstEntry)
-                    {
-                        this.Offset = position;
-                        FirstEntry = false;
-                    }
-                    LastOffset = position;
-                    ClothingSectionCount++;
+                    this.ClothingSectionOffsets.Add(position + 0x1); // add 1 byte to remove the \0,
                 }
 
-                if (this.Offset != 0)
-                {
-                    this.Log.WriteLine("\t\tFound First Clothing Section at offset: 0x" + this.Offset.ToString("x8"));
-                }
-                else
+                if (this.ClothingSectionOffsets.Count == 0)
                 {
                     this.Log.WriteLine("\t\tNo Clothing Section Could be Found!");
                 }
-
-                if (LastOffset != 0)
+                else
                 {
-                    this.Log.WriteLine("\t\tFound Last Clothing Section at offset: 0x" + LastOffset.ToString("x8"));
-                    br.BaseStream.Seek(LastOffset, SeekOrigin.Begin);
-
-                    byte[] magic = br.ReadBytes(4);
-                    string str_magic = ASCIIEncoding.ASCII.GetString(magic);
-                    Debug.Assert(str_magic == PlugingClothing.CLOTHING_MAGIC, string.Format("Excepted: {0} found: {1}", PlugingClothing.CLOTHING_MAGIC, str_magic));
-                    int CLOTH_LENGTH = BitConverter.ToInt32(br.ReadBytes(12), 0);
-
-                    magic = br.ReadBytes(4);
-                    str_magic = ASCIIEncoding.ASCII.GetString(magic);
-                    Debug.Assert(str_magic == PlugingClothing.CLOTHING_NAME_MAGIC, string.Format("Excepted: {0} found: {1}", PlugingClothing.CLOTHING_NAME_MAGIC, str_magic));
-
-                    int b = (CLOTH_LENGTH + (int)br.BaseStream.Position) - PlugingClothing.CLOTHING_NAME_MAGIC.Length;
-
-                    this.Size = b - this.Offset;
-                    this.SectionCount = ClothingSectionCount;
-                    this.Log.WriteLine("\t\tSize of All Clothing Sections (from first entry): 0x" + this.Size.ToString("x8"));
-                    this.Log.WriteLine("\t\tCloting Section: Start=0x" + this.Offset.ToString("x8") + " End=0x" + (this.Offset + this.Size).ToString("x8"));
-                    this.Log.WriteLine("\t\tTotal Clothing Sections: " + this.SectionCount.ToString());
-                }
+                    this.Log.WriteLine("\t\tFound 0x" + this.ClothingSectionOffsets.Count.ToString("x2") + "clothing sections");
+                }              
 
                 return true;
             }
@@ -276,11 +258,11 @@
                 get { return _Size; }
                 private set { _Size = value; }
             }
-            private int _SectionCount;
-            public int SectionCount
+            private List<int> _ClothingSectionOffsets;
+            public List<int> ClothingSectionOffsets
             {
-                get { return _SectionCount; }
-                private set { _SectionCount = value; }
+                get { return _ClothingSectionOffsets; }
+                private set { _ClothingSectionOffsets = value; }
             }
             #endregion
         }

 

  ViewVC Help
Powered by ViewVC 1.1.22