-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tunnel.lua
105 lines (85 loc) · 2.72 KB
/
Tunnel.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
_CMR = {
Delays = {},
Identifier = getResourceName(getThisResource()),
Resource = getResourceRootElement()
}
_CMR.AddEvent = function(event, func)
addEvent(event, true)
addEventHandler(event, _CMR.Resource, func)
end
_CMR.GenerateID = function()
local IDGenerator = {
max = 1,
ids = {}
}
IDGenerator.gen = function()
if #IDGenerator.ids > 0 then
table.remove(IDGenerator.ids)
end
local r = IDGenerator.max
IDGenerator.max = IDGenerator.max+1
return r
end
IDGenerator.free = function(id)
table.insert(IDGenerator.ids, id)
end
return setmetatable({}, { __index = IDGenerator })
end
_CMR.BindInterface = function(name, interface)
_CMR.AddEvent(name..":".._CMR.Identifier..":_cmr_tunnel", function(fname, args, rid, playerSource)
local f = interface[fname]
local rets = {}
if type(f) == "function" then
rets = {f(unpack(args, 1, table.maxn(args)))}
end
if rid >= 0 then
if triggerClientEvent then
triggerClientEvent(playerSource, name..":".._CMR.Identifier..":b_cmr_tunnel", _CMR.Resource, rid, rets)
else
triggerServerEvent(name..":".._CMR.Identifier..":b_cmr_tunnel", _CMR.Resource, rid, rets)
end
end
end)
end
_CMR.TunnelResolve = function(TableValue, key)
local MTable = getmetatable(TableValue)
local Tname = MTable.Name
local Tid = MTable.IDG
local Tcallback = MTable.Callbacks
local Fname = key
local fcall = function(callback, ...)
local Args = {...}
rID = Tid:gen()
Tcallback[tostring(rID)] = function(...)
callback(...)
end
if triggerClientEvent then
player = Args[1]
Args = {unpack(Args, 2, table.maxn(Args))}
triggerClientEvent(player, Tname..":".._CMR.Identifier..":_cmr_tunnel", _CMR.Resource, Fname, Args, rID)
else
triggerServerEvent(Tname..":".._CMR.Identifier..":_cmr_tunnel", _CMR.Resource, Fname, Args, rID, localPlayer)
end
end
TableValue[Fname] = fcall
return fcall
end
function _CMR.GetInterface(name)
local Callbacks = {}
local IDG = _CMR.GenerateID()
local r = setmetatable({}, {
__index = _CMR.TunnelResolve,
Name = name,
IDG = IDG,
Callbacks = Callbacks
})
_CMR.AddEvent(name..":".._CMR.Identifier..":b_cmr_tunnel", function(rID, args)
local callback = Callbacks[tostring(rID)]
if callback then
IDG:free(rID)
Callbacks[tostring(rID)] = nil
callback(unpack(args, 1, table.maxn(args)))
end
end)
return r
end