Skip to content
24 changes: 20 additions & 4 deletions Mapify/AssetCopiers/AssetCopier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,26 @@ 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;
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);
Comment thread
t0stiman marked this conversation as resolved.
Outdated
}

Mapify.LogError($"Failed to instantiate asset {asset}");
var nothing = new GameObject();
Comment thread
t0stiman marked this conversation as resolved.
Outdated
nothing.SetActive(false);
return nothing;
}

public static void Cleanup()
Expand Down
3 changes: 1 addition & 2 deletions Mapify/AssetCopiers/GameContentCopier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ public class GameContentCopier : AssetCopier
foreach (var module in shop.GetComponentsInChildren<ScanItemCashRegisterModule>())
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);

Expand Down
18 changes: 18 additions & 0 deletions Mapify/Components/ForceActive.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using UnityEngine;

namespace Mapify.Components
{
/// <summary>
/// Force the target Gameobject to stay active
/// </summary>
public class ForceActive: MonoBehaviour
{
public GameObject target;

private void Update()
{
if(target.activeSelf) return;
target.SetActive(true);
}
}
}
10 changes: 10 additions & 0 deletions Mapify/Mapify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,22 @@ private static void Patch()

#region Logging

public static void LogDebugExtreme(object msg)
{
LogDebugExtreme(() => msg);
}

public static void LogDebugExtreme(Func<object> resolver)
{
if (Settings.ExtremelyVerboseLogging)
LogDebug(resolver);
}

public static void LogDebug(object msg)
{
LogDebug(() => msg);
}

public static void LogDebug(Func<object> resolver)
{
if (Settings.VerboseLogging)
Expand Down
57 changes: 57 additions & 0 deletions Mapify/Patches/GlobalShopControllerPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Linq;
using DV.Shops;
using HarmonyLib;

namespace Mapify.Patches
{
[HarmonyPatch(typeof(GlobalShopController), nameof(GlobalShopController.InitializeShopData))]
Comment thread
t0stiman marked this conversation as resolved.
public class GlobalShopControllerPatch
{
private static void Postfix(GlobalShopController __instance)
{
VerifyItemTypeEnum(__instance);
}

/// <summary>
/// Verify that the ItemType enum is up to date.
/// </summary>
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<Editor.ItemType>()
.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);
}
}
}
}
39 changes: 39 additions & 0 deletions Mapify/Patches/ItemLocationForcerPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using DV;
using DV.Shops;
using HarmonyLib;
using Mapify.Components;
using Mapify.Map;
using VLB;

namespace Mapify.Patches
{
/// <summary>
/// Something keeps setting the shop scanner inactive. This patch prevents that.
Comment thread
t0stiman marked this conversation as resolved.
/// </summary>
[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<ShopScanner>(out var shopScanner)) return;

Mapify.LogDebug($"Forcing scanner at '{shopScanner.gameObject.GetPath()}' active");
var forceActive = __instance.gameObject.GetOrAddComponent<ForceActive>();
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<ForceActive>(out var forceActive)) return;
forceActive.enabled = false;
}
}

}
3 changes: 2 additions & 1 deletion Mapify/Patches/PlayerDistanceGameObjectsDisablerPatch.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HarmonyLib;
using DV.Optimizers;
using HarmonyLib;
using UnityEngine;

namespace Mapify.Patches
Expand Down
21 changes: 4 additions & 17 deletions Mapify/Patches/RailwayMeshGeneratorPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vector3> ___sleepersAnchorsPositions, NativeList<float> ___sleepersAnchorsTransformBufferData)
private static bool Prefix(TrackChunk chunk)
{
if (Maps.IsDefaultMap)
return true;
if (Maps.IsDefaultMap) return true;

Track track = chunk.track.GetComponent<Track>();
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<Track>();
return track.generateSleepers;
}
}
}
16 changes: 16 additions & 0 deletions Mapify/Patches/ShelfPlacerPatch.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
1 change: 1 addition & 0 deletions Mapify/SceneInitializers/GameContent/StoreSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
111 changes: 111 additions & 0 deletions Mapify/Utils/UnityUtils.cs
Comment thread
t0stiman marked this conversation as resolved.
Outdated
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using UnityEngine;

namespace Mapify
{
// source: https://discussions.unity.com/t/instantiate-inactive-object/39830/5
public static class UnityUtils
{
/// <summary>
/// Will instantiate an object disabled preventing it from calling Awake/OnEnable.
/// </summary>
public static T InstantiateDisabled<T>(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;
}

/// <summary>
/// Will instantiate an object disabled preventing it from calling Awake/OnEnable.
/// </summary>
public static T InstantiateDisabled<T>(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>(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>(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>(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;
}
}
}
}
}
10 changes: 5 additions & 5 deletions MapifyEditor/Store/ItemType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -118,7 +115,7 @@ public enum ItemType
SwitchSetter = 190,
SwitchAlternating = 191,

//build 99.4
// build 99.4
ItemContainerBriefcase = 192,
ItemContainerFolder = 193,
Nameplate = 194,
Expand All @@ -142,6 +139,9 @@ public enum ItemType
ItemContainerFolderBlue = 212,
ItemContainerFolderRed = 213,
ItemContainerFolderYellow = 214,
ItemContainerRegistrator = 215
ItemContainerRegistrator = 215,

// build 99.7
GooglyEye = 216
}
}
Loading