-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPacket.cs
242 lines (191 loc) · 6.98 KB
/
Packet.cs
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using System;
using System.Collections.Generic;
using Hkmp.Networking.Packet;
namespace HkmpTag {
/// <summary>
/// Packet data for client-bound game information. Where to warp to, which transitions in which scenes
/// are restricted.
/// </summary>
public class GameInfoPacket : IPacketData {
/// <summary>
/// The maximum number of scenes to have transition restrictions for.
/// </summary>
private const byte MaxScenes = byte.MaxValue;
/// <summary>
/// The maximum number of transitions in one scene that can be restricted.
/// </summary>
private const byte MaxTransitions = byte.MaxValue;
/// <summary>
/// The <see cref="CondensedGamePreset"/> to use in the game.
/// </summary>
public CondensedGamePreset Preset { get; set; }
/// <summary>
/// The <see cref="Loadouts"/> to use for this game.
/// </summary>
public Loadouts Loadouts { get; set; }
public GameInfoPacket() {
Preset = new CondensedGamePreset();
Loadouts = new Loadouts();
}
/// <inheritdoc />
public void WriteData(IPacket packet) {
packet.Write(Preset.WarpSceneIndex);
packet.Write(Preset.WarpTransitionIndex);
var dictCount = (byte)Math.Min(MaxScenes, Preset.SceneTransitions.Count);
packet.Write(dictCount);
foreach (var sceneTransitionsPair in Preset.SceneTransitions) {
var sceneIndex = sceneTransitionsPair.Key;
var transitions = sceneTransitionsPair.Value;
packet.Write(sceneIndex);
var transitionLength = (byte)Math.Min(MaxTransitions, transitions.Length);
packet.Write(transitionLength);
for (var i = 0; i < transitionLength; i++) {
packet.Write(transitions[i]);
}
}
Loadouts.NormalLoadout.WriteData(packet);
Loadouts.InfectedLoadout.WriteData(packet);
}
/// <inheritdoc />
public void ReadData(IPacket packet) {
Preset.WarpSceneIndex = packet.ReadUShort();
Preset.WarpTransitionIndex = packet.ReadByte();
var dictCount = packet.ReadByte();
for (var i = 0; i < dictCount; i++) {
var sceneIndex = packet.ReadUShort();
var transitionLength = packet.ReadByte();
var transitions = new byte[transitionLength];
for (var j = 0; j < transitionLength; j++) {
transitions[j] = packet.ReadByte();
}
Preset.SceneTransitions[sceneIndex] = transitions;
}
Loadouts.NormalLoadout.ReadData(packet);
Loadouts.InfectedLoadout.ReadData(packet);
}
/// <inheritdoc />
public bool IsReliable => true;
/// <inheritdoc />
public bool DropReliableDataIfNewerExists => true;
}
/// <summary>
/// Packet data for the game start.
/// </summary>
public class GameStartPacket : IPacketData {
/// <summary>
/// Whether the receiving player is infected.
/// </summary>
public bool IsInfected { get; set; }
/// <summary>
/// The list of IDs of infected players.
/// </summary>
public List<ushort> InfectedIds { get; set; }
public GameStartPacket() {
InfectedIds = new List<ushort>();
}
/// <inheritdoc />
public void WriteData(IPacket packet) {
packet.Write(IsInfected);
packet.Write((ushort)InfectedIds.Count);
foreach (var infectedId in InfectedIds) {
packet.Write(infectedId);
}
}
/// <inheritdoc />
public void ReadData(IPacket packet) {
IsInfected = packet.ReadBool();
var length = packet.ReadUShort();
for (var i = 0; i < length; i++) {
InfectedIds.Add(packet.ReadUShort());
}
}
/// <inheritdoc />
public bool IsReliable => true;
/// <inheritdoc />
public bool DropReliableDataIfNewerExists => true;
}
/// <summary>
/// Packet data for the game end.
/// </summary>
public class GameEndPacket : IPacketData {
/// <summary>
/// Whether the game has a winner.
/// </summary>
public bool HasWinner { get; set; }
/// <summary>
/// Whether the local player is the winner.
/// </summary>
public bool IsWinner { get; set; }
/// <summary>
/// The ID of the winner if there is a winner and it is not the local player.
/// </summary>
public ushort WinnerId { get; set; }
/// <inheritdoc />
public void WriteData(IPacket packet) {
packet.Write(HasWinner);
if (HasWinner) {
packet.Write(IsWinner);
if (!IsWinner) {
packet.Write(WinnerId);
}
}
}
/// <inheritdoc />
public void ReadData(IPacket packet) {
HasWinner = packet.ReadBool();
if (HasWinner) {
IsWinner = packet.ReadBool();
if (!IsWinner) {
WinnerId = packet.ReadUShort();
}
}
}
/// <inheritdoc />
public bool IsReliable => true;
/// <inheritdoc />
public bool DropReliableDataIfNewerExists => true;
}
/// <summary>
/// Packet data for a client-bound tag.
/// </summary>
public class ClientTagPacket : IPacketData {
/// <summary>
/// Whether the receiving player was tagged.
/// </summary>
public bool WasTagged { get; set; }
/// <summary>
/// The number of uninfected left.
/// </summary>
public ushort NumLeft { get; set; }
/// <summary>
/// The ID of the tagged player.
/// </summary>
public ushort TaggedId { get; set; }
/// <summary>
/// Whether the tag was caused by a disconnect.
/// </summary>
public bool Disconnect { get; set; }
/// <inheritdoc />
public void WriteData(IPacket packet) {
packet.Write(WasTagged);
packet.Write(NumLeft);
if (!WasTagged) {
packet.Write(TaggedId);
packet.Write(Disconnect);
}
}
/// <inheritdoc />
public void ReadData(IPacket packet) {
WasTagged = packet.ReadBool();
NumLeft = packet.ReadUShort();
if (!WasTagged) {
TaggedId = packet.ReadUShort();
Disconnect = packet.ReadBool();
}
}
/// <inheritdoc />
public bool IsReliable => true;
/// <inheritdoc />
public bool DropReliableDataIfNewerExists => true;
}
}