Skip to content

Commit 1c99956

Browse files
committed
WIP native netmessage constructor
1 parent f5d72f4 commit 1c99956

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_VoiceInit"
@@ -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"
@@ -1121,6 +1146,7 @@ NET_MESSAGES = {
11211146
},
11221147

11231148
[svc_Sounds] = { -- 17
1149+
NativeConstructor = SVC_Sounds,
11241150
__tostring = function(self)
11251151
if not self.initialized then
11261152
return "svc_Sounds"
@@ -1162,6 +1188,7 @@ NET_MESSAGES = {
11621188
},
11631189

11641190
[svc_SetView] = { -- 18
1191+
NativeConstructor = SVC_SetView,
11651192
__tostring = function(self)
11661193
if not self.initialized then
11671194
return "svc_SetView"
@@ -1193,6 +1220,7 @@ NET_MESSAGES = {
11931220
},
11941221

11951222
[svc_FixAngle] = { -- 19
1223+
NativeConstructor = SVC_FixAngle,
11961224
__tostring = function(self)
11971225
if not self.initialized then
11981226
return "svc_FixAngle"
@@ -1230,6 +1258,7 @@ NET_MESSAGES = {
12301258
},
12311259

12321260
[svc_CrosshairAngle] = { -- 20
1261+
NativeConstructor = SVC_CrosshairAngle,
12331262
__tostring = function(self)
12341263
if not self.initialized then
12351264
return "svc_CrosshairAngle"
@@ -1265,6 +1294,7 @@ NET_MESSAGES = {
12651294
},
12661295

12671296
[svc_BSPDecal] = { -- 21
1297+
NativeConstructor = SVC_BSPDecal,
12681298
__tostring = function(self)
12691299
if not self.initialized then
12701300
return "svc_BSPDecal"
@@ -1313,6 +1343,7 @@ NET_MESSAGES = {
13131343
},
13141344

13151345
[svc_UserMessage] = { -- 23
1346+
NativeConstructor = SVC_UserMessage,
13161347
__tostring = function(self)
13171348
if not self.initialized then
13181349
return "svc_UserMessage"
@@ -1352,6 +1383,7 @@ NET_MESSAGES = {
13521383
},
13531384

13541385
[svc_EntityMessage] = { -- 24
1386+
NativeConstructor = SVC_EntityMessage,
13551387
__tostring = function(self)
13561388
if not self.initialized then
13571389
return "svc_EntityMessage"
@@ -1389,6 +1421,7 @@ NET_MESSAGES = {
13891421
},
13901422

13911423
[svc_GameEvent] = { -- 25
1424+
NativeConstructor = SVC_GameEvent,
13921425
__tostring = function(self)
13931426
if not self.initialized then
13941427
return "svc_GameEvent"
@@ -1422,6 +1455,7 @@ NET_MESSAGES = {
14221455
},
14231456

14241457
[svc_PacketEntities] = { -- 26
1458+
NativeConstructor = SVC_PacketEntities,
14251459
__tostring = function(self)
14261460
if not self.initialized then
14271461
return "svc_PacketEntities"
@@ -1469,6 +1503,7 @@ NET_MESSAGES = {
14691503
},
14701504

14711505
[svc_TempEntities] = { -- 27
1506+
NativeConstructor = SVC_TempEntities,
14721507
__tostring = function(self)
14731508
if not self.initialized then
14741509
return "svc_TempEntities"
@@ -1504,6 +1539,7 @@ NET_MESSAGES = {
15041539
},
15051540

15061541
[svc_Prefetch] = { -- 28
1542+
NativeConstructor = SVC_Prefetch,
15071543
__tostring = function(self)
15081544
if not self.initialized then
15091545
return "svc_Prefetch"
@@ -1535,6 +1571,7 @@ NET_MESSAGES = {
15351571
},
15361572

15371573
[svc_Menu] = { -- 29
1574+
NativeConstructor = SVC_Menu,
15381575
__tostring = function(self)
15391576
if not self.initialized then
15401577
return "svc_Menu"
@@ -1570,6 +1607,7 @@ NET_MESSAGES = {
15701607
},
15711608

15721609
[svc_GameEventList] = { -- 30
1610+
NativeConstructor = SVC_GameEventList,
15731611
__tostring = function(self)
15741612
if not self.initialized then
15751613
return "svc_GameEventList"
@@ -1605,6 +1643,7 @@ NET_MESSAGES = {
16051643
},
16061644

16071645
[svc_GetCvarValue] = { -- 31
1646+
NativeConstructor = SVC_GetCvarValue,
16081647
__tostring = function(self)
16091648
if not self.initialized then
16101649
return "svc_GetCvarValue"
@@ -1638,6 +1677,7 @@ NET_MESSAGES = {
16381677
},
16391678

16401679
[svc_CmdKeyValues] = { -- 32
1680+
NativeConstructor = SVC_CmdKeyValues,
16411681
__tostring = function(self)
16421682
if not self.initialized then
16431683
return "svc_CmdKeyValues"
@@ -1675,6 +1715,7 @@ NET_MESSAGES = {
16751715
},
16761716

16771717
[svc_GMod_ServerToClient] = { -- 33
1718+
NativeConstructor = SVC_GMod_ServerToClient,
16781719
__tostring = function(self)
16791720
if not self.initialized then
16801721
return "svc_GMod_ServerToClient"
@@ -1771,7 +1812,7 @@ NET_MESSAGES = {
17711812
}
17721813
}
17731814

1774-
function NetMessage(msgtype, server)
1815+
function NetMessage(netchan, msgtype, server)
17751816
local metatable = NET_MESSAGES[msgtype]
17761817
if not metatable then
17771818
if server then
@@ -1785,5 +1826,15 @@ function NetMessage(msgtype, server)
17851826
end
17861827
end
17871828

1788-
return setmetatable(table, metatable)
1829+
return setmetatable({}, metatable)
1830+
1831+
--[[local nativeConstructor = metatable.NativeConstructor
1832+
if not nativeConstructor then
1833+
nativeConstructor = function()
1834+
return setmetatable({}, metatable)
1835+
end
1836+
end
1837+
1838+
metatable.Native = metatable.Native or nativeConstructor(netchan)
1839+
return metatable.Native]]
17891840
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)