/// <summary> /// This method attempts to disable JIT compiling for the assembly. /// This method will force any member access exceptions by methods to be thrown now instead of later. /// </summary> public static void Precompile(Assembly a) { Type[] types; try { types = a.GetTypes(); } catch (ReflectionTypeLoadException e) { StringBuilder sb = new StringBuilder(); sb.AppendLine("LoaderExceptions: "); foreach (Exception e2 in e.LoaderExceptions) { sb.Append(e2).AppendLine(); } LogFile.WriteLine(sb.ToString()); throw; } foreach (Type t in types) { // Static constructors allow for early code execution which can cause issues later in the game if (HasStaticConstructor(t)) { continue; } foreach (MethodInfo m in t.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)) { if (m.HasAttribute <HarmonyReversePatch>()) { throw new Exception("Harmony attribute 'HarmonyReversePatch' found on the method '" + m.Name + "' is not compatible with Plugin Loader!"); } Precompile(m); } } }
public bool Instantiate() { try { plugin = (IPlugin)Activator.CreateInstance(mainType); inputPlugin = plugin as IHandleInputPlugin; } catch (Exception e) { ThrowError($"Failed to instantiate {data} because of an error: {e}"); return(false); } try { openConfigDialog = AccessTools.DeclaredMethod(mainType, "OpenConfigDialog", Array.Empty <Type>()); } catch (Exception e) { LogFile.WriteLine($"Unable to find OpenConfigDialog() in {data} due to an error: {e}"); openConfigDialog = null; } return(true); }
public Main() { Stopwatch sw = Stopwatch.StartNew(); Splash = new SplashScreen(); Instance = this; Cursor temp = Cursor.Current; Cursor.Current = Cursors.AppStarting; string pluginsDir = LoaderTools.PluginsDir; Directory.CreateDirectory(pluginsDir); LogFile.Init(pluginsDir); LogFile.WriteLine("Starting - v" + Assembly.GetExecutingAssembly().GetName().Version.ToString(3)); // Fix tls 1.2 not supported on Windows 7 - github.com is tls 1.2 only try { ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12; } catch (NotSupportedException e) { LogFile.WriteLine("An error occurred while setting up networking, web requests will probably fail: " + e); } Splash.SetText("Finding references..."); RoslynReferences.GenerateAssemblyList(); AppDomain.CurrentDomain.AssemblyResolve += ResolveDependencies; Config = PluginConfig.Load(pluginsDir); List = new PluginList(pluginsDir, Config); Config.Init(List); StatsClient.OverrideBaseUrl(Config.StatsServerBaseUrl); Splash.SetText("Patching..."); LogFile.WriteLine("Patching"); // Check harmony version Version expectedHarmony = new Version(HarmonyVersion); Version actualHarmony = typeof(Harmony).Assembly.GetName().Version; if (expectedHarmony != actualHarmony) { LogFile.WriteLine($"WARNING: Unexpected Harmony version, plugins may be unstable. Expected {expectedHarmony} but found {actualHarmony}"); } new Harmony("avaness.PluginLoader").PatchAll(Assembly.GetExecutingAssembly()); Splash.SetText("Instantiating plugins..."); LogFile.WriteLine("Instantiating plugins"); foreach (string id in Config) { PluginData data = List[id]; if (data is GitHubPlugin github) { github.Init(pluginsDir); } if (PluginInstance.TryGet(data, out PluginInstance p)) { plugins.Add(p); if (data.IsLocal) { HasLocal = true; } } } sw.Stop(); // FIXME: It can potentially run in the background speeding up the game's startup ReportEnabledPlugins(); LogFile.WriteLine($"Finished startup. Took {sw.ElapsedMilliseconds}ms"); Cursor.Current = temp; Splash.Delete(); Splash = null; }
private void ThrowError(string error) { LogFile.WriteLine(error); data.Error(); Dispose(); }
public void DisablePlugins() { Config.Disable(); plugins.Clear(); LogFile.WriteLine("Disabled all plugins"); }