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

Commit

Permalink
Fixed Filled To Brim
Browse files Browse the repository at this point in the history
  • Loading branch information
Skau committed Apr 16, 2020
1 parent 9af1c8b commit 68549ee
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This is going to be a mod that just fixes up some things in Mount & Blade 2:
* Bow Expert
* Conroi
* Crossbow Expert
* Filled To Brim
* Squires
* Bow
* Mounted Archer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using TaleWorlds.CampaignSystem;
using TaleWorlds.Core;
using HarmonyLib;
using TaleWorlds.CampaignSystem.SandBox.GameComponents.Party;
using static CommunityPatch.HarmonyHelpers;

namespace CommunityPatch.Patches.Perks.Endurance.Riding {

public sealed class FilledToBrimPatch : PatchBase<FilledToBrimPatch> {

public override bool Applied { get; protected set; }

private static readonly MethodInfo TargetMethodInfo = typeof(DefaultInventoryCapacityModel).GetMethod("CalculateInventoryCapacity", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

private static readonly MethodInfo PatchMethodInfoPrefix = typeof(FilledToBrimPatch).GetMethod(nameof(Prefix), BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.DeclaredOnly);

private static readonly MethodInfo PatchMethodInfoPostfix = typeof(FilledToBrimPatch).GetMethod(nameof(Postfix), BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.DeclaredOnly);

public override IEnumerable<MethodBase> GetMethodsChecked() {
yield return TargetMethodInfo;
}

private PerkObject _perk;

private static readonly byte[][] Hashes = {
new byte[] {
// e1.0.11
0x91, 0x8F, 0x69, 0x36, 0xA9, 0x4F, 0xE9, 0x5E,
0x4C, 0x11, 0x97, 0x25, 0x80, 0xBF, 0x34, 0x50,
0x72, 0x06, 0xBC, 0xD7, 0x77, 0x11, 0x96, 0x7B,
0x10, 0x79, 0xA1, 0xD4, 0x8D, 0x6B, 0x8F, 0x43
}
};

public override void Reset()
=> _perk = PerkObject.FindFirst(x => x.Name.GetID() == "jikaakdy");

public override bool? IsApplicable(Game game)
// ReSharper disable once CompareOfFloatsByEqualityOperator
{
var patchInfo = Harmony.GetPatchInfo(TargetMethodInfo);
if (AlreadyPatchedByOthers(patchInfo))
return false;

var hash = TargetMethodInfo.MakeCilSignatureSha256();
return hash.MatchesAnySha256(Hashes);
}

public override void Apply(Game game) {
if (Applied) return;

CommunityPatchSubModule.Harmony.Patch(TargetMethodInfo,
new HarmonyMethod(PatchMethodInfoPrefix),
new HarmonyMethod(PatchMethodInfoPostfix));
Applied = true;
}

// ReSharper disable once InconsistentNaming
[MethodImpl(MethodImplOptions.NoInlining)]
private static void Prefix(ref StatExplainer explanation)
=> explanation ??= new StatExplainer();

// ReSharper disable once InconsistentNaming
[MethodImpl(MethodImplOptions.NoInlining)]
private static void Postfix(ref int __result, MobileParty mobileParty, StatExplainer explanation, bool includeFollowers) {
var perk = ActivePatch._perk;

if (!(mobileParty.LeaderHero?.GetPerkValue(perk) ?? false))
return;

var explainedNumber = new ExplainedNumber(__result, explanation);
var extra = 20 * mobileParty.Party.NumberOfPackAnimals + (includeFollowers ? mobileParty.AttachedParties.Sum(x => x.Party.NumberOfPackAnimals) : 0);
explainedNumber.Add(extra, perk.Name);
__result = (int) explainedNumber.ResultNumber;
}

}

}

0 comments on commit 68549ee

Please sign in to comment.