Initial commit
This commit is contained in:
commit
d5d9fbf47c
1 changed files with 942 additions and 0 deletions
45
LoaderGuard.cs
Normal file
45
LoaderGuard.cs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace BlockMelonLoader
|
||||
{
|
||||
public static class LoaderGuard
|
||||
{
|
||||
public static void EnsureAllowed(string modName)
|
||||
{
|
||||
EnsureAllowed(modName, AppDomain.CurrentDomain.GetAssemblies(), ShowAlert, Environment.FailFast);
|
||||
}
|
||||
|
||||
internal static void EnsureAllowed(string modName, IEnumerable<Assembly> assemblies,
|
||||
Action<string> showAlert, Action<string> terminate)
|
||||
{
|
||||
foreach (var assembly in assemblies)
|
||||
{
|
||||
if (!string.Equals(assembly.GetName().Name, "MelonLoader", StringComparison.OrdinalIgnoreCase))
|
||||
continue;
|
||||
|
||||
var message = modName + " cannot run while MelonLoader is active.\n\n"
|
||||
+ "Remove MelonLoader and install this mod using Unity Mod Manager.\n\n"
|
||||
+ "The game will close after this message.";
|
||||
// Loader exception handlers can swallow a thrown exception, so termination
|
||||
// must happen even if the native dialog fails.
|
||||
try { showAlert(message); }
|
||||
finally { terminate(message); }
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private static void ShowAlert(string message)
|
||||
{
|
||||
Console.Error.WriteLine(message);
|
||||
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
|
||||
MessageBoxW(IntPtr.Zero, message, "Unsupported mod loader", 0x10 | 0x10000);
|
||||
}
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
|
||||
private static extern int MessageBoxW(IntPtr window, string text, string caption, uint type);
|
||||
}
|
||||
}
|
||||
Reference in a new issue