1+ using System ;
2+ using System . Collections . Generic ;
13using Ionic . Zlib ;
24using Multiplayer . Common ;
5+ using Multiplayer . Common . Networking . Packet ;
36using RimWorld ;
4- using RimWorld . Planet ;
5- using System ;
6- using System . Collections . Generic ;
77using UnityEngine ;
88using Verse ;
99
@@ -17,88 +17,78 @@ public class ClientPlayingState(ConnectionBase connection) : ClientBaseState(con
1717 [ PacketHandler ( Packets . Server_TimeControl ) ]
1818 public new void HandleTimeControl ( ByteReader data ) => base . HandleTimeControl ( data ) ;
1919
20- [ PacketHandler ( Packets . Server_Command ) ]
21- public void HandleCommand ( ByteReader data )
20+ [ TypedPacketHandler ]
21+ public void HandleCommand ( ServerCommandPacket packet )
2222 {
23- ScheduledCommand cmd = ScheduledCommand . Deserialize ( data ) ;
24- Session . ScheduleCommand ( cmd ) ;
25-
23+ Session . ScheduleCommand ( packet . ToCommand ( ) ) ;
2624 Multiplayer . session . receivedCmds ++ ;
2725 Multiplayer . session . ProcessTimeControl ( ) ;
2826 }
2927
30- [ PacketHandler ( Packets . Server_PlayerList ) ]
31- public void HandlePlayerList ( ByteReader data )
28+ [ TypedPacketHandler ]
29+ public void HandlePlayerList ( ServerPlayerListPacket packet )
3230 {
33- var action = data . ReadEnum < PlayerListAction > ( ) ;
34- if ( action == PlayerListAction . Add )
31+ if ( packet . action == PlayerListAction . Add )
3532 {
36- var info = PlayerInfo . Read ( data ) ;
37- if ( ! Multiplayer . session . players . Contains ( info ) )
38- {
39- ServerLog . Log ( $ "PlayerList: Adding player { info . id } :{ info . username } ") ;
40- Multiplayer . session . players . Add ( info ) ;
41- }
42- else
33+ foreach ( var info in packet . players )
4334 {
44- ServerLog . Error ( $ "PlayerList: Adding player { info . id } :{ info . username } - player already exists") ;
35+ if ( ! Multiplayer . session . players . Any ( p => p . id == info . id || p . username == info . username ) )
36+ {
37+ ServerLog . Log ( $ "PlayerList: Adding player { info . id } :{ info . username } ") ;
38+ Multiplayer . session . players . Add ( PlayerInfo . FromNet ( info ) ) ;
39+ }
40+ else
41+ {
42+ ServerLog . Error ( $ "PlayerList: Adding player { info . id } :{ info . username } - player already exists") ;
43+ }
4544 }
4645 }
47- else if ( action == PlayerListAction . Remove )
46+ else if ( packet . action == PlayerListAction . Remove )
4847 {
49- int id = data . ReadInt32 ( ) ;
50- ServerLog . Log ( $ "PlayerList: Removing player with id { id } ") ;
51- var matches = Multiplayer . session . players . RemoveAll ( p => p . id == id ) ;
48+ ServerLog . Log ( $ "PlayerList: Removing player with id { packet . playerId } ") ;
49+ var matches = Multiplayer . session . players . RemoveAll ( p => p . id == packet . playerId ) ;
5250 if ( matches > 1 )
5351 {
54- ServerLog . Error ( $ "PlayerList: Removing player with id { id } -- occurred { matches } times. This should not happen") ;
52+ ServerLog . Error ( $ "PlayerList: Removing player with id { packet . playerId } -- occurred { matches } times. This should not happen") ;
5553 }
5654 }
57- else if ( action == PlayerListAction . List )
55+ else if ( packet . action == PlayerListAction . List )
5856 {
59- int count = data . ReadInt32 ( ) ;
60- ServerLog . Log ( $ "PlayerList: Received player list with { count } entries") ;
57+ ServerLog . Log ( $ "PlayerList: Received player list with { packet . players . Length } entries") ;
6158
6259 Multiplayer . session . players . Clear ( ) ;
63- for ( int i = 0 ; i < count ; i ++ )
60+ foreach ( var info in packet . players )
6461 {
65- var info = PlayerInfo . Read ( data ) ;
6662 ServerLog . Log ( $ "PlayerList: Adding player from list { info . id } :{ info . username } ") ;
67- Multiplayer . session . players . Add ( info ) ;
63+ Multiplayer . session . players . Add ( PlayerInfo . FromNet ( info ) ) ;
6864 }
6965 }
70- else if ( action == PlayerListAction . Latencies )
66+ else if ( packet . action == PlayerListAction . Latencies )
7167 {
72- int count = data . ReadInt32 ( ) ;
73-
74- for ( int i = 0 ; i < count ; i ++ )
68+ foreach ( var latency in packet . latencies )
7569 {
76- var id = data . ReadInt32 ( ) ;
77- var player = Multiplayer . session . GetPlayerInfo ( id ) ;
70+ var player = Multiplayer . session . GetPlayerInfo ( latency . playerId ) ;
7871 if ( player == null )
7972 {
80- ServerLog . Log ( $ "PlayerList: Received latency info for unknown player with id { id } ") ;
73+ ServerLog . Log ( $ "PlayerList: Received latency info for unknown player with id { latency . playerId } ") ;
8174 continue ;
8275 }
83- player . latency = data . ReadInt32 ( ) ;
84- player . ticksBehind = data . ReadInt32 ( ) ;
85- player . simulating = data . ReadBool ( ) ;
86- player . frameTime = data . ReadFloat ( ) ;
76+ player . latency = latency . latency ;
77+ player . ticksBehind = latency . ticksBehind ;
78+ player . simulating = latency . simulating ;
79+ player . frameTime = latency . frameTime ;
8780 }
8881 }
89- else if ( action == PlayerListAction . Status )
82+ else if ( packet . action == PlayerListAction . Status )
9083 {
91- var id = data . ReadInt32 ( ) ;
92- var status = data . ReadEnum < PlayerStatus > ( ) ;
93- var player = Multiplayer . session . GetPlayerInfo ( id ) ;
94-
84+ var player = Multiplayer . session . GetPlayerInfo ( packet . playerId ) ;
9585 if ( player == null )
9686 {
97- ServerLog . Log ( $ "PlayerList: Received player status ({ status } ) for unknown player with id { id } ") ;
87+ ServerLog . Log ( $ "PlayerList: Received player status ({ packet . status } ) for unknown player with id { packet . playerId } ") ;
9888 }
9989 else
10090 {
101- player . status = status ;
91+ player . status = packet . status ;
10292 }
10393 }
10494 }
@@ -110,44 +100,26 @@ public void HandleChat(ByteReader data)
110100 Multiplayer . session . AddMsg ( msg ) ;
111101 }
112102
113- [ PacketHandler ( Packets . Server_Cursor ) ]
114- public void HandleCursor ( ByteReader data )
103+ [ TypedPacketHandler ]
104+ public void HandleCursor ( ServerCursorPacket packet )
115105 {
116- int playerId = data . ReadInt32 ( ) ;
117- var player = Multiplayer . session . GetPlayerInfo ( playerId ) ;
106+ var player = Multiplayer . session . GetPlayerInfo ( packet . playerId ) ;
118107 if ( player == null ) return ;
119108
120- byte seq = data . ReadByte ( ) ;
121- if ( seq < player . cursorSeq && player . cursorSeq - seq < 128 ) return ;
122-
123- byte map = data . ReadByte ( ) ;
124- player . map = map ;
125-
126- if ( map == byte . MaxValue ) return ;
109+ var data = packet . data ;
110+ if ( data . seq < player . cursorSeq && player . cursorSeq - data . seq < 128 ) return ;
127111
128- byte icon = data . ReadByte ( ) ;
129- float x = data . ReadShort ( ) / 10f ;
130- float z = data . ReadShort ( ) / 10f ;
112+ player . map = data . map ;
113+ if ( data . map == byte . MaxValue ) return ;
131114
132- player . cursorSeq = seq ;
115+ player . cursorSeq = data . seq ;
133116 player . lastCursor = player . cursor ;
134117 player . lastDelta = Multiplayer . clock . ElapsedMillisDouble ( ) - player . updatedAt ;
135- player . cursor = new Vector3 ( x , 0 , z ) ;
118+ player . cursor = new Vector3 ( data . x , 0 , data . z ) ;
136119 player . updatedAt = Multiplayer . clock . ElapsedMillisDouble ( ) ;
137- player . cursorIcon = icon ;
120+ player . cursorIcon = data . icon ;
138121
139- short dragXRaw = data . ReadShort ( ) ;
140- if ( dragXRaw != - 1 )
141- {
142- float dragX = dragXRaw / 10f ;
143- float dragZ = data . ReadShort ( ) / 10f ;
144-
145- player . dragStart = new Vector3 ( dragX , 0 , dragZ ) ;
146- }
147- else
148- {
149- player . dragStart = PlayerInfo . Invalid ;
150- }
122+ player . dragStart = data . HasDrag ? new Vector3 ( data . dragX , 0 , data . dragZ ) : PlayerInfo . Invalid ;
151123 }
152124
153125 [ PacketHandler ( Packets . Server_Selected ) ]
@@ -171,16 +143,8 @@ public void HandleSelected(ByteReader data)
171143 player . selectedThings . Remove ( remove [ i ] ) ;
172144 }
173145
174- [ PacketHandler ( Packets . Server_PingLocation ) ]
175- public void HandlePing ( ByteReader data )
176- {
177- int player = data . ReadInt32 ( ) ;
178- int map = data . ReadInt32 ( ) ;
179- PlanetTile planetTile = new ( data . ReadInt32 ( ) , data . ReadInt32 ( ) ) ;
180- var loc = new Vector3 ( data . ReadFloat ( ) , data . ReadFloat ( ) , data . ReadFloat ( ) ) ;
181-
182- Session . locationPings . ReceivePing ( player , map , planetTile , loc ) ;
183- }
146+ [ TypedPacketHandler ]
147+ public void HandlePing ( ServerPingLocPacket packet ) => Session . locationPings . ReceivePing ( packet ) ;
184148
185149 [ PacketHandler ( Packets . Server_MapResponse ) ]
186150 public void HandleMapResponse ( ByteReader data )
0 commit comments