quantum-space-buddies/QSB/Player/Messages/PlayerJoinMessage.cs

108 lines
3.4 KiB
C#
Raw Normal View History

using Mirror;
using OWML.Common;
2021-12-22 16:44:03 -08:00
using QSB.Messaging;
using QSB.Utility;
using System.Linq;
2020-08-09 09:37:32 +02:00
2022-03-02 19:46:33 -08:00
namespace QSB.Player.Messages;
public class PlayerJoinMessage : QSBMessage
2020-08-09 09:37:32 +02:00
{
2022-03-02 19:46:33 -08:00
private string PlayerName;
private string QSBVersion;
private string GameVersion;
private bool DlcInstalled;
2020-08-09 09:37:32 +02:00
2022-03-29 17:57:34 -07:00
private int[] AddonHashes;
2022-03-02 19:46:33 -08:00
public PlayerJoinMessage(string name)
{
PlayerName = name;
QSBVersion = QSBCore.QSBVersion;
GameVersion = QSBCore.GameVersion;
DlcInstalled = QSBCore.DLCInstalled;
2022-03-29 17:57:34 -07:00
AddonHashes = QSBCore.Addons.Keys
.Select(x => x.GetStableHashCode())
.ToArray();
2022-03-02 19:46:33 -08:00
}
2021-12-22 16:44:03 -08:00
2022-03-02 19:46:33 -08:00
public override void Serialize(NetworkWriter writer)
{
base.Serialize(writer);
writer.Write(PlayerName);
writer.Write(QSBVersion);
writer.Write(GameVersion);
writer.Write(DlcInstalled);
2022-03-29 17:57:34 -07:00
writer.Write(AddonHashes);
2022-03-02 19:46:33 -08:00
}
2021-12-22 16:44:03 -08:00
2022-03-02 19:46:33 -08:00
public override void Deserialize(NetworkReader reader)
{
base.Deserialize(reader);
PlayerName = reader.ReadString();
QSBVersion = reader.ReadString();
GameVersion = reader.ReadString();
DlcInstalled = reader.Read<bool>();
2022-03-29 17:57:34 -07:00
AddonHashes = reader.Read<int[]>();
2022-03-02 19:46:33 -08:00
}
2021-12-22 16:44:03 -08:00
2022-03-02 19:46:33 -08:00
public override void OnReceiveRemote()
{
if (QSBCore.IsHost)
{
2022-03-02 19:46:33 -08:00
if (QSBVersion != QSBCore.QSBVersion)
{
2022-03-02 19:46:33 -08:00
DebugLog.ToConsole($"Error - Client {PlayerName} connecting with wrong QSB version. (Client:{QSBVersion}, Server:{QSBCore.QSBVersion})", MessageType.Error);
2022-03-18 02:58:01 -07:00
new PlayerKickMessage(From, $"QSB version does not match. (Client:{QSBVersion}, Server:{QSBCore.QSBVersion})").Send();
2022-03-02 19:46:33 -08:00
return;
}
2021-12-22 16:44:03 -08:00
2022-03-02 19:46:33 -08:00
if (GameVersion != QSBCore.GameVersion)
{
DebugLog.ToConsole($"Error - Client {PlayerName} connecting with wrong game version. (Client:{GameVersion}, Server:{QSBCore.GameVersion})", MessageType.Error);
2022-03-18 02:58:01 -07:00
new PlayerKickMessage(From, $"Outer Wilds version does not match. (Client:{GameVersion}, Server:{QSBCore.GameVersion})").Send();
2022-03-02 19:46:33 -08:00
return;
}
2022-03-02 19:46:33 -08:00
if (DlcInstalled != QSBCore.DLCInstalled)
{
DebugLog.ToConsole($"Error - Client {PlayerName} connecting with wrong DLC installation state. (Client:{DlcInstalled}, Server:{QSBCore.DLCInstalled})", MessageType.Error);
2022-03-18 02:58:01 -07:00
new PlayerKickMessage(From, $"DLC installation state does not match. (Client:{DlcInstalled}, Server:{QSBCore.DLCInstalled})").Send();
2022-03-02 19:46:33 -08:00
return;
}
2021-12-22 16:44:03 -08:00
2022-03-02 19:46:33 -08:00
if (QSBPlayerManager.PlayerList.Any(x => x.EyeState >= EyeState.Observatory))
{
DebugLog.ToConsole($"Error - Client {PlayerName} connecting too late into eye scene.", MessageType.Error);
2022-03-18 02:58:01 -07:00
new PlayerKickMessage(From, "Game has progressed too far.").Send();
2022-03-02 19:46:33 -08:00
return;
}
2022-03-29 17:57:34 -07:00
var addonHashes = QSBCore.Addons.Keys
.Select(x => x.GetStableHashCode())
.ToArray();
if (!AddonHashes.SequenceEqual(addonHashes))
{
DebugLog.ToConsole($"Error - Client {PlayerName} connecting with addon mismatch. (Client:{AddonHashes}, Server:{addonHashes})", MessageType.Error);
new PlayerKickMessage(From, $"Addon mismatch. (Client:{AddonHashes.Length} addons, Server:{addonHashes.Length} addons)").Send();
return;
}
}
2022-03-02 19:46:33 -08:00
var player = QSBPlayerManager.GetPlayer(From);
player.Name = PlayerName;
2022-03-11 00:55:10 -08:00
DebugLog.ToAll($"{player.Name} joined!", MessageType.Info);
2022-03-06 01:01:02 -08:00
DebugLog.DebugWrite($"{player} joined. qsbVersion:{QSBVersion}, gameVersion:{GameVersion}, dlcInstalled:{DlcInstalled}", MessageType.Info);
2022-03-02 19:46:33 -08:00
}
public override void OnReceiveLocal()
{
var player = QSBPlayerManager.GetPlayer(QSBPlayerManager.LocalPlayerId);
player.Name = PlayerName;
DebugLog.ToAll($"Connected to server as {player.Name}.", MessageType.Info);
2020-12-02 21:23:01 +00:00
}
2022-03-29 17:57:34 -07:00
}