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 IPluginHeader : IParse, ICommit, IAsBytes, ISizeOffset |
11 |
{ |
12 |
//int HeaderSize { get; } // 4bytes |
13 |
float Version { get; }// 4 bytes |
14 |
int Unknown1 { get; }// 4 bytes |
15 |
byte[] CompanyName { get; } // 32 bytes |
16 |
byte[] PluginDescription { get; } // 256 bytes |
17 |
int RecordCount { get; }// 4 bytes |
18 |
} |
19 |
public class PluginHeader : IPluginHeader |
20 |
{ |
21 |
private ConsoleLog Log; |
22 |
private const string HEADER_MAGIC = "HEDR"; |
23 |
MemoryStream reader = new MemoryStream(); |
24 |
public PluginHeader() |
25 |
{ |
26 |
this.reader = new MemoryStream(); |
27 |
this.Offset = 0; |
28 |
this.Size = 0; |
29 |
} |
30 |
public PluginHeader(BinaryReader br, int offset, int BytesToRead, bool ReadOnly, ConsoleLog Log) |
31 |
: this() |
32 |
{ |
33 |
this.Log = Log; |
34 |
br.BaseStream.Seek(offset, SeekOrigin.Begin); |
35 |
this.Offset = offset; |
36 |
this.reader = new MemoryStream(br.ReadBytes(BytesToRead), 0, BytesToRead); |
37 |
this.Parse(this.reader); |
38 |
} |
39 |
|
40 |
#region IParse Members |
41 |
|
42 |
public bool Parse(MemoryStream stream) |
43 |
{ |
44 |
BinaryReader br = new BinaryReader(stream); |
45 |
this.Log.WriteLine(); |
46 |
// read header section magic |
47 |
byte[] magic = br.ReadBytes(4); |
48 |
string str_magic = ASCIIEncoding.ASCII.GetString(magic); |
49 |
StringBuilder magic_info_message = new StringBuilder(); |
50 |
magic_info_message.Append("\tDetected Header Section: "); |
51 |
magic_info_message.Append(magic.ToStr()); |
52 |
magic_info_message.Append(" : " + str_magic); |
53 |
|
54 |
// throw assert if not true |
55 |
Debug.Assert(str_magic == PluginHeader.HEADER_MAGIC, string.Format("Excepted: {0} found: {1}",PluginHeader.HEADER_MAGIC, str_magic)); |
56 |
|
57 |
this.Log.WriteLine(magic_info_message.ToString()); |
58 |
|
59 |
|
60 |
this.Size = br.ReadInt32(); |
61 |
this.Log.WriteLine("\t\tHEDR Size: " + this.Size); |
62 |
|
63 |
Debug.Assert(this.Size == 300, string.Format("Excepted: {0} found: {1}", 300, this.Size)); |
64 |
|
65 |
this.Version = br.ReadSingle();// 4 bytes |
66 |
this.Log.WriteLine("\t\tVersion: " + this.Version); |
67 |
this.Unknown1 = br.ReadInt32(); // 4 bytes |
68 |
this.Log.WriteLine("\t\tUnknown1: " + this.Unknown1); |
69 |
this.CompanyName = br.ReadBytes(0x20); // 32 bytes |
70 |
this.Log.WriteLine("\t\tCompanyName: " + this.CompanyName.ToAscii()); |
71 |
this.PluginDescription = br.ReadBytes(0x100); // 256 bytes |
72 |
this.Log.WriteLine("\t\tPluginDescription: " + this.PluginDescription.ToAscii()); |
73 |
this.RecordCount = br.ReadInt32();// 4 bytes |
74 |
this.Log.WriteLine("\t\tRecordCount: " + this.RecordCount); |
75 |
this.Log.WriteLine(); |
76 |
return true; |
77 |
} |
78 |
#region IPluginHeader Members |
79 |
private int _HeaderSize; |
80 |
private float _Version; |
81 |
private int _Unknown1; |
82 |
private byte[] _CompanyName; |
83 |
private byte[] _PluginDescription; |
84 |
private int _RecordCount; |
85 |
|
86 |
//public int HeaderSize { get { return _HeaderSize; } private set { _HeaderSize = value; } } // 4bytes |
87 |
public float Version { get { return _Version; } private set { _Version = value; } } // 4 bytes |
88 |
public int Unknown1 { get { return _Unknown1; } private set { _Unknown1 = value; } } // 4 bytes |
89 |
public byte[] CompanyName { get { return _CompanyName; } private set { _CompanyName = value; } } // 32 bytes |
90 |
public byte[] PluginDescription { get { return _PluginDescription; } private set { _PluginDescription = value; } } // 256 bytes |
91 |
public int RecordCount { get { return _RecordCount; } private set { _RecordCount = value; } }// 4 bytes |
92 |
#endregion |
93 |
|
94 |
|
95 |
#endregion |
96 |
#region ICommit Members |
97 |
|
98 |
public bool Commit(BinaryWriter writer) |
99 |
{ |
100 |
throw new NotImplementedException(); |
101 |
} |
102 |
|
103 |
#endregion |
104 |
|
105 |
#region IAsBytes Members |
106 |
|
107 |
public byte[] AsBytes { get { return this.reader.ToArray(); } } |
108 |
|
109 |
#endregion |
110 |
|
111 |
#region ISizeOffset Members |
112 |
|
113 |
private int _Size; |
114 |
public int Size { get { return _Size; } private set { _Size = value; } } // headersize |
115 |
|
116 |
private int _Offset; |
117 |
public int Offset { get { return _Offset; } private set { _Offset = value; } } |
118 |
public int OffsetToNext { get { return this.Size + this.Offset; } } // force this to 16 bytes |
119 |
#endregion |
120 |
} |
121 |
} |
122 |
|