From 4be5a831b18cceab4327da0a8ffb6f195b81742b Mon Sep 17 00:00:00 2001 From: Scionwest_cp Date: Fri, 30 Jul 2010 19:02:05 -0700 Subject: [PATCH] MudEngine: - BaseObject Saving and Loading fully implemented. - BaseCharacter Saving and Loading fully implemented. - Game.cs now contains SaveDataPaths property for allowing users to define where their data will be saved. - --- MudEngine/Commands/CommandRestart.cs | 7 +- MudEngine/FileSystem/FileManager.cs | 46 +++---- MudEngine/FileSystem/SaveDataTypes.cs | 1 + MudEngine/GameManagement/Game.cs | 22 ++- MudEngine/GameObjects/BaseObject.cs | 130 +++++++----------- .../GameObjects/Characters/BaseCharacter.cs | 43 +++++- MudEngine/MudEngine.csproj | 3 + MudEngine/Scripting/GameObjectCollection.cs | 10 -- 8 files changed, 137 insertions(+), 125 deletions(-) diff --git a/MudEngine/Commands/CommandRestart.cs b/MudEngine/Commands/CommandRestart.cs index fa79536..26d4cf1 100644 --- a/MudEngine/Commands/CommandRestart.cs +++ b/MudEngine/Commands/CommandRestart.cs @@ -25,8 +25,13 @@ namespace MudEngine.Commands { if (player.Role == SecurityRoles.Admin) { + string path = player.ActiveGame.DataPaths.Players; + for (int i = 0; i < player.ActiveGame.PlayerCollection.Length; i++) - player.ActiveGame.PlayerCollection[i].Save(); + { + string filename = Path.Combine(path, player.ActiveGame.PlayerCollection[i].Filename); + player.ActiveGame.PlayerCollection[i].Save(filename); + } player.ActiveGame.Server.EndServer(); player.ActiveGame.Server.Initialize(555, ref player.ActiveGame.PlayerCollection); return new CommandResults("Server Restarted."); diff --git a/MudEngine/FileSystem/FileManager.cs b/MudEngine/FileSystem/FileManager.cs index 8520920..9b8505d 100644 --- a/MudEngine/FileSystem/FileManager.cs +++ b/MudEngine/FileSystem/FileManager.cs @@ -28,39 +28,39 @@ namespace MudEngine.FileSystem } /// - /// Saves the object using the specified output format + /// Writes content out to a file. /// - /// - /// - public static void Save(string Filename, object o) + /// + /// + /// + public static void WriteLine(string filename, string value, string name) { - Type t = o.GetType(); + if (!File.Exists(filename)) + { + FileStream s = File.Create(filename); + s.Close(); + } - foreach (PropertyInfo info in t.GetProperties()) + using (StreamWriter sw = File.AppendText(filename)) { - + StringBuilder sb = new StringBuilder(); + sb.Append(name); + sb.Append("="); + sb.Append(value); + sw.WriteLine(sb.ToString()); + sw.Close(); } - /* - if (FileType == OutputFormats.XML) - { - XmlSerialization.Save(Filename, o); - } - */ } - /// - /// Loads the object using the specified FileType format - /// - /// - /// - /// - public static object Load(string Filename, object o) + public static string GetData(string filename, string name) { - if (FileType == OutputFormats.XML) + foreach (string line in File.ReadAllLines(filename)) { - return XmlSerialization.Load(Filename, o); + if (line.StartsWith(name)) + return line.Substring(name.Length + 1); //Accounts for name=value; } - else return null; + + return "No data Found."; } /// diff --git a/MudEngine/FileSystem/SaveDataTypes.cs b/MudEngine/FileSystem/SaveDataTypes.cs index b5dd8c5..e32f9ad 100644 --- a/MudEngine/FileSystem/SaveDataTypes.cs +++ b/MudEngine/FileSystem/SaveDataTypes.cs @@ -3,6 +3,7 @@ public enum SaveDataTypes { Root, + Player, Currencies, Realms, Zones, diff --git a/MudEngine/GameManagement/Game.cs b/MudEngine/GameManagement/Game.cs index a902590..70a9c40 100644 --- a/MudEngine/GameManagement/Game.cs +++ b/MudEngine/GameManagement/Game.cs @@ -146,6 +146,11 @@ namespace MudEngine.GameManagement [Browsable(false)] public string ProjectPath { get; set; } + /// + /// Gets or Sets the paths to various project related objects. + /// + public SaveDataPaths DataPaths { get; set; } + [Category("Environment Settings")] [ReadOnly(true)] public Realm InitialRealm @@ -180,7 +185,12 @@ namespace MudEngine.GameManagement CurrencyList = new List(); scriptEngine = new Scripting.ScriptEngine(this); RealmCollection = new List(); - //PlayerCollection = new List(); + + SaveDataPaths paths = new SaveDataPaths(); + paths.Environment = FileManager.GetDataPath(SaveDataTypes.Realms); + paths.Players = FileManager.GetDataPath(SaveDataTypes.Player); + DataPaths = paths; + GameTitle = "New Game"; _Filename = "Game.xml"; @@ -263,18 +273,18 @@ namespace MudEngine.GameManagement if (!Directory.Exists(directory)) Directory.CreateDirectory(directory); - FileManager.Save(filename, this); + //FileManager.Save(filename, this); } - public object Load(string path) + public bool Load(string path) { string fileName = Path.Combine(path, _Filename); if (!File.Exists(fileName)) { - return null; + return false; } - - return FileManager.Load(fileName, this); + + return true; } public void AddRealm(Realm realm) diff --git a/MudEngine/GameObjects/BaseObject.cs b/MudEngine/GameObjects/BaseObject.cs index a0233c8..0fbe6c8 100644 --- a/MudEngine/GameObjects/BaseObject.cs +++ b/MudEngine/GameObjects/BaseObject.cs @@ -19,37 +19,17 @@ namespace MudEngine.GameObjects [Description("The Display Name assigned to this object.")] //Required to refresh Filename property in the editors propertygrid [RefreshProperties(RefreshProperties.All)] - public string Name - { - get - { - return this._Name; - } - set - { - this._Name = value; - this.Filename = value + "." + this.GetType().Name; - } - } + public string Name { get; set; } [Category("Object Setup")] [Description("A brief description of this object. The description is displayed to users when they use a command for investigating an object")] - public string Description - { - get; - set; - } + public string Description { get; set; } /// /// A detailed description that treats each entry as a seperete line when outputted to the player /// public List DetailedDescription { get; set; } - [Category("Object Setup")] - [Description("The object script that can manipulate the object during the games life.")] - //[EditorAttribute(typeof(UIScriptEditor), typeof(System.Drawing.Design.UITypeEditor))] - public string Script { get; set; } - [Category("Object Setup")] [ReadOnly(true)] [Description("The filename of the current object. This is assigned by the engine and not editable.")] @@ -59,60 +39,47 @@ namespace MudEngine.GameObjects //Filenames are generated by the class itself, users can not assign it. get { - return this._Filename; - } - set - { - string extension = "." + this.GetType().Name; - if (extension.StartsWith("Base")) - extension = extension.Substring("Base".Length); - if (!value.EndsWith(extension)) - value += extension; - - this._Filename = value; + if (this.GetType().Name.StartsWith("Base")) + { + return this.Name + "." + GetType().Name.Substring("Base".Length); + } + else + return this.Name + "." + GetType().Name; } } [Category("Environment Information")] [Description("If a user asks to use his/her senses to investigate an area, this is one of the results that will be displayed. Senses can be used to assist blind characters.")] [DefaultValue("You don't smell anything unsual.")] - public string Smell - { - get; - set; - } + public string Smell { get; set; } [Category("Environment Information")] [Description("If a user asks to use his/her senses to investigate an area, this is one of the results that will be displayed. Senses can be used to assist blind characters.")] [DefaultValue("You hear nothing of interest.")] - public string Listen - { - get; - set; - } + public string Listen { get; set; } [Category("Environment Information")] [Description("If a user asks to use his/her senses to investigate an area, this is one of the results that will be displayed. Senses can be used to assist blind characters.")] [DefaultValue("You feel nothing.")] - public string Feel - { - get; - set; - } + public string Feel { get; set; } - private string _Filename = ""; - private string _Name = ""; - internal Game ActiveGame { get; set; } + public Game ActiveGame { get; set; } + + /// + /// used for serialization only. + /// + internal BaseObject() + { + } /// /// Initializes the base object /// public BaseObject(Game game) { - Script = ""; - _Name = "New " + this.GetType().Name; - _Filename = _Name + "." + this.GetType().Name; + Name = "New " + this.GetType().Name; ActiveGame = game; + DetailedDescription = new List(); this.Feel = "You feel nothing."; this.Listen = "You hear nothing of interest."; @@ -136,35 +103,6 @@ namespace MudEngine.GameObjects } #region Public Methods - /// - /// Loads the supplied filename and returns it. - /// - /// - /// - public virtual object Load(string filename) - { - if (!File.Exists(filename)) - { - return null; - } - - return FileManager.Load(filename, this); - } - - /// - /// Saves the current object with the supplied filename - /// - /// - public void Save() - { - string directory = Path.Combine(FileManager.GetDataPath(SaveDataTypes.Root), "Saved", "Players"); - - if (!Directory.Exists(directory)) - Directory.CreateDirectory(directory); - - FileManager.Save(Filename, this); - } - public override string ToString() { return this.Name; @@ -201,6 +139,32 @@ namespace MudEngine.GameObjects public virtual void OnDismount() { } + + public virtual void Save(string filename) + { + string path = Path.GetDirectoryName(filename); + + if (!Directory.Exists(path)) + Directory.CreateDirectory(path); + + if (File.Exists(filename)) + File.Delete(filename); + + FileManager.WriteLine(filename, this.Name, "Name"); + FileManager.WriteLine(filename, this.Description, "Description"); + FileManager.WriteLine(filename, this.Feel, "Feel"); + FileManager.WriteLine(filename, this.Listen, "Listen"); + FileManager.WriteLine(filename, this.Smell, "Smell"); + } + + public virtual void Load(string filename) + { + this.Name = FileManager.GetData(filename, "Name"); + this.Description = FileManager.GetData(filename, "Description"); + this.Feel = FileManager.GetData(filename, "Feel"); + this.Listen = FileManager.GetData(filename, "Listen"); + this.Smell = FileManager.GetData(filename, "Smell"); + } #endregion } } diff --git a/MudEngine/GameObjects/Characters/BaseCharacter.cs b/MudEngine/GameObjects/Characters/BaseCharacter.cs index 7c91957..2c46049 100644 --- a/MudEngine/GameObjects/Characters/BaseCharacter.cs +++ b/MudEngine/GameObjects/Characters/BaseCharacter.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.IO; //MUD Engine using MudEngine.FileSystem; @@ -53,6 +54,44 @@ namespace MudEngine.GameObjects.Characters } + public override void Load(string filename) + { + base.Load(filename); + + this.IsControlled = Convert.ToBoolean(FileManager.GetData(filename, "IsControlled")); + + //Need to re-assign the enumerator value that was previously assigned to the Role property + Array values = Enum.GetValues(typeof(SecurityRoles)); + foreach (int value in values) + { + //Since enum values are not strings, we can't simply just assign the string to the enum + string displayName = Enum.GetName(typeof(SecurityRoles), value); + + //If the value = the string saved, then perform the needed conversion to get our data back + if (displayName.ToLower() == FileManager.GetData(filename, "Role").ToLower()) + { + Role = (SecurityRoles)Enum.Parse(typeof(SecurityRoles), displayName); + break; + } + } + + //Restore the users current Room. + Realm realm = ActiveGame.GetRealm(FileManager.GetData(filename, "CurrentRealm")); + Zone zone = realm.GetZone(FileManager.GetData(filename, "CurrentZone")); + CurrentRoom = zone.GetRoom(FileManager.GetData(filename, "CurrentRoom")); + } + + public override void Save(string filename) + { + base.Save(filename); + + FileManager.WriteLine(filename, this.IsControlled.ToString(), "IsControlled"); + FileManager.WriteLine(filename, this.Role.ToString(), "Role"); + FileManager.WriteLine(filename, this.CurrentRoom.Name, "CurrentRoom"); + FileManager.WriteLine(filename, this.CurrentRoom.Zone, "CurrentZone"); + FileManager.WriteLine(filename, this.CurrentRoom.Realm, "CurrentRealm"); + } + /// /// Moves the player from one Room to another if the supplied direction contains a doorway. /// Returns false if no doorway is available. @@ -135,8 +174,8 @@ namespace MudEngine.GameObjects.Characters } internal void Disconnect() { - // TODO: Save(); - Save(); + string filePath = Path.Combine(ActiveGame.DataPaths.Players, Filename); + this.Save(filePath); IsActive = false; client.Close(); diff --git a/MudEngine/MudEngine.csproj b/MudEngine/MudEngine.csproj index 7c3f689..b0a7ecf 100644 --- a/MudEngine/MudEngine.csproj +++ b/MudEngine/MudEngine.csproj @@ -53,7 +53,10 @@ + + + diff --git a/MudEngine/Scripting/GameObjectCollection.cs b/MudEngine/Scripting/GameObjectCollection.cs index 17023b3..4347413 100644 --- a/MudEngine/Scripting/GameObjectCollection.cs +++ b/MudEngine/Scripting/GameObjectCollection.cs @@ -16,15 +16,5 @@ namespace MudEngine.Scripting { _GameObjects = new List(); } - - public void Save(string filename) - { - FileManager.Save(filename, _GameObjects); - } - - public void Load(string filename) - { - _GameObjects = (List)FileManager.Load(filename, _GameObjects); - } } }