private void RegisterAttributes() { #if !NETFX_CORE foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { // HACK: IL2CPP crashes if you attempt to get the methods of some classes in these assemblies. if (assembly.FullName.StartsWith("System") || assembly.FullName.StartsWith("mscorlib")) { continue; } foreach (Type type in assembly.GetTypes()) { // FIXME add support for non-static methods (FindObjectByType?) foreach (MethodInfo method in type.GetMethods(BindingFlags.Public | BindingFlags.Static)) { CommandAttribute[] attrs = method.GetCustomAttributes(typeof(CommandAttribute), true) as CommandAttribute[]; if (attrs.Length == 0) { continue; } CommandAttribute.Callback cb = Delegate.CreateDelegate(typeof(CommandAttribute.Callback), method, false) as CommandAttribute.Callback; if (cb == null) { CommandAttribute.CallbackSimple cbs = Delegate.CreateDelegate(typeof(CommandAttribute.CallbackSimple), method, false) as CommandAttribute.CallbackSimple; if (cbs != null) { cb = delegate(string[] args) { cbs(); }; } } if (cb == null) { Debug.LogError(string.Format("Method {0}.{1} takes the wrong arguments for a console command.", type, method.Name)); continue; } // try with a bare action foreach (CommandAttribute cmd in attrs) { if (string.IsNullOrEmpty(cmd.m_command)) { Debug.LogError(string.Format("Method {0}.{1} needs a valid command name.", type, method.Name)); continue; } cmd.m_callback = cb; m_commands.Add(cmd); } } } } #endif }
/* Register a new console command */ public static void RegisterCommand(string command, string desc, CommandAttribute.Callback callback, bool runOnMainThread = true) { if (command == null || command.Length == 0) { throw new Exception("Command String cannot be empty"); } CommandAttribute cmd = new CommandAttribute(command, desc, runOnMainThread); cmd.m_callback = callback; Instance.m_commands.Add(cmd); }
private void RegisterAttributes() { foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach (Type type in assembly.GetTypes()) { // FIXME add support for non-static methods (FindObjectByType?) foreach (MethodInfo method in type.GetMethods(BindingFlags.Public | BindingFlags.Static)) { CommandAttribute[] attrs = method.GetCustomAttributes(typeof(CommandAttribute), true) as CommandAttribute[]; if (attrs.Length == 0) { continue; } CommandAttribute.Callback cb = Delegate.CreateDelegate(typeof(CommandAttribute.Callback), method, false) as CommandAttribute.Callback; if (cb == null) { CommandAttribute.CallbackSimple cbs = Delegate.CreateDelegate(typeof(CommandAttribute.CallbackSimple), method, false) as CommandAttribute.CallbackSimple; if (cbs != null) { cb = delegate(string[] args) { cbs(); }; } } if (cb == null) { Debug.LogError(string.Format("Method {0}.{1} takes the wrong arguments for a console command.", type, method.Name)); continue; } // try with a bare action foreach (CommandAttribute cmd in attrs) { if (string.IsNullOrEmpty(cmd.m_command)) { Debug.LogError(string.Format("Method {0}.{1} needs a valid command name.", type, method.Name)); continue; } cmd.m_callback = cb; m_commands.Add(cmd); m_help += string.Format("\n{0} : {1}", cmd.m_command, cmd.m_help); } } } } }
/* Register a new console command */ public static void RegisterCommand(string command, string desc, CommandAttribute.Callback callback, bool runOnMainThread = true) { if (string.IsNullOrEmpty(command)) { throw new Exception("Command String cannot be empty"); } CommandAttribute cmd = new CommandAttribute(command, desc, runOnMainThread) { m_callback = callback }; Instance.m_commands.Add(cmd); Instance.m_help += $"\n{command} : {desc}"; }
public static bool AddCommandByMethodInfo(MethodInfo method) { CommandAttribute[] attrs = method.GetCustomAttributes(typeof(CommandAttribute), true) as CommandAttribute[]; if (attrs.Length == 0) { return(false); } CommandAttribute.Callback cb = Delegate.CreateDelegate(typeof(CommandAttribute.Callback), method, false) as CommandAttribute.Callback; if (cb == null) { CommandAttribute.CallbackSimple cbs = Delegate.CreateDelegate(typeof(CommandAttribute.CallbackSimple), method, false) as CommandAttribute.CallbackSimple; if (cbs != null) { cb = delegate { cbs(); }; } } if (cb == null) { Debug.LogError($"Method {method.DeclaringType}.{method.Name} takes the wrong arguments for a console command."); return(false); } // try with a bare action foreach (CommandAttribute cmd in attrs) { if (string.IsNullOrEmpty(cmd.m_command)) { Debug.LogError($"Method {method.DeclaringType}.{method.Name} needs a valid command name."); continue; } cmd.m_callback = cb; Instance.m_commands.Add(cmd); Instance.m_help += $"\n{cmd.m_command} : {cmd.m_help}"; } return(true); }