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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
using System;
using System.Linq;
using System.Threading.Tasks;
using Impostor.Api;
using Impostor.Api.Events.Managers;
using Impostor.Api.Innersloth;
using Impostor.Api.Net;
using Impostor.Api.Net.Messages;
using Impostor.Server.Events.Player;
using Impostor.Server.Net.Inner.Objects.Components;
using Impostor.Server.Net.State;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Impostor.Server.Net.Inner.Objects
{
internal partial class InnerPlayerControl : InnerNetObject
{
private readonly ILogger<InnerPlayerControl> _logger;
private readonly IEventManager _eventManager;
private readonly Game _game;
public InnerPlayerControl(ILogger<InnerPlayerControl> logger, IServiceProvider serviceProvider, IEventManager eventManager, Game game)
{
_logger = logger;
_eventManager = eventManager;
_game = game;
Physics = ActivatorUtilities.CreateInstance<InnerPlayerPhysics>(serviceProvider, this, _eventManager, _game);
NetworkTransform = ActivatorUtilities.CreateInstance<InnerCustomNetworkTransform>(serviceProvider, this, _game);
Components.Add(this);
Components.Add(Physics);
Components.Add(NetworkTransform);
PlayerId = byte.MaxValue;
}
public bool IsNew { get; private set; }
public byte PlayerId { get; private set; }
public InnerPlayerPhysics Physics { get; }
public InnerCustomNetworkTransform NetworkTransform { get; }
public InnerPlayerInfo PlayerInfo { get; internal set; }
public override async ValueTask HandleRpc(ClientPlayer sender, ClientPlayer? target, RpcCalls call, IMessageReader reader)
{
switch (call)
{
// Play an animation.
case RpcCalls.PlayAnimation:
{
if (!sender.IsOwner(this))
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.PlayAnimation)} to an unowned {nameof(InnerPlayerControl)}");
}
if (target != null)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.PlayAnimation)} to a specific player instead of broadcast");
}
var animation = reader.ReadByte();
break;
}
// Complete a task.
case RpcCalls.CompleteTask:
{
if (!sender.IsOwner(this))
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.CompleteTask)} to an unowned {nameof(InnerPlayerControl)}");
}
if (target != null)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.CompleteTask)} to a specific player instead of broadcast");
}
var taskId = reader.ReadPackedUInt32();
var task = PlayerInfo.Tasks[(int)taskId];
if (task == null)
{
_logger.LogWarning($"Client sent {nameof(RpcCalls.CompleteTask)} with a taskIndex that is not in their {nameof(InnerPlayerInfo)}");
}
else
{
task.Complete = true;
await _eventManager.CallAsync(new PlayerCompletedTaskEvent(_game, sender, this, task));
}
break;
}
// Update GameOptions.
case RpcCalls.SyncSettings:
{
if (!sender.IsHost)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SyncSettings)} but was not a host");
}
_game.Options.Deserialize(reader.ReadBytesAndSize());
break;
}
// Set Impostors.
case RpcCalls.SetInfected:
{
if (!sender.IsHost)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SetInfected)} but was not a host");
}
var length = reader.ReadPackedInt32();
for (var i = 0; i < length; i++)
{
var playerId = reader.ReadByte();
var player = _game.GameNet.GameData.GetPlayerById(playerId);
if (player != null)
{
player.IsImpostor = true;
}
}
if (_game.GameState == GameStates.Starting)
{
await _game.StartedAsync();
}
break;
}
// Player was voted out.
case RpcCalls.Exiled:
{
if (!sender.IsHost)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.Exiled)} but was not a host");
}
if (target != null)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.Exiled)} to a specific player instead of broadcast");
}
// TODO: Not hit?
Die(DeathReason.Exile);
await _eventManager.CallAsync(new PlayerExileEvent(_game, sender, this));
break;
}
// Validates the player name at the host.
case RpcCalls.CheckName:
{
if (target == null || !target.IsHost)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.CheckName)} to the wrong player");
}
var name = reader.ReadString();
break;
}
// Update the name of a player.
case RpcCalls.SetName:
{
if (!sender.IsHost)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SetName)} but was not a host");
}
if (target != null)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SetName)} to a specific player instead of broadcast");
}
PlayerInfo.PlayerName = reader.ReadString();
break;
}
// Validates the color at the host.
case RpcCalls.CheckColor:
{
if (target == null || !target.IsHost)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.CheckColor)} to the wrong player");
}
var color = reader.ReadByte();
break;
}
// Update the color of a player.
case RpcCalls.SetColor:
{
if (!sender.IsHost)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SetColor)} but was not a host");
}
if (target != null)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SetColor)} to a specific player instead of broadcast");
}
PlayerInfo.ColorId = reader.ReadByte();
break;
}
// Update the hat of a player.
case RpcCalls.SetHat:
{
if (!sender.IsOwner(this))
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SetHat)} to an unowned {nameof(InnerPlayerControl)}");
}
if (target != null)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SetHat)} to a specific player instead of broadcast");
}
PlayerInfo.HatId = reader.ReadPackedUInt32();
break;
}
case RpcCalls.SetSkin:
{
if (!sender.IsOwner(this))
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SetSkin)} to an unowned {nameof(InnerPlayerControl)}");
}
if (target != null)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SetHat)} to a specific player instead of broadcast");
}
PlayerInfo.SkinId = reader.ReadPackedUInt32();
break;
}
// TODO: (ANTICHEAT) Location check?
// only called by a non-host player on to start meeting
case RpcCalls.ReportDeadBody:
{
if (!sender.IsOwner(this))
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.ReportDeadBody)} to an unowned {nameof(InnerPlayerControl)}");
}
if (target != null)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.ReportDeadBody)} to a specific player instead of broadcast");
}
var deadBodyPlayerId = reader.ReadByte();
// deadBodyPlayerId == byte.MaxValue -- means emergency call by button
break;
}
// TODO: (ANTICHEAT) Cooldown check?
case RpcCalls.MurderPlayer:
{
if (!sender.IsOwner(this))
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.MurderPlayer)} to an unowned {nameof(InnerPlayerControl)}");
}
if (target != null)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.MurderPlayer)} to a specific player instead of broadcast");
}
if (!sender.Character.PlayerInfo.IsImpostor)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.MurderPlayer)} as crewmate");
}
if (!sender.Character.PlayerInfo.CanMurder(_game))
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.MurderPlayer)} too fast");
}
sender.Character.PlayerInfo.LastMurder = DateTimeOffset.UtcNow;
var player = reader.ReadNetObject<InnerPlayerControl>(_game);
if (!player.PlayerInfo.IsDead)
{
player.Die(DeathReason.Kill);
await _eventManager.CallAsync(new PlayerMurderEvent(_game, sender, this, player));
}
break;
}
case RpcCalls.SendChat:
{
if (!sender.IsOwner(this))
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SendChat)} to an unowned {nameof(InnerPlayerControl)}");
}
if (target != null)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SendChat)} to a specific player instead of broadcast");
}
var chat = reader.ReadString();
await _eventManager.CallAsync(new PlayerChatEvent(_game, sender, this, chat));
break;
}
case RpcCalls.StartMeeting:
{
if (!sender.IsHost)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.StartMeeting)} but was not a host");
}
if (target != null)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.StartMeeting)} to a specific player instead of broadcast");
}
// deadBodyPlayerId == byte.MaxValue -- means emergency call by button
var deadBodyPlayerId = reader.ReadByte();
var deadPlayer = deadBodyPlayerId != byte.MaxValue
? _game.GameNet.GameData.GetPlayerById(deadBodyPlayerId)?.Controller
: null;
await _eventManager.CallAsync(new PlayerStartMeetingEvent(_game, _game.GetClientPlayer(this.OwnerId), this, deadPlayer));
break;
}
case RpcCalls.SetScanner:
{
if (!sender.IsOwner(this))
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SetScanner)} to an unowned {nameof(InnerPlayerControl)}");
}
if (target != null)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SetScanner)} to a specific player instead of broadcast");
}
var on = reader.ReadBoolean();
var count = reader.ReadByte();
break;
}
case RpcCalls.SendChatNote:
{
if (!sender.IsOwner(this))
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SendChatNote)} to an unowned {nameof(InnerPlayerControl)}");
}
if (target != null)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SendChatNote)} to a specific player instead of broadcast");
}
var playerId = reader.ReadByte();
var chatNote = (ChatNoteType)reader.ReadByte();
break;
}
case RpcCalls.SetPet:
{
if (!sender.IsOwner(this))
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SetPet)} to an unowned {nameof(InnerPlayerControl)}");
}
if (target != null)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SetPet)} to a specific player instead of broadcast");
}
PlayerInfo.PetId = reader.ReadPackedUInt32();
break;
}
// TODO: Understand this RPC
case RpcCalls.SetStartCounter:
{
if (!sender.IsOwner(this))
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SetStartCounter)} to an unowned {nameof(InnerPlayerControl)}");
}
if (target != null)
{
throw new ImpostorCheatException($"Client sent {nameof(RpcCalls.SetStartCounter)} to a specific player instead of broadcast");
}
// Used to compare with LastStartCounter.
var startCounter = reader.ReadPackedUInt32();
// Is either start countdown or byte.MaxValue
var secondsLeft = reader.ReadByte();
if (secondsLeft < byte.MaxValue)
{
await _eventManager.CallAsync(new PlayerSetStartCounterEvent(_game, sender, this, secondsLeft));
}
break;
}
default:
{
_logger.LogWarning("{0}: Unknown rpc call {1}", nameof(InnerPlayerControl), call);
break;
}
}
}
public override bool Serialize(IMessageWriter writer, bool initialState)
{
throw new NotImplementedException();
}
public override void Deserialize(IClientPlayer sender, IClientPlayer? target, IMessageReader reader, bool initialState)
{
if (!sender.IsHost)
{
throw new ImpostorCheatException($"Client attempted to send data for {nameof(InnerPlayerControl)} as non-host");
}
if (initialState)
{
IsNew = reader.ReadBoolean();
}
PlayerId = reader.ReadByte();
}
internal void Die(DeathReason reason)
{
PlayerInfo.IsDead = true;
PlayerInfo.LastDeathReason = reason;
}
}
}
|