diff --git a/src/GameLayer/Patches/Networking/CablePositionsPatch.cs b/src/GameLayer/Patches/Networking/CablePositionsPatch.cs index 84c3f50c..1aa2c30b 100644 --- a/src/GameLayer/Patches/Networking/CablePositionsPatch.cs +++ b/src/GameLayer/Patches/Networking/CablePositionsPatch.cs @@ -57,6 +57,12 @@ private static bool CreateNewCablePrefix( } } + [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] + private static void LogSetBaseId(int baseId) + { + MelonLogger.Msg($"[CablePatch] Cable ID counter set to {baseId + 1}"); + } + public static void SetBaseId(int baseId) { int current; @@ -67,7 +73,7 @@ public static void SetBaseId(int baseId) } while (Interlocked.CompareExchange(ref _nextCableId, baseId + 1, current) != current); - MelonLogger.Msg($"[CablePatch] Cable ID counter set to {baseId + 1}"); + try { LogSetBaseId(baseId); } catch { } } public static int PeekNextId() => _nextCableId; diff --git a/src/Infrastructure/Scripting/Lua/Modules/LuaServerModule.cs b/src/Infrastructure/Scripting/Lua/Modules/LuaServerModule.cs index 17bd8abb..e35c9826 100644 --- a/src/Infrastructure/Scripting/Lua/Modules/LuaServerModule.cs +++ b/src/Infrastructure/Scripting/Lua/Modules/LuaServerModule.cs @@ -21,27 +21,59 @@ 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) + var nm = Il2Cpp.NetworkMap.instance; + if (nm != null && nm.servers != null && nm.servers.Count > 0) + { + foreach (var kvp in nm.servers) + { + var s = kvp.Value; + if (s == null) continue; + try + { + var info = new Table(script); + info["id"] = s.ServerID ?? s.GetHashCode().ToString(); + info["hash"] = s.GetHashCode(); + info["is_on"] = s.isOn; + info["is_broken"] = s.isBroken; + info["server_type"] = (int)s.serverType; + info["size_u"] = s.sizeInU; + var pos = s.transform?.position ?? UnityEngine.Vector3.zero; + info["x"] = (double)pos.x; + info["y"] = (double)pos.y; + info["z"] = (double)pos.z; + result[i++] = info; + } + catch { } + } + } + else { - try + var servers = UnityEngine.Object.FindObjectsOfType(); + if (servers != null) { - var info = new Table(script); - info["id"] = s.ServerID ?? s.GetHashCode().ToString(); - info["hash"] = s.GetHashCode(); - info["is_on"] = s.isOn; - info["is_broken"] = s.isBroken; - info["server_type"] = (int)s.serverType; - info["size_u"] = s.sizeInU; - var pos = s.transform?.position ?? UnityEngine.Vector3.zero; - info["x"] = (double)pos.x; - info["y"] = (double)pos.y; - info["z"] = (double)pos.z; - result[i++] = info; + foreach (var s in servers) + { + if (s == null) continue; + try + { + var info = new Table(script); + info["id"] = s.ServerID ?? s.GetHashCode().ToString(); + info["hash"] = s.GetHashCode(); + info["is_on"] = s.isOn; + info["is_broken"] = s.isBroken; + info["server_type"] = (int)s.serverType; + info["size_u"] = s.sizeInU; + var pos = s.transform?.position ?? UnityEngine.Vector3.zero; + info["x"] = (double)pos.x; + info["y"] = (double)pos.y; + info["z"] = (double)pos.z; + result[i++] = info; + } + catch { } + } } - catch { } } return result; } @@ -80,18 +112,42 @@ public static void Register(Table greg, Script script, string modId) { try { - var servers = UnityEngine.Object.FindObjectsOfType(); - foreach (var s in servers) + var nm = Il2Cpp.NetworkMap.instance; + if (nm != null && nm.brokenServers != null && nm.brokenServers.Count > 0) + { + foreach (var kvp in nm.brokenServers) + { + var s = kvp.Value; + if (s == null) continue; + try + { + if (s.GetHashCode() == hash && s.isBroken) + { + s.RepairDevice(); + return true; + } + } + catch { } + } + } + else { - try + var servers = UnityEngine.Object.FindObjectsOfType(); + if (servers != null) { - if (s.GetHashCode() == hash && s.isBroken) + foreach (var s in servers) { - s.RepairDevice(); - return true; + try + { + if (s.GetHashCode() == hash && s.isBroken) + { + s.RepairDevice(); + return true; + } + } + catch { } } } - catch { } } return false; } @@ -104,18 +160,48 @@ public static void Register(Table greg, Script script, string modId) try { int repaired = 0; - var servers = UnityEngine.Object.FindObjectsOfType(); - foreach (var s in servers) + var nm = Il2Cpp.NetworkMap.instance; + if (nm != null && nm.brokenServers != null) + { + if (nm.brokenServers.Count > 0) + { + var defensiveList = new System.Collections.Generic.List(); + foreach (var kvp in nm.brokenServers) + { + if (kvp.Value != null) defensiveList.Add(kvp.Value); + } + foreach (var s in defensiveList) + { + try + { + if (s.isBroken) + { + s.RepairDevice(); + repaired++; + } + } + catch { } + } + } + } + else { - try + var servers = UnityEngine.Object.FindObjectsOfType(); + if (servers != null) { - if (s.isBroken) + foreach (var s in servers) { - s.RepairDevice(); - repaired++; + try + { + if (s.isBroken) + { + s.RepairDevice(); + repaired++; + } + } + catch { } } } - catch { } } return repaired; }