Reduce repeated ability list reads during pawn ticks
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,39 @@
|
||||
// Copyright (c) 2026
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
#if V1_6
|
||||
using Mono.Cecil;
|
||||
using Mono.Cecil.Cil;
|
||||
using PerformanceFish.Prepatching;
|
||||
|
||||
namespace PerformanceFish;
|
||||
|
||||
public sealed class AbilityTrackerTickOptimization : ClassWithFishPrepatches
|
||||
{
|
||||
public sealed class AbilitiesTickPatch : FishPrepatch
|
||||
{
|
||||
public override string? Description { get; }
|
||||
= "Reads the pawn ability list once per ability tick instead of twice, while still observing list changes between abilities.";
|
||||
|
||||
public override MethodBase TargetMethodBase { get; }
|
||||
= AccessTools.DeclaredMethod(typeof(Pawn_AbilityTracker), nameof(Pawn_AbilityTracker.AbilitiesTick));
|
||||
|
||||
public override void Transpiler(ILProcessor ilProcessor, ModuleDefinition module)
|
||||
=> ilProcessor.ReplaceBodyWith(AbilitiesTick);
|
||||
|
||||
public static void AbilitiesTick(Pawn_AbilityTracker instance)
|
||||
{
|
||||
for (var i = 0;; i++)
|
||||
{
|
||||
var abilities = instance.AllAbilitiesForReading;
|
||||
if (i >= abilities.Count)
|
||||
return;
|
||||
|
||||
abilities[i].AbilityTick();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user