Exemplo n.º 1
0
        public static void SearchAssembly(Assembly ass, Dictionary<string, Command> cmdlist, Dictionary<string, Convar> convars)
        {
            foreach(var cl in ass.GetTypes().Where(t => t.IsClass))
            {
                string cat = "";
                ContextFilter categoryFilter = null;
                bool found = false;
                foreach(var attr in cl.GetCustomAttributes())
                {
                    var attrCat = attr as CategoryAttribute;
                    var attrFilter = attr as ContextFilterAttribute;
                    if (attrCat != null)
                    {
                        cat = attrCat.Category;
                        found = true;
                    }
                    else if (attrFilter != null)
                    {
                        categoryFilter = attrFilter.CreateFilterInternal();
                    }
                }

                if (!found) continue;

                // Load commands
                foreach (var method in cl.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
                {
                    var attrs = method.GetCustomAttributes();
                    var attributes = attrs as Attribute[] ?? attrs.ToArray();
                    var cmdAttr = attributes.FirstOrDefault(attr => attr is CommandAttribute) as CommandAttribute;

                    if (cmdAttr == null) continue;
                    if (cmdlist.ContainsKey(Util.Qualify(cat, cmdAttr.Name))) continue;

                    var filterAttr = attributes.FirstOrDefault(attr => attr is ContextFilterAttribute) as ContextFilterAttribute;
                    var filterAdminDefaultAttr = attributes.FirstOrDefault(attr => attr is DefaultAdminFilterAttribute);

                    var command = new Command(method, cmdAttr.Name, cmdAttr.Description, cat,
                        filterAdminDefaultAttr != null
                        ? ContextFilter.DefaultAdminFilter
                        : categoryFilter ?? (filterAttr == null ? null : filterAttr.CreateFilterInternal()));

                    cmdlist[command.QualifiedName] = command;
                }

                // Load convars
                foreach (var convar in cl.GetProperties()
                            .Where(
                                p => p.GetGetMethod().IsStatic && p.GetGetMethod().IsPublic && p.GetSetMethod().IsPublic)
                            .SelectMany(p => p.GetCustomAttributes<ConvarAttribute>()
                                .Where(attr => !convars.ContainsKey(Util.Qualify(cat, attr.Name)))
                                .Select(attr => new PropertyConvar(p, attr.Name, attr.Description, cat, attr.DefaultValue, attr.Savable))))
                {
                    convars[convar.QualifiedName] = convar;
                }

                foreach (var convar in cl.GetFields()
                    .Where(
                        f => f.IsStatic && f.IsPublic)
                    .SelectMany(f => f.GetCustomAttributes<ConvarAttribute>()
                        .Where(attr => !convars.ContainsKey(Util.Qualify(cat, attr.Name)))
                        .Select(attr => new FieldConvar(f, attr.Name, attr.Description, cat, attr.DefaultValue, attr.Savable))))
                {
                    convars[convar.QualifiedName] = convar;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new command using the specified method, metadata, and an optional filter.
        /// </summary>
        /// <param name="method">The method to associate with the command.</param>
        /// <param name="name">The name of the command.</param>
        /// <param name="description">The description of the command.</param>
        /// <param name="category">The category under which to place the command.</param>
        /// <param name="filter">The filter rules to apply to the command.</param>
        /// <returns></returns>
        public static Command CreateCommand(MethodInfo method, string name, string description, string category, ContextFilter filter = null)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            name = name.Trim().ToLower();
            var qname = Util.Qualify(category ?? "", name);

            if (Commands.ContainsKey(qname)) return null;

            return Commands[qname] = new Command(method, name, description, category, filter);
        }