From 384f1b30920894edfa9799d8dde4e91a9cc26655 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Tue, 22 Dec 2020 20:27:54 -0500 Subject: [PATCH] Test and fix enchantment table float parsing bugfix --- CLre/Fixes/EnchantmentTableFloatParseFix.cs | 29 ++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/CLre/Fixes/EnchantmentTableFloatParseFix.cs b/CLre/Fixes/EnchantmentTableFloatParseFix.cs index 857015e..aaac06a 100644 --- a/CLre/Fixes/EnchantmentTableFloatParseFix.cs +++ b/CLre/Fixes/EnchantmentTableFloatParseFix.cs @@ -14,13 +14,20 @@ namespace CLre.Fixes class Float_TryParse_Patch { [HarmonyPostfix] - public static void BeforeMethodCall(string s, ref float result, ref bool __result) + public static void AfterMethodCall(string s, ref float result, ref bool __result) { if (__result) return; +#if DEBUG + API.Utility.Logging.Log($"Replacing float.TryParse method call with invariant call. Parsing: \"{s}\""); +#endif __result = float.TryParse(s, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent, NumberFormatInfo.InvariantInfo, out result); + +#if DEBUG + API.Utility.Logging.Log($"Parsed \"{s}\" into {result}"); +#endif } [HarmonyTargetMethod] @@ -29,4 +36,24 @@ namespace CLre.Fixes return AccessTools.Method(typeof(float), "TryParse", new []{typeof(string), typeof(float).MakeByRefType()}); } } + + [HarmonyPatch(typeof(float), "Parse", new Type[] {typeof(string)})] + class Float_Parse_Patch + { + [HarmonyPrefix] + public static bool BeforeMethodCall(string s, ref float __result) + { +#if DEBUG + API.Utility.Logging.Log($"Replacing float.Parse method call with invariant call. Parsing: \"{s}\""); +#endif + bool success = float.TryParse(s, + NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | + NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent, + NumberFormatInfo.InvariantInfo, out __result); +#if DEBUG + API.Utility.Logging.Log($"Parsed \"{s}\" into {__result}"); +#endif + return !success; + } + } } \ No newline at end of file