using System; using System.Reflection; using System.Reflection.Emit; using BlockMelonLoader; internal static class BlockMelonLoaderTests { internal static void Run() { var alerts = 0; var terminations = 0; Action alert = message => { if (!message.Contains("SampleMod") || !message.Contains("Unity Mod Manager")) throw new Exception("Missing mod name or installation guidance."); alerts++; }; Action terminate = message => { if (alerts == 0) throw new Exception("Termination preceded notification."); terminations++; }; var unrelated = AppDomain.CurrentDomain.DefineDynamicAssembly( new AssemblyName("MelonLoader.Helper"), AssemblyBuilderAccess.Run); LoaderGuard.EnsureAllowed("SampleMod", new[] { typeof(LoaderGuard).Assembly, unrelated }, alert, terminate); if (alerts != 0 || terminations != 0) throw new Exception("Blocked an unrelated assembly."); var loader = AppDomain.CurrentDomain.DefineDynamicAssembly( new AssemblyName("melonloader"), AssemblyBuilderAccess.Run); LoaderGuard.EnsureAllowed("SampleMod", new[] { loader }, alert, terminate); if (alerts != 1 || terminations != 1) throw new Exception("MelonLoader was not blocked."); try { LoaderGuard.EnsureAllowed("SampleMod", new[] { loader }, message => { throw new InvalidOperationException("Dialog unavailable."); }, message => terminations++); throw new Exception("Expected the simulated dialog failure."); } catch (InvalidOperationException) { } if (terminations != 2) throw new Exception("Dialog failure bypassed termination."); } }