Skip to content

Commit

Permalink
Merge pull request #432 from PhantomGamers/pr-fixes
Browse files Browse the repository at this point in the history
Fix Missing Mod Disconnects
  • Loading branch information
PhantomGamers authored Sep 9, 2021
2 parents 9a362da + d04f93d commit 12952b3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
36 changes: 28 additions & 8 deletions NebulaNetwork/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,43 @@ private void ClientSocket_OnClose(object sender, CloseEventArgs e)
{
// If the client is Quitting by himself, we don't have to inform him of his disconnection.
if (e.Code == (ushort)DisconnectionReason.ClientRequestedDisconnect)
{
return;
}

// Opens the pause menu on disconnection to prevent NRE when leaving the game
if (Multiplayer.Session?.IsGameLoaded ?? false)
{
GameMain.instance._paused = true;
}

if (e.Code == (ushort)DisconnectionReason.ModIsMissing)
{
InGamePopup.ShowWarning(
"Mod Mismatch",
$"You are missing mod {e.Reason}",
"OK".Translate(),
Multiplayer.LeaveGame);
return;
}

if (e.Code == (ushort)DisconnectionReason.ModIsMissingOnServer)
{
InGamePopup.ShowWarning(
"Mod Mismatch",
$"Server is missing mod {e.Reason}",
"OK".Translate(),
Multiplayer.LeaveGame);
return;
}

if (e.Code == (ushort)DisconnectionReason.ModVersionMismatch)
{
string[] versions = e.Reason.Split(';');
InGamePopup.ShowWarning(
"Mod Version Mismatch",
$"Your Nebula Multiplayer Mod is not the same as the Host version.\nYou:{versions[0]} - Remote:{versions[1]}",
"OK",
$"Your mod {versions[0]} version is not the same as the Host version.\nYou:{versions[1]} - Remote:{versions[2]}",
"OK".Translate(),
Multiplayer.LeaveGame);
return;
}
Expand All @@ -197,7 +219,7 @@ private void ClientSocket_OnClose(object sender, CloseEventArgs e)
InGamePopup.ShowWarning(
"Game Version Mismatch",
$"Your version of the game is not the same as the one used by the Host.\nYou:{versions[0]} - Remote:{versions[1]}",
"OK",
"OK".Translate(),
Multiplayer.LeaveGame);
return;
}
Expand All @@ -215,17 +237,15 @@ private void ClientSocket_OnClose(object sender, CloseEventArgs e)
InGamePopup.ShowWarning(
"Server Unavailable",
$"Could not reach the server, please try again later.",
"OK",
"OK".Translate(),
Multiplayer.LeaveGame);
}
});
}



