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

109 lines
3.1 KiB
C#
Raw Normal View History

2021-12-22 16:44:03 -08:00
using OWML.Common;
using QSB.Messaging;
using QSB.Utility;
using QuantumUNET.Transport;
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
2021-12-22 16:44:03 -08:00
public PlayerJoinMessage() { }
2020-12-23 12:58:45 +00:00
public override void Serialize(QNetworkWriter 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(QNetworkReader reader)
{
base.Deserialize(reader);
PlayerName = reader.ReadString();
QSBVersion = reader.ReadString();
GameVersion = reader.ReadString();
Platform = (GamePlatform)reader.ReadInt32();
DlcInstalled = reader.ReadBoolean();
}
public override void OnReceiveRemote()
{
if (QSBVersion != QSBCore.QSBVersion)
{
if (QSBCore.IsHost)
{
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();
2021-12-22 16:44:03 -08:00
}
return;
}
if (GameVersion != QSBCore.GameVersion)
{
if (QSBCore.IsHost)
{
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();
2021-12-22 16:44:03 -08:00
}
return;
}
if (Platform != QSBCore.Platform)
{
if (QSBCore.IsHost)
{
DebugLog.ToConsole($"Error - Client {PlayerName} connecting with wrong game platform. (Client:{Platform}, Server:{QSBCore.Platform})", MessageType.Error);
2021-12-22 17:13:54 -08:00
new PlayerKickMessage(From, KickReason.DLCNotMatching).Send();
2021-12-22 16:44:03 -08:00
}
}
if (DlcInstalled != QSBCore.DLCInstalled)
{
if (QSBCore.IsHost)
{
DebugLog.ToConsole($"Error - Client {PlayerName} connecting with wrong DLC installation state. (Client:{DlcInstalled}, Server:{QSBCore.DLCInstalled})", MessageType.Error);
2021-12-22 17:13:54 -08:00
new PlayerKickMessage(From, KickReason.GamePlatformNotMatching).Send();
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);
if (QSBSceneManager.IsInUniverse)
{
player.IsReady = true;
2021-12-22 17:13:54 -08:00
new PlayerReadyMessage(true).Send();
2021-12-22 16:44:03 -08:00
}
}
2020-12-02 21:23:01 +00:00
}
2021-12-22 16:44:03 -08:00
}