Esempio n. 1
0
        private void RefreshCommands()
        {
            lock (_commandLocker)
            {
                Commands.Clear();
                ConsoleCommands.Clear();

                var assemblies = AppDomain.CurrentDomain.GetAssemblies();

                foreach (var assm in assemblies.Where(p => p.GetTypes().Any(x => typeof(ICSPlugin).IsAssignableFrom(x))))
                {
                    Type[] types = assm.GetTypes();

                    foreach (Type type in types)
                    {
                        foreach (MethodInfo mi in type.GetMethods())
                        {
                            var cmdAttribs        = mi.GetCustomAttributes(typeof(CommandAttribute), false).ToList();
                            var consoleCmdAttribs = mi.GetCustomAttributes(typeof(ConsoleCommandAttribute), false).ToList();

                            if (consoleCmdAttribs.Count == 1)
                            {
                                ConsoleCommandAttribute cCmdAttr = (ConsoleCommandAttribute)cmdAttribs[0];

                                ConsoleCommands.Add(cCmdAttr.CommandName, cCmdAttr.CommandDescription);
                            }
                            else if (cmdAttribs.Count == 1)
                            {
                                var permAttribs = mi.GetCustomAttributes(typeof(CommandPermissionAttribute), false).ToList();

                                CommandAttribute cmdAttrib = (CommandAttribute)cmdAttribs[0];

                                if (permAttribs.Count == 1)
                                {
                                    CommandPermissionAttribute permAttrib = (CommandPermissionAttribute)permAttribs[0];

                                    Commands.Add(Tuple.Create(cmdAttrib.CommandName, cmdAttrib.CommandDescription, permAttrib.Permission, permAttrib.Admin));
                                }
                                else
                                {
                                    Commands.Add(Tuple.Create(cmdAttrib.CommandName, cmdAttrib.CommandDescription, String.Empty, false));
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        public void RegisterConsoleCommandObject(object obj)
        {
            if (!RegisteredConsoleCommandObjects.ContainsKey(obj))
            {
                var dict = new Dictionary <Tuple <string, string>, Action <string[]> >();

                MethodInfo[] methods = obj.GetType().GetMethods();

                foreach (var mi in methods)
                {
                    var attribs = mi.GetCustomAttributes(typeof(ConsoleCommandAttribute), false).ToList();

                    if (attribs.Count == 1)
                    {
                        ConsoleCommandAttribute attrib = (ConsoleCommandAttribute)attribs[0];

                        dict.Add(Tuple.Create(attrib.CommandName, attrib.CommandDescription), (Action <string[]>)Delegate.CreateDelegate(typeof(Action <string[]>), obj, mi));
                    }
                }

                RegisteredConsoleCommandObjects.Add(obj, dict);
            }
        }