public static ICommand GetCommand(string name, Assembly[] fromAssemblies = null) { // This isn't the most efficient. // Considering that GetCommands should really only be returning a few (1-5?) ICommand objects // Filtering the list a second time (filtered once in GetCommands) shouldn't hurt much. // If users start loading dozens of ICommand (bad practice!) in their scripts folder, then // this needs to be revisited. IEnumerable <ICommand> commands = CommandFactory.GetCommands(fromAssemblies); foreach (ICommand command in commands) { Type type = command.GetType(); if (type.Name.ToLower() == name.ToLower()) { return(command); } else if (type.Name == string.Format("{0}command", name.ToLower())) { return(command); } ShorthandNameAttribute attribute = type.GetCustomAttribute <ShorthandNameAttribute>(); if (attribute != null) { if (attribute.Command.ToLower() == name.ToLower()) { return(command); } else if (attribute.Shorthand.ToLower() == name.ToLower()) { return(command); } } } return(null); }
/// <summary> /// Initializes the specified mob. /// </summary> /// <param name="mob">The mob.</param> /// <param name="initialState">The initial state.</param> public void Initialize(IMob mob, IEnumerable <ICommand> commands) { this.Mob = mob; this.States = new Stack <IState>(); this.Commands = commands; var commandsWithShorthand = commands.Where(command => Attribute.IsDefined(command.GetType(), typeof(ShorthandNameAttribute))); foreach (ICommand command in commandsWithShorthand) { ShorthandNameAttribute shorthandAttribute = Attribute.GetCustomAttribute(command.GetType(), typeof(ShorthandNameAttribute)) as ShorthandNameAttribute; if (!this.shorthandCommands.Keys.Any(key => key.Shorthand.ToLower() == shorthandAttribute.Shorthand || key.Command == shorthandAttribute.Command)) { this.shorthandCommands.Add(shorthandAttribute, command); } } }