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.
 -
This commit is contained in:
Scionwest_cp 2010-07-30 19:02:05 -07:00
parent 7f39821216
commit 4be5a831b1
8 changed files with 137 additions and 125 deletions

View file

@ -25,8 +25,13 @@ namespace MudEngine.Commands
{ {
if (player.Role == SecurityRoles.Admin) if (player.Role == SecurityRoles.Admin)
{ {
string path = player.ActiveGame.DataPaths.Players;
for (int i = 0; i < player.ActiveGame.PlayerCollection.Length; i++) 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.EndServer();
player.ActiveGame.Server.Initialize(555, ref player.ActiveGame.PlayerCollection); player.ActiveGame.Server.Initialize(555, ref player.ActiveGame.PlayerCollection);
return new CommandResults("Server Restarted."); return new CommandResults("Server Restarted.");

View file

@ -28,39 +28,39 @@ namespace MudEngine.FileSystem
} }
/// <summary> /// <summary>
/// Saves the object using the specified output format /// Writes content out to a file.
/// </summary> /// </summary>
/// <param name="Filename"></param> /// <param name="filename"></param>
/// <param name="o"></param> /// <param name="name"></param>
public static void Save(string Filename, object o) /// <param name="value"></param>
public static void WriteLine(string filename, string value, string name)
{ {
Type t = o.GetType(); if (!File.Exists(filename))
foreach (PropertyInfo info in t.GetProperties())
{ {
FileStream s = File.Create(filename);
} s.Close();
/*
if (FileType == OutputFormats.XML)
{
XmlSerialization.Save(Filename, o);
}
*/
} }
/// <summary> using (StreamWriter sw = File.AppendText(filename))
/// Loads the object using the specified FileType format
/// </summary>
/// <param name="Filename"></param>
/// <param name="o"></param>
/// <returns></returns>
public static object Load(string Filename, object o)
{ {
if (FileType == OutputFormats.XML) StringBuilder sb = new StringBuilder();
{ sb.Append(name);
return XmlSerialization.Load(Filename, o); sb.Append("=");
sb.Append(value);
sw.WriteLine(sb.ToString());
sw.Close();
} }
else return null; }
public static string GetData(string filename, string name)
{
foreach (string line in File.ReadAllLines(filename))
{
if (line.StartsWith(name))
return line.Substring(name.Length + 1); //Accounts for name=value;
}
return "No data Found.";
} }
/// <summary> /// <summary>

View file

@ -3,6 +3,7 @@
public enum SaveDataTypes public enum SaveDataTypes
{ {
Root, Root,
Player,
Currencies, Currencies,
Realms, Realms,
Zones, Zones,

View file

@ -146,6 +146,11 @@ namespace MudEngine.GameManagement
[Browsable(false)] [Browsable(false)]
public string ProjectPath { get; set; } public string ProjectPath { get; set; }
/// <summary>
/// Gets or Sets the paths to various project related objects.
/// </summary>
public SaveDataPaths DataPaths { get; set; }
[Category("Environment Settings")] [Category("Environment Settings")]
[ReadOnly(true)] [ReadOnly(true)]
public Realm InitialRealm public Realm InitialRealm
@ -180,7 +185,12 @@ namespace MudEngine.GameManagement
CurrencyList = new List<Currency>(); CurrencyList = new List<Currency>();
scriptEngine = new Scripting.ScriptEngine(this); scriptEngine = new Scripting.ScriptEngine(this);
RealmCollection = new List<Realm>(); RealmCollection = new List<Realm>();
//PlayerCollection = new List<BaseCharacter>();
SaveDataPaths paths = new SaveDataPaths();
paths.Environment = FileManager.GetDataPath(SaveDataTypes.Realms);
paths.Players = FileManager.GetDataPath(SaveDataTypes.Player);
DataPaths = paths;
GameTitle = "New Game"; GameTitle = "New Game";
_Filename = "Game.xml"; _Filename = "Game.xml";
@ -263,18 +273,18 @@ namespace MudEngine.GameManagement
if (!Directory.Exists(directory)) if (!Directory.Exists(directory))
Directory.CreateDirectory(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); string fileName = Path.Combine(path, _Filename);
if (!File.Exists(fileName)) if (!File.Exists(fileName))
{ {
return null; return false;
} }
return FileManager.Load(fileName, this); return true;
} }
public void AddRealm(Realm realm) public void AddRealm(Realm realm)

View file

@ -19,37 +19,17 @@ namespace MudEngine.GameObjects
[Description("The Display Name assigned to this object.")] [Description("The Display Name assigned to this object.")]
//Required to refresh Filename property in the editors propertygrid //Required to refresh Filename property in the editors propertygrid
[RefreshProperties(RefreshProperties.All)] [RefreshProperties(RefreshProperties.All)]
public string Name public string Name { get; set; }
{
get
{
return this._Name;
}
set
{
this._Name = value;
this.Filename = value + "." + this.GetType().Name;
}
}
[Category("Object Setup")] [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")] [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 public string Description { get; set; }
{
get;
set;
}
/// <summary> /// <summary>
/// A detailed description that treats each entry as a seperete line when outputted to the player /// A detailed description that treats each entry as a seperete line when outputted to the player
/// </summary> /// </summary>
public List<string> DetailedDescription { get; set; } public List<string> 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")] [Category("Object Setup")]
[ReadOnly(true)] [ReadOnly(true)]
[Description("The filename of the current object. This is assigned by the engine and not editable.")] [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. //Filenames are generated by the class itself, users can not assign it.
get get
{ {
return this._Filename; if (this.GetType().Name.StartsWith("Base"))
}
set
{ {
string extension = "." + this.GetType().Name; return this.Name + "." + GetType().Name.Substring("Base".Length);
if (extension.StartsWith("Base")) }
extension = extension.Substring("Base".Length); else
if (!value.EndsWith(extension)) return this.Name + "." + GetType().Name;
value += extension;
this._Filename = value;
} }
} }
[Category("Environment Information")] [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.")] [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.")] [DefaultValue("You don't smell anything unsual.")]
public string Smell public string Smell { get; set; }
{
get;
set;
}
[Category("Environment Information")] [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.")] [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.")] [DefaultValue("You hear nothing of interest.")]
public string Listen public string Listen { get; set; }
{
get;
set;
}
[Category("Environment Information")] [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.")] [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.")] [DefaultValue("You feel nothing.")]
public string Feel public string Feel { get; set; }
{
get;
set;
}
private string _Filename = ""; public Game ActiveGame { get; set; }
private string _Name = "";
internal Game ActiveGame { get; set; } /// <summary>
/// used for serialization only.
/// </summary>
internal BaseObject()
{
}
/// <summary> /// <summary>
/// Initializes the base object /// Initializes the base object
/// </summary> /// </summary>
public BaseObject(Game game) public BaseObject(Game game)
{ {
Script = ""; Name = "New " + this.GetType().Name;
_Name = "New " + this.GetType().Name;
_Filename = _Name + "." + this.GetType().Name;
ActiveGame = game; ActiveGame = game;
DetailedDescription = new List<string>();
this.Feel = "You feel nothing."; this.Feel = "You feel nothing.";
this.Listen = "You hear nothing of interest."; this.Listen = "You hear nothing of interest.";
@ -136,35 +103,6 @@ namespace MudEngine.GameObjects
} }
#region Public Methods #region Public Methods
/// <summary>
/// Loads the supplied filename and returns it.
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public virtual object Load(string filename)
{
if (!File.Exists(filename))
{
return null;
}
return FileManager.Load(filename, this);
}
/// <summary>
/// Saves the current object with the supplied filename
/// </summary>
/// <param name="filename"></param>
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() public override string ToString()
{ {
return this.Name; return this.Name;
@ -201,6 +139,32 @@ namespace MudEngine.GameObjects
public virtual void OnDismount() 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 #endregion
} }
} }

