71 lines
2.4 KiB
C#
71 lines
2.4 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using Global.API;
|
|
using Global.Shared.Plugin;
|
|
using HarmonyLib;
|
|
using Sandbox.Game.World;
|
|
using VRage.Game;
|
|
using VRage.Utils;
|
|
|
|
namespace Global.Patches
|
|
{
|
|
public class MyScriptManagerPatch
|
|
{
|
|
public static readonly Harmony Patcher = new Harmony("GlobalModApi");
|
|
|
|
public static void ApplyPluginAPIPatch()
|
|
{
|
|
Patches.Patcher.SuffixPatch<MyScriptManager>("AddAssembly",
|
|
BindingFlags.Instance | BindingFlags.NonPublic, new[]
|
|
{
|
|
typeof(MyModContext),
|
|
typeof(MyStringId),
|
|
typeof(Assembly)
|
|
}, "CompileSuffix");
|
|
}
|
|
|
|
private static void CompileSuffix(MyModContext context, MyStringId myStringId, Assembly assembly)
|
|
{
|
|
if (!GlobalPlugin.LegacyConfig.PatchesConfig.EnableModAPIPatch) return;
|
|
var found = false;
|
|
foreach (var type in assembly.GetTypes())
|
|
if (type.Name == "GlobalModApi")
|
|
{
|
|
GlobalInstance.Log.Info($"Found GlobalModApi in file: {type.Name}");
|
|
PatchMethod(type, "IsRunningGlobal");
|
|
PatchMethod(type, "GetAllBlocksOfType");
|
|
PatchMethod(type, "GetAllBlocksOfTypeId");
|
|
found = true;
|
|
}
|
|
}
|
|
|
|
public static void PatchMethod(Type type, string methodName)
|
|
{
|
|
try
|
|
{
|
|
var method = type.GetMethod(methodName,
|
|
BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
|
|
if (method != null)
|
|
{
|
|
Patcher.Patch(method,
|
|
new HarmonyMethod(GetAPIMethod(methodName)));
|
|
GlobalInstance.Log.Info($"Patched {methodName}");
|
|
}
|
|
else
|
|
{
|
|
GlobalInstance.Log.Warning($"Could not find method {methodName} in type {type.FullName}");
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
GlobalInstance.Log.Error(e, $"Failed to patch method {methodName} in {type.FullName}");
|
|
}
|
|
}
|
|
|
|
public static MethodInfo GetAPIMethod(string v)
|
|
{
|
|
return typeof(GlobalServerModApi).GetMethod(v,
|
|
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
|
|
}
|
|
}
|
|
} |