From 1c5b558258d812222d74d9ee4e91b93c6375b999 Mon Sep 17 00:00:00 2001 From: Eris Date: Fri, 28 Aug 2020 00:07:40 +0200 Subject: [PATCH 1/4] Added DateTimeConverter --- IPA.Loader/Config/Stores/Converters.cs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/IPA.Loader/Config/Stores/Converters.cs b/IPA.Loader/Config/Stores/Converters.cs index ba8b6c10..6c247dd6 100644 --- a/IPA.Loader/Config/Stores/Converters.cs +++ b/IPA.Loader/Config/Stores/Converters.cs @@ -112,7 +112,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 +131,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); } } @@ -618,4 +620,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")); + } } From 740bc97e91a8ab040cd2693a0a9d26c95bb6c8d0 Mon Sep 17 00:00:00 2001 From: Eris Date: Fri, 28 Aug 2020 00:20:59 +0200 Subject: [PATCH 2/4] Added HexColorConverter Implementation provided by https://github.com/Caeden117/CountersPlus/blob/rewrite/Counters%2B/ConfigModels/Converters/ColorConverter.cs --- IPA.Loader/Config/Stores/Converters.cs | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/IPA.Loader/Config/Stores/Converters.cs b/IPA.Loader/Config/Stores/Converters.cs index 6c247dd6..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 @@ -483,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 { From ddd96fc59705f328fee5a1ba6b861e096a93fd10 Mon Sep 17 00:00:00 2001 From: Eris Date: Fri, 28 Aug 2020 17:39:18 +0200 Subject: [PATCH 3/4] Added DateTimeOffsetConverter --- IPA.Loader/Config/Stores/Converters.cs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/IPA.Loader/Config/Stores/Converters.cs b/IPA.Loader/Config/Stores/Converters.cs index 6cd104c4..f709b2a6 100644 --- a/IPA.Loader/Config/Stores/Converters.cs +++ b/IPA.Loader/Config/Stores/Converters.cs @@ -114,7 +114,7 @@ namespace IPA.Config.Stores.Converters IValConv, IValConv, IValConv, IValConv, IValConv, IValConv, - IValConv + IValConv, IValConv { internal static readonly ValConvImpls Impl = new ValConvImpls(); Type IValConv.Get() => typeof(CharConverter); @@ -133,6 +133,7 @@ namespace IPA.Config.Stores.Converters Type IValConv.Get() => typeof(DecimalConverter); Type IValConv.Get() => typeof(BooleanConverter); Type IValConv.Get() => typeof(DateTimeConverter); + Type IValConv.Get() => typeof(DateTimeOffsetConverter); } } @@ -657,7 +658,7 @@ 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) @@ -677,4 +678,24 @@ namespace IPA.Config.Stores.Converters public override Value ToValue(DateTime obj, object parent) => Value.Text(obj.ToString("O")); } + + internal class DateTimeOffsetConverter : ValueConverter + { + public override DateTimeOffset FromValue(Value value, object parent) + { + if (!(value is Text text)) + { + throw new ArgumentException("Value is not of type Text", nameof(value)); + } + + if (DateTimeOffset.TryParse(text.Value, out var dateTime)) + { + return dateTime; + } + + throw new ArgumentException($"Parsing failed, {text.Value}"); + } + + public override Value ToValue(DateTimeOffset obj, object parent) => Value.Text(obj.ToString("O")); + } } From 389a016f44d59e4fb1c6e7782209c109e6064e3c Mon Sep 17 00:00:00 2001 From: Eris Date: Fri, 28 Aug 2020 17:51:48 +0200 Subject: [PATCH 4/4] Added TimeSpanConverter --- IPA.Loader/Config/Stores/Converters.cs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/IPA.Loader/Config/Stores/Converters.cs b/IPA.Loader/Config/Stores/Converters.cs index f709b2a6..b5c18a8f 100644 --- a/IPA.Loader/Config/Stores/Converters.cs +++ b/IPA.Loader/Config/Stores/Converters.cs @@ -114,7 +114,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); @@ -134,6 +135,7 @@ namespace IPA.Config.Stores.Converters Type IValConv.Get() => typeof(BooleanConverter); Type IValConv.Get() => typeof(DateTimeConverter); Type IValConv.Get() => typeof(DateTimeOffsetConverter); + Type IValConv.Get() => typeof(TimeSpanConverter); } } @@ -698,4 +700,24 @@ namespace IPA.Config.Stores.Converters public override Value ToValue(DateTimeOffset obj, object parent) => Value.Text(obj.ToString("O")); } + + internal class TimeSpanConverter : ValueConverter + { + public override TimeSpan FromValue(Value value, object parent) + { + if (!(value is Text text)) + { + throw new ArgumentException("Value is not of type Text", nameof(value)); + } + + if (TimeSpan.TryParse(text.Value, out var dateTime)) + { + return dateTime; + } + + throw new ArgumentException($"Parsing failed, {text.Value}"); + } + + public override Value ToValue(TimeSpan obj, object parent) => Value.Text(obj.ToString()); + } }