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

138 lines
4.5 KiB
C#
Raw Normal View History

2022-03-29 19:17:26 -07:00
using HarmonyLib;
using Mirror;
using OWML.Common;
using QSB.Localization;
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;
2022-05-11 00:04:53 +01:00
// empty if no incompatible mods
private string FirstIncompatibleMod;
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
2022-05-11 00:04:53 +01:00
var allEnabledMods = QSBCore.Helper.Interaction.GetMods();
FirstIncompatibleMod = "";
2022-05-14 12:18:48 +01:00
2022-05-11 00:04:53 +01:00
foreach (var mod in allEnabledMods)
{
if (QSBCore.IncompatibleMods.Contains(mod.ModHelper.Manifest.UniqueName))
{
FirstIncompatibleMod = mod.ModHelper.Manifest.UniqueName;
}
}
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-05-11 00:04:53 +01:00
writer.Write(FirstIncompatibleMod);
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-05-11 00:04:53 +01:00
FirstIncompatibleMod = reader.ReadString();
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-08-26 12:52:26 +01:00
if (QSBCore.DebugSettings.KickEveryone)
{
DebugLog.ToConsole($"Kicking {PlayerName} because of DebugSettings.KickEveryone", MessageType.Error);
new PlayerKickMessage(From, "This server has DebugSettings.KickEveryone enabled.").Send();
return;
}
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);
new PlayerKickMessage(From, string.Format(QSBLocalization.Current.QSBVersionMismatch, QSBVersion, 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);
new PlayerKickMessage(From, string.Format(QSBLocalization.Current.OWVersionMismatch, GameVersion, 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);
new PlayerKickMessage(From, string.Format(QSBLocalization.Current.DLCMismatch, DlcInstalled, QSBCore.DLCInstalled)).Send();
2022-03-02 19:46:33 -08:00
return;
}
2021-12-22 16:44:03 -08:00
if (QSBPlayerManager.PlayerList.Any(x => x.EyeState > EyeState.Observatory))
2022-03-02 19:46:33 -08:00
{
DebugLog.ToConsole($"Error - Client {PlayerName} connecting too late into eye scene.", MessageType.Error);
new PlayerKickMessage(From, QSBLocalization.Current.GameProgressLimit).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))
{
2022-03-29 19:17:26 -07:00
DebugLog.ToConsole($"Error - Client {PlayerName} connecting with addon mismatch. (Client:{AddonHashes.Join()}, Server:{addonHashes.Join()})", MessageType.Error);
new PlayerKickMessage(From, string.Format(QSBLocalization.Current.AddonMismatch, AddonHashes.Length, addonHashes.Length)).Send();
2022-03-29 17:57:34 -07:00
return;
}
2022-05-11 00:04:53 +01:00
if (FirstIncompatibleMod != "" && !QSBCore.IncompatibleModsAllowed)
{
DebugLog.ToConsole($"Error - Client {PlayerName} connecting with incompatible mod. (First mod found was {FirstIncompatibleMod})");
new PlayerKickMessage(From, string.Format(QSBLocalization.Current.IncompatibleMod, FirstIncompatibleMod)).Send();
2022-05-11 00:04:53 +01:00
}
}
2022-03-02 19:46:33 -08:00
var player = QSBPlayerManager.GetPlayer(From);
player.Name = PlayerName;
DebugLog.ToAll(string.Format(QSBLocalization.Current.PlayerJoinedTheGame, player.Name), 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;
2020-12-02 21:23:01 +00:00
}
2022-03-29 17:57:34 -07:00
}