Fix compatibility with Rimfactory's digital storage
760/768 being the limit and errors getting thrown when randomly overhauling are issues PRF independently has, with nothing else present too
This commit is contained in:
@@ -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<Verse.Thing, PerformanceFish.Hauling.RimfactoryExtensionCacheValue>;
|
||||
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<DefModExtension, int>? Limit = TryGetExtensionField("limit");
|
||||
|
||||
private static Predicate<DefModExtension> _extensionPredicate = static extension
|
||||
=> extension.GetType() == RimfactoryTypes.ModExtension;
|
||||
|
||||
private static AccessTools.FieldRef<DefModExtension, int>? TryGetExtensionField(string name)
|
||||
=> RimfactoryTypes.ModExtension is { } extensionType
|
||||
? AccessTools.FieldRefAccess<DefModExtension, int>(AccessTools.Field(extensionType, name))
|
||||
: null;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -149,9 +149,11 @@ public sealed class StorageSettingsPatches : ClassWithFishPatches
|
||||
|
||||
private CompProperties? _lwmProps;
|
||||
|
||||
private DefModExtension? _rimfactoryExtension;
|
||||
|
||||
public event Action<StorageSettings>? SettingsChanged;
|
||||
|
||||
public int TotalSlots => _parent!.TrueItemsPerCell(_lwmProps) * _parent!.SlotGroupCellCount();
|
||||
public int TotalSlots => _parent!.TrueTotalSlots(_lwmProps, _rimfactoryExtension);
|
||||
|
||||
public List<Thing> 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
|
||||
<Configurations>v1.4</Configurations>
|
||||
<Version>0.3.999.7</Version>
|
||||
<Version>0.3.999.8</Version>
|
||||
<NoWarn>
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user