Supports url address and ipv6 connecting
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
using System.Net;
|
||||
using Godot;
|
||||
using MegaCrit.Sts2.Core.Nodes.GodotExtensions;
|
||||
|
||||
namespace SlayTheSpire2.LAN.Multiplayer.Components
|
||||
{
|
||||
internal class AddressLineEdit : NMegaLineEdit
|
||||
{
|
||||
public override void _Ready()
|
||||
{
|
||||
TextSubmitted += OnTextSubmitted;
|
||||
}
|
||||
|
||||
public struct AddressInfo(bool isValid, string? address, ushort? port)
|
||||
{
|
||||
public bool IsValid = isValid;
|
||||
public string? Address = address;
|
||||
public ushort? Port = port;
|
||||
}
|
||||
|
||||
public AddressInfo GetAddressInfo()
|
||||
{
|
||||
var addressInfo = new AddressInfo();
|
||||
|
||||
var uriString = Text;
|
||||
|
||||
if (uriString == "localhost")
|
||||
{
|
||||
uriString = $"http://{uriString}";
|
||||
}
|
||||
|
||||
if (Uri.TryCreate(uriString, UriKind.Absolute, out var uri) && uri.Scheme is "http" or "https")
|
||||
{
|
||||
addressInfo.IsValid = true;
|
||||
addressInfo.Address = uri.Host;
|
||||
|
||||
if (uri is { Port: > -1, IsDefaultPort: false })
|
||||
{
|
||||
addressInfo.Port = (ushort)uri.Port;
|
||||
}
|
||||
|
||||
return addressInfo;
|
||||
}
|
||||
|
||||
if (IPAddress.TryParse(Text, out _))
|
||||
{
|
||||
addressInfo.IsValid = true;
|
||||
addressInfo.Address = Text;
|
||||
}
|
||||
else if (IPEndPoint.TryParse(Text, out var ipEndPoint))
|
||||
{
|
||||
addressInfo.IsValid = true;
|
||||
addressInfo.Port = (ushort)ipEndPoint.Port;
|
||||
}
|
||||
|
||||
return addressInfo;
|
||||
}
|
||||
|
||||
private void OnTextSubmitted(string newText)
|
||||
{
|
||||
Modulate = Colors.White;
|
||||
|
||||
if (GetAddressInfo().IsValid)
|
||||
return;
|
||||
|
||||
Modulate = Colors.Red;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Godot;
|
||||
using MegaCrit.Sts2.Core.Nodes.GodotExtensions;
|
||||
|
||||
namespace SlayTheSpire2.LAN.Multiplayer.Components
|
||||
{
|
||||
internal partial class IPAddressLineEdit : NMegaLineEdit
|
||||
{
|
||||
[GeneratedRegex(@"^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$")]
|
||||
private static partial Regex IPRegex();
|
||||
|
||||
[GeneratedRegex(
|
||||
@"^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.){3}(25[0-5]|(2[0-4]|1\d|[1-9]|)\d):((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([1-9][0-9]{0,3})|[0-9])$")]
|
||||
private static partial Regex IPAndPortRegex();
|
||||
|
||||
[GeneratedRegex("^[0-9.:]*$")]
|
||||
private static partial Regex IPAllowRegex();
|
||||
|
||||
private string _oldText = "";
|
||||
|
||||
public bool IsOnlyIP => IPRegex().IsMatch(Text);
|
||||
|
||||
public bool IsIPAndPort => IPAndPortRegex().IsMatch(Text);
|
||||
|
||||
public string? IPAddress => IsOnlyIP ? Text : IsIPAndPort ? Text.Split(':').First() : null;
|
||||
|
||||
public ushort? Port => IsIPAndPort ? Convert.ToUInt16(Text.Split(':').Last()) : null;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
TextChanged += OnTextChanged;
|
||||
TextSubmitted += OnTextSubmitted;
|
||||
}
|
||||
|
||||
private void OnTextChanged(string newText)
|
||||
{
|
||||
if (IPAllowRegex().IsMatch(newText))
|
||||
{
|
||||
_oldText = newText;
|
||||
}
|
||||
else
|
||||
{
|
||||
Text = _oldText;
|
||||
CaretColumn = Text.Length;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTextSubmitted(string newText)
|
||||
{
|
||||
Modulate = Colors.White;
|
||||
|
||||
if (IPRegex().IsMatch(newText))
|
||||
return;
|
||||
|
||||
if (IPAndPortRegex().IsMatch(newText))
|
||||
return;
|
||||
|
||||
Modulate = Colors.Red;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user