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

Contents of /trunk/libTES3FileFormat/TES3PluginLoader.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 34 - (show annotations) (download)
Sat Jul 30 12:33:46 2011 UTC (9 years ago) by william
File size: 8412 byte(s)
** workout Save() and SaveAs()
** SaveAs() does not seem to write the other data out (only contains the data for the clothing section(s))

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

  ViewVC Help
Powered by ViewVC 1.1.22