view api/Objects/WarFoundryObject.cs @ 5:b9346894319c

Fixes #6 - Stop missing data folder being fatal * Add check that directory exists before trying to get file list * Add warning message if directory doesn't exist
author IBBoard <dev@ibboard.co.uk>
date Fri, 26 Dec 2008 12:45:32 +0000
parents 520818033bb6
children 613bc5eaac59
line wrap: on
line source

using System;

namespace IBBoard.WarFoundry.API.Objects
{
	/// <summary>
	/// Summary description for WarFoundryObject.
	/// </summary>
	public abstract class WarFoundryObject : IWarFoundryObject
	{
		protected string name, id;
		public event StringValChangedDelegate NameChanged;
		
		protected WarFoundryObject(){}
				
		protected WarFoundryObject(string objName) : this()
		{
			Name = objName;
		}
		
		protected WarFoundryObject(string objId, string objName) : this(objName)
		{
			ID = objId;
		}	

		public virtual string ID
		{
			get
			{
				if (id == null)
				{
					id = GenerateID();
				}
				
				return id;
			}
			
			set { id = (value == null ? GenerateID() : value.Trim()); }
		}

		public virtual string Name
		{
			get 
			{
				if (HasDefaultName())
				{
					return DefaultName();
				}
				else
				{
					return name;
				} 
			}
			set 
			{ 
				string oldValue = name;
				name = value;

				if (name!=oldValue)
				{
					OnNameChanged(oldValue, name);
				}
			}
		}

		protected bool HasDefaultName()
		{
			return (name == null || name == "");
		}

		protected void OnNameChanged(string oldValue, string newValue)
		{
			if (NameChanged!=null)
			{
				NameChanged(this, oldValue, newValue);
			}
		}

		protected virtual string DefaultName()
		{
			return "-";
		}

		protected string GenerateID()
		{
			return "ID" + UnixTimestamp.GetTimestamp(DateTime.Now) + "." + DateTime.Now.Millisecond;
		}
	}
}