This repository has been archived by the owner on Mar 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/CommunityPatch/Patches/Perks/Endurance/Riding/FilledToBrimPatch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
||
} |