44 lines
1.8 KiB
C#
44 lines
1.8 KiB
C#
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<string> alert = message =>
|
|
{
|
|
if (!message.Contains("SampleMod") || !message.Contains("Unity Mod Manager"))
|
|
throw new Exception("Missing mod name or installation guidance.");
|
|
alerts++;
|
|
};
|
|
Action<string> 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.");
|
|
}
|
|
}
|