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. /// Save method for dumping the object to physical file.
/// </summary> /// </summary>
/// <param name="path"></param> /// <param name="path"></param>
Boolean Save(String filename); Boolean Save();
Boolean Save(String filename, Boolean ignoreFileWrite); Boolean Save(Boolean ignoreFileWrite);
/// <summary> /// <summary>
/// Load method for retrieving saved data from file. /// 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, "Environments"), DataTypes.Environments);
this.SetAbsolutePath(Path.Combine(this._InstallRoot, "Equipment"), DataTypes.Equipment); this.SetAbsolutePath(Path.Combine(this._InstallRoot, "Equipment"), DataTypes.Equipment);
this.SetAbsolutePath(Path.Combine(this._InstallRoot, "Players"), DataTypes.Players); 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) 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. /// Gets a reference to the currently active game.
/// </summary> /// </summary>
public string Filename public new string Filename
{ {
get get
{ {
@ -138,20 +138,20 @@ namespace MudEngine.Game.Characters
this.Commands = null; 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("Immovable", Immovable.ToString());
SaveData.AddSaveData("Password", Password); SaveData.AddSaveData("Password", Password);
SaveData.AddSaveData("Role", this.Role.ToString()); SaveData.AddSaveData("Role", this.Role.ToString());
if (this.SaveData.Save(filename)) if (this.SaveData.Save(this.Filename))
{ {
if (!verbose) if (!verbose)
this.SendMessage("Save completed."); this.SendMessage("Save completed.");
@ -236,7 +236,7 @@ namespace MudEngine.Game.Characters
public void Disconnect() public void Disconnect()
{ {
this.Save(this.Filename); this.Save();
//Purge all of this characters commands. //Purge all of this characters commands.
this.Destroy(); this.Destroy();

View file

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.IO;
using MudEngine.Core; using MudEngine.Core;
using MudEngine.Core.Interfaces; using MudEngine.Core.Interfaces;
@ -11,12 +12,19 @@ using MudEngine.GameScripts;
namespace MudEngine.Game.Environment namespace MudEngine.Game.Environment
{ {
public class Environment : BaseScript, IGameComponent, ISavable, IUpdatable public abstract class Environment : BaseScript, IGameComponent, ISavable, IUpdatable
{ {
/// <summary> /// <summary>
/// Gets or Sets the filename for this environment when it is saved. /// Gets or Sets the filename for this environment when it is saved.
/// </summary> /// </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> /// <summary>
/// Gets or Sets if this object is enabled. When disabled, characters can not traverse it. /// 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; 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("Enabled", this.Enabled.ToString());
SaveData.AddSaveData("RequiredRole", this.RequiredRole.ToString()); SaveData.AddSaveData("RequiredRole", this.RequiredRole.ToString());
if (!ignoreFileWrite) if (!ignoreFileWrite)
return this.SaveData.Save(filename); return this.SaveData.Save(this.Filename);
else else
return true; return true;
} }
@ -70,10 +83,10 @@ namespace MudEngine.Game.Environment
public override void Load(string filename) public override void Load(string filename)
{ {
base.Load(filename); base.Load(filename);
/*
try { this.Filename = this.SaveData.GetData("Filename"); } try { this.Filename = this.SaveData.GetData("Filename"); }
catch { LoadFailedMessage("Filename"); } catch { LoadFailedMessage("Filename"); }
*/
try { this.Enabled = Convert.ToBoolean(this.SaveData.GetData("Enabled")); } try { this.Enabled = Convert.ToBoolean(this.SaveData.GetData("Enabled")); }
catch { this.LoadFailedMessage("Enabled");} catch { this.LoadFailedMessage("Enabled");}

View file

@ -13,6 +13,15 @@ namespace MudEngine.Game.Environment
{ {
public class Realm : 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) public Realm(StandardGame game, String name, String description) : base(game, name, description)
{ {
this._ZoneCollection = new List<Zone>(); this._ZoneCollection = new List<Zone>();
@ -35,6 +44,22 @@ namespace MudEngine.Game.Environment
return v.First(); 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; private List<Zone> _ZoneCollection;
} }
} }

View file

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.IO;
using MudEngine.Core.Interfaces; using MudEngine.Core.Interfaces;
using MudEngine.GameScripts; using MudEngine.GameScripts;
@ -12,6 +13,15 @@ namespace MudEngine.Game.Environment
{ {
public class Zone : 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> /// <summary>
/// Gets or Sets the what stats /// Gets or Sets the what stats
/// </summary> /// </summary>
@ -26,6 +36,16 @@ namespace MudEngine.Game.Environment
this._RoomCollection = new List<Room>(); 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) public Room CreateRoom(String name, String description)
{ {
Room room = new Room(this.Game, name, description, this); Room room = new Room(this.Game, name, description, this);

View file

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

View file

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

View file

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

View file

@ -18,6 +18,14 @@ namespace MudEngine.GameScripts
[DefaultValue("Scripted Object")] [DefaultValue("Scripted Object")]
public String Name { get; set; } 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 ID { get; set; }
public String Description { get; set; } public String Description { get; set; }
@ -37,15 +45,15 @@ namespace MudEngine.GameScripts
this.SaveData = new XMLData(this.GetType().Name); 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)) if (File.Exists(this.Filename))
File.Delete(filename); File.Delete(this.Filename);
try try
{ {
@ -55,7 +63,7 @@ namespace MudEngine.GameScripts
this.SaveData.AddSaveData("Description", Description); this.SaveData.AddSaveData("Description", Description);
if (!ignoreFileWrite) if (!ignoreFileWrite)
this.SaveData.Save(filename); this.SaveData.Save(this.Filename);
} }
catch catch
{ {

View file

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

View file

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