diff --git a/1.6/Assemblies/PerformanceFish.dll b/1.6/Assemblies/PerformanceFish.dll index 2f33257..81dc01b 100644 Binary files a/1.6/Assemblies/PerformanceFish.dll and b/1.6/Assemblies/PerformanceFish.dll differ diff --git a/1.6/Assemblies/PerformanceFish.pdb b/1.6/Assemblies/PerformanceFish.pdb index 7d25cf5..d23cbad 100644 Binary files a/1.6/Assemblies/PerformanceFish.pdb and b/1.6/Assemblies/PerformanceFish.pdb differ diff --git a/About/PublishedFileId.txt b/About/PublishedFileId.txt new file mode 100644 index 0000000..feb695c --- /dev/null +++ b/About/PublishedFileId.txt @@ -0,0 +1 @@ +3811986740 \ No newline at end of file diff --git a/README.md b/README.md index 81c1e79..13bbd79 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ -NOTE: SWAP TO THE rimworld-1.6 BRANCH BEFORE DOWNLOADING OTHERWISE IT WILL NOT WORK FOR 1.6! # Performance Fish ![](About/Preview.png?raw=true) diff --git a/Source/PerformanceFish/GasGridOptimization.cs b/Source/PerformanceFish/GasGridOptimization.cs index a69bd8c..212ed3e 100644 --- a/Source/PerformanceFish/GasGridOptimization.cs +++ b/Source/PerformanceFish/GasGridOptimization.cs @@ -72,13 +72,89 @@ public sealed class GasGridOptimization : ClassWithFishPrepatches } } +#if V1_6 + public abstract class PackedGasPatch : FishPrepatch + { + public override bool ShowSettings => false; + public override bool Enabled + { + get => base.Enabled && Get().Enabled; + set => base.Enabled = value; + } + + protected static uint PackedDensityAt(GasGrid grid, int index) + { + var grids = grid.ParallelGasGrids(); + return ((uint)grids[3].DensityAt(index) << 24) + | ((uint)grids[2].DensityAt(index) << 16) + | ((uint)grids[1].DensityAt(index) << 8) + | grids[0].DensityAt(index); + } + } + + public sealed class GetDirectIndexPatch : PackedGasPatch + { + public override MethodBase TargetMethodBase { get; } + = AccessTools.DeclaredMethod(typeof(GasGrid), nameof(GasGrid.GetDirect), [typeof(int)]); + + public override void Transpiler(ILProcessor ilProcessor, ModuleDefinition module) + => ilProcessor.ReplaceBodyWith(ReplacementBody); + + public static uint ReplacementBody(GasGrid __instance, int index) + => PackedDensityAt(__instance, index); + } + + public sealed class GetDirectCellPatch : PackedGasPatch + { + public override MethodBase TargetMethodBase { get; } + = AccessTools.DeclaredMethod(typeof(GasGrid), nameof(GasGrid.GetDirect), [typeof(IntVec3)]); + + public override void Transpiler(ILProcessor ilProcessor, ModuleDefinition module) + => ilProcessor.ReplaceBodyWith(ReplacementBody); + + public static uint ReplacementBody(GasGrid __instance, IntVec3 c) + => PackedDensityAt(__instance, c.CellToIndex(__instance.map)); + } + + public sealed class SetPackedDirectPatch : PackedGasPatch + { + public override MethodBase TargetMethodBase { get; } + = AccessTools.DeclaredMethod(typeof(GasGrid), nameof(GasGrid.SetDirect), + [typeof(int), typeof(uint)]); + + public static void Postfix(GasGrid __instance, int index) + { + var density = __instance.gasDensity[index]; + SetDirectPatch.ReplacementBody(__instance, index, (byte)density, (byte)(density >> 8), + (byte)(density >> 16), (byte)(density >> 24)); + } + } + + public sealed class ClearCellUnsafePatch : PackedGasPatch + { + public override MethodBase TargetMethodBase { get; } + = AccessTools.DeclaredMethod(typeof(GasGrid), nameof(GasGrid.ClearCellUnsafe)); + + public static void Postfix(GasGrid __instance, IntVec3 cell) + { + var index = cell.CellToIndex(__instance.map); + var grids = __instance.ParallelGasGrids(); + for (var i = 0; i < grids.Length; i++) + grids[i].SetDirect(index, 0); + } + } +#endif + public sealed class TickPatch : FishPrepatch { public override List LinkedPatches { get; } = [ typeof(SetDirectPatch), -#if V1_4 + typeof(CalculateGasEffectsPatch), typeof(ColorAtPatch), +#if V1_6 + typeof(GetDirectIndexPatch), typeof(GetDirectCellPatch), + typeof(SetPackedDirectPatch), typeof(ClearCellUnsafePatch), #endif typeof(AddGasPatch), typeof(AnyGasAtPatch), typeof(DensityAtPatch), typeof(Debug_ClearAllPatch), typeof(Debug_FillAllPatch), @@ -174,6 +250,35 @@ public sealed class GasGridOptimization : ClassWithFishPrepatches #endif } + public sealed class CalculateGasEffectsPatch : FishPrepatch + { + public override bool ShowSettings => false; + public override bool Enabled + { + get => base.Enabled && Get().Enabled; + set => base.Enabled = value; + } + + public override MethodBase TargetMethodBase { get; } + = AccessTools.PropertyGetter(typeof(GasGrid), nameof(GasGrid.CalculateGasEffects)); + + public override void Transpiler(ILProcessor ilProcessor, ModuleDefinition module) + => ilProcessor.ReplaceBodyWith(ReplacementBody); + + public static bool ReplacementBody(GasGrid __instance) + { + // Vanilla uses this to decide whether to equalize gas through buildings. + var gasGrids = __instance.ParallelGasGrids(); + for (var i = 0; i < gasGrids.Length; i++) + { + if (gasGrids[i].CalculateGasEffects) + return true; + } + + return false; + } + } + #if !V1_4 public class ParallelJob : IJobParallelFor { @@ -186,6 +291,11 @@ public sealed class GasGridOptimization : ClassWithFishPrepatches public sealed class ColorAtPatch : FishPrepatch { public override bool ShowSettings => false; + public override bool Enabled + { + get => base.Enabled && Get().Enabled; + set => base.Enabled = value; + } public override MethodBase TargetMethodBase { get; } #if V1_4