//Microsoft .NET Framework using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; using System.Reflection; using System.Xml.Serialization; using System.IO; //MUD Engine using MudEngine.FileSystem; using MudEngine.GameManagement; using rScripting; using rScripting.LateBinding; namespace MudEngine.Core { public abstract class BaseObject { [Category("Object Setup")] [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 _Name; } set { _Name = value; if (this.GetType().Name.StartsWith("Base")) Filename = value + "." + this.GetType().Name.Substring("Base".Length); else Filename = value + "." + this.GetType().Name; } } private String _Name; [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; } /// /// A detailed description that treats each entry as a seperete line when outputted to the player /// [Browsable(false)] //not visible within any Property Controls public List DetailedDescription { get; set; } [Category("Object Setup")] [ReadOnly(true)] [Description("The filename of the current object. This is assigned by the engine and not editable.")] public String Filename { //Returns the name of the object + the objects Type as it's extension. //Filenames are generated by the class itself, users can not assign it. get { return _Filename; } set { if (this.GetType().Name.StartsWith("Base")) { if (value.EndsWith("." + this.GetType().Name.Substring("Base".Length))) { _Filename = value; } else _Filename = value + "." + this.GetType().Name.Substring("Base".Length); } else { if (value.EndsWith("." + this.GetType().Name)) { _Filename = value; } else _Filename =value + "." + this.GetType().Name; } } } String _Filename; protected virtual String SavePath { get; set; } public BaseGame ActiveGame { get; set; } /// /// Initializes the base object /// public BaseObject(BaseGame game) { Name = "New " + this.GetType().Name; ActiveGame = game; DetailedDescription = new List(); this.Name = DefaultName(); this.Filename = DefaultName() + "." + this.GetType().Name; } private Boolean ShouldSerializeName() { return this.Name != DefaultName(); } private void ResetName() { this.Name = DefaultName(); } private String DefaultName() { return "New " + this.GetType().Name; } #region Public Methods public override String ToString() { return this.Name; } public virtual void Initialize() { } public virtual void Save() { string path = this.SavePath; if (String.IsNullOrEmpty(path)) return; if (!Directory.Exists(path)) Directory.CreateDirectory(path); String filename = Path.Combine(path, Filename); if (File.Exists(filename)) File.Delete(filename); FileManager.WriteLine(filename, this.Name, "Name"); FileManager.WriteLine(filename, this.Description, "Description"); foreach (String line in DetailedDescription) FileManager.WriteLine(filename, line, "DetailedDescription"); } public virtual void Load(String filename) { if (String.IsNullOrEmpty(filename)) return; if (!File.Exists(filename)) { Log.Write("Error: Attempted to load file " + filename); return; } this.Name = FileManager.GetData(filename, "Name"); this.Description = FileManager.GetData(filename, "Description"); List data = FileManager.GetCollectionData(filename, "DetailedDescription"); foreach (String line in data) DetailedDescription.Add(line); //Set the Filename property based off the physical filename, as setting this.Name sets a default filename //which might not match that of the actual physical filename on the harddrive. this.Filename = Path.GetFileName(filename); } #endregion } }