Tanoda
TomlTypeConverter.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using UnityEngine;
4
6{
11 internal static class TomlTypeConverter
12 {
16 public class TypeConverter
17 {
22 public Func<object, Type, string> ConvertToString { get; set; }
23
28 public Func<string, Type, object> ConvertToObject { get; set; }
29 }
30
31 private static Dictionary<Type, TypeConverter> TypeConverters = new Dictionary<Type, TypeConverter>();
32
33 static TomlTypeConverter()
34 {
35 AddConverter(typeof(Enum), new TypeConverter
36 {
37 ConvertToString = (obj, type) => obj.ToString(),
38 ConvertToObject = (str, type) => Enum.Parse(type, str, true),
39 });
40
41 AddConverter(typeof(Color), new TypeConverter
42 {
43 ConvertToString = (obj, type) => ColorUtility.ToHtmlStringRGBA((Color)obj),
44 ConvertToObject = (str, type) =>
45 {
46 Color c;
47 if (!ColorUtility.TryParseHtmlString("#" + str.Trim('#', ' '), out c))
48 throw new FormatException("Invalid color string, expected hex #RRGGBBAA");
49 return c;
50 },
51 });
52
53 var jsonConverter = new TypeConverter
54 {
55 ConvertToString = (obj, type) => JsonUtility.ToJson(obj),
56 ConvertToObject = (str, type) => JsonUtility.FromJson(type: type, json: str),
57 };
58
59 AddConverter(typeof(Vector2), jsonConverter);
60 AddConverter(typeof(Vector3), jsonConverter);
61 AddConverter(typeof(Vector4), jsonConverter);
62 AddConverter(typeof(Quaternion), jsonConverter);
63 }
64
68 public static string ConvertToString(object value, Type valueType)
69 {
70 var conv = GetConverter(valueType);
71 if (conv == null)
72 throw new InvalidOperationException(string.Format("Cannot convert from type {0}", valueType));
73
74 return conv.ConvertToString(value, valueType);
75 }
76
80 public static T ConvertToValue<T>(string value)
81 {
82 return (T)ConvertToValue(value, typeof(T));
83 }
84
88 public static object ConvertToValue(string value, Type valueType)
89 {
90 var conv = GetConverter(valueType);
91 if (conv == null)
92 throw new InvalidOperationException(string.Format("Cannot convert to type {0}", valueType.Name));
93
94 return conv.ConvertToObject(value, valueType);
95 }
96
100 public static TypeConverter GetConverter(Type valueType)
101 {
102 if (valueType == null) throw new ArgumentNullException();
103
104 if (valueType.IsEnum)
105 return TypeConverters[typeof(Enum)];
106 TypeConverter result;
107 TypeConverters.TryGetValue(valueType, out result);
108 return result;
109 }
110
115 public static bool AddConverter(Type type, TypeConverter converter)
116 {
117 if (type == null) throw new ArgumentNullException();
118 if (converter == null) throw new ArgumentNullException();
119 if (CanConvert(type))
120 {
121 RuntimeUnityEditorCore.Logger.Log(LogLevel.Warning, "Tried to add a TomlConverter when one already exists for type " + type.FullName);
122 return false;
123 }
124
125 TypeConverters.Add(type, converter);
126 return true;
127 }
128
132 public static bool CanConvert(Type type)
133 {
134 return GetConverter(type) != null;
135 }
136
140 public static IEnumerable<Type> GetSupportedTypes()
141 {
142 return TypeConverters.Keys;
143 }
144 }
145}
RuntimeUnityEditor.Core.LogLevel LogLevel
Definition: RUEInvoker.cs:5
UnityEngine.Color Color
Definition: TestScript.cs:32
A serializer/deserializer combo for some type(s). Used by the config system.
Func< string, Type, object > ConvertToObject
Used to deserialize the type from a string. String is the data to deserialize, Type is the object's t...
Func< object, Type, string > ConvertToString
Used to serialize the type into a (hopefully) human-readable string. Object is the instance to serial...