using System; using System.Globalization; using System.Reflection; using HarmonyLib; namespace CLre.Fixes { public class EnchantmentTableFloatParseFix { } [Bugfix(name = "EnchantmentTableFloatParseFix", description = "Make all float parsing culture-invariant", more = "https://trello.com/c/qawBFb7P/1-enchantment-table-fails-to-work", component = BugfixType.HarmonyPatch, id = 1)] [HarmonyPatch] class Float_TryParse_Patch { [HarmonyPostfix] 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} (successfully? {__result})\n{Environment.StackTrace}"); #endif } [HarmonyTargetMethod] public static MethodBase Target() { return AccessTools.Method(typeof(float), "TryParse", new []{typeof(string), typeof(float).MakeByRefType()}); } } [Bugfix(name = "EnchantmentTableFloatParseFix", description = "Make all float parsing culture-invariant", component = BugfixType.HarmonyPatch, id = 1)] [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} (successfully? {success})"); #endif return !success; } } }