コード例 #1
0
        private static void Loaddependencies(FileID fileId, string fileCode, int degree = 0)
        {
            foreach (Match match in new Regex("// DEP \\\"(.+)\\\"").Matches(fileCode))
            {
                string result = Path.GetFullPath(match.Groups[1].Value);

                FileAttributes fileAttributes = File.GetAttributes(result);

                if (fileAttributes.HasFlag(FileAttributes.Directory) && Directory.Exists(result))
                {
                    CFormat.WriteLine(string.Format("[CommandManager]    {0}Found dependency in \"{1}\": loading \"{2}\" directory.", CFormat.Indent(degree * 3), fileId.Name, result), ConsoleColor.Cyan);
                    LoadDirectory(result, true, true, true, degree + 1);
                }
                else if (File.Exists(result))
                {
                    CFormat.WriteLine(string.Format("[CommandManager]    {0}Found dependency in \"{1}\": loading \"{2}\" file.", CFormat.Indent(degree * 3), fileId.Name, result), ConsoleColor.Cyan);

                    if (Path.GetExtension(result) != _ExternalExtension)
                    {
                        CFormat.WriteLine(string.Format("[CommandManager]       {0}The file dependency \"{1}\" is not a (*.cs) file and was not loaded.", CFormat.Indent(degree * 3), result), ConsoleColor.Red);
                        return;
                    }

                    LoadFile(result, true, true, degree + 1);
                }
                else
                {
                    CFormat.WriteLine(string.Format("[CommandManager]    The dependency \"{0}\" found in \"{1}\" does not exist.", result, fileId.Name), ConsoleColor.DarkYellow);
                }
            }
        }
コード例 #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);
            }
        }