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

Contents of /trunk/libTES3FileFormat/PluginMaster.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 33 - (show annotations) (download)
Sat Jul 30 11:47:53 2011 UTC (8 years, 6 months ago) by william
File size: 4265 byte(s)
*** work out committing individual entries back into the main data stream

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
8 namespace libTES3FileFormat
9 {
10 public interface IPluginMaster : IParse, ICommit, IAsBytes, ISizeOffset
11 {
12
13 }
14 public class PluginMaster : IPluginMaster, ICommitParseAsBytes
15 {
16 private ConsoleLog Log;
17 private const string MASTER_MAGIC = "MAST";
18 private const string MASTER_DATA_MAGIC = "DATA";
19 MemoryStream reader = new MemoryStream();
20 public PluginMaster()
21 {
22 this.reader = new MemoryStream();
23 this.Offset = 0;
24 this.Size = 0;
25 }
26 public PluginMaster(BinaryReader br, int offset, bool ReadOnly, ConsoleLog Log)
27 : this()
28 {
29 this.Log = Log;
30 br.BaseStream.Seek(0, SeekOrigin.Begin);
31 this.Offset = offset;
32 this.reader = new MemoryStream(br.ReadBytes((int)br.BaseStream.Length), offset, (int)br.BaseStream.Length - offset);
33 this.Parse(this.reader);
34 }
35
36 #region IParse Members
37
38 public bool Parse(MemoryStream stream)
39 {
40 BinaryReader br = new BinaryReader(stream);
41 this.Log.WriteLine();
42 // read header section magic
43
44
45 bool NoMoreMasters = false;
46
47 while (NoMoreMasters != true)
48 {
49 // read the MAST tag
50 byte[] magic = br.ReadBytes(4);
51 string str_magic = ASCIIEncoding.ASCII.GetString(magic);
52 StringBuilder magic_info_message = new StringBuilder();
53 magic_info_message.Append("\tDetected Master Section: ");
54 magic_info_message.Append(magic.ToStr());
55 magic_info_message.Append(" : " + str_magic);
56
57 if (str_magic != PluginMaster.MASTER_MAGIC)
58 {
59 NoMoreMasters = true;
60 br.BaseStream.Seek(br.BaseStream.Position - 4, SeekOrigin.Begin); // seek back 4 bytes in the stream
61 break;
62 }
63 // throw assert if not true
64 Debug.Assert(str_magic == PluginMaster.MASTER_MAGIC, string.Format("Excepted: {0} found: {1}", PluginMaster.MASTER_MAGIC, str_magic));
65
66 this.Log.WriteLine(magic_info_message.ToString());
67 int MasterFileNameLength = br.ReadInt32();
68 byte[] MasterFileName = br.ReadBytes(MasterFileNameLength);
69 this.Log.WriteLine("\t\tFileName: " + MasterFileName.ToAscii());
70 // read this master's data section
71 magic = br.ReadBytes(4);
72 str_magic = ASCIIEncoding.ASCII.GetString(magic);
73 Debug.Assert(str_magic == PluginMaster.MASTER_DATA_MAGIC, string.Format("Excepted: {0} found: {1}", PluginMaster.MASTER_DATA_MAGIC, str_magic));
74 int DataSectionLength = br.ReadInt32();
75 byte[] Data = br.ReadBytes(DataSectionLength);
76 this.Log.WriteLine("\t\tData: " + Data.ToStr());
77 }
78
79 this.Size = (int)br.BaseStream.Position;
80 return true;
81 }
82
83 #endregion
84
85 #region ICommit Members
86
87 public bool Commit(ICommitWriter writer)
88 {
89 throw new NotImplementedException();
90 }
91
92 #endregion
93
94 #region IAsBytes Members
95
96 public byte[] AsBytes
97 {
98 get
99 {
100 this.reader.Seek(0, SeekOrigin.Begin);
101 BinaryReader br = new BinaryReader(this.reader);
102 return br.ReadBytes(this.Size);
103 }
104 }
105
106 #endregion
107
108 #region ISizeOffset Members
109
110 private int _Size;
111 public int Size { get { return _Size; } private set { _Size = value; } } // headersize
112
113 private int _Offset;
114 public int Offset { get { return _Offset; } private set { _Offset = value; } }
115 public int OffsetToNext { get { return this.Size + this.Offset; } } // force this to 16 bytes
116 #endregion
117 }
118 }

  ViewVC Help
Powered by ViewVC 1.1.22