Began work on Environment save code.

Restructured the project save data folder layout when the engine starts.
The XMLData class now creates directories if they are missing.  Individual classes no longer need to check if a path is valid before saving.
Filenames are now automatically generated and no longer assigned by developers.  This change has been made across all classes that are savable which required some changing of the Save() method arguments.
This commit is contained in:
Scionwest_cp 2012-03-10 20:47:12 -08:00
parent a6d346da3d
commit ee3cd897f2
12 changed files with 101 additions and 28 deletions

View file

@ -19,9 +19,9 @@ namespace MudEngine.Core.Interfaces
/// Save method for dumping the object to physical file.
/// </summary>
/// <param name="path"></param>
Boolean Save(String filename);
Boolean Save();
Boolean Save(String filename, Boolean ignoreFileWrite);
Boolean Save(Boolean ignoreFileWrite);
/// <summary>
/// Load method for retrieving saved data from file.

View file

@ -48,7 +48,7 @@ namespace MudEngine.DAL
this.SetAbsolutePath(Path.Combine(this._InstallRoot, "Environments"), DataTypes.Environments);
this.SetAbsolutePath(Path.Combine(this._InstallRoot, "Equipment"), DataTypes.Equipment);
this.SetAbsolutePath(Path.Combine(this._InstallRoot, "Players"), DataTypes.Players);
this.SetAbsolutePath(Path.Combine(this._InstallRoot, "GameScripts"), DataTypes.Scripts);
this.SetAbsolutePath(Path.Combine(this.GetInstallPath(), "GameScripts"), DataTypes.Scripts);
}
public void SetAbsolutePath(String path, DataTypes objectType)

View file

