From 65d1d174d6c3926b9636b1d119d49b01f28727ec Mon Sep 17 00:00:00 2001 From: dymanoid <9433345+dymanoid@users.noreply.github.com> Date: Fri, 13 Jul 2018 17:46:00 +0200 Subject: [PATCH] Implement a workaround for multiple methods patch attempts --- src/RealTime/Core/RealTimeCore.cs | 18 +++--------------- src/RealTime/Patching/MethodPatcher.cs | 21 +++++++++++---------- src/RealTime/Patching/PatchBase.cs | 5 +++-- 3 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/RealTime/Core/RealTimeCore.cs b/src/RealTime/Core/RealTimeCore.cs index 413d078e..5744c5a3 100644 --- a/src/RealTime/Core/RealTimeCore.cs +++ b/src/RealTime/Core/RealTimeCore.cs @@ -90,7 +90,7 @@ public static RealTimeCore Run(RealTimeConfig config, string rootPath, ILocaliza catch (Exception ex) { Log.Error("The 'Real Time' mod failed to perform method redirections: " + ex); - SafeRevertPatches(patcher); + patcher.Revert(); return null; } @@ -120,7 +120,7 @@ public static RealTimeCore Run(RealTimeConfig config, string rootPath, ILocaliza if (!SetupCustomAI(timeInfo, config, gameConnections, eventManager)) { Log.Error("The 'Real Time' mod failed to setup the customized AI and will now be deactivated."); - SafeRevertPatches(patcher); + patcher.Revert(); return null; } @@ -164,7 +164,7 @@ public void Stop() return; } - SafeRevertPatches(patcher); + patcher.Revert(); timeAdjustment.Disable(); timeBar.CityEventClick -= CustomTimeBarCityEventClick; @@ -209,18 +209,6 @@ public void Translate(ILocalizationProvider localizationProvider) UIGraphPatches.Translate(localizationProvider.CurrentCulture); } - private static void SafeRevertPatches(MethodPatcher patcher) - { - try - { - patcher.Revert(); - } - catch (Exception ex) - { - Log.Warning("The 'Real Time' mod failed to revert method redirections: " + ex); - } - } - private static bool SetupCustomAI( TimeInfo timeInfo, RealTimeConfig config, diff --git a/src/RealTime/Patching/MethodPatcher.cs b/src/RealTime/Patching/MethodPatcher.cs index 02582921..996dba63 100644 --- a/src/RealTime/Patching/MethodPatcher.cs +++ b/src/RealTime/Patching/MethodPatcher.cs @@ -37,19 +37,13 @@ public MethodPatcher(params IPatch[] patches) /// Applies all patches this object knows about. public void Apply() { - try - { - Revert(); - } - catch (Exception ex) - { - Log.Warning("The 'Real Time' mod failed to clean up methods before patching: " + ex); - } - + Revert(); foreach (IPatch patch in patches) { patch.ApplyPatch(patcher); } + + Log.Info($"The 'Real Time' mod successfully applied {patches.Length} method patches."); } /// Reverts all patches, if any applied. @@ -57,7 +51,14 @@ public void Revert() { foreach (IPatch patch in patches) { - patch.RevertPatch(patcher); + try + { + patch.RevertPatch(patcher); + } + catch (Exception ex) + { + Log.Error($"The 'Real Time' mod failed to revert a patch {patch}. Error message: " + ex); + } } } diff --git a/src/RealTime/Patching/PatchBase.cs b/src/RealTime/Patching/PatchBase.cs index 3149e5cd..bc58b7df 100644 --- a/src/RealTime/Patching/PatchBase.cs +++ b/src/RealTime/Patching/PatchBase.cs @@ -66,9 +66,10 @@ void IPatch.RevertPatch(IPatcher patcher) throw new ArgumentNullException(nameof(patcher)); } - if (method != null) + MethodInfo patchedMethod = method ?? GetMethod(); + if (patchedMethod != null) { - patcher.RevertPatch(method); + patcher.RevertPatch(patchedMethod); method = null; } }