]> git.deb.at Git - rhonda/impostor.git/commitdiff
Add port support to the client
authorAeonLucid <aeonlucid@gmail.com>
Wed, 30 Sep 2020 21:56:17 +0000 (23:56 +0200)
committerAeonLucid <aeonlucid@gmail.com>
Wed, 30 Sep 2020 21:56:17 +0000 (23:56 +0200)
src/Impostor.Client/Core/AmongUsModifier.cs
src/Impostor.Client/Core/Events/SavedEventArgs.cs
src/Impostor.Client/Forms/FrmMain.cs

index d856bd2d6b8b7d37de7be2176f0a7f2ef3d9d53c..e6f7b6e316f0657f0b5ea38cf4263fddd28bb6b7 100644 (file)
@@ -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);
         }
 
         /// <summary>
         ///     Writes an IP Address to the Among Us region file.
         /// </summary>
         /// <param name="ipAddress">The IPv4 address to write.</param>
-        private void WriteIp(IPAddress ipAddress)
+        /// <param name="port"></param>
+        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<ErrorEventArgs> Error;
index b3ffa0f70b226a19485f1afb9178d4c5411a5726..84cba0684d96b4c9e5def675b73f5d216bc8214a 100644 (file)
@@ -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
index 5d08f9e7372e6ff3a30cc311f24107d1e5916db3..c0194809393c8164950e2de3752370fd3c52b5fb 100644 (file)
@@ -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();