@ -27,7 +27,7 @@ namespace MudEngine.Game.Characters
/// Gets a reference to the currently active game.
/// </summary>
public string Filename
public new string Filename
{
get
{
@ -138,20 +138,20 @@ namespace MudEngine.Game.Characters
this.Commands = null;
}
public override bool Save(String filename)
public override bool Save()
{
return this.Save(filename, true);
return this.Save(true);
}
public override bool Save(String filename, Boolean verbose)
public override bool Save(Boolean verbose)
{
base.Save(filename, true);
base.Save(true);
SaveData.AddSaveData("Immovable", Immovable.ToString());
SaveData.AddSaveData("Password", Password);
SaveData.AddSaveData("Role", this.Role.ToString());
if (this.SaveData.Save(filename))
if (this.SaveData.Save(this.Filename))
{
if (!verbose)
this.SendMessage("Save completed.");
@ -236,7 +236,7 @@ namespace MudEngine.Game.Characters
public void Disconnect()
{
this.Save(this.Filename);
this.Save();
//Purge all of this characters commands.
this.Destroy();

View file

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MudEngine.Core;
using MudEngine.Core.Interfaces;
@ -11,12 +12,19 @@ using MudEngine.GameScripts;
namespace MudEngine.Game.Environment
{
public class Environment : BaseScript, IGameComponent, ISavable, IUpdatable
public abstract class Environment : BaseScript, IGameComponent, ISavable, IUpdatable
{
/// <summary>
/// Gets or Sets the filename for this environment when it is saved.
/// </summary>
public string Filename { get; set; }
public new string Filename
{
get
{
String path = Path.Combine(this.Game.SavePaths.GetPath(DAL.DataTypes.Environments), this.Name + this.Game.SavePaths.GetExtension(DAL.DataTypes.Environments));
return path;
}
}
/// <summary>
/// Gets or Sets if this object is enabled. When disabled, characters can not traverse it.
@ -53,16 +61,21 @@ namespace MudEngine.Game.Environment
this.Enabled = false;
}
public override bool Save(String filename, Boolean ignoreFileWrite)
public override bool Save()
{
base.Save(filename, true);
return this.Save(false);
}
SaveData.AddSaveData("Filename", this.Filename);
public override bool Save(Boolean ignoreFileWrite)
{
base.Save(true);
//SaveData.AddSaveData("Filename", this.Filename);
SaveData.AddSaveData("Enabled", this.Enabled.ToString());
SaveData.AddSaveData("RequiredRole", this.RequiredRole.ToString());
if (!ignoreFileWrite)
return this.SaveData.Save(filename);
return this.SaveData.Save(this.Filename);
else
return true;
}
@ -70,10 +83,10 @@ namespace MudEngine.Game.Environment
public override void Load(string filename)
{
base.Load(filename);
/*
try { this.Filename = this.SaveData.GetData("Filename"); }
catch { LoadFailedMessage("Filename"); }
*/
try { this.Enabled = Convert.ToBoolean(this.SaveData.GetData("Enabled")); }
catch { this.LoadFailedMessage("Enabled");}

View file

@ -13,6 +13,15 @@ namespace MudEngine.Game.Environment
{
public class Realm : Environment
{
public new String Filename
{
get
{
String path = Path.Combine(this.Game.SavePaths.GetPath(DAL.DataTypes.Environments), this.Name, this.Name + "." + this.GetType().Name);
return path;
}
}
public Realm(StandardGame game, String name, String description) : base(game, name, description)
{
this._ZoneCollection = new List<Zone>();
@ -35,6 +44,22 @@ namespace MudEngine.Game.Environment
return v.First();
}
public override bool Save(Boolean ignoreFileWrite)
{
if (!base.Save(true))
return false;
foreach (Zone zone in this._ZoneCollection)
{
this.SaveData.AddSaveData("Zone", zone.Name);
zone.Save();
}
this.SaveData.Save(this.Filename);
return true;
}
private List<Zone> _ZoneCollection;
}
}

View file

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MudEngine.Core.Interfaces;
using MudEngine.GameScripts;
@ -12,6 +13,15 @@ namespace MudEngine.Game.Environment
{
public class Zone : Environment
{
public new String Filename
{
get
{
String path = Path.Combine(this.Game.SavePaths.GetPath(DAL.DataTypes.Environments), this.Realm.Name, "Zones", this.Name + "." + this.GetType().Name);
return path;
}
}
/// <summary>
/// Gets or Sets the what stats
/// </summary>
@ -26,6 +36,16 @@ namespace MudEngine.Game.Environment
this._RoomCollection = new List<Room>();
}
public override bool Save()
{
if (!base.Save(true))
return false;
this.SaveData.AddSaveData("Safe", this.Safe.ToString());
this.SaveData.Save(this.Filename);
return true;
}
public Room CreateRoom(String name, String description)
{
Room room = new Room(this.Game, name, description, this);

View file

@ -230,6 +230,8 @@ namespace MudEngine.Game
if (this.Server.Enabled)
this.Enabled = true;
this.World.Save();
return this.Enabled;
}

View file

@ -49,6 +49,10 @@ namespace MudEngine.Game
public void Save()
{
foreach (Realm realm in this._RealmCollection)
{
realm.Save();
}
}
public void Load()

View file

@ -86,7 +86,7 @@ namespace MudEngine.GameScripts.Commands
character.Move(game.World.StartLocation);
//TODO: Create a class and setup Stats.
character.Save(character.Filename, false);
character.Save(false);
}
catch (Exception ex)
{

View file

@ -18,6 +18,14 @@ namespace MudEngine.GameScripts
[DefaultValue("Scripted Object")]
public String Name { get; set; }
public String Filename
{
get
{
return Path.Combine(this.Game.SavePaths.GetPath(DataTypes.Root), this.Name + "." + this.Game.SavePaths.GetExtension(DataTypes.Root));
}
}
public String ID { get; set; }
public String Description { get; set; }
@ -37,15 +45,15 @@ namespace MudEngine.GameScripts
this.SaveData = new XMLData(this.GetType().Name);
}
public virtual Boolean Save(String filename)
public virtual Boolean Save()
{
return this.Save(filename, false);
return this.Save(false);
}
public virtual Boolean Save(String filename, Boolean ignoreFileWrite)
public virtual Boolean Save(Boolean ignoreFileWrite)
{
if (File.Exists(filename))
File.Delete(filename);
if (File.Exists(this.Filename))
File.Delete(this.Filename);
try
{
@ -55,7 +63,7 @@ namespace MudEngine.GameScripts
this.SaveData.AddSaveData("Description", Description);
if (!ignoreFileWrite)
this.SaveData.Save(filename);
this.SaveData.Save(this.Filename);
}
catch
{

View file

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using MudEngine.Game;
using MudEngine.DAL;
using MudEngine.Game.Environment;
using MudEngine.Game;
@ -24,6 +23,7 @@ namespace MudEngine.GameScripts
public override Boolean Start(Int32 maxPlayers, Int32 maxQueueSize)
{
this.Server.ServerOwner = "Admin";
base.Start(maxPlayers, maxQueueSize);
//Quick demonstration on how to create the initial starting room for new players.
this.World.CreateRealm("Azeroth", "Starting Realm for beginning players");
@ -43,6 +43,7 @@ namespace MudEngine.GameScripts
if (startOK && linked)
{
this.World.StartLocation = bedroom;
this.World.Save();
return true;
}
//Otherwise return false and prevent the game from running.

View file

@ -73,9 +73,9 @@
<None Include="GameScripts\BaseCommands\SetRole.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="GameScripts\SampleGame.cs">
<Compile Include="GameScripts\SampleGame.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</Compile>
<Compile Include="Game\Characters\CharacterRoles.cs" />
<Compile Include="Game\Characters\CharacterStats.cs" />
<Compile Include="Game\Characters\StandardCharacter.cs" />