static public void HandleCommand(String commandstring) { if (commandstring == "") { return; } string[] command = commandstring.Split(' '); System.Object[] param = { null }; if (command.Length == 1) { cmd = command[0]; fam = "MAIN"; param = null; } else if (command.Length == 2) { fam = command[0]; cmd = command[1]; param = null; } else if (command.Length > 2) { fam = command[0]; cmd = command[1]; arg = command.Skip(2).ToArray(); if (arg.Length == 1) { param[0] = arg; } else if (arg.Length > 1) { param[0] = arg; } } CommandHandle handle = Handles.Find(s => s.family == fam && s.command == cmd); if (handle != null) { try { System.Object obj = null; handle.MethodInfo.Invoke(obj, param); } catch (Exception ex) { Log.WriteLine(LogType.Error, "Exception Occured"); Log.WriteLine(LogType.Error, "Message: {0}", ex.Message); Log.WriteLine(LogType.Error, "Stacktrace: {0}", ex.StackTrace); } } else { Log.WriteLine(LogType.Error, "Command not found: {0}", commandstring); } }
public static void Initialize() { CommandHandlerAtribute atribute; Assembly asm = Assembly.GetExecutingAssembly(); int x = 0; foreach (Type asmType in asm.GetTypes()) { foreach (MethodInfo method in asmType.GetMethods()) { foreach (Attribute attr in method.GetCustomAttributes(true)) { atribute = attr as CommandHandlerAtribute; if (null != atribute) { CommandHandle handle = new CommandHandle(method, atribute.command, atribute.family); Handles.Add(handle); x++; } } } } Log.WriteLine(LogType.Success, "Loaded {0} Command handlers.", x); }