using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; using System.Windows.Forms; using MudDesigner.MudEngine.Interfaces; using MudDesigner.MudEngine.Characters; using MudDesigner.MudEngine.GameManagement; using MudDesigner.MudEngine.GameObjects.Environment; namespace MudDesigner.MudEngine.GameCommands { public class CommandEngine { #region ====== Public Enumerators, Structures & Properties ====== /// /// Gets or Sets a Dictionary list of available commands to use. /// static internal Dictionary Commands { get; set; } public List GetCommands { get { List temp = new List(); foreach (string name in Commands.Keys) { temp.Add(name); } return temp; } } #endregion #region ====== Public Methods ====== public bool GetCommand(string Name) { if (Commands.ContainsKey(Name.ToLower())) return true; else return false; } /// /// Executes the specified command name if it exists in the Commands Dictionary. /// /// /// /// public static CommandResults ExecuteCommand(string Name, BaseCharacter player, ProjectInformation project, Room room, string command) { Name = Name.Insert(0, "Command"); foreach (string key in Commands.Keys) { if (Name.ToLower().Contains(key.ToLower())) { return Commands[key.ToLower()].Execute(player, project, room, command); } } return new CommandResults(); } /// /// Dynamically loads the specified library into memory and stores all of the /// classess inheriting from MudCreator.InputCommands.ICommand into the CommandEngines /// commands dictionary for use with the project /// /// public static void LoadAllCommands() { System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); Commands = new Dictionary(); foreach (Type t in assembly.GetTypes()) { if (t.GetInterface(typeof(IGameCommand).FullName) != null) { //Use activator to create an instance IGameCommand command = (IGameCommand)Activator.CreateInstance(t); if (command != null) { if (command.Name == null) command.Name = t.Name.ToLower(); else //Make sure the command is always in lower case. command.Name = command.Name.ToLower(); //Add the command to the commands list if it does not already exist if (Commands.ContainsKey(command.Name)) { //Command exists, check if the command is set to override existing commands or not if (command.Override) { Commands[command.Name] = command; } } //Command does not exist, add it to the commands list else Commands.Add(command.Name, command); } } } } public static string GetCommand(object Parameter) { List objectList = (List)Parameter; foreach (object obj in objectList) { if (obj is string) return (string)obj; } return null; } #endregion } }