-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcable.lua
121 lines (100 loc) · 2.4 KB
/
cable.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
--[[
Cable - A simple Garry's Mod net wrapper.
2018 TeslaCloud Studios
--]]
-- Cable shouldn't be reloaded
if cable then return end
if !pon then include 'pon.lua' end
if !table.map then
function table.map(t, c)
local new_table = {}
for k, v in pairs(t) do
local val = c(v)
if val != nil then
table.insert(new_table, val)
end
end
return new_table
end
end
_player = _player or player
cable = cable or {}
local net_cache = {}
function cable.receive(id, callback)
if SERVER then cable.check_networked_string(id) end
return net.Receive(id, function(length, player)
local c_len = net.ReadUInt(8)
local c_tables = table.map(string.Split(net.ReadString(), ';'), function(v) return tonumber(v) end)
local args = {}
if c_len > 0 then
for i = 1, c_len do
table.insert(args, net.ReadType())
end
end
if c_tables then
for k, v in ipairs(c_tables) do
args[v] = pon.decode(args[v])
end
end
if IsValid(player) then
callback(player, unpack(args))
else
callback(unpack(args))
end
end)
end
local function write_sendable_args(...)
local args = {...}
local length = 0
local table_header = ''
local send = {}
for k, v in ipairs(args) do
length = length + 1
if !istable(v) then
table.insert(send, v != nil and v or false)
else
table.insert(send, pon.encode(v))
table_header = table_header..tostring(length)..';'
end
end
net.WriteUInt(length, 8)
net.WriteString(table_header)
for k, v in ipairs(send) do
net.WriteType(v)
end
end
if SERVER then
function cable.check_networked_string(id)
if !net_cache[id] then
net_cache[id] = util.AddNetworkString(id)
return false
end
return true
end
function cable.send(player, id, ...)
if !cable.check_networked_string(id) then
local args = {...}
-- Allow networked strings some time to catch up for the first time.
timer.Simple(0.25, function()
cable.send(player, id, unpack(args))
end)
return
end
if !istable(player) then
if IsValid(player) then
player = { player }
else
player = _player.GetAll()
end
end
net.Start(id)
write_sendable_args(...)
net.Send(player)
end
else
function cable.send(id, ...)
net.Start(id)
write_sendable_args(...)
net.SendToServer()
end
end