private void RegisterAttributes() { foreach (Type type in Assembly.GetExecutingAssembly().GetTypes()) { // FIXME add support for non-static methods (FindObjectByType?) foreach (MethodInfo method in type.GetMethods(BindingFlags.Public | BindingFlags.Static)) { CUDLRCommandAttribute[] attrs = method.GetCustomAttributes(typeof(CUDLRCommandAttribute), true) as CUDLRCommandAttribute[]; if (attrs.Length == 0) { continue; } CUDLRCommandAttribute.Callback cb = Delegate.CreateDelegate(typeof(CUDLRCommandAttribute.Callback), method, false) as CUDLRCommandAttribute.Callback; if (cb == null) { CUDLRCommandAttribute.CallbackSimple cbs = Delegate.CreateDelegate(typeof(CUDLRCommandAttribute.CallbackSimple), method, false) as CUDLRCommandAttribute.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 (CUDLRCommandAttribute cmd in attrs) { if (string.IsNullOrEmpty(cmd.m_command)) { cmd.m_command = method.Name; } 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, CUDLRCommandAttribute.Callback callback, bool runOnMainThread = true) { if (string.IsNullOrEmpty(command)) { if (callback != null && callback.Method != null) { command = callback.Method.Name; } else { throw new Exception("Command String cannot be empty"); } } CUDLRCommandAttribute cmd = new CUDLRCommandAttribute(command, desc, runOnMainThread); cmd.m_callback = callback; Instance.m_commands.Add(cmd); Instance.m_help += string.Format("\n{0} : {1}", command, desc); }