removed separate CreateVisionTorch function and folded it into the regular Make function

This commit is contained in:
FreezeDriedMangoes 2022-05-25 07:26:50 -04:00
parent b1688c0a6d
commit 41c8e17f49

View File

@ -1,247 +1,231 @@
using NewHorizons.External.Configs; using NewHorizons.External.Configs;
using NewHorizons.External.Modules; using NewHorizons.External.Modules;
using NewHorizons.Handlers; using NewHorizons.Handlers;
using NewHorizons.Utility; using NewHorizons.Utility;
using OWML.Common; using OWML.Common;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using UnityEngine; using UnityEngine;
using Logger = NewHorizons.Utility.Logger; using Logger = NewHorizons.Utility.Logger;
namespace NewHorizons.Builder.Props namespace NewHorizons.Builder.Props
{ {
public static class DetailBuilder public static class DetailBuilder
{ {
private static readonly string VISION_TORCH_PATH = "DreamWorld_Body/Sector_DreamWorld/Sector_Underground/Sector_PrisonCell/Interactibles_PrisonCell/PrisonerSequence/VisionTorchWallSocket/Prefab_IP_VisionTorchItem"; private static Dictionary<PropModule.DetailInfo, GameObject> detailInfoToCorrespondingSpawnedGameObject = new Dictionary<PropModule.DetailInfo, GameObject>();
private static Dictionary<PropModule.DetailInfo, GameObject> detailInfoToCorrespondingSpawnedGameObject = new Dictionary<PropModule.DetailInfo, GameObject>();
public static GameObject GetSpawnedGameObjectByDetailInfo(PropModule.DetailInfo detail)
public static GameObject GetSpawnedGameObjectByDetailInfo(PropModule.DetailInfo detail) {
{ if (!detailInfoToCorrespondingSpawnedGameObject.ContainsKey(detail)) return null;
if (!detailInfoToCorrespondingSpawnedGameObject.ContainsKey(detail)) return null; return detailInfoToCorrespondingSpawnedGameObject[detail];
return detailInfoToCorrespondingSpawnedGameObject[detail]; }
}
public static void Make(GameObject go, Sector sector, PlanetConfig config, IModBehaviour mod, string uniqueModName, PropModule.DetailInfo detail)
public static void Make(GameObject go, Sector sector, PlanetConfig config, IModBehaviour mod, string uniqueModName, PropModule.DetailInfo detail) {
{ GameObject detailGO = null;
GameObject detailGO = null;
if (detail.assetBundle != null)
if (detail.assetBundle != null) {
{ var prefab = AssetBundleUtilities.LoadPrefab(detail.assetBundle, detail.path, mod);
var prefab = AssetBundleUtilities.LoadPrefab(detail.assetBundle, detail.path, mod);
detailGO = MakeDetail(go, sector, prefab, detail.position, detail.rotation, detail.scale, detail.alignToNormal);
detailGO = MakeDetail(go, sector, prefab, detail.position, detail.rotation, detail.scale, detail.alignToNormal); }
} else if (detail.objFilePath != null)
else if (detail.objFilePath != null) {
{ try
try {
{ var prefab = mod.ModHelper.Assets.Get3DObject(detail.objFilePath, detail.mtlFilePath);
var prefab = mod.ModHelper.Assets.Get3DObject(detail.objFilePath, detail.mtlFilePath); AssetBundleUtilities.ReplaceShaders(prefab);
AssetBundleUtilities.ReplaceShaders(prefab); prefab.SetActive(false);
prefab.SetActive(false); detailGO = MakeDetail(go, sector, prefab, detail.position, detail.rotation, detail.scale, detail.alignToNormal);
detailGO = MakeDetail(go, sector, prefab, detail.position, detail.rotation, detail.scale, detail.alignToNormal); }
} catch (Exception e)
catch (Exception e) {
{ Logger.LogError($"Could not load 3d object {detail.objFilePath} with texture {detail.mtlFilePath} : {e.Message}");
Logger.LogError($"Could not load 3d object {detail.objFilePath} with texture {detail.mtlFilePath} : {e.Message}"); }
} }
} else detailGO = MakeDetail(go, sector, detail.path, detail.position, detail.rotation, detail.scale, detail.alignToNormal);
else detailGO = MakeDetail(go, sector, detail.path, detail.position, detail.rotation, detail.scale, detail.alignToNormal);
if (detailGO != null && detail.removeChildren != null)
if (detailGO != null && detail.removeChildren != null) {
{ foreach (var childPath in detail.removeChildren)
foreach (var childPath in detail.removeChildren) {
{ var childObj = detailGO.transform.Find(childPath);
var childObj = detailGO.transform.Find(childPath); if (childObj != null) childObj.gameObject.SetActive(false);
if (childObj != null) childObj.gameObject.SetActive(false); else Logger.LogWarning($"Couldn't find {childPath}");
else Logger.LogWarning($"Couldn't find {childPath}"); }
} }
}
if (detailGO != null && detail.removeComponents)
if (detailGO != null && detail.removeComponents) {
{ // Just swap all the children to a new game object
// Just swap all the children to a new game object var newDetailGO = new GameObject(detailGO.name);
var newDetailGO = new GameObject(detailGO.name); newDetailGO.transform.position = detailGO.transform.position;
newDetailGO.transform.position = detailGO.transform.position; newDetailGO.transform.parent = detailGO.transform.parent;
newDetailGO.transform.parent = detailGO.transform.parent; // Can't modify parents while looping through children bc idk
// Can't modify parents while looping through children bc idk var children = new List<Transform>();
var children = new List<Transform>(); foreach (Transform child in detailGO.transform)
foreach (Transform child in detailGO.transform) {
{ children.Add(child);
children.Add(child); }
} foreach (var child in children)
foreach (var child in children) {
{ child.parent = newDetailGO.transform;
child.parent = newDetailGO.transform; }
} GameObject.Destroy(detailGO);
GameObject.Destroy(detailGO); detailGO = newDetailGO;
detailGO = newDetailGO; }
}
detailInfoToCorrespondingSpawnedGameObject[detail] = detailGO;
detailInfoToCorrespondingSpawnedGameObject[detail] = detailGO; }
}
public static GameObject MakeDetail(GameObject go, Sector sector, string propToClone, MVector3 position, MVector3 rotation, float scale, bool alignWithNormal)
public static GameObject MakeDetail(GameObject go, Sector sector, string propToClone, MVector3 position, MVector3 rotation, float scale, bool alignWithNormal) {
{ var prefab = SearchUtilities.Find(propToClone);
if (propToClone == VISION_TORCH_PATH) return MakeVisionTorch(go, sector, position, rotation, scale, alignWithNormal); if (prefab == null) Logger.LogError($"Couldn't find detail {propToClone}");
return MakeDetail(go, sector, prefab, position, rotation, scale, alignWithNormal);
var prefab = SearchUtilities.Find(propToClone); }
if (prefab == null) Logger.LogError($"Couldn't find detail {propToClone}");
return MakeDetail(go, sector, prefab, position, rotation, scale, alignWithNormal); public static GameObject MakeDetail(GameObject planetGO, Sector sector, GameObject prefab, MVector3 position, MVector3 rotation, float scale, bool alignWithNormal)
} {
if (prefab == null) return null;
public static GameObject MakeDetail(GameObject planetGO, Sector sector, GameObject prefab, MVector3 position, MVector3 rotation, float scale, bool alignWithNormal)
{ GameObject prop = prefab.InstantiateInactive();
if (prefab == null) return null; prop.transform.parent = sector?.transform ?? planetGO.transform;
prop.SetActive(false);
GameObject prop = prefab.InstantiateInactive();
prop.transform.parent = sector?.transform ?? planetGO.transform; if (sector != null) sector.OnOccupantEnterSector += (SectorDetector sd) => OWAssetHandler.OnOccupantEnterSector(prop, sd, sector);
prop.SetActive(false); OWAssetHandler.LoadObject(prop);
if (sector != null) sector.OnOccupantEnterSector += (SectorDetector sd) => OWAssetHandler.OnOccupantEnterSector(prop, sd, sector); foreach (var component in prop.GetComponents<Component>().Concat(prop.GetComponentsInChildren<Component>()))
OWAssetHandler.LoadObject(prop); {
// Enable all children or something
foreach (var component in prop.GetComponents<Component>().Concat(prop.GetComponentsInChildren<Component>())) var enabledField = component?.GetType()?.GetField("enabled");
{ if (enabledField != null && enabledField.FieldType == typeof(bool)) Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => enabledField.SetValue(component, true));
// Enable all children or something
var enabledField = component?.GetType()?.GetField("enabled"); // Fix a bunch of sector stuff
if (enabledField != null && enabledField.FieldType == typeof(bool)) Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => enabledField.SetValue(component, true)); if (sector != null)
{
// Fix a bunch of sector stuff if (component is Sector)
if (sector != null) {
{ (component as Sector)._parentSector = sector;
if (component is Sector) }
{
(component as Sector)._parentSector = sector; // TODO: Make this work or smthng
} if (component is GhostIK) (component as GhostIK).enabled = false;
if (component is GhostEffects) (component as GhostEffects).enabled = false;
// TODO: Make this work or smthng
if (component is GhostIK) (component as GhostIK).enabled = false; if (component is DarkMatterVolume)
if (component is GhostEffects) (component as GhostEffects).enabled = false; {
var probeVisuals = component.gameObject.transform.Find("ProbeVisuals");
if (component is DarkMatterVolume) if (probeVisuals != null) probeVisuals.gameObject.SetActive(true);
{ }
var probeVisuals = component.gameObject.transform.Find("ProbeVisuals");
if (probeVisuals != null) probeVisuals.gameObject.SetActive(true); if (component is SectoredMonoBehaviour)
} {
(component as SectoredMonoBehaviour).SetSector(sector);
if (component is SectoredMonoBehaviour) }
{ else
(component as SectoredMonoBehaviour).SetSector(sector); {
} var sectorField = component?.GetType()?.GetField("_sector");
else if (sectorField != null && sectorField.FieldType == typeof(Sector)) Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => sectorField.SetValue(component, sector));
{ }
var sectorField = component?.GetType()?.GetField("_sector");
if (sectorField != null && sectorField.FieldType == typeof(Sector)) Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(() => sectorField.SetValue(component, sector)); if (component is AnglerfishController)
} {
try
if (component is AnglerfishController) {
{ (component as AnglerfishController)._chaseSpeed += OWPhysics.CalculateOrbitVelocity(planetGO.GetAttachedOWRigidbody(), planetGO.GetComponent<AstroObject>().GetPrimaryBody().GetAttachedOWRigidbody()).magnitude;
try }
{ catch (Exception e)
(component as AnglerfishController)._chaseSpeed += OWPhysics.CalculateOrbitVelocity(planetGO.GetAttachedOWRigidbody(), planetGO.GetComponent<AstroObject>().GetPrimaryBody().GetAttachedOWRigidbody()).magnitude; {
} Logger.LogError($"Couldn't update AnglerFish chase speed: {e.Message}");
catch (Exception e) }
{ }
Logger.LogError($"Couldn't update AnglerFish chase speed: {e.Message}");
} // Fix slide reel
} if (component is SlideCollectionContainer)
{
// Fix slide reel sector.OnOccupantEnterSector.AddListener((_) => (component as SlideCollectionContainer).LoadStreamingTextures());
if (component is SlideCollectionContainer) }
{
sector.OnOccupantEnterSector.AddListener((_) => (component as SlideCollectionContainer).LoadStreamingTextures()); if (component is OWItemSocket)
} {
(component as OWItemSocket)._sector = sector;
if (component is OWItemSocket) }
{
(component as OWItemSocket)._sector = sector; // Fix vision torch
} if (component is VisionTorchItem)
} {
else (component as VisionTorchItem).enabled = true;
{ (component as VisionTorchItem).mindProjectorTrigger.enabled = true;
// Remove things that require sectors. Will just keep extending this as things pop up (component as VisionTorchItem).mindSlideProjector._mindProjectorImageEffect = GameObject.Find("Player_Body/PlayerCamera").GetComponent<MindProjectorImageEffect>();
}
if (component is FogLight || component is SectoredMonoBehaviour) }
{ else
GameObject.DestroyImmediate(component); {
continue; // Remove things that require sectors. Will just keep extending this as things pop up
}
} if (component is FogLight || component is SectoredMonoBehaviour)
{
// Fix a bunch of stuff when done loading GameObject.DestroyImmediate(component);
Main.Instance.ModHelper.Events.Unity.RunWhen(() => Main.IsSystemReady, () => continue;
{ }
try }
{
if (component is Animator) (component as Animator).enabled = true; // Fix a bunch of stuff when done loading
else if (component is Collider) (component as Collider).enabled = true; Main.Instance.ModHelper.Events.Unity.RunWhen(() => Main.IsSystemReady, () =>
else if (component is Renderer) (component as Renderer).enabled = true; {
else if (component is Shape) (component as Shape).enabled = true; try
// If it's not a moving anglerfish make sure the anim controller is regular {
else if (component is AnglerfishAnimController && component.GetComponentInParent<AnglerfishController>() == null) if (component is Animator) (component as Animator).enabled = true;
{ else if (component is Collider) (component as Collider).enabled = true;
Logger.Log("Enabling anglerfish animation"); else if (component is Renderer) (component as Renderer).enabled = true;
var angler = (component as AnglerfishAnimController); else if (component is Shape) (component as Shape).enabled = true;
// Remove any reference to its angler // If it's not a moving anglerfish make sure the anim controller is regular
if (angler._anglerfishController) else if (component is AnglerfishAnimController && component.GetComponentInParent<AnglerfishController>() == null)
{ {
angler._anglerfishController.OnChangeAnglerState -= angler.OnChangeAnglerState; Logger.Log("Enabling anglerfish animation");
angler._anglerfishController.OnAnglerTurn -= angler.OnAnglerTurn; var angler = (component as AnglerfishAnimController);
angler._anglerfishController.OnAnglerSuspended -= angler.OnAnglerSuspended; // Remove any reference to its angler
angler._anglerfishController.OnAnglerUnsuspended -= angler.OnAnglerUnsuspended; if (angler._anglerfishController)
} {
angler.enabled = true; angler._anglerfishController.OnChangeAnglerState -= angler.OnChangeAnglerState;
angler.OnChangeAnglerState(AnglerfishController.AnglerState.Lurking); angler._anglerfishController.OnAnglerTurn -= angler.OnAnglerTurn;
} angler._anglerfishController.OnAnglerSuspended -= angler.OnAnglerSuspended;
} angler._anglerfishController.OnAnglerUnsuspended -= angler.OnAnglerUnsuspended;
catch (Exception e) }
{ angler.enabled = true;
Logger.LogWarning($"Exception when modifying component [{component.GetType().Name}] on [{planetGO.name}] : {e.Message}, {e.StackTrace}"); angler.OnChangeAnglerState(AnglerfishController.AnglerState.Lurking);
} }
}); }
} catch (Exception e)
{
prop.transform.position = position == null ? planetGO.transform.position : planetGO.transform.TransformPoint((Vector3)position); Logger.LogWarning($"Exception when modifying component [{component.GetType().Name}] on [{planetGO.name}] : {e.Message}, {e.StackTrace}");
}
Quaternion rot = rotation == null ? Quaternion.identity : Quaternion.Euler((Vector3)rotation); });
if (alignWithNormal) }
{
// Apply the rotation after aligning it with normal prop.transform.position = position == null ? planetGO.transform.position : planetGO.transform.TransformPoint((Vector3)position);
var up = planetGO.transform.InverseTransformPoint(prop.transform.position).normalized;
prop.transform.rotation = Quaternion.FromToRotation(Vector3.up, up); Quaternion rot = rotation == null ? Quaternion.identity : Quaternion.Euler((Vector3)rotation);
prop.transform.rotation *= rot; if (alignWithNormal)
} {
else // Apply the rotation after aligning it with normal
{ var up = planetGO.transform.InverseTransformPoint(prop.transform.position).normalized;
prop.transform.rotation = planetGO.transform.TransformRotation(rot); prop.transform.rotation = Quaternion.FromToRotation(Vector3.up, up);
} prop.transform.rotation *= rot;
}
prop.transform.localScale = scale != 0 ? Vector3.one * scale : prefab.transform.localScale; else
{
prop.SetActive(true); prop.transform.rotation = planetGO.transform.TransformRotation(rot);
}
return prop;
} prop.transform.localScale = scale != 0 ? Vector3.one * scale : prefab.transform.localScale;
public static GameObject MakeVisionTorch(GameObject planetGO, Sector sector, MVector3 position, MVector3 rotation, float scale, bool alignWithNormal) prop.SetActive(true);
{
if (!Main.HasDLC) return prop;
{ }
Logger.LogError("Could not instantiate Prefab_IP_VisionTorchItem, user does not own the DLC."); }
return null; }
}
var prefab = SearchUtilities.Find(VISION_TORCH_PATH);
if (prefab == null) Logger.LogError($"Couldn't find detail Prefab_IP_VisionTorchItem");
GameObject Prefab_IP_VisionTorchItem = MakeDetail(planetGO, sector, prefab, position, rotation, scale, alignWithNormal);
Prefab_IP_VisionTorchItem.GetComponent<VisionTorchItem>().enabled = true;
Prefab_IP_VisionTorchItem.GetComponent<VisionTorchItem>().mindProjectorTrigger.enabled = true;
Prefab_IP_VisionTorchItem.GetComponent<VisionTorchItem>().mindSlideProjector._mindProjectorImageEffect = GameObject.Find("Player_Body/PlayerCamera").GetComponent<MindProjectorImageEffect>();
return Prefab_IP_VisionTorchItem;
}
}
}