From 37c3f45dd62e576b2e23cd355718f2a7ad5bcb9e Mon Sep 17 00:00:00 2001 From: mleem97 <52848568+mleem97@users.noreply.github.com> Date: Mon, 3 Aug 2026 00:16:09 +0000 Subject: [PATCH] Replace expensive FindObjectsOfType calls with game-managed NetworkMap singletons This optimizes operations across GregAPI, GameHooks, and Lua APIs by retrieving active hardware devices directly from `Il2Cpp.NetworkMap.instance` dictionaries instead of scanning the full Unity object hierarchy. This shifts lookup and iteration costs from O(N) to O(1) in the best case, saving massive CPU cycles and reducing GC pressure, particularly in late-game scenes with thousands of instantiated devices. --- src/API/GregAPI.cs | 12 ++- .../DataCenterModLoader/GameHooks.cs | 46 +++++++++- .../Scripting/Lua/Modules/LuaRackModule.cs | 2 +- .../Scripting/Lua/Modules/LuaServerModule.cs | 88 ++++++++++++++++--- 4 files changed, 131 insertions(+), 17 deletions(-) diff --git a/src/API/GregAPI.cs b/src/API/GregAPI.cs index 75331360..57785742 100644 --- a/src/API/GregAPI.cs +++ b/src/API/GregAPI.cs @@ -192,6 +192,11 @@ public static uint GetServerCount() try { var nm = Il2Cpp.NetworkMap.instance; + if (nm != null) + { + var arr = nm.GetNumberOfDevices(); + if (arr != null && arr.Length > 0) return (uint)arr[0]; + } return nm != null && nm.servers != null ? (uint)nm.servers.Count : 0u; } catch { return 0u; } @@ -208,7 +213,7 @@ public static uint GetRackCount() if (arr != null && arr.Length > 2) return (uint)arr[2]; } var racks = UnityEngine.Object.FindObjectsOfType(); - return racks != null ? (uint)racks.Count : 0u; + return racks != null ? (uint)racks.Length : 0u; } catch { return 0u; } } @@ -218,6 +223,11 @@ public static uint GetSwitchCount() try { var nm = Il2Cpp.NetworkMap.instance; + if (nm != null) + { + var arr = nm.GetNumberOfDevices(); + if (arr != null && arr.Length > 1) return (uint)arr[1]; + } return nm != null && nm.switches != null ? (uint)nm.switches.Count : 0u; } catch { return 0u; } diff --git a/src/Compatibility/DataCenterModLoader/GameHooks.cs b/src/Compatibility/DataCenterModLoader/GameHooks.cs index 45e4c0d1..026e4bef 100644 --- a/src/Compatibility/DataCenterModLoader/GameHooks.cs +++ b/src/Compatibility/DataCenterModLoader/GameHooks.cs @@ -69,9 +69,8 @@ public static int EnsureAllRackPositionUIDs() try { - var servers = UnityEngine.Object.FindObjectsOfType(); int updated = 0; - foreach (var srv in servers) + Action processServer = (srv) => { try { @@ -87,6 +86,26 @@ public static int EnsureAllRackPositionUIDs() } } catch { } + }; + + var nm = Il2Cpp.NetworkMap.instance; + if (nm != null && nm.servers != null) + { + foreach (var kvp in nm.servers) + { + processServer(kvp.Value); + } + } + else + { + var servers = UnityEngine.Object.FindObjectsOfType(); + if (servers != null) + { + foreach (var srv in servers) + { + processServer(srv); + } + } } if (updated > 0) CrashLog.Log($"[WorldSync] EnsureAllRackPositionUIDs: updated {updated} server rackPositionUID references"); @@ -98,9 +117,8 @@ public static int EnsureAllRackPositionUIDs() try { - var switches = UnityEngine.Object.FindObjectsOfType(); int swUpdated = 0; - foreach (var sw in switches) + Action processSwitch = (sw) => { try { @@ -116,6 +134,26 @@ public static int EnsureAllRackPositionUIDs() } } catch { } + }; + + var nm = Il2Cpp.NetworkMap.instance; + if (nm != null && nm.switches != null) + { + foreach (var kvp in nm.switches) + { + processSwitch(kvp.Value); + } + } + else + { + var switches = UnityEngine.Object.FindObjectsOfType(); + if (switches != null) + { + foreach (var sw in switches) + { + processSwitch(sw); + } + } } if (swUpdated > 0) CrashLog.Log($"[WorldSync] EnsureAllRackPositionUIDs: updated {swUpdated} switch rackPositionUID references"); diff --git a/src/Infrastructure/Scripting/Lua/Modules/LuaRackModule.cs b/src/Infrastructure/Scripting/Lua/Modules/LuaRackModule.cs index bc7bde3b..632c66f0 100644 --- a/src/Infrastructure/Scripting/Lua/Modules/LuaRackModule.cs +++ b/src/Infrastructure/Scripting/Lua/Modules/LuaRackModule.cs @@ -63,7 +63,7 @@ public static void Register(Table greg, Script script, string modId) if (arr != null && arr.Length > 2) return arr[2]; } var racks = UnityEngine.Object.FindObjectsOfType(); - return racks?.Count ?? 0; + return racks?.Length ?? 0; } catch { return 0; } }); diff --git a/src/Infrastructure/Scripting/Lua/Modules/LuaServerModule.cs b/src/Infrastructure/Scripting/Lua/Modules/LuaServerModule.cs index 17bd8abb..454f7bba 100644 --- a/src/Infrastructure/Scripting/Lua/Modules/LuaServerModule.cs +++ b/src/Infrastructure/Scripting/Lua/Modules/LuaServerModule.cs @@ -21,10 +21,10 @@ public static void Register(Table greg, Script script, string modId) { try { - var servers = UnityEngine.Object.FindObjectsOfType(); var result = new Table(script); int i = 1; - foreach (var s in servers) + + Action processServer = (s) => { try { @@ -42,6 +42,26 @@ public static void Register(Table greg, Script script, string modId) result[i++] = info; } catch { } + }; + + var nm = Il2Cpp.NetworkMap.instance; + if (nm != null && nm.servers != null) + { + foreach (var kvp in nm.servers) + { + processServer(kvp.Value); + } + } + else + { + var servers = UnityEngine.Object.FindObjectsOfType(); + if (servers != null) + { + foreach (var s in servers) + { + processServer(s); + } + } } return result; } @@ -58,12 +78,16 @@ public static void Register(Table greg, Script script, string modId) try { var nm = Il2Cpp.NetworkMap.instance; - if (nm != null && nm.servers != null) + if (nm != null) { - return nm.servers.Count; + var counts = nm.GetNumberOfDevices(); + if (counts != null && counts.Length > 0) + { + return counts[0]; + } } var servers = UnityEngine.Object.FindObjectsOfType(); - return servers?.Count ?? 0; + return servers?.Length ?? 0; } catch { return 0; } }); @@ -80,20 +104,43 @@ public static void Register(Table greg, Script script, string modId) { try { - var servers = UnityEngine.Object.FindObjectsOfType(); - foreach (var s in servers) + bool repaired = false; + Action processServer = (s) => { + if (repaired) return; try { if (s.GetHashCode() == hash && s.isBroken) { s.RepairDevice(); - return true; + repaired = true; } } catch { } + }; + + var nm = Il2Cpp.NetworkMap.instance; + if (nm != null && nm.servers != null) + { + foreach (var kvp in nm.servers) + { + processServer(kvp.Value); + if (repaired) return true; + } } - return false; + else + { + var servers = UnityEngine.Object.FindObjectsOfType(); + if (servers != null) + { + foreach (var s in servers) + { + processServer(s); + if (repaired) return true; + } + } + } + return repaired; } catch { return false; } }); @@ -104,8 +151,7 @@ public static void Register(Table greg, Script script, string modId) try { int repaired = 0; - var servers = UnityEngine.Object.FindObjectsOfType(); - foreach (var s in servers) + Action processServer = (s) => { try { @@ -116,6 +162,26 @@ public static void Register(Table greg, Script script, string modId) } } catch { } + }; + + var nm = Il2Cpp.NetworkMap.instance; + if (nm != null && nm.servers != null) + { + foreach (var kvp in nm.servers) + { + processServer(kvp.Value); + } + } + else + { + var servers = UnityEngine.Object.FindObjectsOfType(); + if (servers != null) + { + foreach (var s in servers) + { + processServer(s); + } + } } return repaired; }