From 08bc2833a9d2cb64bad8f803f74e863cbe33d7bf Mon Sep 17 00:00:00 2001 From: Zetrith Date: Sat, 6 Jan 2024 17:26:36 +0100 Subject: [PATCH] Fix some patches missing a try/catch --- Source/Client/Factions/MultifactionPatches.cs | 2 +- Source/Client/MultiplayerStatic.cs | 66 ++++++------------- 2 files changed, 21 insertions(+), 47 deletions(-) diff --git a/Source/Client/Factions/MultifactionPatches.cs b/Source/Client/Factions/MultifactionPatches.cs index 94abfdca..75e62376 100644 --- a/Source/Client/Factions/MultifactionPatches.cs +++ b/Source/Client/Factions/MultifactionPatches.cs @@ -40,7 +40,7 @@ static IEnumerable Postfix(IEnumerable gizmos, Pawn __instance) yield return new Command_Action { - defaultLabel = $"Change faction relation {Faction.OfPlayer.HostileTo(otherFaction)}", + defaultLabel = "Change faction relation", icon = MultiplayerStatic.ChangeRelationIcon, action = () => { diff --git a/Source/Client/MultiplayerStatic.cs b/Source/Client/MultiplayerStatic.cs index 550864a4..832d772d 100644 --- a/Source/Client/MultiplayerStatic.cs +++ b/Source/Client/MultiplayerStatic.cs @@ -281,6 +281,17 @@ void LogError(string str) var harmony = Multiplayer.harmony; + void TryPatch(MethodBase original, HarmonyMethod prefix = null, HarmonyMethod postfix = null, + HarmonyMethod transpiler = null, HarmonyMethod finalizer = null) + { + try + { + harmony.PatchMeasure(original, prefix, postfix, transpiler, finalizer); + } catch (Exception e) { + LogError($"FAIL: {original.DeclaringType.FullName}:{original.Name} with {e}"); + } + } + SetCategory("Annotated patches"); Assembly.GetCallingAssembly().GetTypes().Do(type => { @@ -314,12 +325,7 @@ void LogError(string str) if (method == null) continue; MethodInfo prefix = AccessTools.Method(typeof(DesignatorPatches), m); - try - { - harmony.PatchMeasure(method, new HarmonyMethod(prefix) { priority = MpPriority.MpFirst }, null, null, new HarmonyMethod(designatorFinalizer)); - } catch (Exception e) { - LogError($"FAIL: {t.FullName}:{method.Name} with {e}"); - } + TryPatch(method, new HarmonyMethod(prefix) { priority = MpPriority.MpFirst }, null, null, new HarmonyMethod(designatorFinalizer)); } } } @@ -353,15 +359,7 @@ void LogError(string str) var ritualMethods = new[] { cannotAssignReason, canEverSpectate }; foreach (MethodBase m in effectMethods.Concat(moteMethods).Concat(fleckMethods).Concat(ritualMethods)) - { - try - { - harmony.PatchMeasure(m, randPatchPrefix, randPatchPostfix); - } catch (Exception e) { - LogError($"FAIL: {m.DeclaringType.FullName}:{m.Name} with {e}"); - } - } - + TryPatch(m, randPatchPrefix, randPatchPostfix); } SetCategory("Non-deterministic patches 2"); @@ -386,20 +384,13 @@ void LogError(string str) // SpawnSetup is patched separately because it sets the map var spawnSetupMethod = t.GetMethod("SpawnSetup", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); if (spawnSetupMethod != null) - harmony.PatchMeasure(spawnSetupMethod, thingMethodPrefixSpawnSetup, finalizer: thingMethodFinalizer); + TryPatch(spawnSetupMethod, thingMethodPrefixSpawnSetup, finalizer: thingMethodFinalizer); foreach ((string m, Type[] args) in thingMethods) { MethodInfo method = t.GetMethod(m, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, null, args, null); if (method != null) - { - try - { - harmony.PatchMeasure(method, thingMethodPrefix, finalizer: thingMethodFinalizer); - } catch (Exception e) { - LogError($"FAIL: {method.DeclaringType.FullName}:{method.Name} with {e}"); - } - } + TryPatch(method, thingMethodPrefix, finalizer: thingMethodFinalizer); } } } @@ -421,14 +412,7 @@ void LogError(string str) { MethodInfo method = t.GetMethod(m, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, null, args, null); if (method != null) - { - try - { - harmony.PatchMeasure(method, prefix, finalizer: finalizer); - } catch (Exception e) { - LogError($"FAIL: {method.DeclaringType.FullName}:{method.Name} with {e}"); - } - } + TryPatch(method, prefix, finalizer: finalizer); } } } @@ -439,8 +423,8 @@ void LogError(string str) var floatSavePrefix = new HarmonyMethod(typeof(ValueSavePatch).GetMethod(nameof(ValueSavePatch.FloatSave_Prefix))); var valueSaveMethod = typeof(Scribe_Values).GetMethod(nameof(Scribe_Values.Look)); - harmony.PatchMeasure(valueSaveMethod.MakeGenericMethod(typeof(double)), doubleSavePrefix); - harmony.PatchMeasure(valueSaveMethod.MakeGenericMethod(typeof(float)), floatSavePrefix); + TryPatch(valueSaveMethod.MakeGenericMethod(typeof(double)), doubleSavePrefix); + TryPatch(valueSaveMethod.MakeGenericMethod(typeof(float)), floatSavePrefix); } SetCategory("Map time gui patches"); @@ -452,25 +436,15 @@ void LogError(string str) var windowMethods = new[] { "DoWindowContents", "WindowUpdate" }; foreach (string m in windowMethods) - harmony.PatchMeasure(typeof(MainTabWindow_Inspect).GetMethod(m), setMapTimePrefix, setMapTimePostfix); + TryPatch(typeof(MainTabWindow_Inspect).GetMethod(m), setMapTimePrefix, setMapTimePostfix); foreach (var t in typeof(InspectTabBase).AllSubtypesAndSelf()) { var method = t.GetMethod("FillTab", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly, null, Type.EmptyTypes, null); if (method != null && !method.IsAbstract) - { - try - { - harmony.PatchMeasure(method, setMapTimePrefix, setMapTimePostfix); - } catch (Exception e) { - LogError($"FAIL: {method.DeclaringType.FullName}:{method.Name} with {e}"); - } - } - + TryPatch(method, setMapTimePrefix, setMapTimePostfix); } } - - SetCategory(""); } }