// 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/. global using LwmCompCache = PerformanceFish.Cache.ByReference; namespace PerformanceFish.Hauling; public record struct LwmCompCacheValue { public CompProperties? CompProperties; public static CompProperties? TryGetLwmProps(Thing slotGroupParent) => ModCompatibility.Types.LWM.CompProperties != null ? slotGroupParent.def.comps?.Find(static props => props.GetType() == ModCompatibility.Types.LWM.CompProperties) : null; static LwmCompCacheValue() => EnsureInitialized(); public static void EnsureInitialized() { if (Interlocked.Exchange(ref _initialized, 1) == 0) LwmCompCache.ValueInitializer = static key => new() { CompProperties = TryGetLwmProps(key) }; } private static volatile int _initialized; public static AccessTools.FieldRef? LwmMaxNumberStacks = TryGetLwmField("maxNumberStacks"), LwmMinNumberStacks = TryGetLwmField("minNumberStacks"); private static AccessTools.FieldRef? TryGetLwmField(string name) => ModCompatibility.Types.LWM.CompProperties is { } propsType ? AccessTools.FieldRefAccess(AccessTools.Field(propsType, name)) : null; }