From f6a9836acf9a3b6c587a11838c22b7192ddcf1fd Mon Sep 17 00:00:00 2001 From: iPherian Date: Mon, 11 May 2020 17:27:29 -0700 Subject: [PATCH] cleanup ruler patch BuildInvoker instead of calling Invoke directly --- .../Perks/Intelligence/Steward/RulerPatch.cs | 20 +++---------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/src/CommunityPatch/Patches/Perks/Intelligence/Steward/RulerPatch.cs b/src/CommunityPatch/Patches/Perks/Intelligence/Steward/RulerPatch.cs index 9edffe4..c2e62b6 100644 --- a/src/CommunityPatch/Patches/Perks/Intelligence/Steward/RulerPatch.cs +++ b/src/CommunityPatch/Patches/Perks/Intelligence/Steward/RulerPatch.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; @@ -18,7 +19,7 @@ public sealed class RulerPatch : PerkPatchBase { private static readonly MethodInfo PatchMethodInfo = typeof(RulerPatch).GetMethod(nameof(CompanionLimitPatched), NonPublic | Static | DeclaredOnly); - private static readonly MethodInfo TownAllTownsGetter = GetGetterForAllTowns(); + private static readonly Func> TownAllTownsGetter = AccessTools.PropertyGetter(typeof(Campaign), "AllTowns").BuildInvoker>>(); public RulerPatch() : base("IcgVKFxZ") { } @@ -61,23 +62,8 @@ private static void CompanionLimitPatched(Clan __instance, ref int __result) { __result += GetAllTowns().Count(t => t.Owner.Owner == __instance.Leader); } - private static MethodInfo GetGetterForAllTowns() { - var campaignType = typeof(Campaign); - - try { - var getter = AccessTools.PropertyGetter(campaignType, "AllTowns"); - if (getter != null) - return getter; - } - catch { - // ignored - } - - throw new KeyNotFoundException("can't find a property for AllTowns like behavior."); - } - private static IReadOnlyList GetAllTowns() - => (IReadOnlyList) TownAllTownsGetter.Invoke(Campaign.Current, null); + => TownAllTownsGetter(Campaign.Current); }