From e225ea52a853052b5b66ba0f3db77a447f660611 Mon Sep 17 00:00:00 2001 From: t0stiman <18124323+t0stiman@users.noreply.github.com> Date: Wed, 13 Aug 2025 13:59:23 +0200 Subject: [PATCH 01/12] new using directives for b99.7 --- Mapify/Patches/PlayerDistanceGameObjectsDisablerPatch.cs | 3 ++- Mapify/SceneInitializers/GameContent/StoreSetup.cs | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Mapify/Patches/PlayerDistanceGameObjectsDisablerPatch.cs b/Mapify/Patches/PlayerDistanceGameObjectsDisablerPatch.cs index 0e883ba9..7bfcec3f 100644 --- a/Mapify/Patches/PlayerDistanceGameObjectsDisablerPatch.cs +++ b/Mapify/Patches/PlayerDistanceGameObjectsDisablerPatch.cs @@ -1,4 +1,5 @@ -using HarmonyLib; +using DV.Optimizers; +using HarmonyLib; using UnityEngine; namespace Mapify.Patches diff --git a/Mapify/SceneInitializers/GameContent/StoreSetup.cs b/Mapify/SceneInitializers/GameContent/StoreSetup.cs index 90bdfdfa..94d1316d 100644 --- a/Mapify/SceneInitializers/GameContent/StoreSetup.cs +++ b/Mapify/SceneInitializers/GameContent/StoreSetup.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using DV.CashRegister; +using DV.Optimizers; using DV.Printers; using DV.Shops; using DV.Utils; From 0a3b805dfbcf813332e7afec41e3b24290efb274 Mon Sep 17 00:00:00 2001 From: t0stiman <18124323+t0stiman@users.noreply.github.com> Date: Wed, 13 Aug 2025 14:00:01 +0200 Subject: [PATCH 02/12] fix missing sleepers on tracks --- Mapify/Patches/RailwayMeshGeneratorPatch.cs | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/Mapify/Patches/RailwayMeshGeneratorPatch.cs b/Mapify/Patches/RailwayMeshGeneratorPatch.cs index 17801e18..3d2f2c68 100644 --- a/Mapify/Patches/RailwayMeshGeneratorPatch.cs +++ b/Mapify/Patches/RailwayMeshGeneratorPatch.cs @@ -52,25 +52,12 @@ private static bool Prefix(RailwayMeshGenerator __instance, TrackChunk chunk, Li [HarmonyPatch(typeof(RailwayMeshGenerator), nameof(RailwayMeshGenerator.UpdateSleepersData))] public class RailwayMeshGenerator_UpdateSleepersData_Patch { - private static bool Prefix(TrackChunk chunk, ref JobHandle ___sleepersHandle, NativeList ___sleepersAnchorsPositions, NativeList ___sleepersAnchorsTransformBufferData) + private static bool Prefix(TrackChunk chunk) { - if (Maps.IsDefaultMap) - return true; + if (Maps.IsDefaultMap) return true; - Track track = chunk.track.GetComponent(); - if (!track.generateSleepers) - return false; - - ___sleepersHandle = new PlaceSleepersAppendJob( - ___sleepersAnchorsTransformBufferData, - ___sleepersAnchorsPositions, - chunk.pointSet, - chunk.minIndex, - chunk.maxIndex, - chunk.track.baseType.randomizeAnchorDirection, - chunk.track.baseType.sleeperVerticalOffset - ).Schedule(___sleepersHandle); - return false; + var track = chunk.track.GetComponent(); + return track.generateSleepers; } } } From 77d5607b1aa9fe14766f1b9ee2d1ab95926c440a Mon Sep 17 00:00:00 2001 From: t0stiman <18124323+t0stiman@users.noreply.github.com> Date: Tue, 28 Jan 2025 14:29:54 +0100 Subject: [PATCH 03/12] ez debug logging --- Mapify/Mapify.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Mapify/Mapify.cs b/Mapify/Mapify.cs index 068ddac3..5b3fd9c7 100644 --- a/Mapify/Mapify.cs +++ b/Mapify/Mapify.cs @@ -57,12 +57,22 @@ private static void Patch() #region Logging + public static void LogDebugExtreme(object msg) + { + LogDebugExtreme(() => msg); + } + public static void LogDebugExtreme(Func resolver) { if (Settings.ExtremelyVerboseLogging) LogDebug(resolver); } + public static void LogDebug(object msg) + { + LogDebug(() => msg); + } + public static void LogDebug(Func resolver) { if (Settings.VerboseLogging) From d734c0d23b530ad11365abf6dfd97ddcba9f676b Mon Sep 17 00:00:00 2001 From: t0stiman <18124323+t0stiman@users.noreply.github.com> Date: Wed, 13 Aug 2025 15:43:01 +0200 Subject: [PATCH 04/12] update item list --- MapifyEditor/Store/ItemType.cs | 10 +++++----- MapifyEditor/Vanilla/VanillaAsset.cs | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/MapifyEditor/Store/ItemType.cs b/MapifyEditor/Store/ItemType.cs index 7a1bf099..9cfdd797 100644 --- a/MapifyEditor/Store/ItemType.cs +++ b/MapifyEditor/Store/ItemType.cs @@ -106,9 +106,6 @@ public enum ItemType PaintCan = 178, PaintCanMuseum = 179, PaintCanSand = 180, - Crate = 181, - CratePlastic = 182, - PaperBox = 183, BeaconAmber = 184, BeaconRed = 185, BeaconBlue = 186, @@ -118,7 +115,7 @@ public enum ItemType SwitchSetter = 190, SwitchAlternating = 191, - //build 99.4 + // build 99.4 ItemContainerBriefcase = 192, ItemContainerFolder = 193, Nameplate = 194, @@ -142,6 +139,9 @@ public enum ItemType ItemContainerFolderBlue = 212, ItemContainerFolderRed = 213, ItemContainerFolderYellow = 214, - ItemContainerRegistrator = 215 + ItemContainerRegistrator = 215, + + // build 99.7 + GooglyEye = 216 } } diff --git a/MapifyEditor/Vanilla/VanillaAsset.cs b/MapifyEditor/Vanilla/VanillaAsset.cs index be2e8fd1..91f0c202 100644 --- a/MapifyEditor/Vanilla/VanillaAsset.cs +++ b/MapifyEditor/Vanilla/VanillaAsset.cs @@ -182,9 +182,6 @@ public enum VanillaAsset StoreItemPaintCan = 178, StoreItemPaintCanMuseum = 179, StoreItemPaintCanSand = 180, - StoreItemCrate = 181, - StoreItemCratePlastic = 182, - StoreItemPaperBox = 183, StoreItemBeaconAmber = 184, StoreItemBeaconRed = 185, StoreItemBeaconBlue = 186, @@ -218,7 +215,10 @@ public enum VanillaAsset StoreItemItemContainerFolderBlue = 212, StoreItemItemContainerFolderRed = 213, StoreItemItemContainerFolderYellow = 214, - StoreItemItemContainerRegistrator = 215 + StoreItemItemContainerRegistrator = 215, + + // build 99.7 + StoreItemGooglyEye = 216 #endregion } From d3b13d741a95b540ea74e096f38929b48bf29d0e Mon Sep 17 00:00:00 2001 From: t0stiman <18124323+t0stiman@users.noreply.github.com> Date: Wed, 13 Aug 2025 15:43:41 +0200 Subject: [PATCH 05/12] store items verifications --- Mapify/AssetCopiers/AssetCopier.cs | 15 ++++-- Mapify/Patches/GlobalShopControllerPatch.cs | 57 +++++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 Mapify/Patches/GlobalShopControllerPatch.cs diff --git a/Mapify/AssetCopiers/AssetCopier.cs b/Mapify/AssetCopiers/AssetCopier.cs index 168382ce..eb7a372a 100644 --- a/Mapify/AssetCopiers/AssetCopier.cs +++ b/Mapify/AssetCopiers/AssetCopier.cs @@ -19,9 +19,18 @@ public abstract class AssetCopier public static GameObject Instantiate(VanillaAsset asset, bool active = true, bool originShift = true) { Mapify.LogDebug(() => "Instantiating asset: " + asset); - var gameObject = GameObject.Instantiate(prefabs[asset], originShift ? WorldMover.OriginShiftParent : null); - gameObject.SetActive(active); - return gameObject; + + if(prefabs.TryGetValue(asset, out var prefab)) + { + var gameObject = GameObject.Instantiate(prefab, originShift ? WorldMover.OriginShiftParent : null); + gameObject.SetActive(active); + return gameObject; + } + + Mapify.LogError($"Failed to instantiate asset {asset}"); + var nothing = new GameObject(); + nothing.SetActive(false); + return nothing; } public static void Cleanup() diff --git a/Mapify/Patches/GlobalShopControllerPatch.cs b/Mapify/Patches/GlobalShopControllerPatch.cs new file mode 100644 index 00000000..6b3865b0 --- /dev/null +++ b/Mapify/Patches/GlobalShopControllerPatch.cs @@ -0,0 +1,57 @@ +using System; +using System.Linq; +using DV.Shops; +using HarmonyLib; + +namespace Mapify.Patches +{ + [HarmonyPatch(typeof(GlobalShopController), nameof(GlobalShopController.InitializeShopData))] + public class GlobalShopControllerPatch + { + private static void Postfix(GlobalShopController __instance) + { + VerifyItemTypeEnum(__instance); + } + + /// + /// Verify that the ItemType enum is up to date. + /// + private static void VerifyItemTypeEnum(GlobalShopController __instance) + { + var inGameItems = __instance.shopItemsData.Select(dat => dat.item.itemPrefabName.ToLower().Replace("_", "")).ToArray(); + + var inModItems = Enum.GetValues(typeof(Editor.ItemType)) + .Cast() + .Select(itemEnum => itemEnum.ToString().ToLower()) + .ToArray(); + + bool verifiedSuccessfully = true; + + foreach (var inModItem in inModItems) + { + if(inGameItems.Contains(inModItem)) continue; + + Mapify.LogError($"{nameof(ItemType)} '{inModItem}' does not exist in-game"); + verifiedSuccessfully = false; + } + + foreach (var inGameItem in inGameItems) + { + if(inModItems.Contains(inGameItem)) continue; + + // the keys are intentionally left out + if(inGameItem.StartsWith("key")) continue; + + Mapify.LogWarning($"Item '{inGameItem}' does not exist in enum {nameof(ItemType)}"); + } + + if(verifiedSuccessfully) return; + + Mapify.LogDebug("Items in game:"); + foreach (var inGameItem in inGameItems) + { + Mapify.LogDebug(inGameItem); + } + } + } +} From d2514c48ba502e97d7815beef241b79eea73f5ab Mon Sep 17 00:00:00 2001 From: t0stiman <18124323+t0stiman@users.noreply.github.com> Date: Wed, 13 Aug 2025 18:06:44 +0200 Subject: [PATCH 06/12] fix shops part 1 --- Mapify/AssetCopiers/AssetCopier.cs | 10 ++- Mapify/Patches/ShelfPlacerPatch.cs | 16 +++++ Mapify/Utils/UnityUtils.cs | 111 +++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 Mapify/Patches/ShelfPlacerPatch.cs create mode 100644 Mapify/Utils/UnityUtils.cs diff --git a/Mapify/AssetCopiers/AssetCopier.cs b/Mapify/AssetCopiers/AssetCopier.cs index eb7a372a..edc2de8c 100644 --- a/Mapify/AssetCopiers/AssetCopier.cs +++ b/Mapify/AssetCopiers/AssetCopier.cs @@ -22,9 +22,13 @@ public static GameObject Instantiate(VanillaAsset asset, bool active = true, boo if(prefabs.TryGetValue(asset, out var prefab)) { - var gameObject = GameObject.Instantiate(prefab, originShift ? WorldMover.OriginShiftParent : null); - gameObject.SetActive(active); - return gameObject; + var parent = originShift ? WorldMover.OriginShiftParent : null; + if (active) + { + return GameObject.Instantiate(prefab, parent); + } + + return UnityUtils.InstantiateDisabled(prefab, parent); } Mapify.LogError($"Failed to instantiate asset {asset}"); diff --git a/Mapify/Patches/ShelfPlacerPatch.cs b/Mapify/Patches/ShelfPlacerPatch.cs new file mode 100644 index 00000000..b5e601c6 --- /dev/null +++ b/Mapify/Patches/ShelfPlacerPatch.cs @@ -0,0 +1,16 @@ +using DV.Shops; +using HarmonyLib; +using Mapify.Map; + +namespace Mapify.Patches +{ + [HarmonyPatch(typeof(ShelfPlacer), nameof(ShelfPlacer.TryPlaceOnAnyShelf))] + public class ShelfPlacer_TryPlaceOnAnyShelf_Patch + { + private static void Postfix(ShelfItem item, bool __result) + { + if(Maps.IsDefaultMap || !__result) return; + item.gameObject.SetActive(true); + } + } +} diff --git a/Mapify/Utils/UnityUtils.cs b/Mapify/Utils/UnityUtils.cs new file mode 100644 index 00000000..a368c4cd --- /dev/null +++ b/Mapify/Utils/UnityUtils.cs @@ -0,0 +1,111 @@ +using UnityEngine; + +namespace Mapify +{ + // source: https://discussions.unity.com/t/instantiate-inactive-object/39830/5 + public static class UnityUtils + { + /// + /// Will instantiate an object disabled preventing it from calling Awake/OnEnable. + /// + public static T InstantiateDisabled(T original, Transform parent = null, bool worldPositionStays = false) where T : Object + { + if (!GetActiveState(original)) + { + return Object.Instantiate(original, parent, worldPositionStays); + } + + (GameObject coreObject, Transform coreObjectTransform) = CreateDisabledCoreObject(parent); + T instance = Object.Instantiate(original, coreObjectTransform, worldPositionStays); + SetActiveState(instance, false); + SetParent(instance, parent, worldPositionStays); + Object.Destroy(coreObject); + return instance; + } + + /// + /// Will instantiate an object disabled preventing it from calling Awake/OnEnable. + /// + public static T InstantiateDisabled(T original, Vector3 position, Quaternion rotation, Transform parent = null) where T : Object + { + if (!GetActiveState(original)) + { + return Object.Instantiate(original, position, rotation, parent); + } + + (GameObject coreObject, Transform coreObjectTransform) = CreateDisabledCoreObject(parent); + T instance = Object.Instantiate(original, position, rotation, coreObjectTransform); + SetActiveState(instance, false); + SetParent(instance, parent, false); + Object.Destroy(coreObject); + return instance; + } + + private static (GameObject coreObject, Transform coreObjectTransform) CreateDisabledCoreObject(Transform parent = null) + { + GameObject coreObject = new GameObject(string.Empty); + coreObject.SetActive(false); + Transform coreObjectTransform = coreObject.transform; + coreObjectTransform.SetParent(parent); + + return (coreObject, coreObjectTransform); + } + + private static bool GetActiveState(T @object) where T : Object + { + switch (@object) + { + case GameObject gameObject: + { + return gameObject.activeSelf; + } + case Component component: + { + return component.gameObject.activeSelf; + } + default: + { + return false; + } + } + } + + private static void SetActiveState(T @object, bool state) where T : Object + { + switch (@object) + { + case GameObject gameObject: + { + gameObject.SetActive(state); + + break; + } + case Component component: + { + component.gameObject.SetActive(state); + + break; + } + } + } + + private static void SetParent(T @object, Transform parent, bool worldPositionStays) where T : Object + { + switch (@object) + { + case GameObject gameObject: + { + gameObject.transform.SetParent(parent, worldPositionStays); + + break; + } + case Component component: + { + component.transform.SetParent(parent, worldPositionStays); + + break; + } + } + } + } +} From 0e138ecc34db4d62eee3b804bc57f1cb2ac68984 Mon Sep 17 00:00:00 2001 From: t0stiman <18124323+t0stiman@users.noreply.github.com> Date: Thu, 14 Aug 2025 14:13:30 +0200 Subject: [PATCH 07/12] fix missing scanner in shops --- Mapify/Components/ForceActive.cs | 18 +++++++++++ Mapify/Patches/ItemLocationForcerPatch.cs | 39 +++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 Mapify/Components/ForceActive.cs create mode 100644 Mapify/Patches/ItemLocationForcerPatch.cs diff --git a/Mapify/Components/ForceActive.cs b/Mapify/Components/ForceActive.cs new file mode 100644 index 00000000..b5ac006b --- /dev/null +++ b/Mapify/Components/ForceActive.cs @@ -0,0 +1,18 @@ +using UnityEngine; + +namespace Mapify.Components +{ + /// + /// Force the target Gameobject to stay active + /// + public class ForceActive: MonoBehaviour + { + public GameObject target; + + private void Update() + { + if(target.activeSelf) return; + target.SetActive(true); + } + } +} diff --git a/Mapify/Patches/ItemLocationForcerPatch.cs b/Mapify/Patches/ItemLocationForcerPatch.cs new file mode 100644 index 00000000..9d880e04 --- /dev/null +++ b/Mapify/Patches/ItemLocationForcerPatch.cs @@ -0,0 +1,39 @@ +using DV; +using DV.Shops; +using HarmonyLib; +using Mapify.Components; +using Mapify.Map; +using VLB; + +namespace Mapify.Patches +{ + /// + /// Something keeps setting the shop scanner inactive. This patch prevents that. + /// + [HarmonyPatch(typeof(ItemLocationForcer), nameof(ItemLocationForcer.Awake))] + public class ItemLocationForcer_Awake_Patch + { + private static void Postfix(ItemLocationForcer __instance) + { + if(__instance.itemGO == null || Maps.IsDefaultMap) return; + if(!__instance.itemGO.TryGetComponent(out var shopScanner)) return; + + Mapify.LogDebug($"Forcing scanner at '{shopScanner.gameObject.GetPath()}' active"); + var forceActive = __instance.gameObject.GetOrAddComponent(); + forceActive.enabled = true; + forceActive.target = shopScanner.gameObject; + } + } + + [HarmonyPatch(typeof(ItemLocationForcer), nameof(ItemLocationForcer.OnDisable))] + public class ItemLocationForcer_OnDisable_Patch + { + private static void Postfix(ItemLocationForcer __instance) + { + if(__instance.itemGO == null || Maps.IsDefaultMap) return; + if(!__instance.itemGO.TryGetComponent(out var forceActive)) return; + forceActive.enabled = false; + } + } + +} From d21e7a5607922c19ec661f5dc80669d88b4a04a9 Mon Sep 17 00:00:00 2001 From: t0stiman <18124323+t0stiman@users.noreply.github.com> Date: Thu, 14 Aug 2025 14:34:03 +0200 Subject: [PATCH 08/12] fix shop poster removal --- Mapify/AssetCopiers/GameContentCopier.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Mapify/AssetCopiers/GameContentCopier.cs b/Mapify/AssetCopiers/GameContentCopier.cs index b0ed8bd6..8057ad67 100644 --- a/Mapify/AssetCopiers/GameContentCopier.cs +++ b/Mapify/AssetCopiers/GameContentCopier.cs @@ -114,8 +114,7 @@ public class GameContentCopier : AssetCopier foreach (var module in shop.GetComponentsInChildren()) Object.Destroy(module.gameObject); - Object.Destroy(shop.transform.FindChildByName("Stopwatch")); - Object.Destroy(shop.transform.FindChildByName("PosterAnchor")); + Object.Destroy(shop.gameObject.FindChildByName("PosterAnchor")); yield return (VanillaAsset.StoreObject, shop.gameObject); From 0bcba5f2b2a8a805f6b0ff68b683d392520b1590 Mon Sep 17 00:00:00 2001 From: t0stiman <18124323+t0stiman@users.noreply.github.com> Date: Thu, 14 Aug 2025 19:34:12 +0200 Subject: [PATCH 09/12] fix VanillaAsset copying --- Mapify/AssetCopiers/AssetCopier.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Mapify/AssetCopiers/AssetCopier.cs b/Mapify/AssetCopiers/AssetCopier.cs index edc2de8c..af5b6460 100644 --- a/Mapify/AssetCopiers/AssetCopier.cs +++ b/Mapify/AssetCopiers/AssetCopier.cs @@ -18,16 +18,19 @@ public abstract class AssetCopier public static GameObject Instantiate(VanillaAsset asset, bool active = true, bool originShift = true) { - Mapify.LogDebug(() => "Instantiating asset: " + asset); + Mapify.LogDebug($"Instantiating asset: {asset}"); if(prefabs.TryGetValue(asset, out var prefab)) { var parent = originShift ? WorldMover.OriginShiftParent : null; if (active) { - return GameObject.Instantiate(prefab, parent); + var instantiated = GameObject.Instantiate(prefab, parent); + instantiated.SetActive(true); + return instantiated; } + // instantiate disabled so Awake, Start etc. don't run return UnityUtils.InstantiateDisabled(prefab, parent); } From 867d09d089ce605d43fdf856d6dd6b95e31cc2ac Mon Sep 17 00:00:00 2001 From: t0stiman <18124323+t0stiman@users.noreply.github.com> Date: Fri, 15 Aug 2025 15:09:37 +0200 Subject: [PATCH 10/12] add #if DEBUG to GlobalShopController patch --- Mapify.sln | 9 +++++++-- Mapify/Patches/GlobalShopControllerPatch.cs | 4 +++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Mapify.sln b/Mapify.sln index ddea985f..8c26d1ae 100644 --- a/Mapify.sln +++ b/Mapify.sln @@ -7,11 +7,16 @@ EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Release|Any CPU = Release|Any CPU + Debug|Any CPU = Debug|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {57D181A9-9D9D-4EBA-BBDD-9172EEB79983}.Release|Any CPU.ActiveCfg = Release|Any CPU - {57D181A9-9D9D-4EBA-BBDD-9172EEB79983}.Release|Any CPU.Build.0 = Release|Any CPU {57D181A9-9D9D-4EBA-BBDD-9172EEB79984}.Release|Any CPU.ActiveCfg = Release|Any CPU {57D181A9-9D9D-4EBA-BBDD-9172EEB79984}.Release|Any CPU.Build.0 = Release|Any CPU + {57D181A9-9D9D-4EBA-BBDD-9172EEB79984}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {57D181A9-9D9D-4EBA-BBDD-9172EEB79984}.Debug|Any CPU.Build.0 = Debug|Any CPU + {57D181A9-9D9D-4EBA-BBDD-9172EEB79983}.Release|Any CPU.ActiveCfg = Release|Any CPU + {57D181A9-9D9D-4EBA-BBDD-9172EEB79983}.Release|Any CPU.Build.0 = Release|Any CPU + {57D181A9-9D9D-4EBA-BBDD-9172EEB79983}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {57D181A9-9D9D-4EBA-BBDD-9172EEB79983}.Debug|Any CPU.Build.0 = Debug|Any CPU EndGlobalSection EndGlobal diff --git a/Mapify/Patches/GlobalShopControllerPatch.cs b/Mapify/Patches/GlobalShopControllerPatch.cs index 6b3865b0..c167f7c3 100644 --- a/Mapify/Patches/GlobalShopControllerPatch.cs +++ b/Mapify/Patches/GlobalShopControllerPatch.cs @@ -5,8 +5,9 @@ namespace Mapify.Patches { +#if DEBUG [HarmonyPatch(typeof(GlobalShopController), nameof(GlobalShopController.InitializeShopData))] - public class GlobalShopControllerPatch + public class GlobalShopController_InitializeShopData_Patch { private static void Postfix(GlobalShopController __instance) { @@ -54,4 +55,5 @@ private static void VerifyItemTypeEnum(GlobalShopController __instance) } } } +#endif } From 536274c9961413e777fd739ae6795dbf2e575afd Mon Sep 17 00:00:00 2001 From: t0stiman <18124323+t0stiman@users.noreply.github.com> Date: Sun, 17 Aug 2025 12:06:04 +0200 Subject: [PATCH 11/12] get rid of InstantiateDisabled --- Mapify/AssetCopiers/AssetCopier.cs | 23 ++---- Mapify/Utils/UnityUtils.cs | 111 ----------------------------- 2 files changed, 4 insertions(+), 130 deletions(-) delete mode 100644 Mapify/Utils/UnityUtils.cs diff --git a/Mapify/AssetCopiers/AssetCopier.cs b/Mapify/AssetCopiers/AssetCopier.cs index af5b6460..aa4c4902 100644 --- a/Mapify/AssetCopiers/AssetCopier.cs +++ b/Mapify/AssetCopiers/AssetCopier.cs @@ -18,26 +18,11 @@ public abstract class AssetCopier public static GameObject Instantiate(VanillaAsset asset, bool active = true, bool originShift = true) { - Mapify.LogDebug($"Instantiating asset: {asset}"); + Mapify.LogDebug("Instantiating asset: " + asset); - if(prefabs.TryGetValue(asset, out var prefab)) - { - var parent = originShift ? WorldMover.OriginShiftParent : null; - if (active) - { - var instantiated = GameObject.Instantiate(prefab, parent); - instantiated.SetActive(true); - return instantiated; - } - - // instantiate disabled so Awake, Start etc. don't run - return UnityUtils.InstantiateDisabled(prefab, parent); - } - - Mapify.LogError($"Failed to instantiate asset {asset}"); - var nothing = new GameObject(); - nothing.SetActive(false); - return nothing; + var gameObject = GameObject.Instantiate(prefabs[asset], originShift ? WorldMover.OriginShiftParent : null); + gameObject.SetActive(active); + return gameObject; } public static void Cleanup() diff --git a/Mapify/Utils/UnityUtils.cs b/Mapify/Utils/UnityUtils.cs deleted file mode 100644 index a368c4cd..00000000 --- a/Mapify/Utils/UnityUtils.cs +++ /dev/null @@ -1,111 +0,0 @@ -using UnityEngine; - -namespace Mapify -{ - // source: https://discussions.unity.com/t/instantiate-inactive-object/39830/5 - public static class UnityUtils - { - /// - /// Will instantiate an object disabled preventing it from calling Awake/OnEnable. - /// - public static T InstantiateDisabled(T original, Transform parent = null, bool worldPositionStays = false) where T : Object - { - if (!GetActiveState(original)) - { - return Object.Instantiate(original, parent, worldPositionStays); - } - - (GameObject coreObject, Transform coreObjectTransform) = CreateDisabledCoreObject(parent); - T instance = Object.Instantiate(original, coreObjectTransform, worldPositionStays); - SetActiveState(instance, false); - SetParent(instance, parent, worldPositionStays); - Object.Destroy(coreObject); - return instance; - } - - /// - /// Will instantiate an object disabled preventing it from calling Awake/OnEnable. - /// - public static T InstantiateDisabled(T original, Vector3 position, Quaternion rotation, Transform parent = null) where T : Object - { - if (!GetActiveState(original)) - { - return Object.Instantiate(original, position, rotation, parent); - } - - (GameObject coreObject, Transform coreObjectTransform) = CreateDisabledCoreObject(parent); - T instance = Object.Instantiate(original, position, rotation, coreObjectTransform); - SetActiveState(instance, false); - SetParent(instance, parent, false); - Object.Destroy(coreObject); - return instance; - } - - private static (GameObject coreObject, Transform coreObjectTransform) CreateDisabledCoreObject(Transform parent = null) - { - GameObject coreObject = new GameObject(string.Empty); - coreObject.SetActive(false); - Transform coreObjectTransform = coreObject.transform; - coreObjectTransform.SetParent(parent); - - return (coreObject, coreObjectTransform); - } - - private static bool GetActiveState(T @object) where T : Object - { - switch (@object) - { - case GameObject gameObject: - { - return gameObject.activeSelf; - } - case Component component: - { - return component.gameObject.activeSelf; - } - default: - { - return false; - } - } - } - - private static void SetActiveState(T @object, bool state) where T : Object - { - switch (@object) - { - case GameObject gameObject: - { - gameObject.SetActive(state); - - break; - } - case Component component: - { - component.gameObject.SetActive(state); - - break; - } - } - } - - private static void SetParent(T @object, Transform parent, bool worldPositionStays) where T : Object - { - switch (@object) - { - case GameObject gameObject: - { - gameObject.transform.SetParent(parent, worldPositionStays); - - break; - } - case Component component: - { - component.transform.SetParent(parent, worldPositionStays); - - break; - } - } - } - } -} From c910d9d44e90d098946296a5946380997c9ce72c Mon Sep 17 00:00:00 2001 From: t0stiman <18124323+t0stiman@users.noreply.github.com> Date: Sun, 17 Aug 2025 12:11:13 +0200 Subject: [PATCH 12/12] ForceActive -> Reactivate --- Mapify/Components/{ForceActive.cs => Reactivate.cs} | 4 +++- Mapify/Patches/ItemLocationForcerPatch.cs | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) rename Mapify/Components/{ForceActive.cs => Reactivate.cs} (81%) diff --git a/Mapify/Components/ForceActive.cs b/Mapify/Components/Reactivate.cs similarity index 81% rename from Mapify/Components/ForceActive.cs rename to Mapify/Components/Reactivate.cs index b5ac006b..20b3dbc8 100644 --- a/Mapify/Components/ForceActive.cs +++ b/Mapify/Components/Reactivate.cs @@ -5,14 +5,16 @@ namespace Mapify.Components /// /// Force the target Gameobject to stay active /// - public class ForceActive: MonoBehaviour + public class Reactivate: MonoBehaviour { public GameObject target; private void Update() { if(target.activeSelf) return; + target.SetActive(true); + Destroy(this); } } } diff --git a/Mapify/Patches/ItemLocationForcerPatch.cs b/Mapify/Patches/ItemLocationForcerPatch.cs index 9d880e04..9a0ce3d8 100644 --- a/Mapify/Patches/ItemLocationForcerPatch.cs +++ b/Mapify/Patches/ItemLocationForcerPatch.cs @@ -19,9 +19,9 @@ private static void Postfix(ItemLocationForcer __instance) if(!__instance.itemGO.TryGetComponent(out var shopScanner)) return; Mapify.LogDebug($"Forcing scanner at '{shopScanner.gameObject.GetPath()}' active"); - var forceActive = __instance.gameObject.GetOrAddComponent(); - forceActive.enabled = true; - forceActive.target = shopScanner.gameObject; + var reactivate = __instance.gameObject.GetOrAddComponent(); + reactivate.enabled = true; + reactivate.target = shopScanner.gameObject; } } @@ -31,8 +31,8 @@ public class ItemLocationForcer_OnDisable_Patch private static void Postfix(ItemLocationForcer __instance) { if(__instance.itemGO == null || Maps.IsDefaultMap) return; - if(!__instance.itemGO.TryGetComponent(out var forceActive)) return; - forceActive.enabled = false; + if(!__instance.itemGO.TryGetComponent(out var reactivate)) return; + reactivate.enabled = false; } }