TomlTypeConverter.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. namespace BepInEx.Configuration
  5. {
  6. public class TypeConverter
  7. {
  8. public Func<object, string> ConvertToString { get; set; }
  9. public Func<string, object> ConvertToObject { get; set; }
  10. }
  11. internal static class TomlTypeConverter
  12. {
  13. public static Dictionary<Type, TypeConverter> TypeConverters { get; } = new Dictionary<Type, TypeConverter>
  14. {
  15. [typeof(string)] = new TypeConverter
  16. {
  17. ConvertToString = (obj) => (string)obj,
  18. ConvertToObject = (str) => str,
  19. },
  20. [typeof(bool)] = new TypeConverter
  21. {
  22. ConvertToString = (obj) => obj.ToString().ToLowerInvariant(),
  23. ConvertToObject = (str) => bool.Parse(str),
  24. },
  25. [typeof(byte)] = new TypeConverter
  26. {
  27. ConvertToString = (obj) => obj.ToString(),
  28. ConvertToObject = (str) => byte.Parse(str),
  29. },
  30. //integral types
  31. [typeof(sbyte)] = new TypeConverter
  32. {
  33. ConvertToString = (obj) => obj.ToString(),
  34. ConvertToObject = (str) => sbyte.Parse(str),
  35. },
  36. [typeof(byte)] = new TypeConverter
  37. {
  38. ConvertToString = (obj) => obj.ToString(),
  39. ConvertToObject = (str) => byte.Parse(str),
  40. },
  41. [typeof(short)] = new TypeConverter
  42. {
  43. ConvertToString = (obj) => obj.ToString(),
  44. ConvertToObject = (str) => short.Parse(str),
  45. },
  46. [typeof(ushort)] = new TypeConverter
  47. {
  48. ConvertToString = (obj) => obj.ToString(),
  49. ConvertToObject = (str) => ushort.Parse(str),
  50. },
  51. [typeof(int)] = new TypeConverter
  52. {
  53. ConvertToString = (obj) => obj.ToString(),
  54. ConvertToObject = (str) => int.Parse(str),
  55. },
  56. [typeof(uint)] = new TypeConverter
  57. {
  58. ConvertToString = (obj) => obj.ToString(),
  59. ConvertToObject = (str) => uint.Parse(str),
  60. },
  61. [typeof(long)] = new TypeConverter
  62. {
  63. ConvertToString = (obj) => obj.ToString(),
  64. ConvertToObject = (str) => long.Parse(str),
  65. },
  66. [typeof(ulong)] = new TypeConverter
  67. {
  68. ConvertToString = (obj) => obj.ToString(),
  69. ConvertToObject = (str) => ulong.Parse(str),
  70. },
  71. //floating point types
  72. [typeof(float)] = new TypeConverter
  73. {
  74. ConvertToString = (obj) => ((float)obj).ToString(NumberFormatInfo.InvariantInfo),
  75. ConvertToObject = (str) => float.Parse(str, NumberFormatInfo.InvariantInfo),
  76. },
  77. [typeof(double)] = new TypeConverter
  78. {
  79. ConvertToString = (obj) => ((double)obj).ToString(NumberFormatInfo.InvariantInfo),
  80. ConvertToObject = (str) => double.Parse(str, NumberFormatInfo.InvariantInfo),
  81. },
  82. [typeof(decimal)] = new TypeConverter
  83. {
  84. ConvertToString = (obj) => ((decimal)obj).ToString(NumberFormatInfo.InvariantInfo),
  85. ConvertToObject = (str) => decimal.Parse(str, NumberFormatInfo.InvariantInfo),
  86. },
  87. };
  88. public static string ConvertToString(object value)
  89. {
  90. Type valueType = value.GetType();
  91. if (!TypeConverters.ContainsKey(valueType))
  92. throw new InvalidOperationException($"Cannot convert from type {valueType}");
  93. return TypeConverters[valueType].ConvertToString(value);
  94. }
  95. public static T ConvertToValue<T>(string value)
  96. {
  97. if (!TypeConverters.ContainsKey(typeof(T)))
  98. throw new InvalidOperationException($"Cannot convert to type {typeof(T)}");
  99. return (T)TypeConverters[typeof(T)].ConvertToObject(value);
  100. }
  101. }
  102. }