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

Contents of /trunk/libTES3FileFormat/TES3PluginLoader.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: 7698 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.Windows.Forms;
7
8 namespace libTES3FileFormat
9 {
10 public interface IPluginData
11 {
12 PluginMagic Magic { get; }
13 PluginHeader Header { get; }
14 PluginMaster MasterSection { get; }
15 //PluginScriptData ScriptSection { get; }
16 PlugingClothing ClothingSection { get; }
17 }
18 public interface ITES3PluginLoader : IPluginData
19 {
20 bool PluginSavingIsImplemented { get; }
21 bool Load();
22 bool Load(bool ReadOnly);
23 bool Save();
24 bool SaveAs(string filename);
25 string PluginLoadFailureReason { get; }
26 bool PluginLoaded { get; }
27 }
28
29 public class TES3PluginLoader : ITES3PluginLoader, IDisposable, IConsoleLoger, ICommit, ICommitWriter
30 {
31 private BinaryReader br;
32 private BinaryWriter bw;
33 public TES3PluginLoader()
34 {
35 this.PluginFile = "";
36 this.AddPluginFailureReason = new StringBuilder();
37 this.Log = new ConsoleLog();
38 this.PluginSavingIsImplemented = true;
39 }
40 public TES3PluginLoader(string PluginFile): this()
41 {
42 this.PluginFile = PluginFile;
43 }
44 public TES3PluginLoader(string PluginFile, Control LogControl)
45 : this(PluginFile)
46 {
47 this.Log = new ConsoleLog(LogControl);
48 }
49 private string PluginFile = "";
50
51 #region ITES3PluginLoader Members
52 private bool _PluginSavingIsImplemented;
53 public bool PluginSavingIsImplemented { get { return _PluginSavingIsImplemented; } private set { _PluginSavingIsImplemented = value; } }
54 private bool _PluginLoaded;
55 public bool PluginLoaded { get { return _PluginLoaded; } private set { _PluginLoaded = value; } }
56 private bool isDirty = false;
57 private bool isReadonly = false;
58 private StringBuilder AddPluginFailureReason;
59 public string PluginLoadFailureReason { get { return AddPluginFailureReason.ToString(); } internal set { this.AddPluginFailureReason.Append(value); } }
60
61
62
63 public bool Load()
64 {
65 if (this.PluginFile == "") return false;
66 this.PluginLoaded = false;
67 //AddPluginFailureReason.Append("Plugin Loading has not been implemented");
68 //this.PluginLoadFailureReason = "Plugin Loading has not been implemented";
69
70 this.Log.Write("Loaded Plugin: " + this.PluginFile);
71
72 if (this.isReadonly)
73 {
74 br = new BinaryReader(new FileStream(this.PluginFile, FileMode.Open, FileAccess.Read));
75 this.bw = new BinaryWriter(br.BaseStream);
76 }
77 else
78 {
79 br = new BinaryReader(new FileStream(this.PluginFile, FileMode.Open, FileAccess.ReadWrite));
80 this.bw = new BinaryWriter(br.BaseStream);
81 }
82
83 // init magic
84 this.Magic = new PluginMagic(br, isReadonly, this.Log);
85 this.PluginLoaded = this.Magic.IsValid;
86 if (!this.PluginLoaded)
87 {
88 this.PluginLoadFailureReason = "This is not a valid TES3:Morrowind ESM or ESP Plugin";
89 }
90 // init header
91 this.Header = new PluginHeader(br, this.Magic.OffsetToNext, this.Magic.HeaderSize, isReadonly, this.Log);
92 this.MasterSection = new PluginMaster(br, this.Header.OffsetToNext, isReadonly, this.Log);
93
94 //this.ScriptSection = new PluginScriptData(br, this.MasterSection.OffsetToNext, isReadonly, this.Log);
95
96 // find the CLOTHING section
97 PlugingClothing.SectionLocater clothing_section_locator = new PlugingClothing.SectionLocater(br, 0, this.Log);
98
99 if (clothing_section_locator.ClothingSectionOffsets.Count == 0)
100 {
101 this.PluginLoadFailureReason = this.Log.GetLastMessage();
102 return false;
103 }
104
105 this.ClothingSection = new PlugingClothing(br, clothing_section_locator.ClothingSectionOffsets, isReadonly, this.Log);
106 return this.PluginLoaded;
107 }
108 public bool Load(bool ReadOnly)
109 {
110 this.isReadonly = ReadOnly;
111 return Load();
112 }
113 public bool Save()
114 {
115 if (this.PluginFile == "") return false;
116 if (isReadonly)
117 {
118 this.PluginLoadFailureReason = "This plugin was opened in readonly mode";
119 return false;
120 }
121 if (!this.PluginLoaded)
122 {
123 this.PluginLoadFailureReason = "No plugin has been loaded";
124 return false;
125 }
126 bool ret = false;
127 //AddPluginFailureReason.Append("Plugin Loading has not been implemented");
128 //this.PluginLoadFailureReason = "Plugin Saving has not been implemented";
129 ret = this.Commit(this);
130
131 return ret;
132 }
133 public bool SaveAs(string filename)
134 {
135 if (this.PluginFile == "") return false;
136 if (this.PluginFile == filename)
137 {
138 if (isReadonly)
139 {
140 this.PluginLoadFailureReason = "This plugin was opened in readonly mode";
141 return false;
142 }
143 }
144 if (!this.PluginLoaded)
145 {
146 this.PluginLoadFailureReason = "No plugin has been loaded";
147 return false;
148 }
149 bool ret = false;
150 //AddPluginFailureReason.Append("Plugin Loading has not been implemented");
151 this.PluginLoadFailureReason = "Plugin Saving has not been implemented";
152 return ret;
153 }
154 #endregion
155
156 #region IDisposable Members
157
158 public void Dispose() { }
159
160 #endregion
161
162 #region IPluginData Members
163 private PluginMagic _Magic;
164 public PluginMagic Magic { get { return _Magic; } internal set { _Magic = value; } }
165 private PluginHeader _Header;
166 public PluginHeader Header { get { return _Header; } internal set { _Header = value; } }
167 private PluginMaster _MasterSection;
168 public PluginMaster MasterSection { get { return _MasterSection; } internal set { _MasterSection = value; } }
169 //private PluginScriptData _ScriptSection;
170 //public PluginScriptData ScriptSection { get { return _ScriptSection; } internal set { _ScriptSection = value; } }
171 private PlugingClothing _ClothingSection;
172 public PlugingClothing ClothingSection { get { return _ClothingSection; } internal set { _ClothingSection = value; } }
173 #endregion
174
175 #region IConsoleLoger Members
176 private ConsoleLog _Log;
177 public ConsoleLog Log
178 {
179 get { return _Log; }
180 private set { _Log = value; }
181 }
182
183 #endregion
184
185 #region ICommit Members
186
187 public bool Commit(ICommitWriter writer)
188 {
189 bool ret = false;
190 ret = this.ClothingSection.Commit(writer);
191
192 if (ret)
193 {
194 writer.GetWriter().Close();
195 }
196
197 return ret;
198 }
199
200 #endregion
201
202 #region ICommitWriter Members
203
204 public BinaryWriter GetWriter()
205 {
206 return this.bw;
207 }
208
209 #endregion
210 }
211 }
212

  ViewVC Help
Powered by ViewVC 1.1.22