Пример #1
0
    public static void LoadCommands()
    {
        Loaded.Clear();

        Assembly a = typeof(Commands).Assembly;

        Debug.Log("Searching for custom commands and variables in assembly '{0}'".Form(a.FullName));

        // Partition on the type list initially.
        var def   = typeof(DebugCommandAttribute);
        var found = from t in a.GetTypes().AsParallel()
                    let methods = t.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
                                  from m in methods
                                  where m.IsDefined(def, true)
                                  select new { Type = t, Method = m, Attribute = m.GetCustomAttribute <DebugCommandAttribute>() };

        int           count = 0;
        StringBuilder str   = new StringBuilder();

        foreach (var cmd in found)
        {
            var type   = cmd.Type;
            var method = cmd.Method;
            var attr   = cmd.Attribute;

            if (!method.IsStatic)
            {
                Debug.LogError("Currently non-static debug commands are not supported: {0}.{1}".Form(type.FullName, method.Name));
                continue;
            }

            DebugCmd c     = new DebugCmd(attr, method);
            string   error = c.GetError();
            if (error != null)
            {
                Debug.LogError("Error with debug command {0}: {1}".Form(c.ToString(), error));
                continue;
            }
            AddCmd(c);

            str.Append("Class: {0}, Method: {1}, Info:\n{2}\n".Form(type.FullName, method.Name, c.GetHelp()));
            count++;
        }

        Debug.Log("Found {0} debug commands:\n{1}\n".Form(count, str.ToString()));
    }