diff --git a/IPA.Loader/Config/Stores/Converters.cs b/IPA.Loader/Config/Stores/Converters.cs index ba8b6c10..6cd104c4 100644 --- a/IPA.Loader/Config/Stores/Converters.cs +++ b/IPA.Loader/Config/Stores/Converters.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using UnityEngine; using Boolean = IPA.Config.Data.Boolean; namespace IPA.Config.Stores.Converters @@ -112,7 +113,8 @@ namespace IPA.Config.Stores.Converters IValConv, IValConv, IValConv, IValConv, IValConv, IValConv, - IValConv, IValConv + IValConv, IValConv, + IValConv { internal static readonly ValConvImpls Impl = new ValConvImpls(); Type IValConv.Get() => typeof(CharConverter); @@ -130,6 +132,7 @@ namespace IPA.Config.Stores.Converters Type IValConv.Get() => typeof(DoubleConverter); Type IValConv.Get() => typeof(DecimalConverter); Type IValConv.Get() => typeof(BooleanConverter); + Type IValConv.Get() => typeof(DateTimeConverter); } } @@ -481,6 +484,42 @@ namespace IPA.Config.Stores.Converters public IReadOnlyDictionaryConverter() : base(new TConverter()) { } } #endif + + /// + /// A converter for objects. + /// + public sealed class HexColorConverter : ValueConverter + { + /// + /// Converts a that is a node to the corresponding object. + /// + /// the to convert + /// the object which will own the created object + /// the deserialized Color object + /// if is not a node or couldn't be parsed into a Color object + public override Color FromValue(Value value, object parent) + { + if (value is Text t) + { + if (ColorUtility.TryParseHtmlString(t.Value, out Color color)) + { + return color; + } + + throw new ArgumentException("Value cannot be parsed into a Color.", nameof(value)); + } + + throw new ArgumentException("Value not a string", nameof(value)); + } + + /// + /// Converts color of type to a node. + /// + /// the object to serialize + /// the object which owns + /// a node representing + public override Value ToValue(Color obj, object parent) => Value.Text($"#{ColorUtility.ToHtmlStringRGB(obj)}"); + } internal class StringConverter : ValueConverter { @@ -618,4 +657,24 @@ namespace IPA.Config.Stores.Converters public override Value ToValue(bool obj, object parent) => Value.From(obj); } + + internal class DateTimeConverter : ValueConverter + { + public override DateTime FromValue(Value value, object parent) + { + if (!(value is Text text)) + { + throw new ArgumentException("Value is not of type Text", nameof(value)); + } + + if (DateTime.TryParse(text.Value, out var dateTime)) + { + return dateTime; + } + + throw new ArgumentException($"Parsing failed, {text.Value}"); + } + + public override Value ToValue(DateTime obj, object parent) => Value.Text(obj.ToString("O")); + } }