Пример #1
0
        internal static void LoadInternalCommands()
        {
            CFormat.WriteLine("[CommandManager] Loading internal commands...");

            List <Type> foundInternalLibraries = ((IEnumerable <Type>)Assembly.GetExecutingAssembly().GetTypes()).Where(t =>
            {
                if (t.IsClass && t.GetCustomAttributes().Where(a => a.GetType() == typeof(MMasterLibrary)).Any())
                {
                    return(t.Namespace == _InternalNamespace);
                }
                return(false);
            }).ToList();

            if (foundInternalLibraries.Count() == 0)
            {
                CFormat.WriteLine(ConsoleColor.Red, "[CommandManager] CRITICAL ISSUE: No internal commands loaded.",
                                  "[CommandManager] Please report bug on GitHub at MMaster-Command-Prompt page.");
                CFormat.JumpLine();
                return;
            }

            foreach (Type library in foundInternalLibraries)
            {
                string libraryCallName = library.GetCustomAttribute <MMasterLibrary>().CallName;
                if (String.IsNullOrEmpty(libraryCallName))
                {
                    libraryCallName = library.Name;
                }

                IEnumerable <MethodInfo> methodInfos = library.GetMethods(BindingFlags.Static | BindingFlags.Public)
                                                       .Where <MethodInfo>((Func <MethodInfo, bool>)(m => ((IEnumerable <object>)m.GetCustomAttributes(false))
                                                                                                     .Where <object>((Func <object, bool>)(a => a.GetType().Name.Contains(typeof(MMasterCommand).Name))).Any()));

                Dictionary <string, MethodInfo> dictionary = new Dictionary <string, MethodInfo>();

                foreach (MethodInfo methodInfo in methodInfos)
                {
                    string commandCallName = methodInfo.GetCustomAttribute <MMasterCommand>().CallName;
                    if (String.IsNullOrEmpty(commandCallName))
                    {
                        commandCallName = methodInfo.Name;
                    }

                    if (dictionary.ContainsKey(commandCallName))
                    {
                        CFormat.WriteLine(string.Format("[CommandManager] Could not load command named \"{0}.{1}\" because one already exists.", libraryCallName, commandCallName), ConsoleColor.Red);
                    }
                    else
                    {
                        dictionary.Add(commandCallName, methodInfo);
                    }
                }

                InternalLibraryCallNames.Add(libraryCallName, library);
                InternalLibraries.Add(library, dictionary);
            }

            CFormat.WriteLine("[CommandManager] Internal commands loaded.");
            CFormat.JumpLine();
        }