From 096ac539a1c09956cb3d205d8e4bb92307d1c4b0 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Sat, 9 Jul 2022 16:13:32 -0700 Subject: [PATCH 1/3] make Find better (cache more) --- NewHorizons/Utility/SearchUtilities.cs | 37 +++++++++++++++----------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/NewHorizons/Utility/SearchUtilities.cs b/NewHorizons/Utility/SearchUtilities.cs index 18f3299e..cd728301 100644 --- a/NewHorizons/Utility/SearchUtilities.cs +++ b/NewHorizons/Utility/SearchUtilities.cs @@ -88,29 +88,34 @@ namespace NewHorizons.Utility public static GameObject FindChild(this GameObject g, string childPath) => g.transform.Find(childPath)?.gameObject; + /// + /// finds active or inactive object by path, + /// or recursively finds an active object by name + /// public static GameObject Find(string path, bool warn = true) { if (CachedGameObjects.TryGetValue(path, out var go)) return go; go = GameObject.Find(path); - if (go != null) return go; - - var names = path.Split('/'); - var rootName = names[0]; - var root = SceneManager.GetActiveScene().GetRootGameObjects().FirstOrDefault(x => x.name == rootName); - if (root == null) - { - if (warn) Logger.LogWarning($"Couldn't find root object in path ({path})"); - return null; - } - - var childPath = names.Skip(1).Join(delimiter: "/"); - go = root.FindChild(childPath); if (go == null) { - var name = names.Last(); - if (warn) Logger.LogWarning($"Couldn't find object in path ({path}), will look for potential matches for name {name}"); - go = FindObjectOfTypeAndName(name); + // find inactive use root + transform.find + var names = path.Split('/'); + var rootName = names[0]; + var root = SceneManager.GetActiveScene().GetRootGameObjects().FirstOrDefault(x => x.name == rootName); + if (root == null) + { + if (warn) Logger.LogWarning($"Couldn't find root object in path ({path})"); + return null; + } + + var childPath = names.Skip(1).Join(delimiter: "/"); + go = root.FindChild(childPath); + if (go == null) + { + if (warn) Logger.LogWarning($"Couldn't find child object in path ({path})"); + return null; + } } CachedGameObjects.Add(path, go); From 58f852bd030598df9276f7cb93480231f45e1fa7 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Sat, 9 Jul 2022 16:17:01 -0700 Subject: [PATCH 2/3] rename to SectorBuilder --- NewHorizons/Builder/General/SectorBuilder.cs | 2 +- NewHorizons/Handlers/PlanetCreationHandler.cs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/NewHorizons/Builder/General/SectorBuilder.cs b/NewHorizons/Builder/General/SectorBuilder.cs index 8093b429..729871e2 100644 --- a/NewHorizons/Builder/General/SectorBuilder.cs +++ b/NewHorizons/Builder/General/SectorBuilder.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using UnityEngine; namespace NewHorizons.Builder.General { - public static class MakeSector + public static class SectorBuilder { public static Sector Make(GameObject planetBody, OWRigidbody owRigidBody, float sphereOfInfluence) { diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index c7b3e814..9f59f66f 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -195,7 +195,7 @@ namespace NewHorizons.Handlers var rb = existingPlanet.GetComponent(); - var sector = MakeSector.Make(existingPlanet, rb, GetSphereOfInfluence(body)); + var sector = SectorBuilder.Make(existingPlanet, rb, GetSphereOfInfluence(body)); sector.name = $"Sector-{existingPlanet.GetComponentsInChildren().Count()}"; SharedGenerateBody(body, existingPlanet, sector, rb); @@ -310,7 +310,7 @@ namespace NewHorizons.Handlers var owRigidBody = RigidBodyBuilder.Make(go, body.Config); var ao = AstroObjectBuilder.Make(go, null, body.Config); - var sector = MakeSector.Make(go, owRigidBody, 2000f); + var sector = SectorBuilder.Make(go, owRigidBody, 2000f); ao._rootSector = sector; ao._type = AstroObject.Type.None; @@ -365,7 +365,7 @@ namespace NewHorizons.Handlers var sphereOfInfluence = GetSphereOfInfluence(body); - var sector = MakeSector.Make(go, owRigidBody, sphereOfInfluence * 2f); + var sector = SectorBuilder.Make(go, owRigidBody, sphereOfInfluence * 2f); ao._rootSector = sector; if (body.Config.Base.surfaceGravity != 0) From 2219b802531f29331a46303a4c2b035b1ce3bae4 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Sat, 9 Jul 2022 16:21:07 -0700 Subject: [PATCH 3/3] big rename --- NewHorizons/Builder/Props/BrambleNodeBuilder.cs | 4 ++-- NewHorizons/Builder/Props/DetailBuilder.cs | 4 ++-- NewHorizons/Builder/Props/NomaiTextBuilder.cs | 12 ++++++------ NewHorizons/Builder/Props/ProjectionBuilder.cs | 8 ++++---- NewHorizons/Builder/Props/RaftBuilder.cs | 4 ++-- NewHorizons/Builder/Props/TornadoBuilder.cs | 4 ++-- .../{OWAssetHandler.cs => StreamingHandler.cs} | 9 ++++++--- NewHorizons/Main.cs | 2 +- 8 files changed, 25 insertions(+), 22 deletions(-) rename NewHorizons/Handlers/{OWAssetHandler.cs => StreamingHandler.cs} (91%) diff --git a/NewHorizons/Builder/Props/BrambleNodeBuilder.cs b/NewHorizons/Builder/Props/BrambleNodeBuilder.cs index 64f75376..b947e16e 100644 --- a/NewHorizons/Builder/Props/BrambleNodeBuilder.cs +++ b/NewHorizons/Builder/Props/BrambleNodeBuilder.cs @@ -187,8 +187,8 @@ namespace NewHorizons.Builder.Props var path = config.isSeed ? brambleSeedPrefabPath : brambleNodePrefabPath; var brambleNode = SearchUtilities.Find(path).InstantiateInactive(); - OWAssetHandler.LoadObject(brambleNode); - sector.OnOccupantEnterSector += (sd) => OWAssetHandler.OnOccupantEnterSector(brambleNode, sd, sector); + StreamingHandler.HookStreaming(brambleNode); + sector.OnOccupantEnterSector += (sd) => StreamingHandler.OnOccupantEnterSector(brambleNode, sd, sector); brambleNode.transform.parent = sector.transform; brambleNode.transform.position = go.transform.TransformPoint(config.position); diff --git a/NewHorizons/Builder/Props/DetailBuilder.cs b/NewHorizons/Builder/Props/DetailBuilder.cs index 0d1c8c7a..684cb7e6 100644 --- a/NewHorizons/Builder/Props/DetailBuilder.cs +++ b/NewHorizons/Builder/Props/DetailBuilder.cs @@ -109,8 +109,8 @@ namespace NewHorizons.Builder.Props prop.transform.parent = sector?.transform ?? planetGO.transform; prop.SetActive(false); - if (sector != null) sector.OnOccupantEnterSector += (SectorDetector sd) => OWAssetHandler.OnOccupantEnterSector(prop, sd, sector); - OWAssetHandler.LoadObject(prop); + if (sector != null) sector.OnOccupantEnterSector += (SectorDetector sd) => StreamingHandler.OnOccupantEnterSector(prop, sd, sector); + StreamingHandler.HookStreaming(prop); foreach (var component in prop.GetComponents().Concat(prop.GetComponentsInChildren())) { diff --git a/NewHorizons/Builder/Props/NomaiTextBuilder.cs b/NewHorizons/Builder/Props/NomaiTextBuilder.cs index 0fa1fd58..c0a821cd 100644 --- a/NewHorizons/Builder/Props/NomaiTextBuilder.cs +++ b/NewHorizons/Builder/Props/NomaiTextBuilder.cs @@ -201,8 +201,8 @@ namespace NewHorizons.Builder.Props AddTranslation(xmlPath); // Make sure the computer model is loaded - OWAssetHandler.LoadObject(computerObject); - sector.OnOccupantEnterSector.AddListener((x) => OWAssetHandler.LoadObject(computerObject)); + StreamingHandler.HookStreaming(computerObject); + sector.OnOccupantEnterSector.AddListener((x) => StreamingHandler.HookStreaming(computerObject)); computerObject.SetActive(true); conversationInfoToCorrespondingSpawnedGameObject[info] = computerObject; @@ -249,8 +249,8 @@ namespace NewHorizons.Builder.Props AddTranslation(xmlPath); // Make sure the computer model is loaded - OWAssetHandler.LoadObject(cairnObject); - sector.OnOccupantEnterSector.AddListener((x) => OWAssetHandler.LoadObject(cairnObject)); + StreamingHandler.HookStreaming(cairnObject); + sector.OnOccupantEnterSector.AddListener((x) => StreamingHandler.HookStreaming(cairnObject)); conversationInfoToCorrespondingSpawnedGameObject[info] = cairnObject; break; } @@ -281,8 +281,8 @@ namespace NewHorizons.Builder.Props AddTranslation(xmlPath); // Make sure the recorder model is loaded - OWAssetHandler.LoadObject(recorderObject); - sector.OnOccupantEnterSector.AddListener((x) => OWAssetHandler.LoadObject(recorderObject)); + StreamingHandler.HookStreaming(recorderObject); + sector.OnOccupantEnterSector.AddListener((x) => StreamingHandler.HookStreaming(recorderObject)); recorderObject.SetActive(true); diff --git a/NewHorizons/Builder/Props/ProjectionBuilder.cs b/NewHorizons/Builder/Props/ProjectionBuilder.cs index 12d87696..a70b0d7d 100644 --- a/NewHorizons/Builder/Props/ProjectionBuilder.cs +++ b/NewHorizons/Builder/Props/ProjectionBuilder.cs @@ -129,8 +129,8 @@ namespace NewHorizons.Builder.Props // Idk why but it wants reveals to be comma delimited not a list if (info.reveals != null) slideCollectionContainer._shipLogOnComplete = string.Join(",", info.reveals); - OWAssetHandler.LoadObject(slideReelObj); - sector.OnOccupantEnterSector.AddListener((x) => OWAssetHandler.LoadObject(slideReelObj)); + StreamingHandler.HookStreaming(slideReelObj); + sector.OnOccupantEnterSector.AddListener((x) => StreamingHandler.HookStreaming(slideReelObj)); slideReelObj.SetActive(true); } @@ -180,8 +180,8 @@ namespace NewHorizons.Builder.Props slideCollectionContainer.slideCollection = slideCollection; - OWAssetHandler.LoadObject(projectorObj); - sector.OnOccupantEnterSector.AddListener((x) => OWAssetHandler.LoadObject(projectorObj)); + StreamingHandler.HookStreaming(projectorObj); + sector.OnOccupantEnterSector.AddListener((x) => StreamingHandler.HookStreaming(projectorObj)); // Change the picture on the lens var lens = projectorObj.transform.Find("Spotlight/Prop_IP_SingleSlideProjector/Projector_Lens").GetComponent(); diff --git a/NewHorizons/Builder/Props/RaftBuilder.cs b/NewHorizons/Builder/Props/RaftBuilder.cs index 5632ad46..c052dc3b 100644 --- a/NewHorizons/Builder/Props/RaftBuilder.cs +++ b/NewHorizons/Builder/Props/RaftBuilder.cs @@ -29,8 +29,8 @@ namespace NewHorizons.Builder.Props raftObject.transform.position = planetGO.transform.TransformPoint(info.position); raftObject.transform.rotation = planetGO.transform.TransformRotation(Quaternion.identity); - sector.OnOccupantEnterSector += (sd) => OWAssetHandler.OnOccupantEnterSector(raftObject, sd, sector); - OWAssetHandler.LoadObject(raftObject); + sector.OnOccupantEnterSector += (sd) => StreamingHandler.OnOccupantEnterSector(raftObject, sd, sector); + StreamingHandler.HookStreaming(raftObject); var raftController = raftObject.GetComponent(); // Since awake already ran we have to unhook these events diff --git a/NewHorizons/Builder/Props/TornadoBuilder.cs b/NewHorizons/Builder/Props/TornadoBuilder.cs index 2624206b..bbb64db9 100644 --- a/NewHorizons/Builder/Props/TornadoBuilder.cs +++ b/NewHorizons/Builder/Props/TornadoBuilder.cs @@ -136,8 +136,8 @@ namespace NewHorizons.Builder.Props controller._midBone.localPosition = controller._midStartPos; controller._topBone.localPosition = controller._topStartPos; - OWAssetHandler.LoadObject(tornadoGO); - sector.OnOccupantEnterSector += (sd) => OWAssetHandler.LoadObject(tornadoGO); + StreamingHandler.HookStreaming(tornadoGO); + sector.OnOccupantEnterSector += (sd) => StreamingHandler.HookStreaming(tornadoGO); tornadoGO.GetComponentInChildren().enabled = true; diff --git a/NewHorizons/Handlers/OWAssetHandler.cs b/NewHorizons/Handlers/StreamingHandler.cs similarity index 91% rename from NewHorizons/Handlers/OWAssetHandler.cs rename to NewHorizons/Handlers/StreamingHandler.cs index dc60fb23..1c20d549 100644 --- a/NewHorizons/Handlers/OWAssetHandler.cs +++ b/NewHorizons/Handlers/StreamingHandler.cs @@ -6,7 +6,7 @@ namespace NewHorizons.Handlers /// /// handles streaming meshes so they stay loaded /// - public static class OWAssetHandler + public static class StreamingHandler { private static Dictionary _materialCache; private static Dictionary> _objectCache; @@ -19,7 +19,7 @@ namespace NewHorizons.Handlers public static void OnOccupantEnterSector(GameObject obj, SectorDetector sd, Sector sector) { - LoadObject(obj); + HookStreaming(obj); // If its too laggy put this back idk /* @@ -30,7 +30,10 @@ namespace NewHorizons.Handlers */ } - public static void LoadObject(GameObject obj) + /// + /// makes it so that this object's streaming stuff will be connected to the given sector + /// + public static void HookStreaming(GameObject obj, Sector sector = null) { var assetBundles = new List(); diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index ac72d5d3..e412a85a 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -272,7 +272,7 @@ namespace NewHorizons BrambleDimensionBuilder.Init(); BrambleNodeBuilder.Init(); AstroObjectLocator.Init(); - OWAssetHandler.Init(); + StreamingHandler.Init(); PlanetCreationHandler.Init(BodyDict[CurrentStarSystem]); VesselWarpHandler.LoadVessel(); SystemCreationHandler.LoadSystem(SystemDict[CurrentStarSystem]);