From 5ee6d5b6850400101d1b044519841dd7635e71d9 Mon Sep 17 00:00:00 2001 From: ZaneYork Date: Sun, 31 May 2020 20:09:57 +0800 Subject: [PATCH] Remove MonoMod dependency --- src/SMAPI/Constants.cs | 4 +--- .../RewriteFacades/HarmonyInstanceFacade.cs | 2 +- .../RewriteFacades/HarmonyInstanceMethods.cs | 6 +++--- src/SMAPI/Framework/Patching/GamePatcher.cs | 12 ++---------- src/SMAPI/Metadata/InstructionMetadata.cs | 2 +- src/SMAPI/SMAPI.csproj | 6 ------ src/SMAPI/SMainActivity.cs | 2 +- 7 files changed, 9 insertions(+), 25 deletions(-) diff --git a/src/SMAPI/Constants.cs b/src/SMAPI/Constants.cs index d1708af6..0da8abed 100644 --- a/src/SMAPI/Constants.cs +++ b/src/SMAPI/Constants.cs @@ -110,7 +110,7 @@ namespace StardewModdingAPI /// The language code for non-translated mod assets. internal static LocalizedContentManager.LanguageCode DefaultLanguage { get; } = LocalizedContentManager.LanguageCode.en; - internal static bool MonoModInit { get; set; } = true; + internal static bool HarmonyEnabled { get; set; } = true; /********* @@ -202,8 +202,6 @@ namespace StardewModdingAPI { typeof(StardewValley.Game1).Assembly, // note: includes Netcode types on Linux/Mac typeof(Microsoft.Xna.Framework.Vector2).Assembly, - typeof(MonoMod.RuntimeDetour.HarmonyDetourBridge).Assembly, - typeof(MonoMod.Utils.Platform).Assembly, typeof(HarmonyLib.Harmony).Assembly, typeof(Mono.Cecil.MethodDefinition).Assembly, typeof(StardewModdingAPI.IManifest).Assembly, diff --git a/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceFacade.cs b/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceFacade.cs index bda5f9c0..98097638 100644 --- a/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceFacade.cs +++ b/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceFacade.cs @@ -34,7 +34,7 @@ namespace StardewModdingAPI.Framework.ModLoading.RewriteFacades if (original != null) original = original.GetDeclaredMember(); - if (!Constants.MonoModInit) + if (!Constants.HarmonyEnabled) return null; // call Harmony 2.0 and show a detailed exception if it fails try diff --git a/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceMethods.cs b/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceMethods.cs index 5389c8fa..cf6ef859 100644 --- a/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceMethods.cs +++ b/src/SMAPI/Framework/ModLoading/RewriteFacades/HarmonyInstanceMethods.cs @@ -14,19 +14,19 @@ namespace StardewModdingAPI.Framework.ModLoading.RewriteFacades HarmonyMethod transpiler = null, HarmonyMethod finalizer = null) { - if (Constants.MonoModInit) + if (Constants.HarmonyEnabled) return instance.Patch(original, prefix, postfix, transpiler, finalizer); else return null; } public static void PatchAll(Harmony instance) { - if (Constants.MonoModInit) + if (Constants.HarmonyEnabled) instance.PatchAll(); } public static void PatchAllToAssembly(Harmony instance, Assembly assembly) { - if (Constants.MonoModInit) + if (Constants.HarmonyEnabled) instance.PatchAll(assembly); } } diff --git a/src/SMAPI/Framework/Patching/GamePatcher.cs b/src/SMAPI/Framework/Patching/GamePatcher.cs index 8baa47aa..8fcd9023 100644 --- a/src/SMAPI/Framework/Patching/GamePatcher.cs +++ b/src/SMAPI/Framework/Patching/GamePatcher.cs @@ -29,25 +29,17 @@ namespace StardewModdingAPI.Framework.Patching /// The patches to apply. public void Apply(params IHarmonyPatch[] patches) { - if (!HarmonyDetourBridge.Initialized && Constants.MonoModInit) - { - try { - HarmonyDetourBridge.Init(); - } - catch { Constants.MonoModInit = false; } - } - Harmony harmony = new Harmony("io.smapi"); foreach (IHarmonyPatch patch in patches) { try { - if(Constants.MonoModInit) + if(Constants.HarmonyEnabled) patch.Apply(harmony); } catch (Exception ex) { - Constants.MonoModInit = false; + Constants.HarmonyEnabled = false; this.Monitor.Log($"Couldn't apply runtime patch '{patch.Name}' to the game. Some SMAPI features may not work correctly. See log file for details.", LogLevel.Error); this.Monitor.Log(ex.GetLogSummary(), LogLevel.Trace); } diff --git a/src/SMAPI/Metadata/InstructionMetadata.cs b/src/SMAPI/Metadata/InstructionMetadata.cs index e37eb87c..015a4446 100644 --- a/src/SMAPI/Metadata/InstructionMetadata.cs +++ b/src/SMAPI/Metadata/InstructionMetadata.cs @@ -126,7 +126,7 @@ namespace StardewModdingAPI.Metadata yield return new Harmony1AssemblyRewriter(); // MonoMod fix - if (!Constants.MonoModInit) + if (!Constants.HarmonyEnabled) { yield return new MethodToAnotherStaticMethodRewriter(typeof(Harmony), (method) => method.Name == "Patch", typeof(HarmonyInstanceMethods), "Patch"); yield return new MethodToAnotherStaticMethodRewriter(typeof(Harmony), (method) => method.Name == "PatchAll" && method.Parameters.Count == 0, typeof(HarmonyInstanceMethods), "PatchAll"); diff --git a/src/SMAPI/SMAPI.csproj b/src/SMAPI/SMAPI.csproj index fd332df1..9443b248 100644 --- a/src/SMAPI/SMAPI.csproj +++ b/src/SMAPI/SMAPI.csproj @@ -109,12 +109,6 @@ ..\Loader\libs\MonoGame.Framework.dll - - ..\Loader\libs\MonoMod.RuntimeDetour.dll - - - ..\Loader\libs\MonoMod.Utils.dll - diff --git a/src/SMAPI/SMainActivity.cs b/src/SMAPI/SMainActivity.cs index 3a6ad9d4..e1521d51 100644 --- a/src/SMAPI/SMainActivity.cs +++ b/src/SMAPI/SMainActivity.cs @@ -132,7 +132,7 @@ namespace StardewModdingAPI { var settings = JsonConvert.DeserializeObject(System.IO.File.ReadAllText(Constants.ApiUserConfigPath)); modPath = settings.ModsPath; - Constants.MonoModInit = !settings.DisableMonoMod; + Constants.HarmonyEnabled = !settings.DisableMonoMod; } if (string.IsNullOrWhiteSpace(modPath))