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

Contents of /trunk/libTES3FileFormat/PluginMagic.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 33 - (show annotations) (download)
Sat Jul 30 11:47:53 2011 UTC (9 years ago) by william
File size: 3781 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 IPluginMagic : IParse, ICommit, IAsBytes, ISizeOffset
11 {
12 bool IsValid { get; }
13
14 int HeaderSize { get; } // 4 bytes, Usually is 0x5A 0x01 0x00 0x00.
15 byte[] HeaderReserved { get; } // 8 bytes, should all be 0
16
17 }
18 public class PluginMagic : IPluginMagic, ICommitParseAsBytes
19 {
20 private ConsoleLog Log;
21 MemoryStream reader = new MemoryStream();
22 private const string TES3_MAGIC = "TES3";
23 private const int MagicSize = 16;
24 private const int MagicOffset = 0;
25 public PluginMagic()
26 {
27 this.IsValid = false;
28 this.reader = new MemoryStream();
29 }
30 public PluginMagic(BinaryReader br, bool ReadOnly, ConsoleLog Log)
31 : this()
32 {
33 this.Log = Log;
34 this.reader = new MemoryStream(br.ReadBytes((int)PluginMagic.MagicSize), PluginMagic.MagicOffset, (int)PluginMagic.MagicSize);
35 this.Parse(this.reader);
36 }
37 #region IPluginHeader Members
38 private bool _IsValid;
39 public bool IsValid { get { return _IsValid; } private set { _IsValid = value; } }
40
41 private int _HeaderSize;
42 public int HeaderSize { get { return _HeaderSize; } private set { _HeaderSize = value; } } // 8 bytes, should all be 0
43
44 private byte[] _HeaderReserved;
45 public byte[] HeaderReserved { get { return _HeaderReserved; } private set { _HeaderReserved = value; } } // 8 bytes, should all be 0
46
47 #endregion
48
49
50
51 #region IParse Members
52
53 public bool Parse(MemoryStream stream)
54 {
55 BinaryReader br = new BinaryReader(stream);
56 this.Log.WriteLine();
57 // read the magic
58 byte[] magic = br.ReadBytes(4);
59 string str_magic = ASCIIEncoding.ASCII.GetString(magic);
60 StringBuilder magic_info_message = new StringBuilder();
61 magic_info_message.Append("\tDetected Magic: ");
62 magic_info_message.Append(magic.ToStr());
63 magic_info_message.Append(" : " + str_magic);
64 if (str_magic == PluginMagic.TES3_MAGIC) { magic_info_message.Append(" (is valid)"); this.IsValid = true; }
65 else { magic_info_message.Append(" (is not valid)"); this.IsValid = false; }
66 this.Log.WriteLine(magic_info_message.ToString());
67
68 this.HeaderSize = br.ReadInt32();
69 this.Log.WriteLine("\t\tHeader Size: " + this.HeaderSize);
70 this.HeaderReserved = br.ReadBytes(8);
71 this.Log.WriteLine("\t\tHeader Reserved: " + this.HeaderReserved.ToStr());
72 return true;
73 }
74
75 #endregion
76
77 #region ICommit Members
78
79 public bool Commit(ICommitWriter writer)
80 {
81 throw new NotImplementedException();
82 }
83
84 #endregion
85
86 #region IAsBytes Members
87
88 public byte[] AsBytes
89 {
90 get
91 {
92 this.reader.Seek(0, SeekOrigin.Begin);
93 BinaryReader br = new BinaryReader(this.reader);
94 return br.ReadBytes(this.Size);
95 }
96 }
97
98 #endregion
99
100 #region ISizeOffset Members
101 public int Size { get { return PluginMagic.MagicSize; } } // size of magic, 15 bytes
102 public int Offset { get { return PluginMagic.MagicOffset; } }
103 public int OffsetToNext { get { return (PluginMagic.MagicSize) + this.Offset; } } // force this to 16 bytes
104 #endregion
105 }
106 }

  ViewVC Help
Powered by ViewVC 1.1.22