-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.lua
368 lines (290 loc) · 10.9 KB
/
server.lua
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
--------------------------------------------------------------------------------
-- Bootloader entrypoint
--------------------------------------------------------------------------------
function onThisResourceStart()
prepareBootloaderSettings()
startBootloaderResources()
setTimer(keepAliveTimerCallback, 10 * 60000, 0)
end
addEventHandler("onResourceStart", resourceRoot, onThisResourceStart, false)
function startBootloaderResources()
local resourceList = getBootloaderResources()
for i = 1, #resourceList do
local resource = resourceList[i]
if getResourceState(resource) == "loaded" then
startResource(resource, true)
end
end
end
function keepAliveTimerCallback()
local keepAlive = getBool("keepAlive", true)
if keepAlive then
startBootloaderResources()
end
end
--------------------------------------------------------------------------------
-- Settings management
--------------------------------------------------------------------------------
local settingsResourceDict = {}
function prepareBootloaderSettings()
local resourceNameList = getBootloaderResourcesSetting()
for i = 1, #resourceNameList do
local resourceName = resourceNameList[i]
settingsResourceDict[resourceName] = true
end
end
function getBootloaderResourceNamesDict()
return settingsResourceDict
end
function isBootloaderResource(resourceName)
return settingsResourceDict[resourceName] ~= nil
end
function getBootloaderResources()
local result = {}
local length = 0
for resourceName in pairs(settingsResourceDict) do
local resource = getResourceFromName(resourceName)
if resource then
length = length + 1
result[length] = resource
end
end
return result
end
function getBootloaderResourcesSetting()
local result = {}
local length = 0
local resourceNameList = get("@resources") or ""
for index, resourceName in pairs(split(resourceNameList, ",")) do
resourceName = utf8.trim(resourceName)
if resourceName ~= "" then
length = length + 1
result[length] = resourceName
end
end
return result
end
function saveBootloaderResourcesSetting()
local resourceNameList = {}
local length = 0
for resourceName in pairs(settingsResourceDict) do
if getResourceFromName(resourceName) then
length = length + 1
resourceNameList[length] = resourceName
end
end
set("@resources", table.concat(resourceNameList, ","))
end
function toggleBootloaderResource(resourceName, enabled)
if enabled and settingsResourceDict[resourceName] == nil then
settingsResourceDict[resourceName] = true
saveBootloaderResourcesSetting()
elseif not enabled and settingsResourceDict[resourceName] ~= nil then
settingsResourceDict[resourceName] = nil
saveBootloaderResourcesSetting()
end
end
--------------------------------------------------------------------------------
-- Access control and session management
--------------------------------------------------------------------------------
local activePlayerSessions = {}
function getPlayerSession(player)
return activePlayerSessions[player]
end
function clearPlayerSession(player)
local session = activePlayerSessions[player]
activePlayerSessions[player] = nil
if session then
stopSessionDataTransfers(session)
end
end
function onPlayerBootloaderCommand(player)
local enabled = false
if activePlayerSessions[player] ~= nil then
activePlayerSessions[player] = nil
else
activePlayerSessions[player] = {}
enabled = true
end
triggerClientEvent(player, "Bootloader.toggleConfigurationPanel", resourceRoot, enabled)
end
addCommandHandler("bootloader", onPlayerBootloaderCommand, true, false)
function onPlayerQuit()
clearPlayerSession(source)
end
addEventHandler("onPlayerQuit", root, onPlayerQuit)
function onClientBootloaderClosePanel()
clearPlayerSession(client)
end
addEvent("BootloaderClient.closePanel", true)
addEventHandler("BootloaderClient.closePanel", resourceRoot, onClientBootloaderClosePanel, false)
function isPlayerBootloaderAuthorized(player)
return activePlayerSessions[player] ~= nil
end
--------------------------------------------------------------------------------
-- Data stream events
--------------------------------------------------------------------------------
function stopSessionDataTransfers(session)
stopSessionResourceDataListTransfer(session)
end
function onClientBootloaderResourceDataListRequest()
sendPlayerBootloaderResourceDataList(client)
end
addEvent("BootloaderClient.requestResourceDataList", true)
addEventHandler("BootloaderClient.requestResourceDataList", resourceRoot, onClientBootloaderResourceDataListRequest, false)
function onClientBootloaderToggleResource(resourceName, enabled)
if type(resourceName) ~= "string" or resourceName == "" then
return
end
if type(enabled) ~= "boolean" then
return
end
local resource = getResourceFromName(resourceName)
if not resource then
return sendPlayerBootloaderResourceDataList(client)
end
if not isPlayerBootloaderAuthorized(client) then
return
end
toggleBootloaderResource(resourceName, enabled)
enabled = isBootloaderResource(resourceName)
outputServerLog(("BOOTLOADER: Resource '%s' has been %s by player '%s' (account: %s, ip: %s, serial: %s)"):format(
resourceName,
(enabled and "enabled" or "disabled"),
getPlayerName(client),
getAccountName(getPlayerAccount(client)),
getPlayerIP(client),
getPlayerSerial(client)
))
local resourceState = getResourceState(resource)
if resourceState == "loaded" and enabled then
startResource(resource, true)
elseif resourceState == "running" and not enabled then
stopResource(resource)
end
setTimer(sendPlayerBootloaderResourceData, 100, 1, client, resourceName)
end
addEvent("BootloaderClient.toggleBootloaderResource", true)
addEventHandler("BootloaderClient.toggleBootloaderResource", resourceRoot, onClientBootloaderToggleResource, false)
function stopSessionResourceDataListTransfer(session)
local transfer = session.resourcesBatchTransfer
session.resourcesBatchTransfer = nil
if transfer ~= nil then
if isTimer(transfer.timer) then
killTimer(transfer.timer)
end
end
end
function sendPlayerResourceDataListThread(player, session, transfer)
local length = #transfer.resources
if length > 0 then
local index = 1
while index <= length do
local batch = {}
local batchLength = 0
local batchSize = 5
while index <= length and batchLength < batchSize do
local resource = transfer.resources[index]
index = index + 1
if getUserdataType(resource) == "resource-data" then
local resourceName = getResourceName(resource)
local data = {
enabled = transfer.enableds[resourceName] ~= nil,
running = getResourceState(resource) == "running",
name = resourceName,
type = getResourceInfo(resource, "type"),
description = getResourceInfo(resource, "description"),
}
batchLength = batchLength + 1
batch[batchLength] = data
end
end
if batchLength > 0 then
triggerClientEvent(player, "Bootloader.resourceDataListBatch", resourceRoot, batch)
coroutine.yield()
end
end
end
triggerClientEvent(player, "Bootloader.resourceDataListComplete", resourceRoot)
stopSessionResourceDataListTransfer(session)
end
function sendPlayerBootloaderResourceDataList(player)
local session = getPlayerSession(player)
if session == nil then
return
end
stopSessionResourceDataListTransfer(session)
local coroutine = coroutine.wrap(sendPlayerResourceDataListThread)
local transfer = {
resources = getResourcesSnapshot(),
enableds = table.shallowcopy(getBootloaderResourceNamesDict()),
coroutine = coroutine,
timer = setTimer(coroutine, 100, 0),
}
session.resourcesBatchTransfer = transfer
coroutine(player, session, transfer)
end
function sendPlayerBootloaderResourceData(player, resourceName)
if not isPlayerBootloaderAuthorized(player) then
return
end
local resource = getResourceFromName(resourceName)
if not resource then
return sendPlayerBootloaderResourceDataList(player)
end
local enabled = isBootloaderResource(resourceName)
local running = getResourceState(resource) == "running"
triggerClientEvent(player, "Bootloader.resourceDataResponse", resourceRoot, resourceName, enabled, running)
end
--------------------------------------------------------------------------------
-- Utility and extension functions
--------------------------------------------------------------------------------
function getResourcesSnapshot()
local thisResource = getThisResource()
local resources = getResources()
local snapshot = {}
local snapshotLength = 0
local showGamemodes = getBool("showGamemodes", false)
local showMaps = getBool("showMaps", false)
local showRaceAddons = getBool("showRaceAddons", true)
for i = 1, #resources do
local resource = resources[i]
if resource ~= thisResource then
local include = true
local resourceType = getResourceInfo(resource, "type")
local resourceAddon = getResourceInfo(resource, "addon")
if resourceType == "gamemode" and not showGamemodes then
include = false
elseif resourceType == "map" and not showMaps then
include = false
elseif resourceAddon == "race" and not showRaceAddons then
include = false
end
if include then
snapshotLength = snapshotLength + 1
snapshot[snapshotLength] = resource
end
end
end
return snapshot
end
function utf8.trim(self)
assert(type(self) == "string", "expected string at argument 1, got ".. type(self))
local from = utf8.match(self, "^%s*()")
return from > utf8.len(self) and "" or utf8.match(self, ".*%S", from)
end
function table.shallowcopy(input)
local result = {}
for key, value in pairs(input) do
result[key] = value
end
return result
end
function getBool(settingName, defaultValue)
local value = get(settingName)
if value == false then
return defaultValue
else
return value == "true"
end
end