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

Contents of /trunk/libTES3FileFormat/PluginScriptData.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: 4684 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 IPluginScriptData : IParse, ICommit, IAsBytes, ISizeOffset
11 {
12
13 }
14 public class PluginScriptData : IPluginScriptData, ICommitParseAsBytes
15 {
16 private ConsoleLog Log;
17 private const string SCRIPT_MAGIC = "SCPT";
18 private const string SCRIPT_HEADER_MAGIC = "SCHD";
19 MemoryStream reader = new MemoryStream();
20 public PluginScriptData()
21 {
22 this.reader = new MemoryStream();
23 this.Offset = 0;
24 this.Size = 0;
25 }
26 public PluginScriptData(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(System.IO.MemoryStream stream)
39 {
40 BinaryReader br = new BinaryReader(stream);
41 this.Log.WriteLine();
42
43
44
45 bool NoMoreScripts = false;
46
47 while (NoMoreScripts != 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 Script Section: ");
54 magic_info_message.Append(magic.ToStr());
55 magic_info_message.Append(" : " + str_magic);
56
57 if (str_magic != PluginScriptData.SCRIPT_MAGIC)
58 {
59 NoMoreScripts = 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 == PluginScriptData.SCRIPT_MAGIC, string.Format("Excepted: {0} found: {1}", PluginScriptData.SCRIPT_MAGIC, str_magic));
65
66 this.Log.WriteLine(magic_info_message.ToString());
67
68
69 int ScriptLength = BitConverter.ToInt32(br.ReadBytes(12), 0);
70 magic = br.ReadBytes(4);
71 str_magic = ASCIIEncoding.ASCII.GetString(magic);
72 Debug.Assert(str_magic == PluginScriptData.SCRIPT_HEADER_MAGIC, string.Format("Excepted: {0} found: {1}", PluginScriptData.SCRIPT_HEADER_MAGIC, str_magic));
73
74 byte[] script_data = br.ReadBytes(ScriptLength -4);
75
76 MemoryStream script_data_reader = new MemoryStream(script_data, 0, script_data.Length);
77 BinaryReader sub_reader = new BinaryReader(script_data_reader);
78
79 int script_header_length = sub_reader.ReadInt32();
80 //Debug.Assert(script_header_length == 34, string.Format("Excepted: {0} found: {1}", 34, script_header_length));
81
82 string script_filename = (char)sub_reader.PeekChar() + sub_reader.ReadString();
83
84
85 this.Log.WriteLine("\t\tName: " + script_filename);
86 this.Log.WriteLine(string.Format("\t\tData[{0}]", ScriptLength));
87 }
88
89 this.Size = (int)br.BaseStream.Position;
90 return true;
91 }
92
93 #endregion
94
95 #region ICommit Members
96
97 public bool Commit(ICommitWriter writer)
98 {
99 throw new NotImplementedException();
100 }
101
102 #endregion
103
104 #region IAsBytes Members
105
106 public byte[] AsBytes
107 {
108 get
109 {
110 this.reader.Seek(0, SeekOrigin.Begin);
111 BinaryReader br = new BinaryReader(this.reader);
112 return br.ReadBytes(this.Size);
113 }
114 }
115
116 #endregion
117
118 #region ISizeOffset Members
119 private int _Size;
120 public int Size
121 {
122 get { return _Size; }
123 private set { _Size = value; }
124 }
125 private int _Offset;
126 public int Offset
127 {
128 get { return _Offset; }
129 private set { _Offset = value; }
130 }
131
132 public int OffsetToNext
133 {
134 get { return this.Size + this.Offset; }
135
136 }
137
138 #endregion
139 }
140 }

  ViewVC Help
Powered by ViewVC 1.1.22