Skip to content

Commit

Permalink
Merge branch 'master' of github.com:bsmg/BeatSaber-IPA-Reloaded into …
Browse files Browse the repository at this point in the history
…master
  • Loading branch information
nike4613 committed Aug 31, 2020
2 parents 0918190 + 7d39fdb commit 83e0664
Showing 1 changed file with 103 additions and 1 deletion.
104 changes: 103 additions & 1 deletion IPA.Loader/Config/Stores/Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Boolean = IPA.Config.Data.Boolean;

namespace IPA.Config.Stores.Converters
Expand Down Expand Up @@ -112,7 +113,9 @@ private class ValConvImpls : IValConv<char>,
IValConv<short>, IValConv<ushort>,
IValConv<sbyte>, IValConv<byte>,
IValConv<float>, IValConv<double>,
IValConv<decimal>, IValConv<bool>
IValConv<decimal>, IValConv<bool>,
IValConv<DateTime>, IValConv<DateTimeOffset>,
IValConv<TimeSpan>
{
internal static readonly ValConvImpls Impl = new ValConvImpls();
Type IValConv<char>.Get() => typeof(CharConverter);
Expand All @@ -130,6 +133,9 @@ private class ValConvImpls : IValConv<char>,
Type IValConv<double>.Get() => typeof(DoubleConverter);
Type IValConv<decimal>.Get() => typeof(DecimalConverter);
Type IValConv<bool>.Get() => typeof(BooleanConverter);
Type IValConv<DateTime>.Get() => typeof(DateTimeConverter);
Type IValConv<DateTimeOffset>.Get() => typeof(DateTimeOffsetConverter);
Type IValConv<TimeSpan>.Get() => typeof(TimeSpanConverter);
}
}

Expand Down Expand Up @@ -481,6 +487,42 @@ public sealed class IReadOnlyDictionaryConverter<TValue, TConverter> : IReadOnly
public IReadOnlyDictionaryConverter() : base(new TConverter()) { }
}
#endif

/// <summary>
/// A converter for <see cref="Color"/> objects.
/// </summary>
public sealed class HexColorConverter : ValueConverter<Color>
{
/// <summary>
/// Converts a <see cref="Value"/> that is a <see cref="Text"/> node to the corresponding <see cref="Color" /> object.
/// </summary>
/// <param name="value">the <see cref="Value"/> to convert</param>
/// <param name="parent">the object which will own the created object</param>
/// <returns>the deserialized Color object</returns>
/// <exception cref="ArgumentException">if <paramref name="value"/> is not a <see cref="Text"/> node or couldn't be parsed into a Color object</exception>
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));
}

/// <summary>
/// Converts color of type <see cref="Color"/> to a <see cref="Value"/> node.
/// </summary>
/// <param name="obj">the object to serialize</param>
/// <param name="parent">the object which owns <paramref name="obj"/></param>
/// <returns>a <see cref="Text"/> node representing <paramref name="obj"/></returns>
public override Value ToValue(Color obj, object parent) => Value.Text($"#{ColorUtility.ToHtmlStringRGB(obj)}");
}

internal class StringConverter : ValueConverter<string>
{
Expand Down Expand Up @@ -618,4 +660,64 @@ public override bool FromValue(Value value, object parent)
public override Value ToValue(bool obj, object parent)
=> Value.From(obj);
}

internal class DateTimeConverter : ValueConverter<DateTime>
{
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"));
}

internal class DateTimeOffsetConverter : ValueConverter<DateTimeOffset>
{
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"));
}

internal class TimeSpanConverter : ValueConverter<TimeSpan>
{
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());
}
}

0 comments on commit 83e0664

Please sign in to comment.