View file

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.IO;
//MUD Engine //MUD Engine
using MudEngine.FileSystem; 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");
}
/// <summary> /// <summary>
/// Moves the player from one Room to another if the supplied direction contains a doorway. /// Moves the player from one Room to another if the supplied direction contains a doorway.
/// Returns false if no doorway is available. /// Returns false if no doorway is available.
@ -135,8 +174,8 @@ namespace MudEngine.GameObjects.Characters
} }
internal void Disconnect() internal void Disconnect()
{ {
// TODO: Save(); string filePath = Path.Combine(ActiveGame.DataPaths.Players, Filename);
Save(); this.Save(filePath);
IsActive = false; IsActive = false;
client.Close(); client.Close();

View file

@ -53,7 +53,10 @@
<Compile Include="Commands\CommandExit.cs" /> <Compile Include="Commands\CommandExit.cs" />
<Compile Include="Commands\CommandLook.cs" /> <Compile Include="Commands\CommandLook.cs" />
<Compile Include="Commands\CommandRestart.cs" /> <Compile Include="Commands\CommandRestart.cs" />
<Compile Include="Commands\CommandSave.cs" />
<Compile Include="Commands\CommandWalk.cs" /> <Compile Include="Commands\CommandWalk.cs" />
<Compile Include="Commands\CommandLoad.cs" />
<Compile Include="FileSystem\SaveDataPaths.cs" />
<Compile Include="GameManagement\CommandEngine.cs" /> <Compile Include="GameManagement\CommandEngine.cs" />
<Compile Include="GameManagement\CommandResults.cs" /> <Compile Include="GameManagement\CommandResults.cs" />
<Compile Include="GameManagement\ICommand.cs" /> <Compile Include="GameManagement\ICommand.cs" />

View file

@ -16,15 +16,5 @@ namespace MudEngine.Scripting
{ {
_GameObjects = new List<GameObject>(); _GameObjects = new List<GameObject>();
} }
public void Save(string filename)
{
FileManager.Save(filename, _GameObjects);
}
public void Load(string filename)
{
_GameObjects = (List<GameObject>)FileManager.Load(filename, _GameObjects);
}
} }
} }