private static void GetCustomClasses() { ccs.Clear(); if (!Directory.Exists(Settings.Settings.CCDirectory)) { Helpers.Logger.Append($"Custom Classes { Settings.Settings.CCDirectory } doesn't exist! Aborting CC loading.."); return; } var files = Directory.GetFiles(Settings.Settings.CCDirectory); int tmpCCCount = 0; int tmpLoadedCCount = 0; foreach (var file in files) { tmpCCCount++; var fileName = Path.GetFileNameWithoutExtension(file); var fullFileName = Path.GetFileName(file); if (!string.IsNullOrEmpty(fileName) && fullFileName.EndsWith(".cs")) { try { using (var sr = new StreamReader(file)) { var compiledSource = CodeCompiler.CompileSource(sr.ReadToEnd()); if (compiledSource == null) { return; } foreach (var t in compiledSource.CompiledAssembly.GetTypes()) { if (t.BaseType != null && t.BaseType.FullName == "ZzukBot.Engines.CustomClass.CustomClass") { var cc = compiledSource.TryLoadCompiledType(t.FullName) as CustomClass; if (cc != null) { ccs.Add(cc); tmpLoadedCCount++; } } } } } catch (Exception ex) { var exception = ex as ReflectionTypeLoadException; if (exception != null) { MessageBox.Show(exception.Message); var typeLoadException = exception; var loaderExceptions = typeLoadException.LoaderExceptions; foreach (var x in loaderExceptions) { MessageBox.Show(x.Message + "\n\n" + x.InnerException); } } } } } Helpers.Logger.Append($"Loaded {tmpLoadedCCount} Custom Classes out of {tmpCCCount} files."); }
private static void GetCustomClasses() { ccs.Clear(); if (!Directory.Exists(Paths.CCFolder)) { Directory.CreateDirectory(Paths.CCFolder); return; } if (Directory.Exists(Paths.CCFolder + "\\Compiled")) { Directory.Delete(Paths.CCFolder + "\\Compiled", true); } var files = Directory.GetFiles(Paths.CCFolder); try { foreach (var file in files) { var fileName = Path.GetFileNameWithoutExtension(file); if (!string.IsNullOrEmpty(fileName)) { using (var sr = new StreamReader(file)) { var compiledSource = CodeCompiler.CompileSource(sr.ReadToEnd()); if (compiledSource == null) { return; } foreach (var t in compiledSource.CompiledAssembly.GetTypes()) { if (t.BaseType != null && t.BaseType.FullName == "ZzukBot.Engines.CustomClass.CustomClass") { var cc = compiledSource.TryLoadCompiledType(t.FullName) as CustomClass; if (cc != null) { ccs.Add(cc); } } } } } } } catch (Exception ex) { var exception = ex as ReflectionTypeLoadException; if (exception != null) { MessageBox.Show(exception.Message); var typeLoadException = exception; var loaderExceptions = typeLoadException.LoaderExceptions; foreach (var x in loaderExceptions) { MessageBox.Show(x.Message + "\n\n" + x.InnerException); } } } }