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)
{
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.");

View file

@ -28,39 +28,39 @@ namespace MudEngine.FileSystem
}
/// <summary>
/// Saves the object using the specified output format
/// Writes content out to a file.
/// </summary>
/// <param name="Filename"></param>
/// <param name="o"></param>
public static void Save(string Filename, object o)
/// <param name="filename"></param>
/// <param name="name"></param>
/// <param name="value"></param>
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);
}
*/
}
/// <summary>
/// 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)
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.";
}
/// <summary>

View file

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

View file

@ -146,6 +146,11 @@ namespace MudEngine.GameManagement
[Browsable(false)]
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")]
[ReadOnly(true)]
public Realm InitialRealm
@ -180,7 +185,12 @@ namespace MudEngine.GameManagement
CurrencyList = new List<Currency>();
scriptEngine = new Scripting.ScriptEngine(this);
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";
_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)

View file

@ -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; }
/// <summary>
/// A detailed description that treats each entry as a seperete line when outputted to the player
/// </summary>
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")]
[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; }
/// <summary>
/// used for serialization only.
/// </summary>
internal BaseObject()
{
}
/// <summary>
/// Initializes the base object
/// </summary>
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<string>();
this.Feel = "You feel nothing.";
this.Listen = "You hear nothing of interest.";
@ -136,35 +103,6 @@ namespace MudEngine.GameObjects
}
#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()
{
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
}
}

View file

@ -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");
}
/// <summary>
/// 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();

View file

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

View file

@ -16,15 +16,5 @@ namespace MudEngine.Scripting
{
_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);
}
}
}