forked from bcl-team/bcl-runcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.lua
116 lines (103 loc) · 3.35 KB
/
run.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
local scripts = {}
local getPlayerId = function(source)
if IsDuplicityVersion() then
return source
end
return PlayerId()
end
local function createContext(source)
return function()
local context = {}
context.playerId = getPlayerId(source)
context.playerPed = GetPlayerPed(context.playerId)
context.currentVehicle = GetVehiclePedIsIn(context.playerPed, false)
context.lastVehicle = GetVehiclePedIsIn(context.playerPed, true)
context.sleep = Citizen.Wait
context.waitUntil = function(predicate, max)
max = max or 1 / 0
local start = GetGameTimer()
while not predicate() do
if GetGameTimer() - start > max then
return false
end
Citizen.Wait(0)
end
end
if not IsDuplicityVersion() then
context.requestModel = function(model)
if not IsModelInCdimage(model) then
error("Model not found: " .. model)
end
RequestModel(model)
while not HasModelLoaded(model) do
Citizen.Wait(0)
end
end
context.requestAnimDict = function(animDict)
if not DoesAnimDictExist(animDict) then
error("Anim dict not found: " .. animDict)
end
RequestAnimDict(animDict)
while not HasAnimDictLoaded(animDict) do
Citizen.Wait(0)
end
end
context.serverId = GetPlayerServerId(context.playerId)
end
return context;
end
end
local function runScript(id, code, source)
if (not scripts[id]) then
local script = Script.new(code.id, createContext(source))
scripts[id] = script
script:on("out", function(out)
if (IsDuplicityVersion()) then
TriggerClientEvent('bcl-runcode:output', source, script.id, out)
else
TriggerEvent('bcl-runcode:output-client', script.id, out)
end
end)
else
scripts[id]:cleanup()
end
local script = scripts[id]
script:execute(code.code)
end
if IsDuplicityVersion() then
RegisterNetEvent('bcl-runcode:run:lua:server', function(pid, code)
local source = source
local id = string.format("%s::%s", source, code.id)
runScript(id, code, source)
TriggerClientEvent('bcl-runcode:run:done-lua', source, pid)
end)
else
RegisterNUICallback('bcl-runcode:run:lua:client', function(data, cb)
local id = data.id
runScript(id, data)
cb(true)
end)
local pIds = 0;
local cbs = {}
RegisterNetEvent('bcl-runcode:run:done-lua', function(id)
if cbs[id] then
cbs[id](true)
cbs[id] = nil
end
end)
RegisterNUICallback('bcl-runcode:run:lua:server', function(data, cb)
pIds = pIds + 1
local id = pIds
TriggerServerEvent('bcl-runcode:run:lua:server', id, data)
cbs[id] = cb
end)
end
AddEventHandler('playerDropped', function()
local source = tostring(source)
for id, script in pairs(scripts) do
if string.sub(id, 1, #source) == source then
script:cleanup()
scripts[id] = nil
end
end
end)