/// <summary> /// Converts a given a string arg to the supported parameter type /// </summary> /// <param name="arg"></param> /// <param name="parameter"></param> /// <returns></returns> public static object Parse(string arg, StratusConsoleCommandParameterInformation info) { object value = null; switch (info.deducedType) { case StratusConsoleCommandParameter.Integer: value = int.Parse(arg); break; case StratusConsoleCommandParameter.Float: value = float.Parse(arg); break; case StratusConsoleCommandParameter.String: value = arg; break; case StratusConsoleCommandParameter.Boolean: string lowercaseArg = arg.ToLower(); if (lowercaseArg.Equals(booleanTrueAlternative)) { value = true; } else if (lowercaseArg.Equals(booleanFalseAlternative)) { value = false; } else { value = bool.Parse(arg); } break; case StratusConsoleCommandParameter.Enum: value = Enum.Parse(info.type, arg); break; case StratusConsoleCommandParameter.Vector2: value = Extensions.ParseVector2(arg); break; case StratusConsoleCommandParameter.Vector3: value = Extensions.ParseVector3(arg); break; case StratusConsoleCommandParameter.Rect: value = Extensions.ParseRect(arg); break; case StratusConsoleCommandParameter.Object: throw new Exception("Submitting parameters for object types is not supported!"); } return(value); }
public static object Parse(StratusConsoleCommandParameterInformation parameter, string arg) { return(StratusConsoleCommandParameterExtensions.Parse(arg, parameter)); }
//------------------------------------------------------------------------/ // 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(); }