public IPsybotModule CompileSourceFiles(string[] files) { // BUILD Console.WriteLine("Compiling..."); var result = codeProvider.CompileAssemblyFromFile(parameters, files); // show need errors if (result.Errors.Count > 0) { Console.WriteLine(string.Format("Need {0} errors:", result.Errors.Count)); foreach (CompilerError ce in result.Errors) { if (ce.IsWarning) { Console.WriteLine(string.Format("\nWarning {0}: {1}", ce.ErrorNumber, ce.ErrorText)); } else { Console.WriteLine(string.Format("\nError {0}: {1}", ce.ErrorNumber, ce.ErrorText)); } if (ce.FileName.Length == 0 || !File.Exists(ce.FileName)) { continue; } // show line text error Console.WriteLine(string.Format("In line {0}, file: {1}", ce.Line, ce.FileName)); using (var sr = File.OpenText(ce.FileName)) { var txt = string.Empty; for (int i = 0; i < ce.Line; i++) { txt = sr.ReadLine(); } Console.WriteLine(txt.Trim()); } } return(null); } Console.WriteLine("Source built successfully!"); var type = result.CompiledAssembly.GetType("TestModule.Baka"); IPsybotModule typeModule = (IPsybotModule)Activator.CreateInstance(type); //typeModule.Load(); Console.WriteLine("Create instance successfully!"); return(typeModule); }
public ModuleData(string fullPath, IPsybotModule module) { Module = module; FullPath = fullPath; FileName = Path.GetFileNameWithoutExtension(fullPath); FullFileName = Path.GetFileName(fullPath); if (fullPath.EndsWith(".dll")) { ModuleAssemblyType = ModuleAssemblyTypeEnum.Library; //Status = StatusEnum.Disable; } }
/// <summary> /// Load modules. /// </summary> /// <param name="path"> Modules full path (root from <see cref="DEFAULT_MODULE_PATH"/>). </param> /// <param name="containsType"> Action flag for contains modules: 2 - [skip] all, 3 - [reload] all. </param> /// <param name="returnLog"> If true, this method return string log (for excecute from channel). </param> /// <returns> Log (if returnLog == true). </returns> public string LoadModuleLibrarys(string[] path, int containsType, bool returnLog) { StringBuilder sb = null; if (returnLog) { sb = new StringBuilder(); } else { Console.Clear(); Console.SetCursorPosition(0, 0); } Term.FastDraw("=== Loading " + path.Length + " modules\n", ConsoleColor.White); sb?.AppendLine("Loading " + path.Length + " modules"); // 0 - skip, 1 - reload, 2 - skip all, 3 - reload all int loaded = 0, skips = 0, errors = 0; for (int i = 0; i < path.Length; i++) { var modFileName = Path.GetFileNameWithoutExtension(path[i]); Term.FastDraw("\n> " + path[i] + "\n", ConsoleColor.White); sb?.AppendLine("> Installing '**" + modFileName + "**'"); if (!File.Exists(path[i])) { Term.FastDraw("Error: File not found.\n", ConsoleColor.Red); sb?.AppendLine(":warning: Error: Module not found."); continue; } try { // check for reload module if (modulesDictData.ContainsKey(modFileName)) { if (returnLog && containsType < 2) { // we can not run Term.ReadKey sb.AppendLine(":warning: This module already exists.\nUse args:\n [skip] - skip all\n [reload] - reload all"); goto RETURN; } else { Term.FastDraw("This module already exists.\n", ConsoleColor.Yellow); if (containsType < 2) { Term.FastDraw("[S] Skip (default) | [R] Reload | [G] Skip all | [U] Reload all ", ConsoleColor.Gray); switch (Term.ReadKey(true).Key) // !!! { case ConsoleKey.S: containsType = 0; break; case ConsoleKey.R: containsType = 1; break; case ConsoleKey.G: containsType = 2; break; case ConsoleKey.U: containsType = 3; break; } Term.FastDraw("\n"); } } switch (containsType) { case 0: case 2: // skip skips++; Term.FastDraw("Skip\n", ConsoleColor.Gray); continue; case 1: case 3: // reload Term.FastDraw("Reload\n", ConsoleColor.Gray); UnloadModule(modFileName, true); break; } } //Assembly asm = Assembly.LoadFile(dllPath); // блокирует файл до закрытия приложения Term.FastDraw("Loading assembly\n", ConsoleColor.Gray); Assembly asm = Assembly.Load(File.ReadAllBytes(path[i])); Term.FastDraw("Search\n", ConsoleColor.Gray); Type[] types = asm.ExportedTypes.ToArray(); // (i'm hate the foreach) int typeIndex = -1; // find class winh interface for (int j = 0; j < types.Length; j++) { if (types[j].IsClass) { Type ipp = types[j].GetInterface(nameof(IPsybotModule), false); if (ipp != null) { typeIndex = j; break; } } } // wad found? if (typeIndex == -1) { errors++; var errorText = "Fail: Interface 'IPsybotModule' not found.\n"; sb?.Append(":warning: " + errorText); Term.FastDraw(errorText, ConsoleColor.Red); continue; } Term.FastDraw("Create instance\n", ConsoleColor.Gray); IPsybotModule mod = Activator.CreateInstance(types[typeIndex]) as IPsybotModule; // loading Term.FastDraw("Loading\n", ConsoleColor.Gray); mod.Load(core); // check if (mod.CheckAllMessage) { Term.FastDraw("Warning! This module requires the access to check all messages.", ConsoleColor.Yellow); } else { if (mod.RunCommandsName == null || mod.RunCommandsName.Length == 0 || string.IsNullOrWhiteSpace(mod.RunCommandsName[0])) { errors++; var errorText = "Error: 'RunCommandName' is not set.\n"; sb?.Append(":warning: " + errorText); Term.FastDraw(errorText, ConsoleColor.Red); continue; } else if (mod.RunCommandsName[0].StartsWith(PsybotCore.CMD_ADMIN, StringComparison.Ordinal)) { errors++; var errorText = "Error: 'RunCommandName' can not start with '" + PsybotCore.CMD_ADMIN + "'.\n"; sb?.Append(":warning: " + errorText); Term.FastDraw(errorText, ConsoleColor.Red); continue; } } // add to dict modulesListData.Add(new ModuleData(path[i], mod)); modulesDictData.Add(modFileName, modulesListData.Count - 1); runCommandsList.Add(modulesListData[modulesListData.Count - 1].Module.RunCommandsName); // success loaded++; sb?.AppendLine(":white_check_mark: Success"); Term.FastDraw("Success\n", ConsoleColor.Green); } catch (Exception ex) { errors++; var exm = ex.Message; if (!returnLog && exm.Length > Console.WindowWidth - 12) { exm = exm.Remove(Console.WindowWidth - 12) + "..."; } var exText = "Exception: " + ex.Message + " \n"; sb?.Append(":warning: " + exText); Term.FastDraw(exText, ConsoleColor.Red); } } var summary = $"\nSummary:\n Loaded: {loaded}\n Errors: {errors}\n Skips: {skips}"; Term.FastDraw(summary, ConsoleColor.Gray); sb?.AppendLine(summary); if (!returnLog) { Term.FastDraw("\nPress any key ", ConsoleColor.White); Term.ReadKey(true); // !!! Console.Clear(); } RETURN: if (returnLog) { return(sb.ToString()); } return(null); }