From d551944207df557d2fad13b8c3cb6152b84c70b5 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Wed, 29 Jan 2025 13:58:57 -0800 Subject: [PATCH 01/30] draft out profilr markers --- NewHorizons/Patches/ProfilerPatch.cs | 46 ++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 NewHorizons/Patches/ProfilerPatch.cs diff --git a/NewHorizons/Patches/ProfilerPatch.cs b/NewHorizons/Patches/ProfilerPatch.cs new file mode 100644 index 00000000..c99b2479 --- /dev/null +++ b/NewHorizons/Patches/ProfilerPatch.cs @@ -0,0 +1,46 @@ +using HarmonyLib; +using System.Collections.Generic; +using System.Diagnostics; +using System.Reflection; + +namespace NewHorizons.Patches; + +/// +/// attach profiler markers to important methods +/// +[HarmonyPatch] +public static class ProfilerPatch +{ + [HarmonyTargetMethods] + public static IEnumerable TargetMethods() + { + foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) + { + // only allow builders for now + if (!type.Name.EndsWith("Builder")) continue; + + foreach (var method in type.GetMethods()) + { + // make and init methods + if (!(method.Name.StartsWith("Make") || method.Name.StartsWith("Init"))) continue; + + Main.Instance.ModHelper.Console.WriteLine($"[profiler] profiling method {method.DeclaringType.Name}.{method.Name}"); + yield return method; + } + } + } + + [HarmonyPrefix] + public static void Prefix(out Stopwatch __state) + { + __state = new Stopwatch(); + __state.Start(); + } + + [HarmonyPostfix] + public static void Postfix(MethodBase __originalMethod, Stopwatch __state) + { + __state.Stop(); + Main.Instance.ModHelper.Console.WriteLine($"[profiler] method {__originalMethod.DeclaringType.Name}.{__originalMethod.Name} took {__state.Elapsed.TotalMilliseconds} ms"); + } +} From 40621c62b0067e1c8568a71310817dd01d856225 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Wed, 29 Jan 2025 14:01:14 -0800 Subject: [PATCH 02/30] actually do profiler samples --- NewHorizons/Patches/ProfilerPatch.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/NewHorizons/Patches/ProfilerPatch.cs b/NewHorizons/Patches/ProfilerPatch.cs index c99b2479..88b973d7 100644 --- a/NewHorizons/Patches/ProfilerPatch.cs +++ b/NewHorizons/Patches/ProfilerPatch.cs @@ -1,7 +1,10 @@ -using HarmonyLib; +// #define ENABLE_PROFILER + +using HarmonyLib; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; +using UnityEngine.Profiling; namespace NewHorizons.Patches; @@ -31,8 +34,10 @@ public static class ProfilerPatch } [HarmonyPrefix] - public static void Prefix(out Stopwatch __state) + public static void Prefix(MethodBase __originalMethod, out Stopwatch __state) { + Profiler.BeginSample($"{__originalMethod.DeclaringType.Name}.{__originalMethod.Name}"); + __state = new Stopwatch(); __state.Start(); } @@ -40,6 +45,8 @@ public static class ProfilerPatch [HarmonyPostfix] public static void Postfix(MethodBase __originalMethod, Stopwatch __state) { + Profiler.EndSample(); + __state.Stop(); Main.Instance.ModHelper.Console.WriteLine($"[profiler] method {__originalMethod.DeclaringType.Name}.{__originalMethod.Name} took {__state.Elapsed.TotalMilliseconds} ms"); } From c5074906d9a8f1d95155e621b99c85261cb15620 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Wed, 29 Jan 2025 14:07:30 -0800 Subject: [PATCH 03/30] fix error (cant patch generic methods) --- NewHorizons/Patches/ProfilerPatch.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/NewHorizons/Patches/ProfilerPatch.cs b/NewHorizons/Patches/ProfilerPatch.cs index 88b973d7..e361368c 100644 --- a/NewHorizons/Patches/ProfilerPatch.cs +++ b/NewHorizons/Patches/ProfilerPatch.cs @@ -19,13 +19,12 @@ public static class ProfilerPatch { foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) { - // only allow builders for now if (!type.Name.EndsWith("Builder")) continue; - foreach (var method in type.GetMethods()) + foreach (var method in type.GetRuntimeMethods()) { - // make and init methods if (!(method.Name.StartsWith("Make") || method.Name.StartsWith("Init"))) continue; + if (method.IsGenericMethod) continue; Main.Instance.ModHelper.Console.WriteLine($"[profiler] profiling method {method.DeclaringType.Name}.{method.Name}"); yield return method; From c08fbe194a017a94d6215eb1d542439a5e22d46a Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Wed, 29 Jan 2025 14:11:22 -0800 Subject: [PATCH 04/30] log --- NewHorizons/Patches/ProfilerPatch.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NewHorizons/Patches/ProfilerPatch.cs b/NewHorizons/Patches/ProfilerPatch.cs index e361368c..166e18ff 100644 --- a/NewHorizons/Patches/ProfilerPatch.cs +++ b/NewHorizons/Patches/ProfilerPatch.cs @@ -26,7 +26,7 @@ public static class ProfilerPatch if (!(method.Name.StartsWith("Make") || method.Name.StartsWith("Init"))) continue; if (method.IsGenericMethod) continue; - Main.Instance.ModHelper.Console.WriteLine($"[profiler] profiling method {method.DeclaringType.Name}.{method.Name}"); + Main.Instance.ModHelper.Console.WriteLine($"[profiler] profiling {method.DeclaringType.Name}.{method.Name}"); yield return method; } } @@ -47,6 +47,6 @@ public static class ProfilerPatch Profiler.EndSample(); __state.Stop(); - Main.Instance.ModHelper.Console.WriteLine($"[profiler] method {__originalMethod.DeclaringType.Name}.{__originalMethod.Name} took {__state.Elapsed.TotalMilliseconds} ms"); + Main.Instance.ModHelper.Console.WriteLine($"[profiler] {__originalMethod.DeclaringType.Name}.{__originalMethod.Name} took {__state.Elapsed.TotalMilliseconds:f1} ms"); } } From f5ac2fd9208729ad1f817b52bc7ae5b30296c5a5 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Wed, 29 Jan 2025 14:13:45 -0800 Subject: [PATCH 05/30] no logging --- NewHorizons/Patches/ProfilerPatch.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/NewHorizons/Patches/ProfilerPatch.cs b/NewHorizons/Patches/ProfilerPatch.cs index 166e18ff..4aba993c 100644 --- a/NewHorizons/Patches/ProfilerPatch.cs +++ b/NewHorizons/Patches/ProfilerPatch.cs @@ -1,4 +1,4 @@ -// #define ENABLE_PROFILER +#define ENABLE_PROFILER using HarmonyLib; using System.Collections.Generic; @@ -33,20 +33,20 @@ public static class ProfilerPatch } [HarmonyPrefix] - public static void Prefix(MethodBase __originalMethod, out Stopwatch __state) + public static void Prefix(MethodBase __originalMethod/*, out Stopwatch __state*/) { Profiler.BeginSample($"{__originalMethod.DeclaringType.Name}.{__originalMethod.Name}"); - __state = new Stopwatch(); - __state.Start(); + // __state = new Stopwatch(); + // __state.Start(); } [HarmonyPostfix] - public static void Postfix(MethodBase __originalMethod, Stopwatch __state) + public static void Postfix(/*MethodBase __originalMethod, Stopwatch __state*/) { Profiler.EndSample(); - __state.Stop(); - Main.Instance.ModHelper.Console.WriteLine($"[profiler] {__originalMethod.DeclaringType.Name}.{__originalMethod.Name} took {__state.Elapsed.TotalMilliseconds:f1} ms"); + // __state.Stop(); + // Main.Instance.ModHelper.Console.WriteLine($"[profiler] {__originalMethod.DeclaringType.Name}.{__originalMethod.Name} took {__state.Elapsed.TotalMilliseconds:f1} ms"); } } From 68c969493fc68d86a8e29dcae4d08600edeb5334 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Wed, 29 Jan 2025 15:52:40 -0800 Subject: [PATCH 06/30] match more methods --- NewHorizons/Patches/ProfilerPatch.cs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/NewHorizons/Patches/ProfilerPatch.cs b/NewHorizons/Patches/ProfilerPatch.cs index 4aba993c..b6650d7e 100644 --- a/NewHorizons/Patches/ProfilerPatch.cs +++ b/NewHorizons/Patches/ProfilerPatch.cs @@ -2,7 +2,6 @@ using HarmonyLib; using System.Collections.Generic; -using System.Diagnostics; using System.Reflection; using UnityEngine.Profiling; @@ -14,39 +13,48 @@ namespace NewHorizons.Patches; [HarmonyPatch] public static class ProfilerPatch { + private static string FriendlyName(this MethodBase @this) => $"{@this.DeclaringType.Name}.{@this.Name}"; + [HarmonyTargetMethods] public static IEnumerable TargetMethods() { foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) { - if (!type.Name.EndsWith("Builder")) continue; + // if (!type.Name.EndsWith("Builder")) continue; + // if (!(type.FullName.Contains("Builder") || type.FullName.Contains("Utility"))) continue; - foreach (var method in type.GetRuntimeMethods()) + foreach (var method in type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)) { - if (!(method.Name.StartsWith("Make") || method.Name.StartsWith("Init"))) continue; - if (method.IsGenericMethod) continue; + if (!( + method.Name.Contains("Make") || + method.Name.Contains("Init") || + method.Name.Contains("Find") || + method.Name.Contains("OnSceneLoaded") + )) continue; - Main.Instance.ModHelper.Console.WriteLine($"[profiler] profiling {method.DeclaringType.Name}.{method.Name}"); + if (method.ContainsGenericParameters) continue; + + Main.Instance.ModHelper.Console.WriteLine($"[profiler] profiling {method.FriendlyName()}"); yield return method; } } } [HarmonyPrefix] - public static void Prefix(MethodBase __originalMethod/*, out Stopwatch __state*/) + public static void Prefix(MethodBase __originalMethod /*, out Stopwatch __state*/) { - Profiler.BeginSample($"{__originalMethod.DeclaringType.Name}.{__originalMethod.Name}"); + Profiler.BeginSample(__originalMethod.FriendlyName()); // __state = new Stopwatch(); // __state.Start(); } [HarmonyPostfix] - public static void Postfix(/*MethodBase __originalMethod, Stopwatch __state*/) + public static void Postfix( /*MethodBase __originalMethod, Stopwatch __state*/) { Profiler.EndSample(); // __state.Stop(); - // Main.Instance.ModHelper.Console.WriteLine($"[profiler] {__originalMethod.DeclaringType.Name}.{__originalMethod.Name} took {__state.Elapsed.TotalMilliseconds:f1} ms"); + // Main.Instance.ModHelper.Console.WriteLine($"[profiler] {__originalMethod.MethodName()} took {__state.Elapsed.TotalMilliseconds:f1} ms"); } } From 6da9071208dd2a6ceb9197ae5683b1b4b371cf68 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Wed, 29 Jan 2025 16:26:34 -0800 Subject: [PATCH 07/30] try and apply some simple optimizations --- .../Components/Props/ConditionalObjectActivation.cs | 4 ++-- NewHorizons/Handlers/StreamingHandler.cs | 4 +++- NewHorizons/Patches/ProfilerPatch.cs | 9 +++++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/NewHorizons/Components/Props/ConditionalObjectActivation.cs b/NewHorizons/Components/Props/ConditionalObjectActivation.cs index a0322ecc..4036d610 100644 --- a/NewHorizons/Components/Props/ConditionalObjectActivation.cs +++ b/NewHorizons/Components/Props/ConditionalObjectActivation.cs @@ -13,7 +13,7 @@ namespace NewHorizons.Components.Props public bool CloseEyes; public bool SetActiveWithCondition; - private PlayerCameraEffectController _playerCameraEffectController; + private static PlayerCameraEffectController _playerCameraEffectController; private bool _changeConditionOnExitConversation; private bool _inConversation; @@ -45,7 +45,7 @@ namespace NewHorizons.Components.Props public void Awake() { - _playerCameraEffectController = GameObject.FindObjectOfType(); + if (_playerCameraEffectController == null) _playerCameraEffectController = GameObject.FindObjectOfType(); GlobalMessenger.AddListener("DialogueConditionChanged", OnDialogueConditionChanged); GlobalMessenger.AddListener("ExitConversation", OnExitConversation); GlobalMessenger.AddListener("EnterConversation", OnEnterConversation); diff --git a/NewHorizons/Handlers/StreamingHandler.cs b/NewHorizons/Handlers/StreamingHandler.cs index c037594d..24c75a6c 100644 --- a/NewHorizons/Handlers/StreamingHandler.cs +++ b/NewHorizons/Handlers/StreamingHandler.cs @@ -51,6 +51,8 @@ namespace NewHorizons.Handlers /// public static void SetUpStreaming(GameObject obj, Sector sector) { + return; + // find the asset bundles to load // tries the cache first, then builds if (!_objectCache.TryGetValue(obj, out var assetBundles)) @@ -152,4 +154,4 @@ namespace NewHorizons.Handlers } } } -} \ No newline at end of file +} diff --git a/NewHorizons/Patches/ProfilerPatch.cs b/NewHorizons/Patches/ProfilerPatch.cs index b6650d7e..6e31af24 100644 --- a/NewHorizons/Patches/ProfilerPatch.cs +++ b/NewHorizons/Patches/ProfilerPatch.cs @@ -26,10 +26,11 @@ public static class ProfilerPatch foreach (var method in type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)) { if (!( - method.Name.Contains("Make") || - method.Name.Contains("Init") || - method.Name.Contains("Find") || - method.Name.Contains("OnSceneLoaded") + method.Name.StartsWith("Make") || + method.Name.StartsWith("Init") || + method.Name.StartsWith("Find") || + method.Name == "SetUpStreaming" || + method.Name == "OnSceneLoaded" )) continue; if (method.ContainsGenericParameters) continue; From 6be9150223f958b4c1782e9e2c231941595bd6d8 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Wed, 29 Jan 2025 16:34:22 -0800 Subject: [PATCH 08/30] search utilities fast and slow path --- NewHorizons/Utility/SearchUtilities.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/NewHorizons/Utility/SearchUtilities.cs b/NewHorizons/Utility/SearchUtilities.cs index 36e656e6..d52266e1 100644 --- a/NewHorizons/Utility/SearchUtilities.cs +++ b/NewHorizons/Utility/SearchUtilities.cs @@ -1,7 +1,10 @@ +#define ENABLE_PROFILER + using NewHorizons.Utility.OWML; using System.Collections.Generic; using System.Linq; using UnityEngine; +using UnityEngine.Profiling; using UnityEngine.SceneManagement; using Object = UnityEngine.Object; @@ -116,13 +119,17 @@ namespace NewHorizons.Utility if (CachedGameObjects.TryGetValue(path, out var go)) return go; // 1: normal find + Profiler.BeginSample("1"); go = GameObject.Find(path); if (go) { CachedGameObjects.Add(path, go); + Profiler.EndSample(); return go; } + Profiler.EndSample(); + Profiler.BeginSample("1"); // 2: find inactive using root + transform.find var names = path.Split('/'); @@ -142,9 +149,12 @@ namespace NewHorizons.Utility if (go) { CachedGameObjects.Add(path, go); + Profiler.EndSample(); return go; } + Profiler.EndSample(); + Profiler.BeginSample("3"); var name = names.Last(); if (warn) NHLogger.LogWarning($"Couldn't find object in path {path}, will look for potential matches for name {name}"); // 3: find resource to include inactive objects (but skip prefabs) @@ -153,10 +163,12 @@ namespace NewHorizons.Utility if (go) { CachedGameObjects.Add(path, go); + Profiler.EndSample(); return go; } if (warn) NHLogger.LogWarning($"Couldn't find object with name {name}"); + Profiler.EndSample(); return null; } From 043269e6ac7dd066621b0a44dd4dc6d7717515b2 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Wed, 29 Jan 2025 16:38:14 -0800 Subject: [PATCH 09/30] preprocessor define for profiler --- NewHorizons/NewHorizons.csproj | 2 ++ NewHorizons/Patches/ProfilerPatch.cs | 4 +++- NewHorizons/Utility/SearchUtilities.cs | 2 -- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/NewHorizons/NewHorizons.csproj b/NewHorizons/NewHorizons.csproj index 5ae0cd1c..15857053 100644 --- a/NewHorizons/NewHorizons.csproj +++ b/NewHorizons/NewHorizons.csproj @@ -10,6 +10,8 @@ true None 1701;1702;1591 + + ENABLE_PROFILER none diff --git a/NewHorizons/Patches/ProfilerPatch.cs b/NewHorizons/Patches/ProfilerPatch.cs index 6e31af24..31821d3e 100644 --- a/NewHorizons/Patches/ProfilerPatch.cs +++ b/NewHorizons/Patches/ProfilerPatch.cs @@ -1,4 +1,4 @@ -#define ENABLE_PROFILER +#if ENABLE_PROFILER using HarmonyLib; using System.Collections.Generic; @@ -59,3 +59,5 @@ public static class ProfilerPatch // Main.Instance.ModHelper.Console.WriteLine($"[profiler] {__originalMethod.MethodName()} took {__state.Elapsed.TotalMilliseconds:f1} ms"); } } + +#endif diff --git a/NewHorizons/Utility/SearchUtilities.cs b/NewHorizons/Utility/SearchUtilities.cs index d52266e1..b6d45310 100644 --- a/NewHorizons/Utility/SearchUtilities.cs +++ b/NewHorizons/Utility/SearchUtilities.cs @@ -1,5 +1,3 @@ -#define ENABLE_PROFILER - using NewHorizons.Utility.OWML; using System.Collections.Generic; using System.Linq; From 1961076664fe1eaa863016c8cd156441666a2728 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Wed, 29 Jan 2025 17:10:28 -0800 Subject: [PATCH 10/30] cache LoadPrefab --- NewHorizons/Builder/Props/DetailBuilder.cs | 7 +------ NewHorizons/Handlers/StreamingHandler.cs | 2 +- NewHorizons/Main.cs | 1 + NewHorizons/Utility/Files/AssetBundleUtilities.cs | 10 +++++++++- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/NewHorizons/Builder/Props/DetailBuilder.cs b/NewHorizons/Builder/Props/DetailBuilder.cs index ffdefcb5..abf3d27a 100644 --- a/NewHorizons/Builder/Props/DetailBuilder.cs +++ b/NewHorizons/Builder/Props/DetailBuilder.cs @@ -36,11 +36,6 @@ namespace NewHorizons.Builder.Props } } - static DetailBuilder() - { - SceneManager.sceneUnloaded += SceneManager_sceneUnloaded; - } - #region obsolete // Never change method signatures, people directly reference the NH dll and it can break backwards compatibility // In particular, Outer Wives needs this method signature @@ -54,7 +49,7 @@ namespace NewHorizons.Builder.Props => Make(go, sector, mod: null, detail); #endregion - private static void SceneManager_sceneUnloaded(Scene scene) + public static void ClearCache() { foreach (var prefab in _fixedPrefabCache.Values) { diff --git a/NewHorizons/Handlers/StreamingHandler.cs b/NewHorizons/Handlers/StreamingHandler.cs index 24c75a6c..a6de5650 100644 --- a/NewHorizons/Handlers/StreamingHandler.cs +++ b/NewHorizons/Handlers/StreamingHandler.cs @@ -51,7 +51,7 @@ namespace NewHorizons.Handlers /// public static void SetUpStreaming(GameObject obj, Sector sector) { - return; + // TODO: used OFTEN by detail builder. 20ms adds up to seconds. speed up! // find the asset bundles to load // tries the cache first, then builds diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index c964744f..4f005116 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -308,6 +308,7 @@ namespace NewHorizons ImageUtilities.ClearCache(); AudioUtilities.ClearCache(); AssetBundleUtilities.ClearCache(); + DetailBuilder.ClearCache(); } IsSystemReady = false; diff --git a/NewHorizons/Utility/Files/AssetBundleUtilities.cs b/NewHorizons/Utility/Files/AssetBundleUtilities.cs index 87170770..0cdd2a9b 100644 --- a/NewHorizons/Utility/Files/AssetBundleUtilities.cs +++ b/NewHorizons/Utility/Files/AssetBundleUtilities.cs @@ -12,6 +12,7 @@ namespace NewHorizons.Utility.Files public static class AssetBundleUtilities { public static Dictionary AssetBundles = new(); + private static Dictionary _prefabCache = new(); private static readonly List _loadingBundles = new(); @@ -52,6 +53,7 @@ namespace NewHorizons.Utility.Files } AssetBundles = AssetBundles.Where(x => x.Value.keepLoaded).ToDictionary(x => x.Key, x => x.Value); + _prefabCache.Clear(); } public static void PreloadBundle(string assetBundleRelativeDir, IModBehaviour mod) @@ -113,11 +115,17 @@ namespace NewHorizons.Utility.Files public static GameObject LoadPrefab(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod) { - var prefab = Load(assetBundleRelativeDir, pathInBundle, mod); + if (_prefabCache.TryGetValue(assetBundleRelativeDir + pathInBundle, out var prefab)) + return prefab; + + prefab = Load(assetBundleRelativeDir, pathInBundle, mod); prefab.SetActive(false); ReplaceShaders(prefab); + + // replacing shaders is expensive, so cache it + _prefabCache.Add(assetBundleRelativeDir + pathInBundle, prefab); return prefab; } From 0bb2c52947a2251700be5e5d7e5d2ef72257270e Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Wed, 29 Jan 2025 17:17:03 -0800 Subject: [PATCH 11/30] womp womp --- NewHorizons/Builder/Props/DetailBuilder.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/NewHorizons/Builder/Props/DetailBuilder.cs b/NewHorizons/Builder/Props/DetailBuilder.cs index abf3d27a..763eb687 100644 --- a/NewHorizons/Builder/Props/DetailBuilder.cs +++ b/NewHorizons/Builder/Props/DetailBuilder.cs @@ -36,6 +36,11 @@ namespace NewHorizons.Builder.Props } } + static DetailBuilder() + { + SceneManager.sceneUnloaded += SceneManager_sceneUnloaded; + } + #region obsolete // Never change method signatures, people directly reference the NH dll and it can break backwards compatibility // In particular, Outer Wives needs this method signature @@ -49,8 +54,9 @@ namespace NewHorizons.Builder.Props => Make(go, sector, mod: null, detail); #endregion - public static void ClearCache() + private static void SceneManager_sceneUnloaded(Scene scene) { + // would be nice to only clear when system changes, but fixed prefabs rely on stuff in the scene foreach (var prefab in _fixedPrefabCache.Values) { UnityEngine.Object.Destroy(prefab.prefab); From 011cd4b44acd76ab7474f19c3c7aa8f51bc90067 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Wed, 29 Jan 2025 17:17:03 -0800 Subject: [PATCH 12/30] womp womp --- NewHorizons/Builder/Props/DetailBuilder.cs | 8 +++++++- NewHorizons/Main.cs | 1 - 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/NewHorizons/Builder/Props/DetailBuilder.cs b/NewHorizons/Builder/Props/DetailBuilder.cs index abf3d27a..763eb687 100644 --- a/NewHorizons/Builder/Props/DetailBuilder.cs +++ b/NewHorizons/Builder/Props/DetailBuilder.cs @@ -36,6 +36,11 @@ namespace NewHorizons.Builder.Props } } + static DetailBuilder() + { + SceneManager.sceneUnloaded += SceneManager_sceneUnloaded; + } + #region obsolete // Never change method signatures, people directly reference the NH dll and it can break backwards compatibility // In particular, Outer Wives needs this method signature @@ -49,8 +54,9 @@ namespace NewHorizons.Builder.Props => Make(go, sector, mod: null, detail); #endregion - public static void ClearCache() + private static void SceneManager_sceneUnloaded(Scene scene) { + // would be nice to only clear when system changes, but fixed prefabs rely on stuff in the scene foreach (var prefab in _fixedPrefabCache.Values) { UnityEngine.Object.Destroy(prefab.prefab); diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index 4f005116..c964744f 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -308,7 +308,6 @@ namespace NewHorizons ImageUtilities.ClearCache(); AudioUtilities.ClearCache(); AssetBundleUtilities.ClearCache(); - DetailBuilder.ClearCache(); } IsSystemReady = false; From 511ffda580e4a038725fbcbfdb8f02190413aeda Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 13 Feb 2025 18:18:08 -0800 Subject: [PATCH 13/30] evil patch --- NewHorizons/Patches/ProfilerPatch.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/NewHorizons/Patches/ProfilerPatch.cs b/NewHorizons/Patches/ProfilerPatch.cs index 31821d3e..39f6cb83 100644 --- a/NewHorizons/Patches/ProfilerPatch.cs +++ b/NewHorizons/Patches/ProfilerPatch.cs @@ -3,6 +3,7 @@ using HarmonyLib; using System.Collections.Generic; using System.Reflection; +using UnityEngine; using UnityEngine.Profiling; namespace NewHorizons.Patches; @@ -28,9 +29,9 @@ public static class ProfilerPatch if (!( method.Name.StartsWith("Make") || method.Name.StartsWith("Init") || - method.Name.StartsWith("Find") || method.Name == "SetUpStreaming" || - method.Name == "OnSceneLoaded" + method.Name == "OnSceneLoaded" || + method.DeclaringType.Name.EndsWith("Utilities") )) continue; if (method.ContainsGenericParameters) continue; @@ -60,4 +61,13 @@ public static class ProfilerPatch } } +[HarmonyPatch] +public static class EvilPatch +{ + [HarmonyPrefix] + [HarmonyPatch(typeof(StackTraceUtility), "ExtractStackTrace")] + [HarmonyPatch(typeof(Application), "CallLogCallback")] + private static bool DisableShaderLogSpam() => false; +} + #endif From d0a61ba88d7077fb6b9427fb04e0a1f1b8a21aa9 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 13 Feb 2025 19:11:09 -0800 Subject: [PATCH 14/30] revert load prefab cache cuz i think it already happens --- NewHorizons/Utility/Files/AssetBundleUtilities.cs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/NewHorizons/Utility/Files/AssetBundleUtilities.cs b/NewHorizons/Utility/Files/AssetBundleUtilities.cs index 0cdd2a9b..68e1499d 100644 --- a/NewHorizons/Utility/Files/AssetBundleUtilities.cs +++ b/NewHorizons/Utility/Files/AssetBundleUtilities.cs @@ -12,7 +12,6 @@ namespace NewHorizons.Utility.Files public static class AssetBundleUtilities { public static Dictionary AssetBundles = new(); - private static Dictionary _prefabCache = new(); private static readonly List _loadingBundles = new(); @@ -53,7 +52,6 @@ namespace NewHorizons.Utility.Files } AssetBundles = AssetBundles.Where(x => x.Value.keepLoaded).ToDictionary(x => x.Key, x => x.Value); - _prefabCache.Clear(); } public static void PreloadBundle(string assetBundleRelativeDir, IModBehaviour mod) @@ -115,17 +113,11 @@ namespace NewHorizons.Utility.Files public static GameObject LoadPrefab(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod) { - if (_prefabCache.TryGetValue(assetBundleRelativeDir + pathInBundle, out var prefab)) - return prefab; - - prefab = Load(assetBundleRelativeDir, pathInBundle, mod); + var prefab = Load(assetBundleRelativeDir, pathInBundle, mod); prefab.SetActive(false); ReplaceShaders(prefab); - - // replacing shaders is expensive, so cache it - _prefabCache.Add(assetBundleRelativeDir + pathInBundle, prefab); return prefab; } @@ -221,4 +213,4 @@ namespace NewHorizons.Utility.Files } } } -} \ No newline at end of file +} From 6d2739f9316a5e4d0b75599e3b7f7d1e038b0089 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 13 Feb 2025 19:13:42 -0800 Subject: [PATCH 15/30] broaden profile filter --- NewHorizons/Patches/ProfilerPatch.cs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/NewHorizons/Patches/ProfilerPatch.cs b/NewHorizons/Patches/ProfilerPatch.cs index 39f6cb83..fb480722 100644 --- a/NewHorizons/Patches/ProfilerPatch.cs +++ b/NewHorizons/Patches/ProfilerPatch.cs @@ -21,22 +21,18 @@ public static class ProfilerPatch { foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) { - // if (!type.Name.EndsWith("Builder")) continue; - // if (!(type.FullName.Contains("Builder") || type.FullName.Contains("Utility"))) continue; + if (!( + type.Name == "Main" || + type.Name.EndsWith("Builder") || + type.Name.EndsWith("Handler") || + type.Name.EndsWith("Utilities") + )) continue; foreach (var method in type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly)) { - if (!( - method.Name.StartsWith("Make") || - method.Name.StartsWith("Init") || - method.Name == "SetUpStreaming" || - method.Name == "OnSceneLoaded" || - method.DeclaringType.Name.EndsWith("Utilities") - )) continue; - if (method.ContainsGenericParameters) continue; - Main.Instance.ModHelper.Console.WriteLine($"[profiler] profiling {method.FriendlyName()}"); + // Main.Instance.ModHelper.Console.WriteLine($"[profiler] profiling {method.FriendlyName()}"); yield return method; } } From ece07be2d6044582dfe321b9d39358c007d3b825 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 13 Feb 2025 20:32:21 -0800 Subject: [PATCH 16/30] oops --- NewHorizons/Utility/SearchUtilities.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NewHorizons/Utility/SearchUtilities.cs b/NewHorizons/Utility/SearchUtilities.cs index b6d45310..0349e1b6 100644 --- a/NewHorizons/Utility/SearchUtilities.cs +++ b/NewHorizons/Utility/SearchUtilities.cs @@ -127,13 +127,13 @@ namespace NewHorizons.Utility } Profiler.EndSample(); - Profiler.BeginSample("1"); + Profiler.BeginSample("2"); // 2: find inactive using root + transform.find var names = path.Split('/'); // Cache the root objects so we don't loop through all of them each time var rootName = names[0]; - if (!CachedRootGameObjects.TryGetValue(rootName, out var root)) + if (!CachedRootGameObjects.TryGetValue(rootName, out var root)) { root = SceneManager.GetActiveScene().GetRootGameObjects().FirstOrDefault(x => x.name == rootName); if (root != null) From 5beb803835b6853c64fb25f974d0d42d5cc07549 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 13 Feb 2025 21:24:25 -0800 Subject: [PATCH 17/30] Revert "revert load prefab cache cuz i think it already happens" This reverts commit d0a61ba88d7077fb6b9427fb04e0a1f1b8a21aa9. --- NewHorizons/Utility/Files/AssetBundleUtilities.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/NewHorizons/Utility/Files/AssetBundleUtilities.cs b/NewHorizons/Utility/Files/AssetBundleUtilities.cs index 68e1499d..0cdd2a9b 100644 --- a/NewHorizons/Utility/Files/AssetBundleUtilities.cs +++ b/NewHorizons/Utility/Files/AssetBundleUtilities.cs @@ -12,6 +12,7 @@ namespace NewHorizons.Utility.Files public static class AssetBundleUtilities { public static Dictionary AssetBundles = new(); + private static Dictionary _prefabCache = new(); private static readonly List _loadingBundles = new(); @@ -52,6 +53,7 @@ namespace NewHorizons.Utility.Files } AssetBundles = AssetBundles.Where(x => x.Value.keepLoaded).ToDictionary(x => x.Key, x => x.Value); + _prefabCache.Clear(); } public static void PreloadBundle(string assetBundleRelativeDir, IModBehaviour mod) @@ -113,11 +115,17 @@ namespace NewHorizons.Utility.Files public static GameObject LoadPrefab(string assetBundleRelativeDir, string pathInBundle, IModBehaviour mod) { - var prefab = Load(assetBundleRelativeDir, pathInBundle, mod); + if (_prefabCache.TryGetValue(assetBundleRelativeDir + pathInBundle, out var prefab)) + return prefab; + + prefab = Load(assetBundleRelativeDir, pathInBundle, mod); prefab.SetActive(false); ReplaceShaders(prefab); + + // replacing shaders is expensive, so cache it + _prefabCache.Add(assetBundleRelativeDir + pathInBundle, prefab); return prefab; } @@ -213,4 +221,4 @@ namespace NewHorizons.Utility.Files } } } -} +} \ No newline at end of file From b8d7af90f1051d425e3e7c71934c765abd930ced Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 13 Feb 2025 21:31:03 -0800 Subject: [PATCH 18/30] field to not check for existing body --- NewHorizons/External/Configs/PlanetConfig.cs | 9 +++- NewHorizons/Handlers/PlanetCreationHandler.cs | 45 ++++++++++--------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/NewHorizons/External/Configs/PlanetConfig.cs b/NewHorizons/External/Configs/PlanetConfig.cs index 6184ed36..ff973769 100644 --- a/NewHorizons/External/Configs/PlanetConfig.cs +++ b/NewHorizons/External/Configs/PlanetConfig.cs @@ -64,6 +64,11 @@ namespace NewHorizons.External.Configs /// public string[] removeChildren; + /// + /// optimization. turn this off if you know you're generating a new body and aren't worried about other mods editing it. + /// + [DefaultValue(true)] public bool checkForExisting = true; + #endregion #region Modules @@ -530,7 +535,7 @@ namespace NewHorizons.External.Configs Spawn.shipSpawnPoints = new SpawnModule.ShipSpawnPoint[] { Spawn.shipSpawn }; } - // Because these guys put TWO spawn points + // Because these guys put TWO spawn points if (starSystem == "2walker2.OogaBooga" && name == "The Campground") { Spawn.playerSpawnPoints[0].isDefault = true; @@ -742,4 +747,4 @@ namespace NewHorizons.External.Configs } #endregion } -} \ No newline at end of file +} diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index 6898382f..4de7e6a2 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -168,26 +168,29 @@ namespace NewHorizons.Handlers // I don't remember doing this why is it exceptions what am I doing GameObject existingPlanet = null; - try + if (body.Config.checkForExisting) { - existingPlanet = AstroObjectLocator.GetAstroObject(body.Config.name).gameObject; - } - catch (Exception) - { - if (body?.Config?.name == null) + try { - NHLogger.LogError($"How is there no name for {body}"); + existingPlanet = AstroObjectLocator.GetAstroObject(body.Config.name).gameObject; } - else + catch (Exception) { - existingPlanet = SearchUtilities.Find(body.Config.name.Replace(" ", "") + "_Body", false); + if (body?.Config?.name == null) + { + NHLogger.LogError($"How is there no name for {body}"); + } + else + { + existingPlanet = SearchUtilities.Find(body.Config.name.Replace(" ", "") + "_Body", false); + } } - } - if (existingPlanet == null && body.Config.destroy) - { - NHLogger.LogError($"{body.Config.name} was meant to be destroyed, but was not found"); - return false; + if (existingPlanet == null && body.Config.destroy) + { + NHLogger.LogError($"{body.Config.name} was meant to be destroyed, but was not found"); + return false; + } } if (existingPlanet != null) @@ -287,9 +290,9 @@ namespace NewHorizons.Handlers try { NHLogger.Log($"Creating [{body.Config.name}]"); - var planetObject = GenerateBody(body, defaultPrimaryToSun) + var planetObject = GenerateBody(body, defaultPrimaryToSun) ?? throw new NullReferenceException("Something went wrong when generating the body but no errors were logged."); - + planetObject.SetActive(true); var ao = planetObject.GetComponent(); @@ -316,7 +319,7 @@ namespace NewHorizons.Handlers { NHLogger.LogError($"Error in event handler for OnPlanetLoaded on body {body.Config.name}: {e}"); } - + body.UnloadCache(true); _loadedBodies.Add(body); return true; @@ -390,7 +393,7 @@ namespace NewHorizons.Handlers body.Config.MapMarker.enabled = false; const float sphereOfInfluence = 2000f; - + var owRigidBody = RigidBodyBuilder.Make(go, sphereOfInfluence, body.Config); var ao = AstroObjectBuilder.Make(go, null, body, false); @@ -402,7 +405,7 @@ namespace NewHorizons.Handlers BrambleDimensionBuilder.Make(body, go, ao, sector, body.Mod, owRigidBody); go = SharedGenerateBody(body, go, sector, owRigidBody); - + // Not included in SharedGenerate to not mess up gravity on base game planets if (body.Config.Base.surfaceGravity != 0) { @@ -467,7 +470,7 @@ namespace NewHorizons.Handlers } var sphereOfInfluence = GetSphereOfInfluence(body); - + var owRigidBody = RigidBodyBuilder.Make(go, sphereOfInfluence, body.Config); var ao = AstroObjectBuilder.Make(go, primaryBody, body, false); @@ -686,7 +689,7 @@ namespace NewHorizons.Handlers SunOverrideBuilder.Make(go, sector, body.Config.Atmosphere, body.Config.Water, surfaceSize); } } - + if (body.Config.Atmosphere.fogSize != 0) { fog = FogBuilder.Make(go, sector, body.Config.Atmosphere, body.Mod); From c9d6335a6b5cff16b962041af804c503ca3fe76f Mon Sep 17 00:00:00 2001 From: Ben C Date: Fri, 14 Feb 2025 05:35:02 +0000 Subject: [PATCH 19/30] Updated Schemas --- NewHorizons/Schemas/body_schema.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/NewHorizons/Schemas/body_schema.json b/NewHorizons/Schemas/body_schema.json index 1b18d953..f83ad9fb 100644 --- a/NewHorizons/Schemas/body_schema.json +++ b/NewHorizons/Schemas/body_schema.json @@ -43,6 +43,11 @@ "type": "string" } }, + "checkForExisting": { + "type": "boolean", + "description": "optimization. turn this off if you know you're generating a new body and aren't worried about other mods editing it.", + "default": true + }, "AmbientLights": { "type": "array", "description": "Add ambient lights to this body", From ed011f17328f5bc1861792bc3a55733549f9348d Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 13 Feb 2025 21:49:13 -0800 Subject: [PATCH 20/30] use SpriteMeshType.FullRect for all sprites cuz that makes it faster apparently --- NewHorizons/Builder/ShipLog/MapModeBuilder.cs | 10 +++++----- NewHorizons/Builder/ShipLog/RumorModeBuilder.cs | 2 +- NewHorizons/Components/ShipLog/ShipLogStarChartMode.cs | 4 ++-- NewHorizons/Handlers/PlanetCreationHandler.cs | 2 +- NewHorizons/Handlers/SubtitlesHandler.cs | 2 +- NewHorizons/Handlers/VesselCoordinatePromptHandler.cs | 3 ++- 6 files changed, 12 insertions(+), 11 deletions(-) diff --git a/NewHorizons/Builder/ShipLog/MapModeBuilder.cs b/NewHorizons/Builder/ShipLog/MapModeBuilder.cs index 9d66447f..372400cf 100644 --- a/NewHorizons/Builder/ShipLog/MapModeBuilder.cs +++ b/NewHorizons/Builder/ShipLog/MapModeBuilder.cs @@ -28,7 +28,7 @@ namespace NewHorizons.Builder.ShipLog if (_astroObjectToMapModeInfo.TryGetValue(slao, out var mapModeInfo)) { return mapModeInfo; - } + } else { return null; @@ -149,7 +149,7 @@ namespace NewHorizons.Builder.ShipLog Rect rect = new Rect(0, 0, texture.width, texture.height); Vector2 pivot = new Vector2(texture.width / 2, texture.height / 2); - newImage.sprite = Sprite.Create(texture, rect, pivot); + newImage.sprite = Sprite.Create(texture, rect, pivot, 100, 0, SpriteMeshType.FullRect, Vector4.zero, false); return newImageGO; } @@ -190,7 +190,7 @@ namespace NewHorizons.Builder.ShipLog Texture2D image = null; Texture2D outline = null; - + string imagePath = body.Config.ShipLog?.mapMode?.revealedSprite; string outlinePath = body.Config.ShipLog?.mapMode?.outlineSprite; @@ -591,7 +591,7 @@ namespace NewHorizons.Builder.ShipLog GameObject newNodeGO = CreateMapModeGameObject(node.mainBody, parent, layer, position); ShipLogAstroObject astroObject = AddShipLogAstroObject(newNodeGO, node.mainBody, greyScaleMaterial, layer); if (node.mainBody.Config.FocalPoint != null) - { + { astroObject._imageObj.GetComponent().enabled = false; astroObject._outlineObj.GetComponent().enabled = false; astroObject._unviewedObj.GetComponent().enabled = false; @@ -687,7 +687,7 @@ namespace NewHorizons.Builder.ShipLog } private static void ReplaceExistingMapModeIcon(NewHorizonsBody body, ModBehaviour mod, MapModeInfo info) - { + { var astroObject = _astroObjectToShipLog[body.Object]; var gameObject = astroObject.gameObject; var layer = gameObject.layer; diff --git a/NewHorizons/Builder/ShipLog/RumorModeBuilder.cs b/NewHorizons/Builder/ShipLog/RumorModeBuilder.cs index 7f4d28f2..daab11bb 100644 --- a/NewHorizons/Builder/ShipLog/RumorModeBuilder.cs +++ b/NewHorizons/Builder/ShipLog/RumorModeBuilder.cs @@ -246,7 +246,7 @@ namespace NewHorizons.Builder.ShipLog Texture2D newTexture = ImageUtilities.GetTexture(body.Mod, relativePath); Rect rect = new Rect(0, 0, newTexture.width, newTexture.height); Vector2 pivot = new Vector2(newTexture.width / 2, newTexture.height / 2); - return Sprite.Create(newTexture, rect, pivot); + return Sprite.Create(newTexture, rect, pivot, 100, 0, SpriteMeshType.FullRect, Vector4.zero, false); } catch (Exception) { diff --git a/NewHorizons/Components/ShipLog/ShipLogStarChartMode.cs b/NewHorizons/Components/ShipLog/ShipLogStarChartMode.cs index cc5636eb..0ee7a574 100644 --- a/NewHorizons/Components/ShipLog/ShipLogStarChartMode.cs +++ b/NewHorizons/Components/ShipLog/ShipLogStarChartMode.cs @@ -63,7 +63,7 @@ namespace NewHorizons.Components.ShipLog } } - /* + /* if(VesselCoordinatePromptHandler.KnowsEyeCoordinates()) { AddSystemCard("EyeOfTheUniverse"); @@ -279,7 +279,7 @@ namespace NewHorizons.Components.ShipLog { var rect = new Rect(0, 0, texture.width, texture.height); var pivot = new Vector2(texture.width / 2, texture.height / 2); - return Sprite.Create(texture, rect, pivot); + return Sprite.Create(texture, rect, pivot, 100, 0, SpriteMeshType.FullRect, Vector4.zero, false); } private void OnTargetReferenceFrame(ReferenceFrame referenceFrame) diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index 4de7e6a2..75cfd0ba 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -168,7 +168,7 @@ namespace NewHorizons.Handlers // I don't remember doing this why is it exceptions what am I doing GameObject existingPlanet = null; - if (body.Config.checkForExisting) + if (false && body.Config.checkForExisting) { try { diff --git a/NewHorizons/Handlers/SubtitlesHandler.cs b/NewHorizons/Handlers/SubtitlesHandler.cs index 1a42bc79..326b2e0c 100644 --- a/NewHorizons/Handlers/SubtitlesHandler.cs +++ b/NewHorizons/Handlers/SubtitlesHandler.cs @@ -109,7 +109,7 @@ namespace NewHorizons.Handlers var tex = ImageUtilities.GetTexture(mod, filepath, false); if (tex == null) return; - var sprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f); + var sprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100, 0, SpriteMeshType.FullRect, Vector4.zero, false); AddSubtitle(sprite); } diff --git a/NewHorizons/Handlers/VesselCoordinatePromptHandler.cs b/NewHorizons/Handlers/VesselCoordinatePromptHandler.cs index d53eb875..a55be9c9 100644 --- a/NewHorizons/Handlers/VesselCoordinatePromptHandler.cs +++ b/NewHorizons/Handlers/VesselCoordinatePromptHandler.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; using UnityEngine; +using UnityEngine.UI; using static NewHorizons.External.Configs.StarSystemConfig; namespace NewHorizons.Handlers @@ -47,7 +48,7 @@ namespace NewHorizons.Handlers if (_textureCache == null) _textureCache = new List(); _textureCache.Add(texture); - var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(texture.width / 2f, texture.height / 2f)); + var sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(texture.width / 2f, texture.height / 2f), 100, 0, SpriteMeshType.FullRect, Vector4.zero, false); var name = ShipLogStarChartMode.UniqueIDToName(systemID); From a51e87f002ec8f461b46c37e4d2ca584268433e4 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 13 Feb 2025 22:34:47 -0800 Subject: [PATCH 21/30] manual sample markers for streaming --- NewHorizons/Handlers/StreamingHandler.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/NewHorizons/Handlers/StreamingHandler.cs b/NewHorizons/Handlers/StreamingHandler.cs index a6de5650..321a07d6 100644 --- a/NewHorizons/Handlers/StreamingHandler.cs +++ b/NewHorizons/Handlers/StreamingHandler.cs @@ -2,6 +2,7 @@ using NewHorizons.Utility; using System.Collections.Generic; using System.Linq; using UnityEngine; +using UnityEngine.Profiling; namespace NewHorizons.Handlers { @@ -51,8 +52,9 @@ namespace NewHorizons.Handlers /// public static void SetUpStreaming(GameObject obj, Sector sector) { - // TODO: used OFTEN by detail builder. 20ms adds up to seconds. speed up! + // TODO: used OFTEN by detail builder. 20-40ms adds up to seconds. speed up! + Profiler.BeginSample("get bundles"); // find the asset bundles to load // tries the cache first, then builds if (!_objectCache.TryGetValue(obj, out var assetBundles)) @@ -96,7 +98,9 @@ namespace NewHorizons.Handlers assetBundles = assetBundlesList.ToArray(); _objectCache[obj] = assetBundles; } + Profiler.EndSample(); + Profiler.BeginSample("get sectors"); foreach (var assetBundle in assetBundles) { // Track the sector even if its null. null means stay loaded forever @@ -107,7 +111,9 @@ namespace NewHorizons.Handlers } sectors.SafeAdd(sector); } + Profiler.EndSample(); + Profiler.BeginSample("load assets"); if (sector) { sector.OnOccupantEnterSector += _ => @@ -130,6 +136,7 @@ namespace NewHorizons.Handlers foreach (var assetBundle in assetBundles) StreamingManager.LoadStreamingAssets(assetBundle); } + Profiler.EndSample(); } public static bool IsBundleInUse(string assetBundle) From bd125c0182aab3bf6121064869d6e6dc7cb83273 Mon Sep 17 00:00:00 2001 From: Will Corby Date: Fri, 14 Feb 2025 01:31:46 -0800 Subject: [PATCH 22/30] Update PlanetConfig.cs --- NewHorizons/External/Configs/PlanetConfig.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NewHorizons/External/Configs/PlanetConfig.cs b/NewHorizons/External/Configs/PlanetConfig.cs index ff973769..9cd9e0f0 100644 --- a/NewHorizons/External/Configs/PlanetConfig.cs +++ b/NewHorizons/External/Configs/PlanetConfig.cs @@ -65,7 +65,7 @@ namespace NewHorizons.External.Configs public string[] removeChildren; /// - /// optimization. turn this off if you know you're generating a new body and aren't worried about other mods editing it. + /// optimization. turn this off if you know you're generating a new body and aren't worried about other addons editing it. /// [DefaultValue(true)] public bool checkForExisting = true; From ff445e0cb57cc0e325e6b32ea165e0dec809c97f Mon Sep 17 00:00:00 2001 From: Ben C Date: Fri, 14 Feb 2025 09:33:59 +0000 Subject: [PATCH 23/30] Updated Schemas --- NewHorizons/Schemas/body_schema.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NewHorizons/Schemas/body_schema.json b/NewHorizons/Schemas/body_schema.json index f83ad9fb..a690468b 100644 --- a/NewHorizons/Schemas/body_schema.json +++ b/NewHorizons/Schemas/body_schema.json @@ -45,7 +45,7 @@ }, "checkForExisting": { "type": "boolean", - "description": "optimization. turn this off if you know you're generating a new body and aren't worried about other mods editing it.", + "description": "optimization. turn this off if you know you're generating a new body and aren't worried about other addons editing it.", "default": true }, "AmbientLights": { From 2543d8b94d43915ead6d5f6ec2d5498bc2541b74 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Fri, 14 Feb 2025 16:27:47 -0800 Subject: [PATCH 24/30] SearchUtilities.FindAll --- NewHorizons/Utility/SearchUtilities.cs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/NewHorizons/Utility/SearchUtilities.cs b/NewHorizons/Utility/SearchUtilities.cs index 0349e1b6..b6086917 100644 --- a/NewHorizons/Utility/SearchUtilities.cs +++ b/NewHorizons/Utility/SearchUtilities.cs @@ -179,5 +179,31 @@ namespace NewHorizons.Utility } return children; } + + /// + /// transform.find but works for gameobjects with same name + /// + public static List FindAll(this Transform @this, string path) + { + var names = path.Split('/'); + var currentTransforms = new List { @this }; + foreach (var name in names) + { + var newTransforms = new List(); + foreach (var currentTransform in currentTransforms) + { + foreach (Transform child in currentTransform) + { + if (child.name == name) + { + newTransforms.Add(child); + } + } + } + currentTransforms = newTransforms; + } + + return currentTransforms; + } } } From 0fa7cfe2c528bbf9cb3bd598395c04d4dd3df39f Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Fri, 14 Feb 2025 21:15:53 -0800 Subject: [PATCH 25/30] test both remove children methods --- NewHorizons/Builder/Props/DetailBuilder.cs | 31 +++++++++++-------- NewHorizons/Handlers/PlanetCreationHandler.cs | 7 ++++- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/NewHorizons/Builder/Props/DetailBuilder.cs b/NewHorizons/Builder/Props/DetailBuilder.cs index d2064b68..78c0d6a0 100644 --- a/NewHorizons/Builder/Props/DetailBuilder.cs +++ b/NewHorizons/Builder/Props/DetailBuilder.cs @@ -1,3 +1,4 @@ +using HarmonyLib; using NewHorizons.Builder.General; using NewHorizons.Components; using NewHorizons.Components.Orbital; @@ -228,12 +229,16 @@ namespace NewHorizons.Builder.Props var path = $"{detailPath}/{childPath}"; var flag = true; - foreach (var childObj in transforms.Where(x => x.GetPath() == path)) + var childObjs = transforms.Where(x => x.GetPath() == path).ToList(); + var childObjs2 = go.transform.FindAll(childPath); + foreach (var childObj in childObjs) { flag = false; childObj.gameObject.SetActive(false); } + Main.Instance.ModHelper.Console.WriteLine($"remove children detail\n{prop.transform.GetPath()}\n{childPath}\n\n{childObjs.Join()}\n{childObjs2.Join()}"); + if (flag) NHLogger.LogWarning($"Couldn't find \"{childPath}\"."); } } @@ -264,7 +269,7 @@ namespace NewHorizons.Builder.Props UnityEngine.Object.DestroyImmediate(prop); prop = newDetailGO; } - + if (isItem) { // Else when you put them down you can't pick them back up @@ -276,7 +281,7 @@ namespace NewHorizons.Builder.Props // For DLC related props // Make sure to do this before its set active - if (!string.IsNullOrEmpty(detail?.path) && + if (!string.IsNullOrEmpty(detail?.path) && (detail.path.ToLowerInvariant().StartsWith("ringworld") || detail.path.ToLowerInvariant().StartsWith("dreamworld"))) { prop.AddComponent()._destroyOnDLCNotOwned = true; @@ -295,7 +300,7 @@ namespace NewHorizons.Builder.Props if (!string.IsNullOrEmpty(detail.activationCondition)) { - ConditionalObjectActivation.SetUp(prop, detail.activationCondition, detail.blinkWhenActiveChanged, true); + ConditionalObjectActivation.SetUp(prop, detail.activationCondition, detail.blinkWhenActiveChanged, true); } if (!string.IsNullOrEmpty(detail.deactivationCondition)) { @@ -569,22 +574,22 @@ namespace NewHorizons.Builder.Props // Manually copied these values from a artifact lantern so that we don't have to find it (works in Eye) lantern._origLensFlareBrightness = 0f; - lantern._focuserPetalsBaseEulerAngles = new Vector3[] - { - new Vector3(0.7f, 270.0f, 357.5f), - new Vector3(288.7f, 270.1f, 357.4f), + lantern._focuserPetalsBaseEulerAngles = new Vector3[] + { + new Vector3(0.7f, 270.0f, 357.5f), + new Vector3(288.7f, 270.1f, 357.4f), new Vector3(323.3f, 90.0f, 177.5f), - new Vector3(35.3f, 90.0f, 177.5f), - new Vector3(72.7f, 270.1f, 357.5f) + new Vector3(35.3f, 90.0f, 177.5f), + new Vector3(72.7f, 270.1f, 357.5f) }; lantern._dirtyFlag_focus = true; - lantern._concealerRootsBaseScale = new Vector3[] + lantern._concealerRootsBaseScale = new Vector3[] { Vector3.one, Vector3.one, Vector3.one }; - lantern._concealerCoversStartPos = new Vector3[] + lantern._concealerCoversStartPos = new Vector3[] { new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, -0.1f, 0.0f), @@ -595,7 +600,7 @@ namespace NewHorizons.Builder.Props }; lantern._dirtyFlag_concealment = true; lantern.UpdateVisuals(); - + Destroy(this); } } diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index 75cfd0ba..eab46ce2 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -1,3 +1,4 @@ +using HarmonyLib; using NewHorizons.Builder.Atmosphere; using NewHorizons.Builder.Body; using NewHorizons.Builder.General; @@ -1008,7 +1009,9 @@ namespace NewHorizons.Handlers var path = $"{goPath}/{childPath}"; var flag = true; - foreach (var childObj in transforms.Where(x => x.GetPath() == path)) + var childObjs = transforms.Where(x => x.GetPath() == path).ToList(); + var childObjs2 = go.transform.FindAll(childPath); + foreach (var childObj in childObjs) { flag = false; // idk why we wait here but we do @@ -1021,6 +1024,8 @@ namespace NewHorizons.Handlers }, 2); } + Main.Instance.ModHelper.Console.WriteLine($"remove children planet\n{go.transform.GetPath()}\n{childPath}\n\n{childObjs.Join()}\n{childObjs2.Join()}"); + if (flag) NHLogger.LogWarning($"Couldn't find \"{childPath}\"."); } } From 165f94d1cd89b1c6000c7a1c22406d62dbbfa174 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Fri, 14 Feb 2025 21:19:50 -0800 Subject: [PATCH 26/30] i made an oopsie --- NewHorizons/Handlers/PlanetCreationHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index eab46ce2..25b75383 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -169,7 +169,7 @@ namespace NewHorizons.Handlers // I don't remember doing this why is it exceptions what am I doing GameObject existingPlanet = null; - if (false && body.Config.checkForExisting) + if (body.Config.checkForExisting) // TODO: remove this when we cache name->fullpath in Find { try { From c5ae20d22bbf1c42783b74709f543423e125884a Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Fri, 14 Feb 2025 21:26:51 -0800 Subject: [PATCH 27/30] Revert "test both remove children methods" This reverts commit 0fa7cfe2c528bbf9cb3bd598395c04d4dd3df39f. --- NewHorizons/Builder/Props/DetailBuilder.cs | 31 ++++++++----------- NewHorizons/Handlers/PlanetCreationHandler.cs | 7 +---- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/NewHorizons/Builder/Props/DetailBuilder.cs b/NewHorizons/Builder/Props/DetailBuilder.cs index 78c0d6a0..d2064b68 100644 --- a/NewHorizons/Builder/Props/DetailBuilder.cs +++ b/NewHorizons/Builder/Props/DetailBuilder.cs @@ -1,4 +1,3 @@ -using HarmonyLib; using NewHorizons.Builder.General; using NewHorizons.Components; using NewHorizons.Components.Orbital; @@ -229,16 +228,12 @@ namespace NewHorizons.Builder.Props var path = $"{detailPath}/{childPath}"; var flag = true; - var childObjs = transforms.Where(x => x.GetPath() == path).ToList(); - var childObjs2 = go.transform.FindAll(childPath); - foreach (var childObj in childObjs) + foreach (var childObj in transforms.Where(x => x.GetPath() == path)) { flag = false; childObj.gameObject.SetActive(false); } - Main.Instance.ModHelper.Console.WriteLine($"remove children detail\n{prop.transform.GetPath()}\n{childPath}\n\n{childObjs.Join()}\n{childObjs2.Join()}"); - if (flag) NHLogger.LogWarning($"Couldn't find \"{childPath}\"."); } } @@ -269,7 +264,7 @@ namespace NewHorizons.Builder.Props UnityEngine.Object.DestroyImmediate(prop); prop = newDetailGO; } - + if (isItem) { // Else when you put them down you can't pick them back up @@ -281,7 +276,7 @@ namespace NewHorizons.Builder.Props // For DLC related props // Make sure to do this before its set active - if (!string.IsNullOrEmpty(detail?.path) && + if (!string.IsNullOrEmpty(detail?.path) && (detail.path.ToLowerInvariant().StartsWith("ringworld") || detail.path.ToLowerInvariant().StartsWith("dreamworld"))) { prop.AddComponent()._destroyOnDLCNotOwned = true; @@ -300,7 +295,7 @@ namespace NewHorizons.Builder.Props if (!string.IsNullOrEmpty(detail.activationCondition)) { - ConditionalObjectActivation.SetUp(prop, detail.activationCondition, detail.blinkWhenActiveChanged, true); + ConditionalObjectActivation.SetUp(prop, detail.activationCondition, detail.blinkWhenActiveChanged, true); } if (!string.IsNullOrEmpty(detail.deactivationCondition)) { @@ -574,22 +569,22 @@ namespace NewHorizons.Builder.Props // Manually copied these values from a artifact lantern so that we don't have to find it (works in Eye) lantern._origLensFlareBrightness = 0f; - lantern._focuserPetalsBaseEulerAngles = new Vector3[] - { - new Vector3(0.7f, 270.0f, 357.5f), - new Vector3(288.7f, 270.1f, 357.4f), + lantern._focuserPetalsBaseEulerAngles = new Vector3[] + { + new Vector3(0.7f, 270.0f, 357.5f), + new Vector3(288.7f, 270.1f, 357.4f), new Vector3(323.3f, 90.0f, 177.5f), - new Vector3(35.3f, 90.0f, 177.5f), - new Vector3(72.7f, 270.1f, 357.5f) + new Vector3(35.3f, 90.0f, 177.5f), + new Vector3(72.7f, 270.1f, 357.5f) }; lantern._dirtyFlag_focus = true; - lantern._concealerRootsBaseScale = new Vector3[] + lantern._concealerRootsBaseScale = new Vector3[] { Vector3.one, Vector3.one, Vector3.one }; - lantern._concealerCoversStartPos = new Vector3[] + lantern._concealerCoversStartPos = new Vector3[] { new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, -0.1f, 0.0f), @@ -600,7 +595,7 @@ namespace NewHorizons.Builder.Props }; lantern._dirtyFlag_concealment = true; lantern.UpdateVisuals(); - + Destroy(this); } } diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index 25b75383..42884d9d 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -1,4 +1,3 @@ -using HarmonyLib; using NewHorizons.Builder.Atmosphere; using NewHorizons.Builder.Body; using NewHorizons.Builder.General; @@ -1009,9 +1008,7 @@ namespace NewHorizons.Handlers var path = $"{goPath}/{childPath}"; var flag = true; - var childObjs = transforms.Where(x => x.GetPath() == path).ToList(); - var childObjs2 = go.transform.FindAll(childPath); - foreach (var childObj in childObjs) + foreach (var childObj in transforms.Where(x => x.GetPath() == path)) { flag = false; // idk why we wait here but we do @@ -1024,8 +1021,6 @@ namespace NewHorizons.Handlers }, 2); } - Main.Instance.ModHelper.Console.WriteLine($"remove children planet\n{go.transform.GetPath()}\n{childPath}\n\n{childObjs.Join()}\n{childObjs2.Join()}"); - if (flag) NHLogger.LogWarning($"Couldn't find \"{childPath}\"."); } } From 3a490cee896b93d3dbfb0963c4a9ded2dfb0d007 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Fri, 14 Feb 2025 21:28:13 -0800 Subject: [PATCH 28/30] use FindAll --- NewHorizons/Builder/Props/DetailBuilder.cs | 31 ++++++++----------- NewHorizons/Handlers/PlanetCreationHandler.cs | 7 +---- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/NewHorizons/Builder/Props/DetailBuilder.cs b/NewHorizons/Builder/Props/DetailBuilder.cs index d2064b68..a598f7ed 100644 --- a/NewHorizons/Builder/Props/DetailBuilder.cs +++ b/NewHorizons/Builder/Props/DetailBuilder.cs @@ -220,15 +220,10 @@ namespace NewHorizons.Builder.Props if (detail.removeChildren != null) { - var detailPath = prop.transform.GetPath(); - var transforms = prop.GetComponentsInChildren(true); foreach (var childPath in detail.removeChildren) { - // Multiple children can have the same path so we delete all that match - var path = $"{detailPath}/{childPath}"; - var flag = true; - foreach (var childObj in transforms.Where(x => x.GetPath() == path)) + foreach (var childObj in prop.transform.FindAll(childPath)) { flag = false; childObj.gameObject.SetActive(false); @@ -264,7 +259,7 @@ namespace NewHorizons.Builder.Props UnityEngine.Object.DestroyImmediate(prop); prop = newDetailGO; } - + if (isItem) { // Else when you put them down you can't pick them back up @@ -276,7 +271,7 @@ namespace NewHorizons.Builder.Props // For DLC related props // Make sure to do this before its set active - if (!string.IsNullOrEmpty(detail?.path) && + if (!string.IsNullOrEmpty(detail?.path) && (detail.path.ToLowerInvariant().StartsWith("ringworld") || detail.path.ToLowerInvariant().StartsWith("dreamworld"))) { prop.AddComponent()._destroyOnDLCNotOwned = true; @@ -295,7 +290,7 @@ namespace NewHorizons.Builder.Props if (!string.IsNullOrEmpty(detail.activationCondition)) { - ConditionalObjectActivation.SetUp(prop, detail.activationCondition, detail.blinkWhenActiveChanged, true); + ConditionalObjectActivation.SetUp(prop, detail.activationCondition, detail.blinkWhenActiveChanged, true); } if (!string.IsNullOrEmpty(detail.deactivationCondition)) { @@ -569,22 +564,22 @@ namespace NewHorizons.Builder.Props // Manually copied these values from a artifact lantern so that we don't have to find it (works in Eye) lantern._origLensFlareBrightness = 0f; - lantern._focuserPetalsBaseEulerAngles = new Vector3[] - { - new Vector3(0.7f, 270.0f, 357.5f), - new Vector3(288.7f, 270.1f, 357.4f), + lantern._focuserPetalsBaseEulerAngles = new Vector3[] + { + new Vector3(0.7f, 270.0f, 357.5f), + new Vector3(288.7f, 270.1f, 357.4f), new Vector3(323.3f, 90.0f, 177.5f), - new Vector3(35.3f, 90.0f, 177.5f), - new Vector3(72.7f, 270.1f, 357.5f) + new Vector3(35.3f, 90.0f, 177.5f), + new Vector3(72.7f, 270.1f, 357.5f) }; lantern._dirtyFlag_focus = true; - lantern._concealerRootsBaseScale = new Vector3[] + lantern._concealerRootsBaseScale = new Vector3[] { Vector3.one, Vector3.one, Vector3.one }; - lantern._concealerCoversStartPos = new Vector3[] + lantern._concealerCoversStartPos = new Vector3[] { new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, -0.1f, 0.0f), @@ -595,7 +590,7 @@ namespace NewHorizons.Builder.Props }; lantern._dirtyFlag_concealment = true; lantern.UpdateVisuals(); - + Destroy(this); } } diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index 42884d9d..b188fd2d 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -1000,15 +1000,10 @@ namespace NewHorizons.Handlers private static void RemoveChildren(GameObject go, NewHorizonsBody body) { - var goPath = go.transform.GetPath(); - var transforms = go.GetComponentsInChildren(true); foreach (var childPath in body.Config.removeChildren) { - // Multiple children can have the same path so we delete all that match - var path = $"{goPath}/{childPath}"; - var flag = true; - foreach (var childObj in transforms.Where(x => x.GetPath() == path)) + foreach (var childObj in go.transform.FindAll(childPath)) { flag = false; // idk why we wait here but we do From c7f3ea8bc295bc4234d73092207af0c5b173c87f Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Sat, 15 Feb 2025 18:35:24 -0800 Subject: [PATCH 29/30] disable profiler --- NewHorizons/NewHorizons.csproj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/NewHorizons/NewHorizons.csproj b/NewHorizons/NewHorizons.csproj index 15857053..a38229e0 100644 --- a/NewHorizons/NewHorizons.csproj +++ b/NewHorizons/NewHorizons.csproj @@ -10,8 +10,8 @@ true None 1701;1702;1591 - - ENABLE_PROFILER + + none @@ -41,4 +41,4 @@ - \ No newline at end of file + From ec5d42c042d235aa95dc5e41fa88a2afbe2e7b41 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Sat, 15 Feb 2025 20:35:30 -0800 Subject: [PATCH 30/30] doc --- NewHorizons/Patches/ProfilerPatch.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/NewHorizons/Patches/ProfilerPatch.cs b/NewHorizons/Patches/ProfilerPatch.cs index fb480722..9cdde508 100644 --- a/NewHorizons/Patches/ProfilerPatch.cs +++ b/NewHorizons/Patches/ProfilerPatch.cs @@ -57,8 +57,12 @@ public static class ProfilerPatch } } +/// +/// bundle loading causes log spam that slows loading, but only in unity dev profiler mode. +/// patch it out so it doesnt do false-positive slowness. +/// [HarmonyPatch] -public static class EvilPatch +public static class DisableShaderLogSpamPatch { [HarmonyPrefix] [HarmonyPatch(typeof(StackTraceUtility), "ExtractStackTrace")]