-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaServerModule.cs
More file actions
198 lines (183 loc) · 6.75 KB
/
Copy pathLuaServerModule.cs
File metadata and controls
198 lines (183 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/// <file-summary>
/// Schicht: Infrastructure
/// Zweck: Lua-API für Server-Management.
/// Maintainer: greg.server.get_all(), power_on/off(), repair(), count()
/// </file-summary>
using System;
using MoonSharp.Interpreter;
using MelonLoader;
using System.Linq;
namespace gregCore.Infrastructure.Scripting.Lua.Modules;
public static class LuaServerModule
{
public static void Register(Table greg, Script script, string modId)
{
var serverTable = new Table(script);
// greg.server.get_all() → table of server info
serverTable["get_all"] = (Func<Table>)(() =>
{
try
{
var result = new Table(script);
int i = 1;
var nm = Il2Cpp.NetworkMap.instance;
if (nm != null && nm.servers != null)
{
foreach (var kvp in nm.servers)
{
try
{
var s = kvp.Value;
if (s == null) continue;
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 { }
}
return result;
}
var servers = UnityEngine.Object.FindObjectsOfType<Il2Cpp.Server>();
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 { }
}
return result;
}
catch (Exception ex)
{
MelonLogger.Error($"[LuaMod:{modId}] server.get_all() failed: {ex.Message}");
return new Table(script);
}
});
// greg.server.count() → number
serverTable["count"] = (Func<int>)(() =>
{
try
{
var nm = Il2Cpp.NetworkMap.instance;
if (nm != null && nm.servers != null)
{
return nm.servers.Count;
}
var servers = UnityEngine.Object.FindObjectsOfType<Il2Cpp.Server>();
return servers?.Count ?? 0;
}
catch { return 0; }
});
// greg.server.broken_count() → number
serverTable["broken_count"] = (Func<int>)(() =>
{
try { return (int)API.GregAPI.GetBrokenServerCount(); }
catch { return 0; }
});
// greg.server.repair(server_hash) → bool
serverTable["repair"] = (Func<int, bool>)((hash) =>
{
try
{
var nm = Il2Cpp.NetworkMap.instance;
if (nm != null && nm.brokenServers != null)
{
foreach (var kvp in nm.brokenServers)
{
try
{
var s = kvp.Value;
if (s != null && s.GetHashCode() == hash && s.isBroken)
{
s.RepairDevice();
return true;
}
}
catch { }
}
return false;
}
var servers = UnityEngine.Object.FindObjectsOfType<Il2Cpp.Server>();
foreach (var s in servers)
{
try
{
if (s.GetHashCode() == hash && s.isBroken)
{
s.RepairDevice();
return true;
}
}
catch { }
}
return false;
}
catch { return false; }
});
// greg.server.repair_all() → number of repaired
serverTable["repair_all"] = (Func<int>)(() =>
{
try
{
int repaired = 0;
var nm = Il2Cpp.NetworkMap.instance;
if (nm != null && nm.brokenServers != null)
{
if (nm.brokenServers.Count == 0) return 0;
var brokenList = new System.Collections.Generic.List<Il2Cpp.Server>();
foreach (var kvp in nm.brokenServers) brokenList.Add(kvp.Value);
foreach (var s in brokenList)
{
try
{
if (s != null && s.isBroken)
{
s.RepairDevice();
repaired++;
}
}
catch { }
}
return repaired;
}
var servers = UnityEngine.Object.FindObjectsOfType<Il2Cpp.Server>();
foreach (var s in servers)
{
try
{
if (s.isBroken)
{
s.RepairDevice();
repaired++;
}
}
catch { }
}
return repaired;
}
catch { return 0; }
});
greg["server"] = serverTable;
}
}