From 5350191d40f42de820176b3775c58163481e2b9d Mon Sep 17 00:00:00 2001 From: AeonLucid Date: Wed, 30 Sep 2020 23:56:17 +0200 Subject: [PATCH] Add port support to the client --- src/Impostor.Client/Core/AmongUsModifier.cs | 34 ++++++++++++++----- .../Core/Events/SavedEventArgs.cs | 4 ++- src/Impostor.Client/Forms/FrmMain.cs | 8 +++-- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/Impostor.Client/Core/AmongUsModifier.cs b/src/Impostor.Client/Core/AmongUsModifier.cs index d856bd2..e6f7b6e 100644 --- a/src/Impostor.Client/Core/AmongUsModifier.cs +++ b/src/Impostor.Client/Core/AmongUsModifier.cs @@ -13,6 +13,7 @@ namespace Impostor.Client.Core public class AmongUsModifier { private const string RegionName = "Impostor"; + public const ushort DefaultPort = 22023; private readonly string _amongUsDir; private readonly string _regionFile; @@ -26,10 +27,26 @@ namespace Impostor.Client.Core _regionFile = Path.Combine(amongUsDir, "regionInfo.dat"); } - public async Task SaveIp(string ip) + public async Task SaveIp(string input) { // Filter out whitespace. - ip = ip.Trim(); + input = input.Trim(); + + // Split port from ip. + // Only IPv4 is supported so just do it simple. + var ip = string.Empty; + var port = DefaultPort; + + var parts = input.Split(':'); + if (parts.Length >= 1) + { + ip = parts[0]; + } + + if (parts.Length >= 2) + { + ushort.TryParse(parts[1], out port); + } // Check if a valid IP address was entered. if (!IPAddress.TryParse(ip, out var ipAddress)) @@ -61,14 +78,15 @@ namespace Impostor.Client.Core return; } - WriteIp(ipAddress); + WriteIp(ipAddress, port); } /// /// Writes an IP Address to the Among Us region file. /// /// The IPv4 address to write. - private void WriteIp(IPAddress ipAddress) + /// + private void WriteIp(IPAddress ipAddress, ushort port) { if (ipAddress == null || ipAddress.AddressFamily != AddressFamily.InterNetwork) @@ -88,12 +106,12 @@ namespace Impostor.Client.Core var ip = ipAddress.ToString(); var region = new RegionInfo(RegionName, ip, new[] { - new ServerInfo($"{RegionName}-Master-1", ip, 22023) + new ServerInfo($"{RegionName}-Master-1", ip, port) }); region.Serialize(writer); - OnSaved(ip); + OnSaved(ip, port); } } @@ -129,9 +147,9 @@ namespace Impostor.Client.Core Error?.Invoke(this, new ErrorEventArgs(message)); } - private void OnSaved(string ipAddress) + private void OnSaved(string ipAddress, ushort port) { - Saved?.Invoke(this, new SavedEventArgs(ipAddress)); + Saved?.Invoke(this, new SavedEventArgs(ipAddress, port)); } public event EventHandler Error; diff --git a/src/Impostor.Client/Core/Events/SavedEventArgs.cs b/src/Impostor.Client/Core/Events/SavedEventArgs.cs index b3ffa0f..84cba06 100644 --- a/src/Impostor.Client/Core/Events/SavedEventArgs.cs +++ b/src/Impostor.Client/Core/Events/SavedEventArgs.cs @@ -4,11 +4,13 @@ namespace Impostor.Client.Core.Events { public class SavedEventArgs : EventArgs { - public SavedEventArgs(string ipAddress) + public SavedEventArgs(string ipAddress, ushort port) { IpAddress = ipAddress; + Port = port; } public string IpAddress { get; } + public ushort Port { get; } } } \ No newline at end of file diff --git a/src/Impostor.Client/Forms/FrmMain.cs b/src/Impostor.Client/Forms/FrmMain.cs index 5d08f9e..c019480 100644 --- a/src/Impostor.Client/Forms/FrmMain.cs +++ b/src/Impostor.Client/Forms/FrmMain.cs @@ -44,11 +44,15 @@ namespace Impostor.Client.Forms MessageBoxButtons.OK, MessageBoxIcon.Information); - comboIp.Text = e.IpAddress; + var ipText = e.Port == AmongUsModifier.DefaultPort + ? e.IpAddress + : $"{e.IpAddress}:{e.Port}"; + + comboIp.Text = ipText; comboIp.Enabled = true; buttonLaunch.Enabled = true; - _config.AddIp(e.IpAddress); + _config.AddIp(ipText); _config.Save(); RefreshComboIps(); -- 2.39.5