TomlTypeConverter.cs 3.0 KB

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