Пример #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();
        }
Пример #2
0
        internal static void LoadFile(string rawPath, bool successMessage = true, bool isdependency = false, int degree = 0)
        {
            string path = Path.GetFullPath(rawPath);

            FileID fileId = new FileID(CommandManager.LoadedFileIDs.Count + (isdependency ? 1:0), path, isdependency);

            try
            {
                if (LoadedFileIDs.Any(x => x.Path == path && !x.LoadedAsdependency))
                {
                    CFormat.WriteLine(string.Format("[CommandManager] {0}Could not load file named \"{1}\" because it has already been loaded.", CFormat.Indent(degree * 3), fileId.Name), ConsoleColor.Red);
                    return;
                }
                else if (LoadedFileIDs.Any(x => x.Path == path && x.LoadedAsdependency))
                {
                    return;
                }
                else
                {
                    CFormat.WriteLine(string.Format("[CommandManager] {0}Loading \"{1}\"...", CFormat.Indent(degree * 3), fileId.Name));

                    string fileCode = "";
                    using (StreamReader streamReader = new StreamReader(path))
                        fileCode = streamReader.ReadToEnd();

                    CompilerParameters parameters = new CompilerParameters()
                    {
                        GenerateInMemory = true
                    };

                    parameters.ReferencedAssemblies.Add(Assembly.GetEntryAssembly().Location);
                    ReferenceAssemblies(ref parameters, fileCode, fileId.Name, isdependency, degree);
                    Loaddependencies(fileId, fileCode, degree);
                    CompilerResults compilerResults = _provider.CompileAssemblyFromSource(parameters, fileCode);

                    if ((uint)compilerResults.Errors.Count > 0U)
                    {
                        CFormat.WriteLine(string.Format("[CommandManager] {0}Could not load file named \"{1}\":", CFormat.Indent(degree * 3), fileId.Name), ConsoleColor.Red);
                        foreach (CompilerError error in compilerResults.Errors)
                        {
                            CFormat.WriteLine(string.Format("[CommandManager] {0}({1},{2}): error {3}: {4}", CFormat.Indent(degree * 3), error.Line, error.Column, error.ErrorNumber, error.ErrorText), ConsoleColor.Red);
                        }
                    }
                    else
                    {
                        List <Type> foundExternalLibraries = compilerResults.CompiledAssembly.GetTypes()
                                                             .Where(t => t.IsClass && t.GetCustomAttributes().Where(a => a.GetType() == typeof(MMasterLibrary)).Any()).ToList();

                        if (foundExternalLibraries.Count == 0)
                        {
                            CFormat.WriteLine(string.Format("[CommandManager]    {0}\"{1}\" does not contain any library.", CFormat.Indent(degree * 3), fileId.Name), ConsoleColor.DarkYellow);
                            return;
                        }

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

                            if (InternalLibraryCallNames.ContainsKey(libraryCallName) || ExternalLibraryCallNames.ContainsKey(libraryCallName))
                            {
                                CFormat.WriteLine(string.Format("[CommandManager]    {0}Could not load library named \"{1}\" in \"{2}\" because one with the same name already exists.", CFormat.Indent(degree * 3), libraryCallName, fileId.Name), ConsoleColor.DarkYellow);
                                continue;
                            }

                            IEnumerable <MethodInfo> methodInfos = library.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
                                                                   .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]    {0}Could not load command named \"{1}.{2}\" in \"{3}\" because one with the same name already exists.", CFormat.Indent(degree * 3), libraryCallName, commandCallName, fileId.Name), ConsoleColor.DarkYellow);
                                }
                                else
                                {
                                    dictionary.Add(commandCallName, methodInfo);
                                }
                            }

                            ExternalLibraryCallNames.Add(libraryCallName, library);
                            ExternalLibraries.Add(library, dictionary);
                            fileId.LibraryCallNames.Add(libraryCallName);
                        }

                        LoadedFileIDs.Add(fileId);
                        if (!successMessage)
                        {
                            return;
                        }
                        CFormat.WriteLine(string.Format("[CommandManager] {0}Loaded \"{1}\" successfully!", CFormat.Indent(degree * 3), fileId.Name), isdependency ? ConsoleColor.Cyan : ConsoleColor.Green);
                    }
                }
            }
            catch (Exception ex)
            {
                CFormat.WriteLine(string.Format("[CommandManager] {0}Could not load file named \"{1}\": {2}", CFormat.Indent(degree * 3), fileId.Name, ex.Message), ConsoleColor.Red);
            }
        }