Skip to content

Instantly share code, notes, and snippets.

@Citillara
Created December 16, 2023 23:03
Show Gist options
  • Save Citillara/009efcc0bb4b50d0f0d4cc9d4926be9d to your computer and use it in GitHub Desktop.
Save Citillara/009efcc0bb4b50d0f0d4cc9d4926be9d to your computer and use it in GitHub Desktop.
using HarmonyLib;
using System;
using System.Reflection;
using TaleWorlds.CampaignSystem;
using TaleWorlds.Localization;
namespace BannerlordCitillaraTweaks.Patches
{
internal class BannerKingsPatch
{
public static void ApplyPatch(Harmony harmony, Assembly bannerKingsAssembly)
{
Type targetType = bannerKingsAssembly.GetType("BannerKings.Models.Vanilla.BKPartyWageModel");
if (targetType != null)
{
MethodInfo targetMethod = targetType.GetMethod("GetCharacterLevelWage", BindingFlags.Public | BindingFlags.Instance);
if (targetMethod != null)
{
MethodInfo postfix = typeof(BannerKingsPatch).GetMethod(nameof(GetCharacterLevelWagePostfix), BindingFlags.NonPublic | BindingFlags.Static);
harmony.Patch(targetMethod, postfix: new HarmonyMethod(postfix));
}
}
Type councilMemberType = bannerKingsAssembly.GetType("BannerKings.Managers.Court.CouncilMember");
if (councilMemberType != null)
{
MethodInfo isValidCandidateMethod = councilMemberType.GetMethod("IsValidCandidate", BindingFlags.Public | BindingFlags.Instance);
if (isValidCandidateMethod != null)
{
MethodInfo prefix = typeof(BannerKingsPatch).GetMethod(nameof(IsValidCandidatePrefix), BindingFlags.Static | BindingFlags.Public);
harmony.Patch(isValidCandidateMethod, prefix: new HarmonyMethod(prefix));
}
MethodInfo isValidCandidateInternalMethod = councilMemberType.GetMethod("IsValidCandidateInternal", BindingFlags.NonPublic | BindingFlags.Instance);
if (isValidCandidateInternalMethod != null)
{
MethodInfo prefix = typeof(BannerKingsPatch).GetMethod(nameof(IsValidCandidatePrefix), BindingFlags.Static | BindingFlags.Public);
harmony.Patch(isValidCandidateInternalMethod, prefix: new HarmonyMethod(prefix));
}
}
}
// Fixes a null reference by returning false before the function can execute
public static bool IsValidCandidatePrefix(Hero candidate, ref bool __result)
{
if (candidate == null)
{
__result = false;
return false; // Skip the original method
}
return true; // Run the original method
}
// Makes sure that characters from different tiers have different wages
public static void GetCharacterLevelWagePostfix(ref int __result, CharacterObject character)
{
__result += character.Tier;
}
public static bool IsValidCandidatePrefix(Hero candidate, ref ValueTuple<bool, TextObject> __result)
{
if (candidate == null)
{
__result = (false, null); // Set both parts of the tuple
return false; // Skip the original method
}
return true; // Run the original method
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment