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

Diff of /trunk/libTES3FileFormat/TES3PluginLoader.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/libTES3FileFormat/TES3PluginLoader.cs	2011/07/30 09:59:11	32
+++ trunk/libTES3FileFormat/TES3PluginLoader.cs	2011/07/30 11:47:53	33
@@ -17,14 +17,16 @@
     }
     public interface ITES3PluginLoader : IPluginData
     {
+        bool PluginSavingIsImplemented { get; }
         bool Load();
         bool Load(bool ReadOnly);
         bool Save();
         bool SaveAs(string filename);
         string PluginLoadFailureReason { get; }
+        bool PluginLoaded { get; }
     }
 
-    public class TES3PluginLoader : ITES3PluginLoader, IDisposable, IConsoleLoger
+    public class TES3PluginLoader : ITES3PluginLoader, IDisposable, IConsoleLoger, ICommit, ICommitWriter
     {
         private BinaryReader br;
         private BinaryWriter bw;
@@ -33,6 +35,7 @@
             this.PluginFile = "";
             this.AddPluginFailureReason = new StringBuilder();
             this.Log = new ConsoleLog();
+            this.PluginSavingIsImplemented = true;
         }
         public TES3PluginLoader(string PluginFile): this()
         {
@@ -46,7 +49,10 @@
         private string PluginFile = "";
 
         #region ITES3PluginLoader Members
-        private bool Loaded = false;
+        private bool _PluginSavingIsImplemented;
+        public bool PluginSavingIsImplemented { get { return _PluginSavingIsImplemented; } private set { _PluginSavingIsImplemented = value; } }
+        private bool _PluginLoaded;
+        public bool PluginLoaded { get { return _PluginLoaded; } private set { _PluginLoaded = value; } }
         private bool isDirty = false;
         private bool isReadonly = false;
         private StringBuilder AddPluginFailureReason;
@@ -57,18 +63,27 @@
         public bool Load()
         {
             if (this.PluginFile == "") return false;
-            Loaded = false;
+            this.PluginLoaded = false;
             //AddPluginFailureReason.Append("Plugin Loading has not been implemented");
             //this.PluginLoadFailureReason = "Plugin Loading has not been implemented";
 
             this.Log.Write("Loaded Plugin: " + this.PluginFile);
 
-            br = new BinaryReader(new FileStream(this.PluginFile, FileMode.Open, FileAccess.Read));
+            if (this.isReadonly)
+            {
+                br = new BinaryReader(new FileStream(this.PluginFile, FileMode.Open, FileAccess.Read));
+                this.bw = new BinaryWriter(br.BaseStream);
+            }
+            else
+            {
+                br = new BinaryReader(new FileStream(this.PluginFile, FileMode.Open, FileAccess.ReadWrite));
+                this.bw = new BinaryWriter(br.BaseStream);
+            }
 
             // init magic
             this.Magic = new PluginMagic(br, isReadonly, this.Log);
-            Loaded = this.Magic.IsValid;
-            if (!Loaded)
+            this.PluginLoaded = this.Magic.IsValid;
+            if (!this.PluginLoaded)
             {
                 this.PluginLoadFailureReason = "This is not a valid TES3:Morrowind ESM or ESP Plugin";
             }
@@ -88,7 +103,7 @@
             }
 
             this.ClothingSection = new PlugingClothing(br, clothing_section_locator.ClothingSectionOffsets, isReadonly, this.Log);
-            return Loaded;
+            return this.PluginLoaded;
         }
         public bool Load(bool ReadOnly)
         {
@@ -103,14 +118,16 @@
                 this.PluginLoadFailureReason = "This plugin was opened in readonly mode";
                 return false;
             }
-            if (!Loaded)
+            if (!this.PluginLoaded)
             {
                 this.PluginLoadFailureReason = "No plugin has been loaded";
                 return false;
             }
             bool ret = false;
             //AddPluginFailureReason.Append("Plugin Loading has not been implemented");
-            this.PluginLoadFailureReason = "Plugin Saving has not been implemented";
+            //this.PluginLoadFailureReason = "Plugin Saving has not been implemented";
+            ret = this.Commit(this);
+
             return ret;
         }
         public bool SaveAs(string filename)
@@ -124,7 +141,7 @@
                     return false;
                 }
             }
-            if (!Loaded)
+            if (!this.PluginLoaded)
             {
                 this.PluginLoadFailureReason = "No plugin has been loaded";
                 return false;
@@ -164,6 +181,32 @@
         }
 
         #endregion
+
+        #region ICommit Members
+
+        public bool Commit(ICommitWriter writer)
+        {
+            bool ret = false;
+            ret = this.ClothingSection.Commit(writer);
+
+            if (ret)
+            {
+                writer.GetWriter().Close();
+            }
+
+            return ret;
+        }
+
+        #endregion
+
+        #region ICommitWriter Members
+
+        public BinaryWriter GetWriter()
+        {
+            return this.bw;
+        }
+
+        #endregion
     }
 }
 

 

  ViewVC Help
Powered by ViewVC 1.1.22