diff --git a/src/Infrastructure/Scripting/Lua/Modules/LuaServerModule.cs b/src/Infrastructure/Scripting/Lua/Modules/LuaServerModule.cs index 17bd8abb..24ad3b52 100644 --- a/src/Infrastructure/Scripting/Lua/Modules/LuaServerModule.cs +++ b/src/Infrastructure/Scripting/Lua/Modules/LuaServerModule.cs @@ -21,27 +21,55 @@ 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) { - try + foreach (var kvp in nm.servers) { - 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; + 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 + { + var servers = UnityEngine.Object.FindObjectsOfType(); + foreach (var s in servers) + { + 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,6 +108,21 @@ 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; // If we didn't find it in broken servers, it's either not broken or not a server. + } + var servers = UnityEngine.Object.FindObjectsOfType(); foreach (var s in servers) { @@ -104,6 +147,29 @@ 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 toRepair = new System.Collections.Generic.List(); + foreach (var kvp in nm.brokenServers) + { + if (kvp.Value != null && kvp.Value.isBroken) + toRepair.Add(kvp.Value); + } + foreach (var s in toRepair) + { + try + { + s.RepairDevice(); + repaired++; + } + catch { } + } + return repaired; + } + var servers = UnityEngine.Object.FindObjectsOfType(); foreach (var s in servers) {