Files
Performance-Fish/Source/PerformanceFish/MeditationUtilityCaching.cs
T
bbradson be3dae46bb Added stat caching, fixed all the bugs
Added RitualObligationTriggers analyzer category,
Fixed defs from other mods with broken market value breaking fish's storage patches,
Fixed storage caches not updating for quality, rotting and similar changes to stored things,
Fixed storage caches not updating capacity when reinstalling storage,
Fixed prison labor compatibility,
Improved multiplayer compatibility,
Fixed ideology style dominance and altar sharing patches throwing errors instead of defaulting to disabled when not having ideology,
Fixed compatibility with RIMMSqol's world pawn GC patch by disabling that patch,
Fixed rare threading issues throwing errors on startup
2024-03-10 20:05:24 +01:00

66 lines
2.1 KiB
C#

// Copyright (c) 2023 bradson
// 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/.
using PsyfocusGainCache
= PerformanceFish.Cache.ByInt<Verse.Pawn, Verse.Thing,
PerformanceFish.MeditationUtilityCaching.PsyfocusCacheValue>;
namespace PerformanceFish;
// Suggested by https://github.com/notfood
public sealed class MeditationUtilityCaching : ClassWithFishPatches
{
public sealed class PsyfocusGainPerTick : FirstPriorityFishPatch
{
public override string? Description { get; } = "Caches psyfocus gain per tick while the target remains "
+ "constant. Impactful in psycasting focused playthroughs";
public override Delegate TargetMethodGroup { get; } = MeditationUtility.PsyfocusGainPerTick;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Prefix(Pawn pawn, Thing? focus, ref float __result, out bool __state)
{
ref var cache = ref PsyfocusGainCache.GetOrAddReference(pawn.thingIDNumber, focus?.thingIDNumber ?? 0);
if (cache.Dirty)
return __state = true;
__result = cache.PsyfocusGain;
return __state = false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Postfix(Pawn pawn, Thing? focus, float __result, bool __state)
{
if (!__state)
return;
UpdateCache(pawn, focus, __result);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void UpdateCache(Pawn pawn, Thing? focus, float __result)
=> PsyfocusGainCache.GetExistingReference(pawn.thingIDNumber, focus?.thingIDNumber ?? 0)
.Update(__result, pawn);
}
public record struct PsyfocusCacheValue()
{
private int _nextRefreshTick = -2;
public float PsyfocusGain;
public void Update(float psyfocusGain, Pawn pawn)
{
PsyfocusGain = psyfocusGain;
_nextRefreshTick = TickHelper.Add(GenTicks.TickRareInterval, pawn.thingIDNumber);
}
public bool Dirty
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => TickHelper.Past(_nextRefreshTick);
}
}
}