-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnetdata.lua
More file actions
85 lines (77 loc) · 2.24 KB
/
netdata.lua
File metadata and controls
85 lines (77 loc) · 2.24 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
local Entity = FindMetaTable("Entity")
if SERVER then
function Entity:NetDataUpdate()
net.Start("netdata")
net.WriteEntity(self)
net.WriteUInt(self.NetDataHash and tonumber(util.CRC(self:NetDataHash())) or 0, 32)
self:NetDataWrite()
net.SendPVS(self:GetPos())
end
util.AddNetworkString("netdata")
net.Receive("netdata", function(len, cl)
local ent = net.ReadEntity()
local prevHash = net.ReadUInt(32)
if IsValid(ent) and ent.NetDataWrite then
local curHash = ent.NetDataHash and tonumber(util.CRC(ent:NetDataHash())) or 0
if curHash == 0 or prevHash == 0 or curHash ~= prevHash then
net.Start("netdata")
net.WriteEntity(ent)
net.WriteUInt(curHash, 32)
ent:NetDataWrite()
net.Send(cl)
end
end
end)
end
if CLIENT then
hook.Add("NotifyShouldTransmit", "NetDataRequest", function(e, should)
if e.NetDataRead and should then
local prevHash = e._NetDataPrevHash or 0
net.Start("netdata")
net.WriteEntity(e)
net.WriteUInt(prevHash, 32)
net.SendToServer()
if cvars.Number("developer") > 0 then
print("[NetData] Asking for netdata due to PVS for " .. tostring(e) .. " using hash " .. prevHash)
end
end
end)
hook.Add("NetworkEntityCreated", "NetDataRequest", function(e)
local sent = scripted_ents.GetStored(e:GetClass())
if e.NetDataRead or (sent and sent.t and sent.t.NetDataRead) then
net.Start("netdata")
net.WriteEntity(e)
net.WriteUInt(0, 32)
net.SendToServer()
if cvars.Number("developer") > 0 then
print("[NetData] Asking for netdata due to NEC for " .. tostring(e))
end
end
end)
net.Receive("netdata", function(len, cl)
local ent = net.ReadEntity()
local hash = net.ReadUInt(32)
if IsValid(ent) then
if cvars.Number("developer") > 0 then
print("[NetData] Received netdata for " .. tostring(ent) .. " with hash " .. hash)
end
ent:NetDataRead()
ent._NetDataPrevHash = hash
end
end)
end
if SERVER then
util.AddNetworkString("entaction")
net.Receive("entaction", function(len, cl)
local ent = net.ReadEntity()
if IsValid(ent) and ent.ReceiveNetAction then
ent:ReceiveNetAction(cl)
end
end)
end
if CLIENT then
function Entity:StartNetAction(unreliable)
net.Start("entaction", unreliable)
net.WriteEntity(self)
end
end