From a98e2bc069f3a40121ad674ecbf0b405df060dc6 Mon Sep 17 00:00:00 2001 From: Scionwest_cp Date: Mon, 18 Jan 2010 21:31:47 -0800 Subject: [PATCH] Designer: - Corrected Project Information not being displayed when loaded for editing - Re-wrote Designer Save code to use the objects save code instead of FileManager. Resulted in 26% less code and cleaner source. - Project Explorer now checks Saved State of currently loaded object just like the Right-Click->Edit Object menu item does. Engine: - ProjectInformation.Save() now implemented. - BaseObject.Save() now implemented. All objects inheriting from BaseObject can save itself. Including all Environment objects (Realms,Zones, Rooms) and Currency objects. --- Mud Designer/Designer.cs | 82 ++++++++----------- .../GameManagement/ProjectInformation.cs | 6 ++ .../MudEngine/GameObjects/BaseObject.cs | 13 +++ 3 files changed, 52 insertions(+), 49 deletions(-) diff --git a/Mud Designer/Designer.cs b/Mud Designer/Designer.cs index 4f4f2fc..cdf188e 100644 --- a/Mud Designer/Designer.cs +++ b/Mud Designer/Designer.cs @@ -64,6 +64,7 @@ namespace MudDesigner //ensure the path exists ValidatePath(projectPath); + //Display the Project directory structure in the Project Explorer RefreshProjectExplorer(); } @@ -144,6 +145,7 @@ namespace MudDesigner //Project Information file case ObjectType.Project: _Project = (ProjectInformation)_Project.Load(FileManager.GetDataPath(SaveDataTypes.Root)); + propertyObject.SelectedObject = _Project; break; //Currency File case ObjectType.Currency: @@ -285,60 +287,43 @@ namespace MudDesigner public void SaveSelected() { - //Get the object Type - Type t = propertyObject.SelectedObject.GetType(); //We can use to get a copy of the currently selected object //if it is a BaseObject (Aquire it's BaseObject.Filename) - var obj = new BaseObject(); + var obj = (BaseObject)propertyObject.SelectedObject; //Filepaths - string projectPath = Path.Combine(Application.StartupPath, "Project"); string objectPath = ""; string filename = ""; - //Start checking to see what object we are saving - if (t == typeof(ProjectInformation)) + switch (obj.GetType().Name) { - filename = Path.Combine(projectPath, "Game.xml"); - FileManager.Save(filename, _Project); - } - else if (t == typeof(Currency)) - { - obj = (Currency)propertyObject.SelectedObject; - objectPath = Path.Combine(projectPath, "Currencies"); - ValidatePath(objectPath); - filename = Path.Combine(objectPath, obj.Filename); - FileManager.Save(filename, obj); - } - else if (t == typeof(Realm)) - { - obj = (Realm)propertyObject.SelectedObject; - objectPath = Path.Combine(projectPath, "Realms"); - objectPath = Path.Combine(objectPath, obj.Name); - filename = Path.Combine(objectPath, obj.Filename); - objectPath = Path.Combine(objectPath, "Zones"); - ValidatePath(objectPath); - FileManager.Save(filename, obj); - } - else if (t == typeof(Zone)) - { - Zone zone = (Zone)propertyObject.SelectedObject; - if (string.IsNullOrEmpty(zone.Realm)) - { - objectPath = Path.Combine(projectPath, "Zones"); - objectPath = Path.Combine(objectPath, zone.Name); - } - else - { - objectPath = Path.Combine(projectPath, "Realms"); - objectPath = Path.Combine(objectPath, zone.Realm); - objectPath = Path.Combine(objectPath, "Zones"); - objectPath = Path.Combine(objectPath, zone.Name); - filename = Path.Combine(objectPath, zone.Filename); - } - ValidatePath(objectPath); - filename = Path.Combine(objectPath, zone.Filename); - FileManager.Save(filename, zone); + case "ProjectInformation": + filename = Path.Combine(FileManager.GetDataPath(SaveDataTypes.Root), "Game.xml"); + _Project.Save(filename); + break; + case "Currency": + filename = Path.Combine(FileManager.GetDataPath(SaveDataTypes.Currencies), obj.Filename); + obj.Save(filename); + break; + case "Realm": + filename = Path.Combine(FileManager.GetDataPath(SaveDataTypes.Realms), obj.Filename); + obj.Save(filename); + break; + case "Zone": + Zone z = new Zone(); + z = (Zone)obj; + if (String.IsNullOrEmpty(z.Realm)) + { + objectPath = Path.Combine(FileManager.GetDataPath(SaveDataTypes.Zones), z.Name); + filename = Path.Combine(objectPath, z.Filename); + } + else + { + objectPath = FileManager.GetDataPath(z.Realm, z.Name); + filename = Path.Combine(objectPath, z.Filename); + } + obj.Save(filename); + break; } RefreshProjectExplorer(); @@ -569,9 +554,8 @@ namespace MudDesigner private void treeExplorer_DoubleClick(object sender, EventArgs e) { - if (treeExplorer.SelectedNode.Text == "Project") - return; - mnuEditObject_Click(sender, e); + if (CheckSavedState()) + LoadObject(treeExplorer.SelectedNode); } private void mnuNewRoom_Click(object sender, EventArgs e) diff --git a/Mud Designer/MudEngine/GameManagement/ProjectInformation.cs b/Mud Designer/MudEngine/GameManagement/ProjectInformation.cs index 1bd0075..1983ff9 100644 --- a/Mud Designer/MudEngine/GameManagement/ProjectInformation.cs +++ b/Mud Designer/MudEngine/GameManagement/ProjectInformation.cs @@ -146,6 +146,12 @@ namespace MudDesigner.MudEngine.GameManagement public void Save(string filename) { + string directory = Path.GetDirectoryName(filename); + + if (!Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + FileManager.Save(filename, this); } public object Load(string path) diff --git a/Mud Designer/MudEngine/GameObjects/BaseObject.cs b/Mud Designer/MudEngine/GameObjects/BaseObject.cs index a92b6d9..191a1cc 100644 --- a/Mud Designer/MudEngine/GameObjects/BaseObject.cs +++ b/Mud Designer/MudEngine/GameObjects/BaseObject.cs @@ -159,6 +159,11 @@ namespace MudDesigner.MudEngine.GameObjects #endregion #region Public Methods + /// + /// Loads the supplied filename and returns it. + /// + /// + /// public object Load(string filename) { if (!File.Exists(filename)) @@ -169,8 +174,16 @@ namespace MudDesigner.MudEngine.GameObjects return FileManager.Load(filename, this); } + /// + /// Saves the current object with the supplied filename + /// + /// public void Save(string filename) { + string directory = Path.GetDirectoryName(filename); + if (!Directory.Exists(directory)) + Directory.CreateDirectory(directory); + FileManager.Save(filename, this); } public override string ToString() {