Skip to content
This repository has been archived by the owner on Mar 29, 2022. It is now read-only.

Commit

Permalink
Merge pull request #290 from iPherian/cleanup_ruler_patch
Browse files Browse the repository at this point in the history
cleanup ruler patch
  • Loading branch information
Tyler-IN authored May 12, 2020
2 parents 508bfcc + f6a9836 commit 3b61b8a
Showing 1 changed file with 3 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
Expand All @@ -18,7 +19,7 @@ public sealed class RulerPatch : PerkPatchBase<RulerPatch> {

private static readonly MethodInfo PatchMethodInfo = typeof(RulerPatch).GetMethod(nameof(CompanionLimitPatched), NonPublic | Static | DeclaredOnly);

private static readonly MethodInfo TownAllTownsGetter = GetGetterForAllTowns();
private static readonly Func<Campaign, IReadOnlyList<Town>> TownAllTownsGetter = AccessTools.PropertyGetter(typeof(Campaign), "AllTowns").BuildInvoker<Func<Campaign, IReadOnlyList<Town>>>();

public RulerPatch() : base("IcgVKFxZ") {
}
Expand Down Expand Up @@ -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<Town> GetAllTowns()
=> (IReadOnlyList<Town>) TownAllTownsGetter.Invoke(Campaign.Current, null);
=> TownAllTownsGetter(Campaign.Current);

}

Expand Down

0 comments on commit 3b61b8a

Please sign in to comment.