1+ TYPE_COLOR = 255
2+
3+ local assert = assert
4+ local TypeID = TypeID
5+ local type = type
6+ local net = net
7+
8+ local TYPE_NIL = TYPE_NIL
9+ local TYPE_BOOL = TYPE_BOOL
10+ local TYPE_NUMBER = TYPE_NUMBER
11+ local TYPE_STRING = TYPE_STRING
12+ local TYPE_TABLE = TYPE_TABLE
13+ local TYPE_FUNCTION = TYPE_FUNCTION
14+ local TYPE_ENTITY = TYPE_ENTITY
15+ local TYPE_VECTOR = TYPE_VECTOR
16+ local TYPE_ANGLE = TYPE_ANGLE
17+ local TYPE_MATRIX = TYPE_MATRIX
18+ local TYPE_COLOR = TYPE_COLOR
19+
20+ if (SERVER ) then
21+
22+ local util_NetworkStringToID = util .NetworkStringToID
23+ local util_AddNetworkString = util .AddNetworkString
24+ local net_Start = net .Start
25+
26+ function net .Start ( networkString )
27+ assert ( TypeID ( networkString ) == TYPE_STRING , ' net.Start: string expected, got ' .. type ( networkString ) )
28+
29+ local id = util_NetworkStringToID ( networkString )
30+ if (id < 1 ) then
31+ util_AddNetworkString ( networkString )
32+ end
33+
34+ net_Start ( networkString )
35+ end
36+
37+ end
38+
39+ local util_NetworkIDToString = util .NetworkIDToString
40+ local string_lower = string.lower
41+ local IsColor = IsColor
42+ local IsValid = IsValid
43+ local Entity = Entity
44+ local Color = Color
45+ local pairs = pairs
46+ local error = error
47+ local NULL = NULL
48+
49+ module ( ' net' )
50+
51+ Receivers = {}
52+
53+ --
54+ -- Set up a function to receive network messages
55+ --
56+ function Receive ( str , func , identifier )
57+ assert ( TypeID ( str ) == TYPE_STRING , ' net.Receive: string expected, got ' .. type ( str ) )
58+ assert ( TypeID ( func ) == TYPE_FUNCTION , ' net.Receive: function expected, got ' .. type ( func ) )
59+
60+ local networkString = string_lower ( str )
61+ Receivers [ networkString ] = Receivers [ networkString ] or {}
62+ Receivers [ networkString ][ identifier or ' default' ] = func
63+ end
64+
65+ --
66+ -- A message has been received from the network..
67+ --
68+ function Incoming ( length , ply )
69+ local networkStringID = net .ReadHeader ()
70+ if TypeID ( networkStringID ) == TYPE_NUMBER then
71+ local networkString = util_NetworkIDToString ( networkStringID )
72+ if TypeID ( networkString ) == TYPE_STRING then
73+ local functions = Receivers [ string_lower ( networkString ) ]
74+ if TypeID ( functions ) == TYPE_TABLE then
75+ length = length - 16
76+
77+ for _ , func in pairs ( functions ) do
78+ func ( length , ply )
79+ end
80+ end
81+ end
82+ end
83+ end
84+
85+ --
86+ -- Read/Write a boolean to the stream
87+ --
88+ function ReadBool ()
89+ return net .ReadBit () == 1
90+ end
91+
92+ WriteBool = net .WriteBit
93+
94+ --
95+ -- Read/Write an entity to the stream
96+ --
97+ function ReadEntity ()
98+ local entIndex = net .ReadUInt ( 16 )
99+ if (entIndex > 0 ) then
100+ return Entity ( entIndex )
101+ end
102+
103+ return NULL
104+ end
105+
106+ function WriteEntity ( entity )
107+ assert ( TypeID ( entity ) == TYPE_ENTITY , ' net.WriteEntity: Entity expected, got ' .. type ( entity ) )
108+
109+ if IsValid ( entity ) then
110+ net .WriteUInt ( entity :EntIndex (), 16 )
111+ return
112+ end
113+
114+ net .WriteUInt ( 0 , 16 )
115+ end
116+
117+ --
118+ -- Read/Write a color to/from the stream
119+ --
120+ function ReadColor ( readAlpha )
121+ if (readAlpha == false ) then
122+ return Color ( net .ReadUInt ( 8 ), net .ReadUInt ( 8 ), net .ReadUInt ( 8 ), 255 )
123+ else
124+ return Color ( net .ReadUInt ( 8 ), net .ReadUInt ( 8 ), net .ReadUInt ( 8 ), net .ReadUInt ( 8 ) )
125+ end
126+ end
127+
128+ function WriteColor ( color , writeAlpha )
129+ assert ( IsColor ( color ), ' net.WriteColor: color expected, got ' .. type ( color ) )
130+ net .WriteUInt ( color .r , 8 )
131+ net .WriteUInt ( color .g , 8 )
132+ net .WriteUInt ( color .b , 8 )
133+
134+ if (writeAlpha == false ) then return end
135+ net .WriteUInt ( color .a , 8 )
136+ end
137+
138+ ReadVars = {
139+ [TYPE_NIL ] = function () end ,
140+ [TYPE_STRING ] = net .ReadString ,
141+ [TYPE_NUMBER ] = net .ReadDouble ,
142+ [TYPE_BOOL ] = net .ReadBool ,
143+ [TYPE_ENTITY ] = ReadEntity ,
144+ [TYPE_VECTOR ] = net .ReadVector ,
145+ [TYPE_ANGLE ] = net .ReadAngle ,
146+ [TYPE_MATRIX ] = net .ReadMatrix ,
147+ [TYPE_COLOR ] = ReadColor
148+ }
149+
150+ function ReadType ( typeID )
151+ if (typeID == nil ) then
152+ typeID = net .ReadUInt ( 8 )
153+ end
154+
155+ local func = ReadVars [ typeID ]
156+ if (func ) then
157+ return func ()
158+ end
159+
160+ error ( ' net.ReadType: Couldn\' t read type ' .. typeID )
161+ end
162+
163+ WriteVars = {
164+ [TYPE_NIL ] = function () end ,
165+ [TYPE_STRING ] = net .WriteString ,
166+ [TYPE_NUMBER ] = net .WriteDouble ,
167+ [TYPE_BOOL ] = net .WriteBool ,
168+ [TYPE_ENTITY ] = WriteEntity ,
169+ [TYPE_VECTOR ] = net .WriteVector ,
170+ [TYPE_ANGLE ] = net .WriteAngle ,
171+ [TYPE_MATRIX ] = net .WriteMatrix ,
172+ [TYPE_COLOR ] = WriteColor
173+ }
174+
175+ function WriteType ( value )
176+ local typeID = IsColor ( value ) and TYPE_COLOR or TypeID ( value )
177+ net .WriteUInt ( typeID , 8 )
178+
179+ local func = net .WriteVars [ typeID ]
180+ if (func ) then
181+ return func ( value )
182+ end
183+
184+ error ( ' net.WriteType: Couldn\' t write ' .. type ( value ) .. ' (type ' .. typeID .. ' )' )
185+ end
186+
187+ --
188+ -- Write a whole table to the stream
189+ -- This is less optimal than writing each
190+ -- item indivdually and in a specific order
191+ -- because it adds type information before each var
192+ --
193+
194+ function WriteTable ( tbl )
195+ assert ( TypeID ( tbl ) == TYPE_TABLE , ' net.WriteTable: table expected, got ' .. type ( tbl ) )
196+ for key , value in pairs ( tbl ) do
197+ net .WriteBool ( true )
198+ WriteType ( key )
199+ WriteType ( value )
200+ end
201+
202+ net .WriteBool ( false )
203+ end
204+
205+ WriteVars [TYPE_TABLE ] = WriteTable
206+
207+ function ReadTable ()
208+ local tbl = {}
209+ while net .ReadBool () do
210+ tbl [ net .ReadType () ] = net .ReadType ()
211+ end
212+
213+ return tbl
214+ end
215+
216+ ReadVars [TYPE_TABLE ] = ReadTable
0 commit comments