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

100 lines
3.2 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
2021-12-23 13:49:47 -08:00
namespace QSB.Player.Messages
2020-08-09 09:37:32 +02:00
{
2021-12-22 16:44:03 -08:00
public class PlayerJoinMessage : QSBMessage
2020-12-02 21:23:01 +00:00
{
2021-12-22 17:13:54 -08:00
private string PlayerName;
private string QSBVersion;
private string GameVersion;
private GamePlatform Platform;
private bool DlcInstalled;
2020-08-09 09:37:32 +02:00
2021-12-22 16:44:03 -08:00
public PlayerJoinMessage(string name)
2020-12-02 21:23:01 +00:00
{
2021-12-22 16:44:03 -08:00
PlayerName = name;
QSBVersion = QSBCore.QSBVersion;
GameVersion = QSBCore.GameVersion;
Platform = QSBCore.Platform;
DlcInstalled = QSBCore.DLCInstalled;
2020-12-02 21:23:01 +00:00
}
2020-08-09 09:37:32 +02:00
public override void Serialize(NetworkWriter writer)
2020-12-02 21:23:01 +00:00
{
base.Serialize(writer);
writer.Write(PlayerName);
2021-03-09 16:43:41 +00:00
writer.Write(QSBVersion);
2021-08-10 14:54:23 +01:00
writer.Write(GameVersion);
2021-10-21 17:21:54 +01:00
writer.Write((int)Platform);
2021-12-07 13:35:03 +00:00
writer.Write(DlcInstalled);
2020-12-02 21:23:01 +00:00
}
2021-12-22 16:44:03 -08:00
public override void Deserialize(NetworkReader reader)
2021-12-22 16:44:03 -08:00
{
base.Deserialize(reader);
PlayerName = reader.ReadString();
QSBVersion = reader.ReadString();
GameVersion = reader.ReadString();
Platform = (GamePlatform)reader.Read<int>();
DlcInstalled = reader.Read<bool>();
2021-12-22 16:44:03 -08:00
}
public override void OnReceiveRemote()
{
if (QSBCore.IsHost)
2021-12-22 16:44:03 -08:00
{
if (QSBVersion != QSBCore.QSBVersion)
2021-12-22 16:44:03 -08:00
{
DebugLog.ToConsole($"Error - Client {PlayerName} connecting with wrong QSB version. (Client:{QSBVersion}, Server:{QSBCore.QSBVersion})", MessageType.Error);
2021-12-22 17:13:54 -08:00
new PlayerKickMessage(From, KickReason.QSBVersionNotMatching).Send();
return;
2021-12-22 16:44:03 -08:00
}
if (GameVersion != QSBCore.GameVersion)
2021-12-22 16:44:03 -08:00
{
DebugLog.ToConsole($"Error - Client {PlayerName} connecting with wrong game version. (Client:{GameVersion}, Server:{QSBCore.GameVersion})", MessageType.Error);
2021-12-22 17:13:54 -08:00
new PlayerKickMessage(From, KickReason.GameVersionNotMatching).Send();
return;
2021-12-22 16:44:03 -08:00
}
if (Platform != QSBCore.Platform)
2021-12-22 16:44:03 -08:00
{
DebugLog.ToConsole($"Error - Client {PlayerName} connecting with wrong game platform. (Client:{Platform}, Server:{QSBCore.Platform})", MessageType.Error);
2022-01-08 16:36:47 +00:00
new PlayerKickMessage(From, KickReason.GamePlatformNotMatching).Send();
return;
2021-12-22 16:44:03 -08:00
}
if (DlcInstalled != QSBCore.DLCInstalled)
2021-12-22 16:44:03 -08:00
{
DebugLog.ToConsole($"Error - Client {PlayerName} connecting with wrong DLC installation state. (Client:{DlcInstalled}, Server:{QSBCore.DLCInstalled})", MessageType.Error);
2022-01-08 16:36:47 +00:00
new PlayerKickMessage(From, KickReason.DLCNotMatching).Send();
return;
}
if (QSBPlayerManager.PlayerList.Any(x => x.EyeState >= EyeState.Observatory))
{
DebugLog.ToConsole($"Error - Client {PlayerName} connecting too late into eye scene.", MessageType.Error);
new PlayerKickMessage(From, KickReason.InEye).Send();
return;
2021-12-22 16:44:03 -08:00
}
}
var player = QSBPlayerManager.GetPlayer(From);
player.Name = PlayerName;
DebugLog.ToAll($"{player.Name} joined!", MessageType.Info);
DebugLog.DebugWrite($"{player.Name} joined. id:{player.PlayerId}, qsbVersion:{QSBVersion}, gameVersion:{GameVersion}, platform:{Platform}. dlcInstalled:{DlcInstalled}", MessageType.Info);
}
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
}
2021-12-22 16:44:03 -08:00
}