Skip to content

Commit

Permalink
Added version checking with disconnect reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
Mushroom committed Apr 29, 2021
1 parent e465998 commit 17b652f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
6 changes: 4 additions & 2 deletions NebulaClient/MultiplayerClientSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,21 @@ private void ClientSocket_OnClose(object sender, CloseEventArgs e)

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.\nMake sure to use the same version.",
$"Your Nebula Multiplayer Mod is not the same as the Host version.\nYou:{versions[0]} - Remote:{versions[1]}",
"OK",
OnDisconnectPopupCloseBeforeGameLoad);
return;
}

if (e.Code == (ushort)DisconnectionReason.GameVersionMismatch)
{
string[] versions = e.Reason.Split(';');
InGamePopup.ShowWarning(
"Game Version Mismatch",
$"Your version of the game is not the same as the one used by the Host.\nMake sure to use the same version.",
$"Your version of the game is not the same as the one used by the Host.\nYou:{versions[0]} - Remote:{versions[1]}",
"OK",
OnDisconnectPopupCloseBeforeGameLoad);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public void ProcessPacket(HandshakeRequest packet, NebulaConnection conn)
}


if (packet.ModVersion != Config.ModVersion.ToString())
if (packet.ModVersion != Config.ModVersion)
{
conn.Disconnect(DisconnectionReason.ModVersionMismatch);
conn.Disconnect(DisconnectionReason.ModVersionMismatch, $"{ packet.ModVersion };{ Config.ModVersion }");
return;
}

if (packet.GameVersionSig != GameConfig.gameVersion.sig)
{
conn.Disconnect(DisconnectionReason.GameVersionMismatch);
conn.Disconnect(DisconnectionReason.GameVersionMismatch, $"{ packet.GameVersionSig };{ GameConfig.gameVersion.sig }");
return;
}

Expand Down
2 changes: 1 addition & 1 deletion NebulaModel/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace NebulaModel
public static class Config
{
public static PluginInfo ModInfo { get; set; }
public static System.Version ModVersion => ModInfo.Metadata.Version;
public static string ModVersion => ThisAssembly.AssemblyInformationalVersion;
public static int DefaultPort => 8469;
}
}
19 changes: 17 additions & 2 deletions NebulaModel/Networking/NebulaConnection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NebulaModel.Logger;
using NebulaModel.Networking.Serialization;
using System;
using System.Net;
using WebSocketSharp;

Expand Down Expand Up @@ -44,9 +45,23 @@ public void SendRawPacket(byte[] rawData)
}
}

public void Disconnect(DisconnectionReason reason = DisconnectionReason.Normal)
public void Disconnect(DisconnectionReason reason = DisconnectionReason.Normal, string reasonString = null)
{
peerSocket.Close((ushort)reason);
if(string.IsNullOrEmpty(reasonString))
{
peerSocket.Close((ushort)reason);
}
else
{
if (System.Text.Encoding.UTF8.GetBytes(reasonString).Length <= 123)
{
peerSocket.Close((ushort)reason, reasonString);
}
else
{
throw new ArgumentException("Reason string cannot take up more than 123 bytes");
}
}
}

public static bool operator ==(NebulaConnection left, NebulaConnection right)
Expand Down
2 changes: 1 addition & 1 deletion NebulaModel/Packets/Session/HandshakeRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public HandshakeRequest() { }
public HandshakeRequest(byte[] clientCert, string username)
{
this.Username = username;
this.ModVersion = Config.ModVersion.ToString();
this.ModVersion = Config.ModVersion;
this.GameVersionSig = GameConfig.gameVersion.sig;
this.ClientCert = clientCert;
}
Expand Down

0 comments on commit 17b652f

Please sign in to comment.