static void DisableNagleAlgorithm(WebSocket socket)
private static void DisableNagleAlgorithm(WebSocket socket)
{
var tcpClient = AccessTools.FieldRefAccess<WebSocket, TcpClient>("_tcpClient")(socket);
TcpClient tcpClient = AccessTools.FieldRefAccess<WebSocket, TcpClient>("_tcpClient")(socket);
tcpClient.NoDelay = true;
}
}
Expand Down
31 changes: 19 additions & 12 deletions NebulaNetwork/PacketProcessors/Session/HandshakeRequestProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace NebulaNetwork.PacketProcessors.Session
[RegisterPacketProcessor]
public class HandshakeRequestProcessor : PacketProcessor<HandshakeRequest>
{
private IPlayerManager playerManager;
private readonly IPlayerManager playerManager;

public HandshakeRequestProcessor()
{
Expand All @@ -23,10 +23,13 @@ public HandshakeRequestProcessor()

public override void ProcessPacket(HandshakeRequest packet, NebulaConnection conn)
{
if (IsClient) return;
if (IsClient)
{
return;
}

INebulaPlayer player;
using (playerManager.GetPendingPlayers(out var pendingPlayers))
using (playerManager.GetPendingPlayers(out Dictionary<INebulaConnection, INebulaPlayer> pendingPlayers))
{
if (!pendingPlayers.TryGetValue(conn, out player))
{
Expand All @@ -50,13 +53,14 @@ public override void ProcessPacket(HandshakeRequest packet, NebulaConnection con
if (!BepInEx.Bootstrap.Chainloader.PluginInfos.ContainsKey(guid))
{
conn.Disconnect(DisconnectionReason.ModIsMissingOnServer, guid);
return;
}

clientMods.Add(guid, version);
}
}

foreach (var pluginInfo in BepInEx.Bootstrap.Chainloader.PluginInfos)
foreach (KeyValuePair<string, BepInEx.PluginInfo> pluginInfo in BepInEx.Bootstrap.Chainloader.PluginInfos)
{
if (pluginInfo.Value.Instance is IMultiplayerMod mod)
{
Expand All @@ -68,7 +72,10 @@ public override void ProcessPacket(HandshakeRequest packet, NebulaConnection con

string version = clientMods[pluginInfo.Key];

if (mod.CheckVersion(mod.Version, version)) continue;
if (mod.CheckVersion(mod.Version, version))
{
continue;
}

conn.Disconnect(DisconnectionReason.ModVersionMismatch, $"{pluginInfo.Key};{version};{mod.Version}");
return;
Expand All @@ -88,9 +95,9 @@ public override void ProcessPacket(HandshakeRequest packet, NebulaConnection con
//TODO: some validation of client cert / generating auth challenge for the client
// Load old data of the client
string clientCertHash = CryptoUtils.Hash(packet.ClientCert);
using (playerManager.GetSavedPlayerData(out var savedPlayerData))
using (playerManager.GetSavedPlayerData(out Dictionary<string, IPlayerData> savedPlayerData))
{
if (savedPlayerData.TryGetValue(clientCertHash, out var value))
if (savedPlayerData.TryGetValue(clientCertHash, out IPlayerData value))
{
player.LoadUserData(value);
}
Expand All @@ -109,16 +116,16 @@ public override void ProcessPacket(HandshakeRequest packet, NebulaConnection con

// Make sure that each player that is currently in the game receives that a new player as join so they can create its RemotePlayerCharacter
PlayerJoining pdata = new PlayerJoining((PlayerData)player.Data.CreateCopyWithoutMechaData()); // Remove inventory from mecha data
using (playerManager.GetConnectedPlayers(out var connectedPlayers))
using (playerManager.GetConnectedPlayers(out Dictionary<INebulaConnection, INebulaPlayer> connectedPlayers))
{
foreach (var kvp in connectedPlayers)
foreach (KeyValuePair<INebulaConnection, INebulaPlayer> kvp in connectedPlayers)
{
kvp.Value.SendPacket(pdata);
}
}

// Add the new player to the list
using (playerManager.GetSyncingPlayers(out var syncingPlayers))
using (playerManager.GetSyncingPlayers(out Dictionary<INebulaConnection, INebulaPlayer> syncingPlayers))
{
syncingPlayers.Add(conn, player);
}
Expand All @@ -129,7 +136,7 @@ public override void ProcessPacket(HandshakeRequest packet, NebulaConnection con
using (BinaryUtils.Writer p = new BinaryUtils.Writer())
{
int count = 0;
foreach (var pluginInfo in BepInEx.Bootstrap.Chainloader.PluginInfos)
foreach (KeyValuePair<string, BepInEx.PluginInfo> pluginInfo in BepInEx.Bootstrap.Chainloader.PluginInfos)
{
if (pluginInfo.Value.Instance is IMultiplayerModWithSettings mod)
{
Expand All @@ -139,7 +146,7 @@ public override void ProcessPacket(HandshakeRequest packet, NebulaConnection con
}
}

var gameDesc = GameMain.data.gameDesc;
GameDesc gameDesc = GameMain.data.gameDesc;
player.SendPacket(new HandshakeResponse(gameDesc.galaxyAlgo, gameDesc.galaxySeed, gameDesc.starCount, gameDesc.resourceMultiplier, isNewUser, (PlayerData)player.Data, p.CloseAndGetBytes(), count));
}
}
Expand Down

0 comments on commit 12952b3

Please sign in to comment.