TomlTypeConverter.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Runtime.CompilerServices;
  5. using System.Text.RegularExpressions;
  6. using UnityEngine;
  7. using Logger = BepInEx.Logging.Logger;
  8. namespace BepInEx.Configuration
  9. {
  10. /// <summary>
  11. /// Serializer/deserializer used by the config system.
  12. /// </summary>
  13. public static class TomlTypeConverter
  14. {
  15. // Don't put anything from UnityEngine here or it will break preloader, use LazyTomlConverterLoader instead
  16. private static Dictionary<Type, TypeConverter> TypeConverters { get; } = new Dictionary<Type, TypeConverter>
  17. {
  18. [typeof(string)] = new TypeConverter
  19. {
  20. ConvertToString = (obj, type) => Regex.Escape((string)obj),
  21. ConvertToObject = (str, type) =>
  22. {
  23. // Check if the string is a file path with unescaped \ path separators (e.g. D:\test and not D:\\test)
  24. if (Regex.IsMatch(str, @"^""?\w:\\(?!\\)(?!.+\\\\)"))
  25. return str;
  26. return Regex.Unescape(str);
  27. },
  28. },
  29. [typeof(bool)] = new TypeConverter
  30. {
  31. ConvertToString = (obj, type) => obj.ToString().ToLowerInvariant(),
  32. ConvertToObject = (str, type) => bool.Parse(str),
  33. },
  34. [typeof(byte)] = new TypeConverter
  35. {
  36. ConvertToString = (obj, type) => obj.ToString(),
  37. ConvertToObject = (str, type) => byte.Parse(str),
  38. },
  39. //integral types
  40. [typeof(sbyte)] = new TypeConverter
  41. {
  42. ConvertToString = (obj, type) => obj.ToString(),
  43. ConvertToObject = (str, type) => sbyte.Parse(str),
  44. },
  45. [typeof(byte)] = new TypeConverter
  46. {
  47. ConvertToString = (obj, type) => obj.ToString(),
  48. ConvertToObject = (str, type) => byte.Parse(str),
  49. },
  50. [typeof(short)] = new TypeConverter
  51. {
  52. ConvertToString = (obj, type) => obj.ToString(),
  53. ConvertToObject = (str, type) => short.Parse(str),
  54. },
  55. [typeof(ushort)] = new TypeConverter
  56. {
  57. ConvertToString = (obj, type) => obj.ToString(),
  58. ConvertToObject = (str, type) => ushort.Parse(str),
  59. },
  60. [typeof(int)] = new TypeConverter
  61. {
  62. ConvertToString = (obj, type) => obj.ToString(),
  63. ConvertToObject = (str, type) => int.Parse(str),
  64. },
  65. [typeof(uint)] = new TypeConverter
  66. {
  67. ConvertToString = (obj, type) => obj.ToString(),
  68. ConvertToObject = (str, type) => uint.Parse(str),
  69. },
  70. [typeof(long)] = new TypeConverter
  71. {
  72. ConvertToString = (obj, type) => obj.ToString(),
  73. ConvertToObject = (str, type) => long.Parse(str),
  74. },
  75. [typeof(ulong)] = new TypeConverter
  76. {
  77. ConvertToString = (obj, type) => obj.ToString(),
  78. ConvertToObject = (str, type) => ulong.Parse(str),
  79. },
  80. //floating point types
  81. [typeof(float)] = new TypeConverter
  82. {
  83. ConvertToString = (obj, type) => ((float)obj).ToString(NumberFormatInfo.InvariantInfo),
  84. ConvertToObject = (str, type) => float.Parse(str, NumberFormatInfo.InvariantInfo),
  85. },
  86. [typeof(double)] = new TypeConverter
  87. {
  88. ConvertToString = (obj, type) => ((double)obj).ToString(NumberFormatInfo.InvariantInfo),
  89. ConvertToObject = (str, type) => double.Parse(str, NumberFormatInfo.InvariantInfo),
  90. },
  91. [typeof(decimal)] = new TypeConverter
  92. {
  93. ConvertToString = (obj, type) => ((decimal)obj).ToString(NumberFormatInfo.InvariantInfo),
  94. ConvertToObject = (str, type) => decimal.Parse(str, NumberFormatInfo.InvariantInfo),
  95. },
  96. //enums are special
  97. [typeof(Enum)] = new TypeConverter
  98. {
  99. ConvertToString = (obj, type) => obj.ToString(),
  100. ConvertToObject = (str, type) => Enum.Parse(type, str, true),
  101. },
  102. };
  103. /// <summary>
  104. /// Convert object of a given type to a string using available converters.
  105. /// </summary>
  106. public static string ConvertToString(object value, Type valueType)
  107. {
  108. var conv = GetConverter(valueType);
  109. if (conv == null)
  110. throw new InvalidOperationException($"Cannot convert from type {valueType}");
  111. return conv.ConvertToString(value, valueType);
  112. }
  113. /// <summary>
  114. /// Convert string to an object of a given type using available converters.
  115. /// </summary>
  116. public static T ConvertToValue<T>(string value)
  117. {
  118. return (T)ConvertToValue(value, typeof(T));
  119. }
  120. /// <summary>
  121. /// Convert string to an object of a given type using available converters.
  122. /// </summary>
  123. public static object ConvertToValue(string value, Type valueType)
  124. {
  125. var conv = GetConverter(valueType);
  126. if (conv == null)
  127. throw new InvalidOperationException($"Cannot convert to type {valueType.Name}");
  128. return conv.ConvertToObject(value, valueType);
  129. }
  130. /// <summary>
  131. /// Get a converter for a given type if there is any.
  132. /// </summary>
  133. public static TypeConverter GetConverter(Type valueType)
  134. {
  135. if (valueType == null) throw new ArgumentNullException(nameof(valueType));
  136. if (valueType.IsEnum)
  137. return TypeConverters[typeof(Enum)];
  138. if (!TypeConverters.TryGetValue(valueType, out var result) && !_lazyLoadedConverters)
  139. {
  140. _lazyLoadedConverters = true;
  141. LazyLoadConverters();
  142. TypeConverters.TryGetValue(valueType, out result);
  143. }
  144. return result;
  145. }
  146. /// <summary>
  147. /// Add a new type converter for a given type.
  148. /// If a different converter is already added, this call is ignored and false is returned.
  149. /// </summary>
  150. public static bool AddConverter(Type type, TypeConverter converter)
  151. {
  152. if (type == null) throw new ArgumentNullException(nameof(type));
  153. if (converter == null) throw new ArgumentNullException(nameof(converter));
  154. if (CanConvert(type))
  155. {
  156. Logger.LogWarning("Tried to add a TomlConverter when one already exists for type " + type.FullName);
  157. return false;
  158. }
  159. TypeConverters.Add(type, converter);
  160. return true;
  161. }
  162. /// <summary>
  163. /// Check if a given type can be converted to and from string.
  164. /// </summary>
  165. public static bool CanConvert(Type type)
  166. {
  167. return GetConverter(type) != null;
  168. }
  169. /// <summary>
  170. /// Give a list of types with registered converters.
  171. /// </summary>
  172. public static IEnumerable<Type> GetSupportedTypes()
  173. {
  174. return TypeConverters.Keys;
  175. }
  176. private static bool _lazyLoadedConverters;
  177. private static void LazyLoadConverters()
  178. {
  179. try { LazyTomlConverterLoader.AddUnityEngineConverters(); }
  180. catch (Exception ex) { Logger.LogWarning("Failed to load UnityEngine Toml converters - " + ex.Message); }
  181. }
  182. }
  183. /// <summary>
  184. /// For types that are in assemblies that can't get loaded before preloader runs (or it won't work on these assemblies)
  185. /// </summary>
  186. internal static class LazyTomlConverterLoader
  187. {
  188. [MethodImpl(MethodImplOptions.NoInlining)]
  189. public static void AddUnityEngineConverters()
  190. {
  191. var colorConverter = new TypeConverter
  192. {
  193. ConvertToString = (obj, type) => ColorUtility.ToHtmlStringRGBA((Color)obj),
  194. ConvertToObject = (str, type) =>
  195. {
  196. if (!ColorUtility.TryParseHtmlString("#" + str.Trim('#', ' '), out var c))
  197. throw new FormatException("Invalid color string, expected hex #RRGGBBAA");
  198. return c;
  199. },
  200. };
  201. TomlTypeConverter.AddConverter(typeof(Color), colorConverter);
  202. var jsonConverter = new TypeConverter
  203. {
  204. ConvertToString = (obj, type) => JsonUtility.ToJson(obj),
  205. ConvertToObject = (str, type) => JsonUtility.FromJson(type: type, json: str),
  206. };
  207. TomlTypeConverter.AddConverter(typeof(Vector2), jsonConverter);
  208. TomlTypeConverter.AddConverter(typeof(Vector3), jsonConverter);
  209. TomlTypeConverter.AddConverter(typeof(Vector4), jsonConverter);
  210. TomlTypeConverter.AddConverter(typeof(Quaternion), jsonConverter);
  211. }
  212. }
  213. }