Skip to content

Commit

Permalink
feat: add PrefsIPAddressAndPort. PrefsIPEndPoint is now obsolete.
Browse files Browse the repository at this point in the history
  • Loading branch information
fuqunaga committed Aug 29, 2024
1 parent 3daad89 commit 2c8b203
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ namespace PrefsGUI.Example
{
public class PrefsGUIExample_Primitive : MonoBehaviour
{
public PrefsBool prefsBool = new("PrefsBool");
public PrefsInt prefsInt = new("PrefsInt");
public PrefsFloat prefsFloat = new("PrefsFloat");
public PrefsString prefsString = new("PrefsString");
public PrefsParam<EnumSample> prefsEnum = new("PrefsEnum");
public PrefsColor prefsColor = new("PrefsColor");
public PrefsGradient prefsGradient = new("PrefsGradient");
public PrefsVector2 prefsVector2 = new("PrefsVector2");
public PrefsVector3 prefsVector3 = new("PrefsVector3");
public PrefsVector4 prefsVector4 = new("PrefsVector4");
public PrefsBool prefsBool = new("PrefsBool");
public PrefsInt prefsInt = new("PrefsInt");
public PrefsFloat prefsFloat = new("PrefsFloat");
public PrefsString prefsString = new("PrefsString");
public PrefsParam<EnumSample> prefsEnum = new("PrefsEnum");
public PrefsColor prefsColor = new("PrefsColor");
public PrefsGradient prefsGradient = new("PrefsGradient");
public PrefsVector2 prefsVector2 = new("PrefsVector2");
public PrefsVector3 prefsVector3 = new("PrefsVector3");
public PrefsVector4 prefsVector4 = new("PrefsVector4");
public PrefsVector2Int prefsVector2Int = new("PrefsVector2Int");
public PrefsVector3Int prefsVector3Int = new("PrefsVector3Int");
public PrefsRect prefsRect = new("PrefsRect");
public PrefsBounds prefsBounds = new("PrefsBounds");
public PrefsBoundsInt prefsBoundsInt = new("PrefsBoundsInt");
public PrefsAny<CustomClass> prefsClass = new("PrefsClass");
public PrefsList<CustomClass> prefsList = new("PrefsList");
public PrefsIPEndPoint prefsIPEndPoint = new("PrefsIPEndPoint");
public PrefsRect prefsRect = new("PrefsRect");
public PrefsBounds prefsBounds = new("PrefsBounds");
public PrefsBoundsInt prefsBoundsInt = new("PrefsBoundsInt");
public PrefsAny<CustomClass> prefsClass = new("PrefsClass");
public PrefsList<CustomClass> prefsList = new("PrefsList");
public PrefsIPAddressAndPort prefsIPEndPoint = new("PrefsIPAddressAndPort");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public static Element CreateFieldRaw<T>(this PrefsParamOuter<T> prefs, LabelElem


public static Element CreateElement<TPrefs0, TPrefs1, TOuter0, TOuter1>(
#pragma warning disable CS0618 // Type or member is obsolete
this PrefsSet<TPrefs0, TPrefs1, TOuter0, TOuter1> prefs)
#pragma warning restore CS0618 // Type or member is obsolete
where TPrefs0 : PrefsParamOuter<TOuter0>
where TPrefs1 : PrefsParamOuter<TOuter1>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using RosettaUI;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#else
Expand All @@ -27,6 +26,20 @@ public static void RegisterUICustom()

return prefs.CreateElement(label);
});

UICustom.RegisterElementCreationFunc(typeof(AddressAndPort), (label, binder) =>
{
var addressName = nameof(AddressAndPort.address);
var portName = nameof(AddressAndPort.port);
var addressBinder = PropertyOrFieldBinder.Create(binder, addressName);
var portBinder = PropertyOrFieldBinder.Create(binder, portName);

return UI.ClipboardContextMenu(new CompositeFieldElement(label, new[]
{
UI.Field(addressName, addressBinder).SetWidth(180f),
UI.Field(portName, portBinder).SetWidth(120f)
}), binder);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Net;

namespace PrefsGUI
{
[Serializable]
[Obsolete("PrefsIPEndPoint is obsolete, consider using PrefsIPAddressAndPort instead.")]
public class PrefsIPEndPoint : PrefsSet<PrefsString, PrefsInt, string, int>
{
public string address => prefs0.Get();
public int port => prefs1.Get();

public PrefsIPEndPoint(string key, string hostname = "localhost", int port = 10000) : base(key, hostname, port, "address", "port") { }

public static implicit operator IPEndPoint(PrefsIPEndPoint me) => CreateIPEndPoint(me.address, me.port);

public static IPEndPoint CreateIPEndPoint(string address, int port) =>
IPEndPointUtility.CreateIPEndPoint(address, port);

public static IPAddress FindFromHostName(string hostname) => IPEndPointUtility.FindFromHostName(hostname);
}


[Serializable]
public class PrefsIPAddressAndPort : PrefsParam<AddressAndPort>
{
public PrefsIPAddressAndPort(string key, string address = "localhost", int port = 10000) : this(key, new AddressAndPort {address = address, port = port})
{}

public PrefsIPAddressAndPort(string key, AddressAndPort defaultValue) : base(key, defaultValue)
{
}

public static implicit operator IPEndPoint(PrefsIPAddressAndPort me) => IPEndPointUtility.CreateIPEndPoint(me.Get());
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using UnityEngine;

namespace PrefsGUI
Expand Down Expand Up @@ -132,28 +129,4 @@ public PrefsBoundsInt(string key, BoundsInt defaultValue = default) : base(key,

public override BoundsInt defaultMax => new(Vector3Int.one * 100, Vector3Int.one * 100);
}


[Serializable]
public class PrefsIPEndPoint : PrefsSet<PrefsString, PrefsInt, string, int>
{
public string address => prefs0.Get();
public int port => prefs1.Get();

public PrefsIPEndPoint(string key, string hostname = "localhost", int port = 10000) : base(key, hostname, port, "address", "port") { }

public static implicit operator IPEndPoint(PrefsIPEndPoint me) => CreateIPEndPoint(me.address, me.port);

public static IPEndPoint CreateIPEndPoint(string address, int port)
{
var ip = FindFromHostName(address);
return (!Equals(ip, IPAddress.None)) ? new IPEndPoint(ip, port) : null;
}

public static IPAddress FindFromHostName(string hostname)
{
var address = Dns.GetHostAddresses(hostname).FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
return address ?? IPAddress.None;
}
}
}
3 changes: 2 additions & 1 deletion Packages/ga.fuquna.prefsgui/Runtime/PrefsParam/PrefsSet.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics;
using System;
using System.Diagnostics.CodeAnalysis;
using UnityEngine.Assertions;

Expand All @@ -7,6 +7,7 @@ namespace PrefsGUI
/// <summary>
/// Combination of PrefsParams
/// </summary>
[Obsolete("PrefsSet is obsolete, consider using PrefsAny instead.")]
[SuppressMessage("ReSharper", "FieldCanBeMadeReadOnly.Global")]
public abstract class PrefsSet<TPrefs0, TPrefs1, TOuter0, TOuter1>
where TPrefs0 : PrefsParamOuter<TOuter0>
Expand Down
42 changes: 42 additions & 0 deletions Packages/ga.fuquna.prefsgui/Runtime/Utilities/IPEndPointUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;

namespace PrefsGUI
{
[Serializable]
public struct AddressAndPort
{
public string address;
public int port;

public void Deconstruct(out string a, out int p)
{
a = address;
p = port;
}
}


public static class IPEndPointUtility
{
public static IPEndPoint CreateIPEndPoint(AddressAndPort addressAndPort)
{
var (address, port) = addressAndPort;
return CreateIPEndPoint(address, port);
}

public static IPEndPoint CreateIPEndPoint(string address, int port)
{
var ip = FindFromHostName(address);
return (!Equals(ip, IPAddress.None)) ? new IPEndPoint(ip, port) : null;
}

public static IPAddress FindFromHostName(string hostname)
{
var address = Dns.GetHostAddresses(hostname).FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
return address ?? IPAddress.None;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2c8b203

Please sign in to comment.