From 41a8083a7df1178e98d61f75441382a647ffff81 Mon Sep 17 00:00:00 2001 From: ManlyMarco <39247311+ManlyMarco@users.noreply.github.com> Date: Fri, 11 Nov 2022 22:13:25 +0100 Subject: [PATCH 1/3] Add TomlTypeConverter for UnityEngine.Rect --- BepInEx/Configuration/TomlTypeConverter.cs | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/BepInEx/Configuration/TomlTypeConverter.cs b/BepInEx/Configuration/TomlTypeConverter.cs index 1e31e5f..519b7ce 100644 --- a/BepInEx/Configuration/TomlTypeConverter.cs +++ b/BepInEx/Configuration/TomlTypeConverter.cs @@ -341,6 +341,50 @@ namespace BepInEx.Configuration TomlTypeConverter.AddConverter(typeof(Vector3), jsonConverter); TomlTypeConverter.AddConverter(typeof(Vector4), jsonConverter); TomlTypeConverter.AddConverter(typeof(Quaternion), jsonConverter); + + TomlTypeConverter.AddConverter(typeof(Rect), new TypeConverter + { + ConvertToObject = delegate (string s, Type type) + { + Rect result = default(Rect); + if (s != null) + { + string cleaned = s.Trim(new char[] { '{', '}' }).Replace(" ", ""); + foreach (string part in cleaned.Split(new char[] { ',' })) + { + string[] parts = part.Split(new char[] { ':' }); + float value; + if (parts.Length == 2 && float.TryParse(parts[1], out value)) + { + string id = parts[0].Trim(new char[] { '"' }); + if (id == "x") + { + result.x = value; + } + else if (id == "y") + { + result.y = value; + } + else if (id == "width" || id == "z") + { + result.width = value; + } + else if (id == "height" || id == "w") + { + result.height = value; + } + } + } + } + + return result; + }, + ConvertToString = delegate (object o, Type type) + { + Rect rect = (Rect)o; + return string.Format(CultureInfo.InvariantCulture, "{{ \"x\":{0}, \"y\":{1}, \"width\":{2}, \"height\":{3} }}", new object[] { rect.x, rect.y, rect.width, rect.height }); + } + }); } } } \ No newline at end of file From 1162baf58267b5276aa9d58b3b3f7cb15dbc579f Mon Sep 17 00:00:00 2001 From: ManlyMarco <39247311+ManlyMarco@users.noreply.github.com> Date: Mon, 14 Nov 2022 10:45:18 +0100 Subject: [PATCH 2/3] Refactor into methods --- BepInEx/Configuration/TomlTypeConverter.cs | 76 ++++++++++------------ 1 file changed, 36 insertions(+), 40 deletions(-) diff --git a/BepInEx/Configuration/TomlTypeConverter.cs b/BepInEx/Configuration/TomlTypeConverter.cs index 519b7ce..150603a 100644 --- a/BepInEx/Configuration/TomlTypeConverter.cs +++ b/BepInEx/Configuration/TomlTypeConverter.cs @@ -344,47 +344,43 @@ namespace BepInEx.Configuration TomlTypeConverter.AddConverter(typeof(Rect), new TypeConverter { - ConvertToObject = delegate (string s, Type type) - { - Rect result = default(Rect); - if (s != null) - { - string cleaned = s.Trim(new char[] { '{', '}' }).Replace(" ", ""); - foreach (string part in cleaned.Split(new char[] { ',' })) - { - string[] parts = part.Split(new char[] { ':' }); - float value; - if (parts.Length == 2 && float.TryParse(parts[1], out value)) - { - string id = parts[0].Trim(new char[] { '"' }); - if (id == "x") - { - result.x = value; - } - else if (id == "y") - { - result.y = value; - } - else if (id == "width" || id == "z") - { - result.width = value; - } - else if (id == "height" || id == "w") - { - result.height = value; - } - } - } - } - - return result; - }, - ConvertToString = delegate (object o, Type type) - { - Rect rect = (Rect)o; - return string.Format(CultureInfo.InvariantCulture, "{{ \"x\":{0}, \"y\":{1}, \"width\":{2}, \"height\":{3} }}", new object[] { rect.x, rect.y, rect.width, rect.height }); - } + ConvertToObject = StringToRect, + ConvertToString = RectToString }); } + + private static object StringToRect(string s, Type type) + { + Rect result = default(Rect); + + if (s == null) + return result; + + string cleaned = s.Trim('{', '}').Replace(" ", ""); + foreach (string part in cleaned.Split(',')) + { + string[] parts = part.Split(':'); + if (parts.Length != 2 || !float.TryParse(parts[1], out float value)) + continue; + + string id = parts[0].Trim('"'); + if (id == "x") + result.x = value; + else if (id == "y") + result.y = value; + else if (id == "width" || id == "z") + result.width = value; + else if (id == "height" || id == "w") + result.height = value; + } + + return result; + } + + private static string RectToString(object o, Type type) + { + Rect rect = (Rect)o; + return string.Format(CultureInfo.InvariantCulture, "{{ \"x\":{0}, \"y\":{1}, \"width\":{2}, \"height\":{3} }}", new object[] { rect.x, rect.y, rect.width, rect.height }); + } } } \ No newline at end of file From 1a0d73e1227e888dcf1b0b7abf98bf5b45da6821 Mon Sep 17 00:00:00 2001 From: ManlyMarco <39247311+ManlyMarco@users.noreply.github.com> Date: Mon, 14 Nov 2022 19:47:46 +0100 Subject: [PATCH 3/3] Update BepInEx/Configuration/TomlTypeConverter.cs Co-authored-by: Geoffrey Horsington --- BepInEx/Configuration/TomlTypeConverter.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/BepInEx/Configuration/TomlTypeConverter.cs b/BepInEx/Configuration/TomlTypeConverter.cs index 150603a..15a5cab 100644 --- a/BepInEx/Configuration/TomlTypeConverter.cs +++ b/BepInEx/Configuration/TomlTypeConverter.cs @@ -351,6 +351,8 @@ namespace BepInEx.Configuration private static object StringToRect(string s, Type type) { + // JsonUtility doesn't work with Rect on all Unity versions, so parse it manually + Rect result = default(Rect); if (s == null)