Skip to content

Commit 5733d6b

Browse files
committed
WIP native netmessage constructor
1 parent 9408e5d commit 5733d6b

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

premake5.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PROJECT_GENERATOR_VERSION = 2
1+
PROJECT_GENERATOR_VERSION = 3
22

33
newoption({
44
trigger = "gmcommon",

sourcenet/incoming.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ hook.Add("PreProcessMessages", "InFilter", function(netchan, read, write, localc
2424

2525
while read:GetNumBitsLeft() >= NET_MESSAGE_BITS do
2626
local msg = read:ReadUInt(NET_MESSAGE_BITS)
27-
local handler = NetMessage(msg, not SERVER)
27+
local handler = NetMessage(netchan, msg, not SERVER)
2828
if not handler then
2929
MsgC(Color(255, 0, 0), "Unknown outgoing message " .. msg .. " with " .. read:GetNumBitsLeft() .. " bit(s) left\n")
3030
return false
@@ -42,7 +42,7 @@ hook.Add("PreProcessMessages", "InFilter", function(netchan, read, write, localc
4242
return false
4343
end
4444

45-
MsgC(Color(255, 255, 255), "NetMessage: " .. tostring(handler) .. "\n")
45+
--MsgC(Color(255, 255, 255), "NetMessage: " .. tostring(handler) .. "\n")
4646
end
4747

4848
local bitsleft = read:GetNumBitsLeft()
@@ -52,7 +52,7 @@ hook.Add("PreProcessMessages", "InFilter", function(netchan, read, write, localc
5252
write:WriteBits(data)
5353
end
5454

55-
MsgC(Color(0, 255, 0), "Fully parsed stream with " .. totalbits .. " bit(s) written\n")
55+
--MsgC(Color(0, 255, 0), "Fully parsed stream with " .. totalbits .. " bit(s) written\n")
5656
return true
5757
end)
5858

sourcenet/netmessages.lua

+53-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ NET_MESSAGES = {
5050
},
5151

5252
[net_Disconnect] = { -- 1
53+
NativeConstructor = NET_Disconnect,
5354
__tostring = function(self)
5455
if not self.initialized then
5556
return "net_Disconnect"
@@ -81,6 +82,7 @@ NET_MESSAGES = {
8182
},
8283

8384
[net_File] = { -- 2
85+
NativeConstructor = NET_File,
8486
__tostring = function(self)
8587
if not self.initialized then
8688
return "net_File"
@@ -132,6 +134,7 @@ NET_MESSAGES = {
132134
},
133135

134136
[net_Tick] = { -- 3
137+
NativeConstructor = NET_Tick,
135138
__tostring = function(self)
136139
if not self.initialized then
137140
return "net_Tick"
@@ -167,6 +170,7 @@ NET_MESSAGES = {
167170
},
168171

169172
[net_StringCmd] = { -- 4
173+
NativeConstructor = NET_StringCmd,
170174
__tostring = function(self)
171175
if not self.initialized then
172176
return "net_StringCmd"
@@ -198,6 +202,7 @@ NET_MESSAGES = {
198202
},
199203

200204
[net_SetConVar] = { -- 5
205+
NativeConstructor = NET_SetConVar,
201206
__tostring = function(self)
202207
if not self.initialized then
203208
return "net_SetConVar"
@@ -253,6 +258,7 @@ NET_MESSAGES = {
253258
},
254259

255260
[net_SignonState] = { -- 6
261+
NativeConstructor = NET_SignonState,
256262
__tostring = function(self)
257263
if not self.initialized then
258264
return "net_SignonState"
@@ -287,6 +293,7 @@ NET_MESSAGES = {
287293

288294
CLC = {
289295
[clc_ClientInfo] = { -- 8
296+
NativeConstructor = CLC_ClientInfo,
290297
__tostring = function(self)
291298
if not self.initialized then
292299
return "clc_ClientInfo"
@@ -340,6 +347,7 @@ NET_MESSAGES = {
340347
},
341348

342349
[clc_Move] = { -- 9
350+
NativeConstructor = CLC_Move,
343351
__tostring = function(self)
344352
if not self.initialized then
345353
return "clc_Move"
@@ -377,6 +385,7 @@ NET_MESSAGES = {
377385
},
378386

379387
[clc_VoiceData] = { -- 10
388+
NativeConstructor = CLC_VoiceData,
380389
__tostring = function(self)
381390
if not self.initialized then
382391
return "clc_VoiceData"
@@ -410,6 +419,7 @@ NET_MESSAGES = {
410419
},
411420

412421
[clc_BaselineAck] = { -- 11
422+
NativeConstructor = CLC_BaselineAck,
413423
__tostring = function(self)
414424
if not self.initialized then
415425
return "clc_BaselineAck"
@@ -443,6 +453,7 @@ NET_MESSAGES = {
443453
},
444454

445455
[clc_ListenEvents] = { -- 12
456+
NativeConstructor = CLC_ListenEvents,
446457
__tostring = function()
447458
return "clc_ListenEvents"
448459
end,
@@ -478,6 +489,7 @@ NET_MESSAGES = {
478489
},
479490

480491
[clc_RespondCvarValue] = { -- 13
492+
NativeConstructor = CLC_RespondCvarValue,
481493
__tostring = function(self)
482494
if not self.initialized then
483495
return "clc_RespondCvarValue"
@@ -515,6 +527,7 @@ NET_MESSAGES = {
515527
},
516528

517529
[clc_FileCRCCheck] = { -- 14
530+
NativeConstructor = CLC_FileCRCCheck,
518531
__tostring = function(self)
519532
if not self.initialized then
520533
return "clc_FileCRCCheck"
@@ -561,6 +574,7 @@ NET_MESSAGES = {
561574
},
562575

563576
[clc_CmdKeyValues] = { -- 16
577+
NativeConstructor = CLC_CmdKeyValues,
564578
__tostring = function(self)
565579
if not self.initialized then
566580
return "clc_CmdKeyValues"
@@ -598,6 +612,7 @@ NET_MESSAGES = {
598612
},
599613

600614
[clc_FileMD5Check] = { -- 17
615+
NativeConstructor = CLC_FileMD5Check,
601616
__tostring = function(self)
602617
if not self.initialized then
603618
return "clc_FileMD5Check"
@@ -644,6 +659,7 @@ NET_MESSAGES = {
644659
},
645660

646661
[clc_GMod_ClientToServer] = { -- 18
662+
NativeConstructor = CLC_GMod_ClientToServer,
647663
__tostring = function(self)
648664
if not self.initialized then
649665
return "clc_GMod_ClientToServer"
@@ -728,6 +744,7 @@ NET_MESSAGES = {
728744

729745
SVC = {
730746
[svc_Print] = { -- 7
747+
NativeConstructor = SVC_Print,
731748
__tostring = function(self)
732749
if not self.initialized then
733750
return "svc_Print"
@@ -759,6 +776,7 @@ NET_MESSAGES = {
759776
},
760777

761778
[svc_ServerInfo] = { -- 8
779+
NativeConstructor = SVC_ServerInfo,
762780
__tostring = function(self)
763781
if not self.initialized then
764782
return "svc_ServerInfo"
@@ -842,6 +860,7 @@ NET_MESSAGES = {
842860
},
843861

844862
[svc_SendTable] = { -- 9
863+
NativeConstructor = SVC_SendTable,
845864
__tostring = function(self)
846865
if not self.initialized then
847866
return "svc_SendTable"
@@ -877,6 +896,7 @@ NET_MESSAGES = {
877896
},
878897

879898
[svc_ClassInfo] = { -- 10
899+
NativeConstructor = SVC_ClassInfo,
880900
__tostring = function(self)
881901
if not self.initialized then
882902
return "svc_ClassInfo"
@@ -929,6 +949,7 @@ NET_MESSAGES = {
929949
},
930950

931951
[svc_SetPause] = { -- 11
952+
NativeConstructor = SVC_SetPause,
932953
__tostring = function(self)
933954
if not self.initialized then
934955
return "svc_SetPause"
@@ -960,6 +981,7 @@ NET_MESSAGES = {
960981
},
961982

962983
[svc_CreateStringTable] = { -- 12
984+
NativeConstructor = SVC_CreateStringTable,
963985
__tostring = function(self)
964986
if not self.initialized then
965987
return "svc_CreateStringTable"
@@ -1011,6 +1033,7 @@ NET_MESSAGES = {
10111033
},
10121034

10131035
[svc_UpdateStringTable] = { -- 13
1036+
NativeConstructor = SVC_UpdateStringTable,
10141037
__tostring = function(self)
10151038
if not self.initialized then
10161039
return "svc_UpdateStringTable"
@@ -1051,6 +1074,7 @@ NET_MESSAGES = {
10511074
},
10521075

10531076
[svc_VoiceInit] = { -- 14
1077+
NativeConstructor = SVC_VoiceInit,
10541078
__tostring = function(self)
10551079
if not self.initialized then
10561080
return "svc_VoiceInit"
@@ -1084,6 +1108,7 @@ NET_MESSAGES = {
10841108
},
10851109

10861110
[svc_VoiceData] = { -- 15
1111+
NativeConstructor = SVC_VoiceData,
10871112
__tostring = function(self)
10881113
if not self.initialized then
10891114
return "svc_VoiceData"
@@ -1125,6 +1150,7 @@ NET_MESSAGES = {
11251150
},
11261151

11271152
[svc_Sounds] = { -- 17
1153+
NativeConstructor = SVC_Sounds,
11281154
__tostring = function(self)
11291155
if not self.initialized then
11301156
return "svc_Sounds"
@@ -1166,6 +1192,7 @@ NET_MESSAGES = {
11661192
},
11671193

11681194
[svc_SetView] = { -- 18
1195+
NativeConstructor = SVC_SetView,
11691196
__tostring = function(self)
11701197
if not self.initialized then
11711198
return "svc_SetView"
@@ -1197,6 +1224,7 @@ NET_MESSAGES = {
11971224
},
11981225

11991226
[svc_FixAngle] = { -- 19
1227+
NativeConstructor = SVC_FixAngle,
12001228
__tostring = function(self)
12011229
if not self.initialized then
12021230
return "svc_FixAngle"
@@ -1234,6 +1262,7 @@ NET_MESSAGES = {
12341262
},
12351263

12361264
[svc_CrosshairAngle] = { -- 20
1265+
NativeConstructor = SVC_CrosshairAngle,
12371266
__tostring = function(self)
12381267
if not self.initialized then
12391268
return "svc_CrosshairAngle"
@@ -1269,6 +1298,7 @@ NET_MESSAGES = {
12691298
},
12701299

12711300
[svc_BSPDecal] = { -- 21
1301+
NativeConstructor = SVC_BSPDecal,
12721302
__tostring = function(self)
12731303
if not self.initialized then
12741304
return "svc_BSPDecal"
@@ -1317,6 +1347,7 @@ NET_MESSAGES = {
13171347
},
13181348

13191349
[svc_UserMessage] = { -- 23
1350+
NativeConstructor = SVC_UserMessage,
13201351
__tostring = function(self)
13211352
if not self.initialized then
13221353
return "svc_UserMessage"
@@ -1356,6 +1387,7 @@ NET_MESSAGES = {
13561387
},
13571388

13581389
[svc_EntityMessage] = { -- 24
1390+
NativeConstructor = SVC_EntityMessage,
13591391
__tostring = function(self)
13601392
if not self.initialized then
13611393
return "svc_EntityMessage"
@@ -1393,6 +1425,7 @@ NET_MESSAGES = {
13931425
},
13941426

13951427
[svc_GameEvent] = { -- 25
1428+
NativeConstructor = SVC_GameEvent,
13961429
__tostring = function(self)
13971430
if not self.initialized then
13981431
return "svc_GameEvent"
@@ -1426,6 +1459,7 @@ NET_MESSAGES = {
14261459
},
14271460

14281461
[svc_PacketEntities] = { -- 26
1462+
NativeConstructor = SVC_PacketEntities,
14291463
__tostring = function(self)
14301464
if not self.initialized then
14311465
return "svc_PacketEntities"
@@ -1473,6 +1507,7 @@ NET_MESSAGES = {
14731507
},
14741508

14751509
[svc_TempEntities] = { -- 27
1510+
NativeConstructor = SVC_TempEntities,
14761511
__tostring = function(self)
14771512
if not self.initialized then
14781513
return "svc_TempEntities"
@@ -1508,6 +1543,7 @@ NET_MESSAGES = {
15081543
},
15091544

15101545
[svc_Prefetch] = { -- 28
1546+
NativeConstructor = SVC_Prefetch,
15111547
__tostring = function(self)
15121548
if not self.initialized then
15131549
return "svc_Prefetch"
@@ -1539,6 +1575,7 @@ NET_MESSAGES = {
15391575
},
15401576

15411577
[svc_Menu] = { -- 29
1578+
NativeConstructor = SVC_Menu,
15421579
__tostring = function(self)
15431580
if not self.initialized then
15441581
return "svc_Menu"
@@ -1574,6 +1611,7 @@ NET_MESSAGES = {
15741611
},
15751612

15761613
[svc_GameEventList] = { -- 30
1614+
NativeConstructor = SVC_GameEventList,
15771615
__tostring = function(self)
15781616
if not self.initialized then
15791617
return "svc_GameEventList"
@@ -1609,6 +1647,7 @@ NET_MESSAGES = {
16091647
},
16101648

16111649
[svc_GetCvarValue] = { -- 31
1650+
NativeConstructor = SVC_GetCvarValue,
16121651
__tostring = function(self)
16131652
if not self.initialized then
16141653
return "svc_GetCvarValue"
@@ -1642,6 +1681,7 @@ NET_MESSAGES = {
16421681
},
16431682

16441683
[svc_CmdKeyValues] = { -- 32
1684+
NativeConstructor = SVC_CmdKeyValues,
16451685
__tostring = function(self)
16461686
if not self.initialized then
16471687
return "svc_CmdKeyValues"
@@ -1679,6 +1719,7 @@ NET_MESSAGES = {
16791719
},
16801720

16811721
[svc_GMod_ServerToClient] = { -- 33
1722+
NativeConstructor = SVC_GMod_ServerToClient,
16821723
__tostring = function(self)
16831724
if not self.initialized then
16841725
return "svc_GMod_ServerToClient"
@@ -1775,7 +1816,7 @@ NET_MESSAGES = {
17751816
}
17761817
}
17771818

1778-
function NetMessage(msgtype, server)
1819+
function NetMessage(netchan, msgtype, server)
17791820
local metatable = NET_MESSAGES[msgtype]
17801821
if not metatable then
17811822
if server then
@@ -1789,5 +1830,15 @@ function NetMessage(msgtype, server)
17891830
end
17901831
end
17911832

1792-
return setmetatable(table, metatable)
1833+
return setmetatable({}, metatable)
1834+
1835+
--[[local nativeConstructor = metatable.NativeConstructor
1836+
if not nativeConstructor then
1837+
nativeConstructor = function()
1838+
return setmetatable({}, metatable)
1839+
end
1840+
end
1841+
1842+
metatable.Native = metatable.Native or nativeConstructor(netchan)
1843+
return metatable.Native]]
17931844
end

sourcenet/outgoing.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ local function HandleStream(name, netchan, write)
2424
local read = sn_bf_read(write:GetBasePointer(), totalbits)
2525
while read:GetNumBitsLeft() >= NET_MESSAGE_BITS do
2626
local msg = read:ReadUInt(NET_MESSAGE_BITS)
27-
local handler = NetMessage(msg, SERVER)
27+
local handler = NetMessage(netchan, msg, SERVER)
2828
if not handler then
2929
MsgC(Color(255, 0, 0), "Unknown outgoing message " .. msg .. " on " .. name .. " stream with " .. read:GetNumBitsLeft() .. " bit(s) left\n")
3030
return false

0 commit comments

Comments
 (0)