//initial game callback public static void Init() { //wiredly App.Awake() calls Init multiple times, but we do not want multiple instances if (init) { return; } init = true; Console.WriteLine("ModLoader version: " + ModLoader.getVersion()); if (Updater.tryUpdate()) //update { Application.Quit(); return; } //Install global mod exception helper AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler; instance = new ModLoader(); MethodBodyReplacementProviderRegistry.SetProvider(new SimpleMethodReplacementProvider(instance)); //otherwise we can finally load instance.loadMods(); //delete checks for loading crashes if (System.IO.File.Exists(Platform.getGlobalScrollsInstallPath() + System.IO.Path.DirectorySeparatorChar + "check.txt")) { System.IO.File.Delete(Platform.getGlobalScrollsInstallPath() + System.IO.Path.DirectorySeparatorChar + "check.txt"); } }
public void ShouldInvokeClassMethodReplacementProviderIfInterceptionIsEnabled() { Func <MethodReference, bool> methodFilter = m => m.Name == "DoSomething"; var replacement = new SampleMethodReplacement(); var provider = new SampleMethodReplacementProvider(replacement); MethodBodyReplacementProviderRegistry.SetProvider(provider); Action <Type> doTest = type => { var doSomethingMethod = type.GetMethod("DoSomething"); Assert.IsNotNull(doSomethingMethod); doSomethingMethod.Invoke(null, new object[0]); }; Test("SampleLibrary.dll", "SampleStaticClassWithStaticMethod", methodFilter, doTest); Assert.IsTrue(replacement.HasBeenCalled); }
public void ShouldNotInvokeClassMethodReplacementProviderIfInterceptionIsDisabled() { var sampleInterceptor = new SampleInterceptor(); var sampleProvider = new SampleMethodReplacementProvider(sampleInterceptor); MethodBodyReplacementProviderRegistry.SetProvider(sampleProvider); Action <object> condition = (instance) => { Assert.IsNotNull(instance); var modified = (IModifiableType)instance; modified.IsInterceptionDisabled = true; instance.Invoke("DoSomething"); Assert.IsFalse(sampleInterceptor.HasBeenInvoked); }; Test(condition); }
public static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e) { if ((e.ExceptionObject as Exception).TargetSite.Module.Assembly.GetName().Name.Equals("UnityEngine") || (e.ExceptionObject as Exception).TargetSite.Module.Assembly.GetName().Name.Equals("Assembly-CSharp") || (e.ExceptionObject as Exception).TargetSite.Module.Assembly.GetName().Name.Equals("ScrollsModLoader") || (e.ExceptionObject as Exception).TargetSite.Module.Assembly.Location.ToLower().Equals(Platform.getGlobalScrollsInstallPath().ToLower()) || (e.ExceptionObject as Exception).TargetSite.Module.Assembly.Location.Equals("")) //no location or Managed => mod loader crash //log { Console.WriteLine(e.ExceptionObject); new ExceptionLogger().logException((Exception)e.ExceptionObject); //unload ScrollsModLoader MethodBodyReplacementProviderRegistry.SetProvider(new NoMethodReplacementProvider()); //check for frequent crashes if (!System.IO.File.Exists(Platform.getGlobalScrollsInstallPath() + System.IO.Path.DirectorySeparatorChar + "check.txt")) { System.IO.File.CreateText(Platform.getGlobalScrollsInstallPath() + System.IO.Path.DirectorySeparatorChar + "check.txt"); Platform.RestartGame(); } else { try { foreach (String id in instance.modOrder) { BaseMod mod = instance.modInstances [id]; if (mod != null) { try { instance.unloadMod((LocalMod)instance.modManager.installedMods.Find(delegate(Item lmod) { return((lmod as LocalMod).id.Equals(id)); })); } catch (Exception exp) { Console.WriteLine(exp); } } } } catch (Exception exp) { Console.WriteLine(exp); } instance.repatch(); } } else if (instance != null && logger != null && logger.Count > 0) { Console.WriteLine(e.ExceptionObject); Assembly asm = (e.ExceptionObject as Exception).TargetSite.Module.Assembly; Type modClass = (from _modClass in asm.GetTypes() where _modClass.InheritsFrom(typeof(BaseMod)) select _modClass).First(); //no mod classes?? if (modClass == null) { return; } foreach (String id in instance.modOrder) { BaseMod mod = null; try { mod = instance.modInstances [id]; } catch (Exception exp) { Console.WriteLine(exp); } if (mod != null) { if (modClass.Equals(mod.GetType())) { String folder = Path.GetDirectoryName(asm.Location); if (File.Exists(folder + Path.DirectorySeparatorChar + "config.json")) { JsonReader reader = new JsonReader(); LocalMod lmod = (LocalMod)reader.Read(File.ReadAllText(folder + Path.DirectorySeparatorChar + "config.json"), typeof(LocalMod)); if (!lmod.localInstall) { logger [lmod.localId].logException((Exception)e.ExceptionObject); } } } } } } }