Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added version checking with disconnect reasons #250

Merged
merged 1 commit into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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