Skip to content

Commit

Permalink
Implement a workaround for multiple methods patch attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
dymanoid committed Jul 13, 2018
1 parent 04b03bd commit 65d1d17
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 27 deletions.
18 changes: 3 additions & 15 deletions src/RealTime/Core/RealTimeCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -164,7 +164,7 @@ public void Stop()
return;
}

SafeRevertPatches(patcher);
patcher.Revert();

timeAdjustment.Disable();
timeBar.CityEventClick -= CustomTimeBarCityEventClick;
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 11 additions & 10 deletions src/RealTime/Patching/MethodPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,28 @@ public MethodPatcher(params IPatch[] patches)
/// <summary>Applies all patches this object knows about.</summary>
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.");
}

/// <summary>Reverts all patches, if any applied.</summary>
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);
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/RealTime/Patching/PatchBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 65d1d17

Please sign in to comment.