//------------------------------------------------------------------------/ // Procedures //------------------------------------------------------------------------/ private static void RegisterCommands() { commandsByName = new Dictionary <string, IStratusConsoleCommand>(); commandActions = new Dictionary <string, Action <string> >(); List <IStratusConsoleCommand> commands = new List <IStratusConsoleCommand>(); List <string> variableNames = new List <string>(); handlerTypes = Stratus.Utilities.StratusReflection.GetInterfaces(typeof(IStratusConsoleCommandProvider)); handlerTypesByName = new Dictionary <string, Type>(); handlerTypesByName.AddRange(x => x.Name, handlerTypes); foreach (Type handler in handlerTypes) { // Methods foreach (MethodInfo method in handler.GetMethods(flags)) { TryAddCommand(method, (command) => { command.parameters = StratusConsoleCommandParameterExtensions.DeduceMethodParameters(method); if (command.usage.IsNullOrEmpty()) { command.usage = $"({method.GetParameterNames()})"; } commandActions.Add(command.name, (string args) => { object[] parsedArgs = Parse(command, args); object returnValue = method.Invoke(null, parsedArgs); if (returnValue != null) { RecordResult($"{command.name}({args}) = {returnValue}", $"{returnValue}"); } }); }); } // Fields foreach (FieldInfo field in handler.GetFields(flags)) { TryAddCommand(field, (command) => { command.parameters = StratusConsoleCommandParameterExtensions.DeduceParameters(field); StratusConsoleCommandParameterInformation parameter = command.parameters[0]; if (command.usage.IsNullOrEmpty()) { command.usage = $"{parameter.deducedType}"; } commandActions.Add(command.name, (string args) => { bool hasValue = args.IsValid(); if (hasValue) { field.SetValue(null, Parse(parameter, args)); } else { object value = field.GetValue(null); RecordResult($"{command.name} = {value}", value); } }); }); } // Properties foreach (PropertyInfo property in handler.GetProperties(flags)) { TryAddCommand(property, (command) => { command.parameters = StratusConsoleCommandParameterExtensions.DeduceParameters(property); StratusConsoleCommandParameterInformation parameter = command.parameters[0]; if (command.usage.IsNullOrEmpty()) { command.usage = $"{parameter.deducedType}"; } bool hasSetter = property.GetSetMethod(true) != null; if (hasSetter) { commandActions.Add(command.name, (string args) => { bool hasValue = args.IsValid(); if (hasValue) { property.SetValue(null, Parse(parameter, args)); } else { object value = property.GetValue(null); RecordResult($"{command.name} = {value}", value); } }); } else { commandActions.Add(command.name, (args) => { bool hasValue = args.IsValid(); if (hasValue) { RecordCommand($"{command.name} has no setters!"); } else { object value = property.GetValue(null); RecordResult($"{command.name} = {value}", value); } }); } }); } } IStratusConsoleCommand TryAddCommand(MemberInfo member, Action <IStratusConsoleCommand> onCommandAdded) { IStratusConsoleCommand command = member.GetAttribute <StratusConsoleCommandAttribute>(); if (command != null) { if (command.name.IsNullOrEmpty()) { command.name = member.Name; } onCommandAdded(command); commandsByName.Add(command.name, command); commands.Add(command); } return(command); } StratusConsoleCommand.variableNames = variableNames.ToArray(); StratusConsoleCommand.commands = commands.ToArray(); }