TomlTypeConverter.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  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) => 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 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)
  136. throw new ArgumentNullException(nameof(valueType));
  137. if (valueType.IsEnum)
  138. return TypeConverters[typeof(Enum)];
  139. TypeConverters.TryGetValue(valueType, out var result);
  140. return result;
  141. }
  142. /// <summary>
  143. /// Add a new type converter for a given type.
  144. /// If a different converter is already added, this call is ignored and false is returned.
  145. /// </summary>
  146. public static bool AddConverter(Type type, TypeConverter converter)
  147. {
  148. if (type == null) throw new ArgumentNullException(nameof(type));
  149. if (converter == null) throw new ArgumentNullException(nameof(converter));
  150. if (CanConvert(type))
  151. {
  152. Logger.LogWarning("Tried to add a TomlConverter when one already exists for type " + type.FullName);
  153. return false;
  154. }
  155. TypeConverters.Add(type, converter);
  156. return true;
  157. }
  158. /// <summary>
  159. /// Check if a given type can be converted to and from string.
  160. /// </summary>
  161. public static bool CanConvert(Type type)
  162. {
  163. return GetConverter(type) != null;
  164. }
  165. /// <summary>
  166. /// Give a list of types with registered converters.
  167. /// </summary>
  168. public static IEnumerable<Type> GetSupportedTypes()
  169. {
  170. return TypeConverters.Keys;
  171. }
  172. private static string Escape(this string txt)
  173. {
  174. if (string.IsNullOrEmpty(txt)) return string.Empty;
  175. var stringBuilder = new StringBuilder(txt.Length + 2);
  176. foreach (char c in txt)
  177. switch (c)
  178. {
  179. case '\0':
  180. stringBuilder.Append(@"\0");
  181. break;
  182. case '\a':
  183. stringBuilder.Append(@"\a");
  184. break;
  185. case '\b':
  186. stringBuilder.Append(@"\b");
  187. break;
  188. case '\t':
  189. stringBuilder.Append(@"\t");
  190. break;
  191. case '\n':
  192. stringBuilder.Append(@"\n");
  193. break;
  194. case '\v':
  195. stringBuilder.Append(@"\v");
  196. break;
  197. case '\f':
  198. stringBuilder.Append(@"\f");
  199. break;
  200. case '\r':
  201. stringBuilder.Append(@"\r");
  202. break;
  203. case '\'':
  204. stringBuilder.Append(@"\'");
  205. break;
  206. case '\\':
  207. stringBuilder.Append(@"\");
  208. break;
  209. case '\"':
  210. stringBuilder.Append(@"\""");
  211. break;
  212. default:
  213. stringBuilder.Append(c);
  214. break;
  215. }
  216. return stringBuilder.ToString();
  217. }
  218. private static string Unescape(this string txt)
  219. {
  220. if (string.IsNullOrEmpty(txt))
  221. return txt;
  222. var stringBuilder = new StringBuilder(txt.Length);
  223. for (int i = 0; i < txt.Length;)
  224. {
  225. int num = txt.IndexOf('\\', i);
  226. if (num < 0 || num == txt.Length - 1)
  227. num = txt.Length;
  228. stringBuilder.Append(txt, i, num - i);
  229. if (num >= txt.Length)
  230. break;
  231. char c = txt[num + 1];
  232. switch (c)
  233. {
  234. case '0':
  235. stringBuilder.Append('\0');
  236. break;
  237. case 'a':
  238. stringBuilder.Append('\a');
  239. break;
  240. case 'b':
  241. stringBuilder.Append('\b');
  242. break;
  243. case 't':
  244. stringBuilder.Append('\t');
  245. break;
  246. case 'n':
  247. stringBuilder.Append('\n');
  248. break;
  249. case 'v':
  250. stringBuilder.Append('\v');
  251. break;
  252. case 'f':
  253. stringBuilder.Append('\f');
  254. break;
  255. case 'r':
  256. stringBuilder.Append('\r');
  257. break;
  258. case '\'':
  259. stringBuilder.Append('\'');
  260. break;
  261. case '\"':
  262. stringBuilder.Append('\"');
  263. break;
  264. case '\\':
  265. stringBuilder.Append('\\');
  266. break;
  267. default:
  268. stringBuilder.Append('\\').Append(c);
  269. break;
  270. }
  271. i = num + 2;
  272. }
  273. return stringBuilder.ToString();
  274. }
  275. }
  276. }