From c4d9bbf5a2ba48a9314b11553f3a7dbb05ea5c32 Mon Sep 17 00:00:00 2001 From: SokyranTheDragon <36712560+SokyranTheDragon@users.noreply.github.com> Date: Mon, 15 Jan 2024 02:46:24 +0100 Subject: [PATCH] Updated Biomes! Core compat (#416) Dropped patches for special terrain (it no longer uses the stopwatch) and hash code calls (the method that was unsafe got dropped, replaced with 2 safe ones). Added extra System RNG patch. The method had System RNG before, but it was unused... It still is unused, but they added a second RNG usage. Updated DebugActions to check Biomes! Core and VFE Core special terrain update methods. --- Source/DebugActions.cs | 6 ++++ Source/Mods/BiomesCore.cs | 60 ++++----------------------------------- 2 files changed, 11 insertions(+), 55 deletions(-) diff --git a/Source/DebugActions.cs b/Source/DebugActions.cs index 55d447b4..1985a680 100644 --- a/Source/DebugActions.cs +++ b/Source/DebugActions.cs @@ -68,6 +68,12 @@ internal enum StuffToSearch AccessTools.DeclaredMethod("HugsLib.ModBase:Update"), AccessTools.DeclaredMethod("HugsLib.ModBase:FixedUpdate"), AccessTools.DeclaredMethod("HugsLib.ModBase:OnGUI"), + AccessTools.DeclaredMethod("BiomesCore.TerrainInstance:Update"), + AccessTools.DeclaredMethod("BiomesCore.TerrainComp:CompUpdate"), + AccessTools.DeclaredMethod("BiomesCore.DefExtensionActive:DoWork", new [] { AccessTools.TypeByName("Verse.TerrainDef") }), + AccessTools.DeclaredMethod("VFECore.TerrainInstance:Update"), + AccessTools.DeclaredMethod("VFECore.TerrainComp:CompUpdate"), + AccessTools.DeclaredMethod("VFECore.DefExtensionActive:DoWork", new [] { AccessTools.TypeByName("Verse.TerrainDef") }), }.Where(x => x != null).ToHashSet(); private static readonly HashSet NonTickingUpdateMethodCalls = new[] diff --git a/Source/Mods/BiomesCore.cs b/Source/Mods/BiomesCore.cs index 915c59f2..36c4f6d1 100644 --- a/Source/Mods/BiomesCore.cs +++ b/Source/Mods/BiomesCore.cs @@ -11,14 +11,8 @@ namespace Multiplayer.Compat [MpCompatFor("BiomesTeam.BiomesCore")] public class BiomesCore { - private static Type terrainCompType; - private static AccessTools.FieldRef terrainCompParentField; - private static AccessTools.FieldRef terrainInstancePositionField; - public BiomesCore(ModContentPack mod) { - LongEventHandler.ExecuteWhenFinished(LatePatch); - // Gizmos (only dev mode gizmos) { var typeNames = new[] @@ -51,7 +45,11 @@ public BiomesCore(ModContentPack mod) // RNG + GenView.ShouldSpawnMotesAt { - // BiomesCore.GenSteps.ValleyPatch:Postfix - System RNG called, but never used + PatchingUtilities.PatchSystemRand(new[] + { + // Two 'new Random()' calls, one is never used + "BiomesCore.GenSteps.ValleyPatch:Postfix", + }, false); PatchingUtilities.PatchPushPopRand(new[] { @@ -59,54 +57,6 @@ public BiomesCore(ModContentPack mod) "BiomesCore.TerrainComp_MoteSpawner:ThrowMote", }); } - - // Stopwatch - { - MpCompat.harmony.Patch(AccessTools.DeclaredMethod("BiomesCore.SpecialTerrainList:TerrainUpdate"), - prefix: new HarmonyMethod(typeof(BiomesCore), nameof(RemoveTerrainUpdateTimeBudget))); - } - } - - private static void LatePatch() - { - // HashCode on unsafe type - { - // Some extra setup for stuff that will be used by the patch - terrainCompType = AccessTools.TypeByName("BiomesCore.TerrainComp"); - terrainCompParentField = AccessTools.FieldRefAccess(terrainCompType, "parent"); - terrainInstancePositionField = AccessTools.FieldRefAccess("BiomesCore.TerrainInstance:positionInt"); - - // Patch the method using hash code to use different object if it's unsupported - MpCompat.harmony.Patch(AccessTools.DeclaredMethod("BiomesCore.ActiveTerrainUtility:HashCodeToMod"), - prefix: new HarmonyMethod(typeof(BiomesCore), nameof(HashCodeOnSafeObject))); - } - } - - private static void RemoveTerrainUpdateTimeBudget(ref long timeBudget) - { - if (MP.IsInMultiplayer) - timeBudget = long.MaxValue; // Basically limitless time - - // The method is limited in updating a max of 1/3 of all active special terrains. - // If we'd want to work on having a performance option of some sort, we'd have to - // base it around amount of terrain updates per tick, instead of basing it on actual time. - } - - private static void HashCodeOnSafeObject(ref object obj) - { - // We don't care out of MP - if (!MP.IsInMultiplayer) - return; - - if (obj is ThingComp comp) - obj = comp.parent; - else if (obj.GetType().IsAssignableFrom(terrainCompType)) - { - // Get the parent field (TerrainInstance) - obj = terrainCompParentField(obj); - // Get the IntVec3 and use that for hash code (since it's safe for MP) - obj = terrainInstancePositionField(obj); - } } } } \ No newline at end of file