diff --git a/1.4/Assemblies/PerformanceFish.dll b/1.4/Assemblies/PerformanceFish.dll index ddf6940..3800413 100644 Binary files a/1.4/Assemblies/PerformanceFish.dll and b/1.4/Assemblies/PerformanceFish.dll differ diff --git a/1.4/Assemblies/PerformanceFish.pdb b/1.4/Assemblies/PerformanceFish.pdb index 76ee268..08bf6a8 100644 Binary files a/1.4/Assemblies/PerformanceFish.pdb and b/1.4/Assemblies/PerformanceFish.pdb differ diff --git a/Source/PerformanceFish/Hauling/RimfactoryExtensionCacheValue.cs b/Source/PerformanceFish/Hauling/RimfactoryExtensionCacheValue.cs new file mode 100644 index 0000000..86e077b --- /dev/null +++ b/Source/PerformanceFish/Hauling/RimfactoryExtensionCacheValue.cs @@ -0,0 +1,45 @@ +// 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 RimfactoryExtensionCache + = PerformanceFish.Cache.ByReference; +using RimfactoryTypes = PerformanceFish.ModCompatibility.Types.Rimfactory; + +namespace PerformanceFish.Hauling; + +public record struct RimfactoryExtensionCacheValue +{ + public DefModExtension? ModExtension; + + public static DefModExtension? TryGetExtension(Thing slotGroupParent) + => RimfactoryTypes.ModExtension != null + ? slotGroupParent.def.modExtensions?.Find(_extensionPredicate) + : null; + + public static bool HasExtension(Thing slotGroupParent) + => RimfactoryTypes.ModExtension != null + && (slotGroupParent.def.modExtensions?.Exists(_extensionPredicate) + ?? false); + + static RimfactoryExtensionCacheValue() => EnsureInitialized(); + + public static void EnsureInitialized() + { + if (Interlocked.Exchange(ref _initialized, 1) == 0) + RimfactoryExtensionCache.ValueInitializer = static key => new() { ModExtension = TryGetExtension(key) }; + } + + private static volatile int _initialized; + + public static AccessTools.FieldRef? Limit = TryGetExtensionField("limit"); + + private static Predicate _extensionPredicate = static extension + => extension.GetType() == RimfactoryTypes.ModExtension; + + private static AccessTools.FieldRef? TryGetExtensionField(string name) + => RimfactoryTypes.ModExtension is { } extensionType + ? AccessTools.FieldRefAccess(AccessTools.Field(extensionType, name)) + : null; +} \ No newline at end of file diff --git a/Source/PerformanceFish/Hauling/SlotGroupExtensions.cs b/Source/PerformanceFish/Hauling/SlotGroupExtensions.cs index 7f4da02..a1eb918 100644 --- a/Source/PerformanceFish/Hauling/SlotGroupExtensions.cs +++ b/Source/PerformanceFish/Hauling/SlotGroupExtensions.cs @@ -18,7 +18,9 @@ public static class SlotGroupExtensions public static bool ShouldHaveDistricts([NotNullWhen(true)] this SlotGroup? slotGroup, out int cellCountIfTrue, out int totalSlotsIfTrue) { - if (slotGroup is null || slotGroup.parent is Building { Spawned: false }) + if (slotGroup is null + || (slotGroup.parent is Building building + && (!building.IsSpawned() || RimfactoryExtensionCacheValue.HasExtension(building)))) { Unsafe.SkipInit(out cellCountIfTrue); goto False; diff --git a/Source/PerformanceFish/Hauling/StorageExtensions.cs b/Source/PerformanceFish/Hauling/StorageExtensions.cs index ea16f52..337cb37 100644 --- a/Source/PerformanceFish/Hauling/StorageExtensions.cs +++ b/Source/PerformanceFish/Hauling/StorageExtensions.cs @@ -9,18 +9,27 @@ public static class StorageExtensions { public static int TrueItemsPerCell(this Thing slotGroupParent) => slotGroupParent.TrueItemsPerCell(slotGroupParent.TryGetLwmCompProperties()); - + public static int TrueItemsPerCell(this Thing slotGroupParent, CompProperties? lwmProps) - => lwmProps != null - ? Math.Max(LwmCompCacheValue.LwmMinNumberStacks!(lwmProps), LwmCompCacheValue.LwmMaxNumberStacks!(lwmProps)) - : slotGroupParent is Building building - ? building.MaxItemsInCell - : 1; + => Math.Max(lwmProps == null + ? 0 + : Math.Max(LwmCompCacheValue.LwmMinNumberStacks!(lwmProps), + LwmCompCacheValue.LwmMaxNumberStacks!(lwmProps)), + slotGroupParent is Building building ? building.MaxItemsInCell : 1); + + public static int TrueTotalSlots(this Thing slotGroupParent, CompProperties? lwmProps, + DefModExtension? rimfactoryExtension) + => Math.Max(rimfactoryExtension == null ? 0 : RimfactoryExtensionCacheValue.Limit!(rimfactoryExtension), + slotGroupParent.TrueItemsPerCell(lwmProps) * slotGroupParent.SlotGroupCellCount()); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static CompProperties? TryGetLwmCompProperties(this Thing thing) => LwmCompCache.GetOrAdd(thing).CompProperties; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static DefModExtension? TryGetRimfactoryExtension(this Thing thing) + => RimfactoryExtensionCache.GetOrAdd(thing).ModExtension; + public static int GetTotalSlots(this SlotGroup slotGroup) => slotGroup.GetTotalSlots(slotGroup.CellsList.Count); public static int GetTotalSlots(this SlotGroup slotGroup, int cellCount) @@ -28,5 +37,9 @@ public static class StorageExtensions ? storage.TrueItemsPerCell() * cellCount : cellCount; - static StorageExtensions() => LwmCompCacheValue.EnsureInitialized(); + static StorageExtensions() + { + LwmCompCacheValue.EnsureInitialized(); + RimfactoryExtensionCacheValue.EnsureInitialized(); + } } \ No newline at end of file diff --git a/Source/PerformanceFish/Hauling/StorageSettingsPatches.cs b/Source/PerformanceFish/Hauling/StorageSettingsPatches.cs index 3ff1298..f7c005a 100644 --- a/Source/PerformanceFish/Hauling/StorageSettingsPatches.cs +++ b/Source/PerformanceFish/Hauling/StorageSettingsPatches.cs @@ -149,9 +149,11 @@ public sealed class StorageSettingsPatches : ClassWithFishPatches private CompProperties? _lwmProps; + private DefModExtension? _rimfactoryExtension; + public event Action? SettingsChanged; - public int TotalSlots => _parent!.TrueItemsPerCell(_lwmProps) * _parent!.SlotGroupCellCount(); + public int TotalSlots => _parent!.TrueTotalSlots(_lwmProps, _rimfactoryExtension); public List StoredThingsOfDef(ThingDef def) => _storedThingsByDef.GetOrAdd(def.shortHash); @@ -167,6 +169,7 @@ public sealed class StorageSettingsPatches : ClassWithFishPatches _storedThingsByDef.Clear(); AllowedToAccept.Clear(); _lwmProps = slotGroupParent.TryGetLwmCompProperties(); + _rimfactoryExtension = slotGroupParent.TryGetRimfactoryExtension(); FreeSlots = TotalSlots; } diff --git a/Source/PerformanceFish/ModCompatibility/Types.cs b/Source/PerformanceFish/ModCompatibility/Types.cs index de48ff7..62a35e8 100644 --- a/Source/PerformanceFish/ModCompatibility/Types.cs +++ b/Source/PerformanceFish/ModCompatibility/Types.cs @@ -29,4 +29,9 @@ public static class Types { public static Type? CompProperties = Reflection.Type("LWM.DeepStorage.Properties"); } + + public static class Rimfactory + { + public static Type? ModExtension = Reflection.Type("ProjectRimFactory.Storage.Editables.DefModExtension_Crate"); + } } \ No newline at end of file diff --git a/Source/PerformanceFish/PerformanceFish.csproj b/Source/PerformanceFish/PerformanceFish.csproj index 52be3fb..660cea5 100644 --- a/Source/PerformanceFish/PerformanceFish.csproj +++ b/Source/PerformanceFish/PerformanceFish.csproj @@ -17,7 +17,7 @@ enable True v1.4 - 0.3.999.7 + 0.3.999.8 1701;1702;IL2121;CS8500;CS9082;CA1000;CA1002;CA1003;CA1707;CA1010;CA1715;CA1716;CA1724;CA1031;CA1033; CA1034;CA1043;CA1051;CA1305;CA1310;CA1710;CA1711;CA1810;CA1812;CA1819;CA1851;CA2002;CA5394;CA1725;CA1062