internal static void UnloadFile(int id) { try { foreach (string libraryCallName in LoadedFileIDs[id].LibraryCallNames) { ExternalLibraries.Remove(ExternalLibraryCallNames[libraryCallName]); ExternalLibraryCallNames.Remove(libraryCallName); } LoadedFileIDs.RemoveAll(x => x.ID == id); CFormat.WriteLine("[ComandManager] File unloaded successfully!", ConsoleColor.Green); } catch (Exception ex) { CFormat.WriteLine("[CommandManager] Could not unload file \"" + CommandManager.LoadedFileIDs[id].Path + "\". Details: " + ex.Message, ConsoleColor.Red); } }
internal static void ClearExternalCommands() { ExternalLibraryCallNames.Clear(); ExternalLibraries.Clear(); LoadedFileIDs.Clear(); }
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); } }