diff --git a/src/Static/Calc.bf b/src/Static/Calc.bf index e9c905c..b32cc77 100644 --- a/src/Static/Calc.bf +++ b/src/Static/Calc.bf @@ -66,6 +66,34 @@ namespace Strawberry return current >= startDelay && (current - startDelay) % (interval * 2) >= interval; } + static public String StringArgs(String str, Object[] args) + { + for (let i < args.Count) + { + if (args[i] is delegate void(String)) + { + let del = args[i] as delegate void(String); + let argStr = scope:: String(); + del(argStr); + args[i] = argStr; + } + } + + for (let i < args.Count) + { + String arg = scope String(); + String num = scope String("{x}"); + i.ToString(arg); + num.Replace("x", arg); + + arg.Clear(); + args[i].ToString(arg); + str.Replace(num, arg); + } + + return str; + } + [Inline] static public void Log() { @@ -88,23 +116,7 @@ namespace Strawberry Debug.WriteLine(string); } - static private String StringArgs(String str, Object[] args) - { - for (let i < args.Count) - { - String arg = scope String(); - String num = scope String("{x}"); - i.ToString(arg); - num.Replace("x", arg); - - arg.Clear(); - args[i].ToString(arg); - str.Replace(num, arg); - } - - return str; - } - + [Inline] static public void Log(StringView str, params Object[] args) { let string = StringArgs(scope String(str), args); diff --git a/src/Static/Console.bf b/src/Static/Console.bf index 1115aa5..392992c 100644 --- a/src/Static/Console.bf +++ b/src/Static/Console.bf @@ -5,6 +5,7 @@ using Strawberry; namespace Strawberry { + [AttributeUsage(.Method, .AlwaysIncludeTarget | .ReflectAttribute)] public struct CommandAttribute : Attribute { public String Name; @@ -48,7 +49,6 @@ namespace Strawberry commands.Add(attr.Value.Name, new CommandInfo(method, attr.Value)); } } - Calc.Log(commands.Count); } static public void Dispose() @@ -57,7 +57,9 @@ namespace Strawberry delete entry; DeleteContainerAndItems!(commandHistory); DeleteContainerAndItems!(messages); - DeleteDictionaryAndKeysAndItems!(commands); + for (let c in commands.Values) + delete c; + delete commands; } static public bool Enabled @@ -87,7 +89,6 @@ namespace Strawberry Log(string); } - [AlwaysInclude] [Reflect] [Command("clear", "Clears the console window")] static public void Clear() @@ -123,12 +124,14 @@ namespace Strawberry { let list = String.StackSplit!(line, ' '); list.RemoveAll(scope => String.IsNullOrWhiteSpace); - Calc.Log("hey {0}"); if (commands.ContainsKey(list[0])) { Log(line); - //Do it + let args = scope String[list.Count - 1]; + for (let i < args.Count) + args[i] = list[i + 1]; + commands[list[0]].Call(args); } else Log("Command '{0}' not recognized.", list[0]); @@ -170,13 +173,58 @@ namespace Strawberry private class CommandInfo { - public delegate void(String[]) Action ~ delete _; public String Help; public String Usage ~ delete _; + public MethodInfo Method; public this(MethodInfo method, CommandAttribute attr) { Help = attr.Help; + Method = method; + } + + public void Call(String[] args) + { + let objs = scope Object[Method.ParamCount]; + for (let i < objs.Count) + { + if (i < args.Count) + objs[i] = Convert(args[i], Method.GetParamType(i)); + else + objs[i] = Method.GetParamType(i).CreateValueDefault(); + } + + Method.Invoke(null, objs); + } + } + + static private Object Convert(String str, Type type) + { + switch (type) + { + case typeof(StringView): + return str; + + case typeof(int): + return int.Parse(str).Value; + + case typeof(float): + return float.Parse(str).Value; + + case typeof(bool): + return bool.Parse(str).Value; + + case typeof(String): + Runtime.FatalError("String arguments not supported in commands. Use StringView instead."); + + default: + { + let name = scope String(); + type.GetName(name); + let error = Calc.StringArgs("{0} type arguments not supported in commands.", scope Object[] { name }); + Runtime.FatalError(error); + } + } } }