You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

397 lines
18 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using IPA.Config.Data;
  2. using IPA.Config.Stores.Attributes;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Boolean = IPA.Config.Data.Boolean;
  9. namespace IPA.Config.Stores.Converters
  10. {
  11. /// <summary>
  12. /// Provides utility functions for custom converters.
  13. /// </summary>
  14. public static class Converter
  15. {
  16. /// <summary>
  17. /// Gets the integral value of a <see cref="Value"/>, coercing a <see cref="FloatingPoint"/> if necessary,
  18. /// or <see langword="null"/> if <paramref name="val"/> is not an <see cref="Integer"/> or <see cref="FloatingPoint"/>.
  19. /// </summary>
  20. /// <param name="val">the <see cref="Value"/> to get the integral value of</param>
  21. /// <returns>the integral value of <paramref name="val"/>, or <see langword="null"/></returns>
  22. public static long? IntValue(Value val)
  23. => val is Integer inte ? inte.Value :
  24. val is FloatingPoint fp ? fp.AsInteger()?.Value :
  25. null;
  26. /// <summary>
  27. /// Gets the floaing point value of a <see cref="Value"/>, coercing an <see cref="Integer"/> if necessary,
  28. /// or <see langword="null"/> if <paramref name="val"/> is not an <see cref="Integer"/> or <see cref="FloatingPoint"/>.
  29. /// </summary>
  30. /// <param name="val">the <see cref="Value"/> to get the floaing point value of</param>
  31. /// <returns>the floaing point value of <paramref name="val"/>, or <see langword="null"/></returns>
  32. public static decimal? FloatValue(Value val)
  33. => val is FloatingPoint fp ? fp.Value :
  34. val is Integer inte ? inte.AsFloat()?.Value :
  35. null;
  36. internal interface IValConv<T>
  37. {
  38. ValueConverter<T> Get();
  39. }
  40. internal class ValConv<T> : IValConv<T> where T : struct
  41. {
  42. private static readonly IValConv<T> Impl = ValConvImpls.Impl as IValConv<T> ?? new ValConv<T>();
  43. public ValueConverter<T> Get() => Impl.Get();
  44. ValueConverter<T> IValConv<T>.Get()
  45. => null; // default to null
  46. }
  47. private class ValConvImpls : IValConv<char>,
  48. IValConv<IntPtr>, IValConv<UIntPtr>,
  49. IValConv<long>, IValConv<ulong>,
  50. IValConv<int>, IValConv<uint>,
  51. IValConv<short>, IValConv<ushort>,
  52. IValConv<sbyte>, IValConv<byte>,
  53. IValConv<float>, IValConv<double>,
  54. IValConv<decimal>, IValConv<bool>
  55. {
  56. internal static readonly ValConvImpls Impl = new ValConvImpls();
  57. ValueConverter<char> IValConv<char>.Get() => new CharConverter();
  58. ValueConverter<long> IValConv<long>.Get() => new LongConverter();
  59. ValueConverter<ulong> IValConv<ulong>.Get() => new ULongConverter();
  60. ValueConverter<IntPtr> IValConv<IntPtr>.Get() => new IntPtrConverter();
  61. ValueConverter<UIntPtr> IValConv<UIntPtr>.Get() => new UIntPtrConverter();
  62. ValueConverter<int> IValConv<int>.Get() => new IntConverter();
  63. ValueConverter<uint> IValConv<uint>.Get() => new UIntConverter();
  64. ValueConverter<short> IValConv<short>.Get() => new ShortConverter();
  65. ValueConverter<ushort> IValConv<ushort>.Get() => new UShortConverter();
  66. ValueConverter<byte> IValConv<byte>.Get() => new ByteConverter();
  67. ValueConverter<sbyte> IValConv<sbyte>.Get() => new SByteConverter();
  68. ValueConverter<float> IValConv<float>.Get() => new FloatConverter();
  69. ValueConverter<double> IValConv<double>.Get() => new DoubleConverter();
  70. ValueConverter<decimal> IValConv<decimal>.Get() => new DecimalConverter();
  71. ValueConverter<bool> IValConv<bool>.Get() => new BooleanConverter();
  72. }
  73. }
  74. /// <summary>
  75. /// Provides generic utilities for converters for certain types.
  76. /// </summary>
  77. /// <typeparam name="T">the type of the <see cref="ValueConverter{T}"/> that this works on</typeparam>
  78. public static class Converter<T>
  79. {
  80. private static ValueConverter<T> defaultConverter = null;
  81. /// <summary>
  82. /// Gets the default <see cref="ValueConverter{T}"/> for the current type.
  83. /// </summary>
  84. public static ValueConverter<T> Default
  85. => defaultConverter ??= MakeDefault();
  86. private static ValueConverter<T> MakeDefault()
  87. {
  88. var t = typeof(T);
  89. if (t.IsValueType)
  90. { // we have to do this garbo to make it accept the thing that we know is a value type at instantiation time
  91. if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>))
  92. { // this is a Nullable
  93. return Activator.CreateInstance(typeof(NullableConverter<>).MakeGenericType(Nullable.GetUnderlyingType(t))) as ValueConverter<T>;
  94. }
  95. var valConv = Activator.CreateInstance(typeof(Converter.ValConv<>).MakeGenericType(t)) as Converter.IValConv<T>;
  96. return valConv.Get();
  97. }
  98. else if (t == typeof(string))
  99. {
  100. return new StringConverter() as ValueConverter<T>;
  101. }
  102. else
  103. {
  104. return Activator.CreateInstance(typeof(CustomObjectConverter<>).MakeGenericType(t)) as ValueConverter<T>;
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// A converter for a <see cref="Nullable{T}"/>.
  110. /// </summary>
  111. /// <typeparam name="T">the underlying type of the <see cref="Nullable{T}"/></typeparam>
  112. public class NullableConverter<T> : ValueConverter<T?> where T : struct
  113. {
  114. private readonly ValueConverter<T> baseConverter;
  115. /// <summary>
  116. /// Creates a converter with the default converter for the base type.
  117. /// Equivalent to
  118. /// <code>
  119. /// new NullableConverter(Converter&lt;T&gt;.Default)
  120. /// </code>
  121. /// </summary>
  122. /// <seealso cref="NullableConverter{T}.NullableConverter(ValueConverter{T})"/>
  123. /// <seealso cref="Converter{T}.Default"/>
  124. public NullableConverter() : this(Converter<T>.Default) { }
  125. /// <summary>
  126. /// Creates a converter with the given underlying <see cref="ValueConverter{T}"/>.
  127. /// </summary>
  128. /// <param name="underlying">the undlerlying <see cref="ValueConverter{T}"/> to use</param>
  129. public NullableConverter(ValueConverter<T> underlying)
  130. => baseConverter = underlying;
  131. /// <summary>
  132. /// Converts a <see cref="Value"/> tree to a value.
  133. /// </summary>
  134. /// <param name="value">the <see cref="Value"/> tree to convert</param>
  135. /// <param name="parent">the object which will own the created object</param>
  136. /// <returns>the object represented by <paramref name="value"/></returns>
  137. public override T? FromValue(Value value, object parent)
  138. => value == null ? null : new T?(baseConverter.FromValue(value, parent));
  139. /// <summary>
  140. /// Converts a nullable <typeparamref name="T"/> to a <see cref="Value"/> tree.
  141. /// </summary>
  142. /// <param name="obj">the value to serialize</param>
  143. /// <param name="parent">the object which owns <paramref name="obj"/></param>
  144. /// <returns>a <see cref="Value"/> tree representing <paramref name="obj"/>.</returns>
  145. public override Value ToValue(T? obj, object parent)
  146. => obj == null ? null : baseConverter.ToValue(obj.Value, parent);
  147. }
  148. /// <summary>
  149. /// A converter for a <see cref="Nullable{T}"/> that default-constructs a converter of type <typeparamref name="TConverter"/>
  150. /// to use as the underlying converter. Use this in the <see cref="UseConverterAttribute"/>.
  151. /// </summary>
  152. /// <typeparam name="T">the underlying type of the <see cref="Nullable{T}"/></typeparam>
  153. /// <typeparam name="TConverter">the type to use as an underlying converter</typeparam>
  154. /// <seealso cref="NullableConverter{T}"/>
  155. public sealed class NullableConverter<T, TConverter> : NullableConverter<T>
  156. where T : struct
  157. where TConverter : ValueConverter<T>, new()
  158. {
  159. /// <summary>
  160. /// Creates a converter with a new <typeparamref name="TConverter"/> as the underlying converter.
  161. /// </summary>
  162. /// <seealso cref="NullableConverter{T}.NullableConverter(ValueConverter{T})"/>
  163. public NullableConverter() : base(new TConverter()) { }
  164. }
  165. /// <summary>
  166. /// A converter for an enum of type <typeparamref name="T"/>, that converts the enum to its string representation and back.
  167. /// </summary>
  168. /// <typeparam name="T">the enum type</typeparam>
  169. public sealed class EnumConverter<T> : ValueConverter<T>
  170. where T : Enum
  171. {
  172. /// <summary>
  173. /// Converts a <see cref="Value"/> that is a <see cref="Text"/> node to the corresponding enum value.
  174. /// </summary>
  175. /// <param name="value">the <see cref="Value"/> to convert</param>
  176. /// <param name="parent">the object which will own the created object</param>
  177. /// <returns>the deserialized enum value</returns>
  178. /// <exception cref="ArgumentException">if <paramref name="value"/> is not a <see cref="Text"/> node</exception>
  179. public override T FromValue(Value value, object parent)
  180. => value is Text t
  181. ? (T)Enum.Parse(typeof(T), t.Value)
  182. : throw new ArgumentException("Value not a string", nameof(value));
  183. /// <summary>
  184. /// Converts an enum of type <typeparamref name="T"/> to a <see cref="Value"/> node corresponding to its value.
  185. /// </summary>
  186. /// <param name="obj">the value to serialize</param>
  187. /// <param name="parent">the object which owns <paramref name="obj"/></param>
  188. /// <returns>a <see cref="Text"/> node representing <paramref name="obj"/></returns>
  189. public override Value ToValue(T obj, object parent)
  190. => Value.Text(obj.ToString());
  191. }
  192. /// <summary>
  193. /// A converter for an enum of type <typeparamref name="T"/>, that converts the enum to its string representation and back,
  194. /// ignoring the case of the serialized value for deseiralization.
  195. /// </summary>
  196. /// <typeparam name="T">the enum type</typeparam>
  197. public sealed class CaseInsensitiveEnumConverter<T> : ValueConverter<T>
  198. where T : Enum
  199. {
  200. /// <summary>
  201. /// Converts a <see cref="Value"/> that is a <see cref="Text"/> node to the corresponding enum value.
  202. /// </summary>
  203. /// <param name="value">the <see cref="Value"/> to convert</param>
  204. /// <param name="parent">the object which will own the created object</param>
  205. /// <returns>the deserialized enum value</returns>
  206. /// <exception cref="ArgumentException">if <paramref name="value"/> is not a <see cref="Text"/> node</exception>
  207. public override T FromValue(Value value, object parent)
  208. => value is Text t
  209. ? (T)Enum.Parse(typeof(T), t.Value, true)
  210. : throw new ArgumentException("Value not a string", nameof(value));
  211. /// <summary>
  212. /// Converts an enum of type <typeparamref name="T"/> to a <see cref="Value"/> node corresponding to its value.
  213. /// </summary>
  214. /// <param name="obj">the value to serialize</param>
  215. /// <param name="parent">the object which owns <paramref name="obj"/></param>
  216. /// <returns>a <see cref="Text"/> node representing <paramref name="obj"/></returns>
  217. public override Value ToValue(T obj, object parent)
  218. => Value.Text(obj.ToString());
  219. }
  220. /// <summary>
  221. /// A converter for an enum of type <typeparamref name="T"/>, that converts the enum to its underlying value for serialization.
  222. /// </summary>
  223. /// <typeparam name="T">the enum type</typeparam>
  224. public sealed class NumericEnumConverter<T> : ValueConverter<T>
  225. where T : Enum
  226. {
  227. /// <summary>
  228. /// Converts a <see cref="Value"/> that is a numeric node to the corresponding enum value.
  229. /// </summary>
  230. /// <param name="value">the <see cref="Value"/> to convert</param>
  231. /// <param name="parent">the object which will own the created object</param>
  232. /// <returns>the deserialized enum value</returns>
  233. /// <exception cref="ArgumentException">if <paramref name="value"/> is not a numeric node</exception>
  234. public override T FromValue(Value value, object parent)
  235. => (T)Enum.ToObject(typeof(T), Converter.IntValue(value)
  236. ?? throw new ArgumentException("Value not a numeric node", nameof(value)));
  237. /// <summary>
  238. /// Converts an enum of type <typeparamref name="T"/> to a <see cref="Value"/> node corresponding to its value.
  239. /// </summary>
  240. /// <param name="obj">the value to serialize</param>
  241. /// <param name="parent">the object which owns <paramref name="obj"/></param>
  242. /// <returns>an <see cref="Integer"/> node representing <paramref name="obj"/></returns>
  243. public override Value ToValue(T obj, object parent)
  244. => Value.Integer(Convert.ToInt64(obj));
  245. }
  246. internal class StringConverter : ValueConverter<string>
  247. {
  248. public override string FromValue(Value value, object parent)
  249. => (value as Text)?.Value;
  250. public override Value ToValue(string obj, object parent)
  251. => Value.From(obj);
  252. }
  253. internal class CharConverter : ValueConverter<char>
  254. {
  255. public override char FromValue(Value value, object parent)
  256. => (value as Text)?.Value[0]
  257. ?? throw new ArgumentException("Value not a text node", nameof(value)); // can throw nullptr
  258. public override Value ToValue(char obj, object parent)
  259. => Value.From(char.ToString(obj));
  260. }
  261. internal class LongConverter : ValueConverter<long>
  262. {
  263. public override long FromValue(Value value, object parent)
  264. => Converter.IntValue(value)
  265. ?? throw new ArgumentException("Value not a numeric value", nameof(value));
  266. public override Value ToValue(long obj, object parent)
  267. => Value.From(obj);
  268. }
  269. internal class ULongConverter : ValueConverter<ulong>
  270. {
  271. public override ulong FromValue(Value value, object parent)
  272. => (ulong)(Converter.FloatValue(value)
  273. ?? throw new ArgumentException("Value not a numeric value", nameof(value)));
  274. public override Value ToValue(ulong obj, object parent)
  275. => Value.From(obj);
  276. }
  277. internal class IntPtrConverter : ValueConverter<IntPtr>
  278. {
  279. public override IntPtr FromValue(Value value, object parent)
  280. => (IntPtr)Converter<long>.Default.FromValue(value, parent);
  281. public override Value ToValue(IntPtr obj, object parent)
  282. => Value.From((long)obj);
  283. }
  284. internal class UIntPtrConverter : ValueConverter<UIntPtr>
  285. {
  286. public override UIntPtr FromValue(Value value, object parent)
  287. => (UIntPtr)Converter<ulong>.Default.FromValue(value, parent);
  288. public override Value ToValue(UIntPtr obj, object parent)
  289. => Value.From((decimal)obj);
  290. }
  291. internal class IntConverter : ValueConverter<int>
  292. {
  293. public override int FromValue(Value value, object parent)
  294. => (int)Converter<long>.Default.FromValue(value, parent);
  295. public override Value ToValue(int obj, object parent)
  296. => Value.From(obj);
  297. }
  298. internal class UIntConverter : ValueConverter<uint>
  299. {
  300. public override uint FromValue(Value value, object parent)
  301. => (uint)Converter<long>.Default.FromValue(value, parent);
  302. public override Value ToValue(uint obj, object parent)
  303. => Value.From(obj);
  304. }
  305. internal class ShortConverter : ValueConverter<short>
  306. {
  307. public override short FromValue(Value value, object parent)
  308. => (short)Converter<long>.Default.FromValue(value, parent);
  309. public override Value ToValue(short obj, object parent)
  310. => Value.From(obj);
  311. }
  312. internal class UShortConverter : ValueConverter<ushort>
  313. {
  314. public override ushort FromValue(Value value, object parent)
  315. => (ushort)Converter<long>.Default.FromValue(value, parent);
  316. public override Value ToValue(ushort obj, object parent)
  317. => Value.From(obj);
  318. }
  319. internal class ByteConverter : ValueConverter<byte>
  320. {
  321. public override byte FromValue(Value value, object parent)
  322. => (byte)Converter<long>.Default.FromValue(value, parent);
  323. public override Value ToValue(byte obj, object parent)
  324. => Value.From(obj);
  325. }
  326. internal class SByteConverter : ValueConverter<sbyte>
  327. {
  328. public override sbyte FromValue(Value value, object parent)
  329. => (sbyte)Converter<long>.Default.FromValue(value, parent);
  330. public override Value ToValue(sbyte obj, object parent)
  331. => Value.From(obj);
  332. }
  333. internal class DecimalConverter : ValueConverter<decimal>
  334. {
  335. public override decimal FromValue(Value value, object parent)
  336. => Converter.FloatValue(value) ?? throw new ArgumentException("Value not a numeric value", nameof(value));
  337. public override Value ToValue(decimal obj, object parent)
  338. => Value.From(obj);
  339. }
  340. internal class FloatConverter : ValueConverter<float>
  341. {
  342. public override float FromValue(Value value, object parent)
  343. => (float)Converter<decimal>.Default.FromValue(value, parent);
  344. public override Value ToValue(float obj, object parent)
  345. => Value.From((decimal)obj);
  346. }
  347. internal class DoubleConverter : ValueConverter<double>
  348. {
  349. public override double FromValue(Value value, object parent)
  350. => (double)Converter<decimal>.Default.FromValue(value, parent);
  351. public override Value ToValue(double obj, object parent)
  352. => Value.From((decimal)obj);
  353. }
  354. internal class BooleanConverter : ValueConverter<bool>
  355. {
  356. public override bool FromValue(Value value, object parent)
  357. => (value as Boolean)?.Value ?? throw new ArgumentException("Value not a Boolean", nameof(value));
  358. public override Value ToValue(bool obj, object parent)
  359. => Value.From(obj);
  360. }
  361. }