Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@
## 2025-05-21 - Optimized GetRackCount calls (FindObjectsOfType)
**Learning:** Using `UnityEngine.Object.FindObjectsOfType<Rack>` to simply get the rack count is an O(N) operation over all objects, creating unnecessary GC pressure and CPU overhead, especially as the data center grows.
**Action:** Optimized `GetRackCount` implementation in `GameHooks.cs` by using the game-managed O(1) singleton `Il2Cpp.NetworkMap.instance.GetNumberOfDevices()` (index 2 for racks), providing a fallback to `FindObjectsOfType` only during uninitialized states.
## 2025-05-22 - Optimize FindObjectsOfType in Mod-facing Lua APIs
**Learning:** Found O(N) scene queries `UnityEngine.Object.FindObjectsOfType<Il2Cpp.Server>()` used in mod-facing Lua APIs `greg.server.get_all()`, `repair()`, and `repair_all()`. Because these functions may be polled from Lua scripts, this blocking operation can introduce intense CPU and GC overhead.
**Action:** Replaced these calls with O(1) lookups via `Il2Cpp.NetworkMap.instance.servers` and `.brokenServers`. For functions that iterate and mutate the collection simultaneously (like `repair_all()`), create a defensive copy (`List<T>`) while including an early return (`if (collection.Count == 0) return;`) to avoid allocations in the most common empty state.
8 changes: 7 additions & 1 deletion src/GameLayer/Patches/Networking/CablePositionsPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
}
}

[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
private static void LogSetBaseId(int val)
{
MelonLogger.Msg($"[CablePatch] Cable ID counter set to {val}");
}

public static void SetBaseId(int baseId)
{
int current;
Expand All @@ -67,7 +73,7 @@
}
while (Interlocked.CompareExchange(ref _nextCableId, baseId + 1, current) != current);

MelonLogger.Msg($"[CablePatch] Cable ID counter set to {baseId + 1}");
try { LogSetBaseId(baseId + 1); } catch { }

Check warning on line 76 in src/GameLayer/Patches/Networking/CablePositionsPatch.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/GameLayer/Patches/Networking/CablePositionsPatch.cs#L76

Handle the exception or explain in a comment why it can be ignored.
}

public static int PeekNextId() => _nextCableId;
Expand Down
52 changes: 51 additions & 1 deletion src/Infrastructure/Scripting/Lua/Modules/LuaServerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,23 @@ public static void Register(Table greg, Script script, string modId)
{
try
{
var servers = UnityEngine.Object.FindObjectsOfType<Il2Cpp.Server>();
var nm = Il2Cpp.NetworkMap.instance;
var servers = new System.Collections.Generic.List<Il2Cpp.Server>();
if (nm != null && nm.servers != null)
{
foreach (var kvp in nm.servers)
{
servers.Add(kvp.Value);
}
}
else
{
var found = UnityEngine.Object.FindObjectsOfType<Il2Cpp.Server>();
if (found != null)
{
foreach (var s in found) servers.Add(s);
}
}
var result = new Table(script);
int i = 1;
foreach (var s in servers)
Expand Down Expand Up @@ -80,6 +96,20 @@ public static void Register(Table greg, Script script, string modId)
{
try
{
var nm = Il2Cpp.NetworkMap.instance;
if (nm != null && nm.brokenServers != null)
{
foreach (var kvp in nm.brokenServers)
{
var s = kvp.Value;
if (s != null && s.GetHashCode() == hash && s.isBroken)
{
s.RepairDevice();
return true;
}
}
return false;
}
var servers = UnityEngine.Object.FindObjectsOfType<Il2Cpp.Server>();
foreach (var s in servers)
{
Expand All @@ -104,6 +134,26 @@ public static void Register(Table greg, Script script, string modId)
try
{
int repaired = 0;
var nm = Il2Cpp.NetworkMap.instance;
if (nm != null && nm.brokenServers != null)
{
if (nm.brokenServers.Count == 0) return 0;
var defensiveCopy = new System.Collections.Generic.List<Il2Cpp.Server>();
foreach (var kvp in nm.brokenServers) defensiveCopy.Add(kvp.Value);
foreach (var s in defensiveCopy)
{
try
{
if (s != null && s.isBroken)
{
s.RepairDevice();
repaired++;
}
}
catch { }
}
return repaired;
}
var servers = UnityEngine.Object.FindObjectsOfType<Il2Cpp.Server>();
foreach (var s in servers)
{
Expand Down
Loading