Unofficial CardLife revival project, pronounced like "celery"
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
2.0KB

  1. using System;
  2. using System.Globalization;
  3. using System.Reflection;
  4. using HarmonyLib;
  5. namespace CLre.Fixes
  6. {
  7. public class EnchantmentTableFloatParseFix
  8. {
  9. }
  10. [HarmonyPatch]
  11. class Float_TryParse_Patch
  12. {
  13. [HarmonyPostfix]
  14. public static void AfterMethodCall(string s, ref float result, ref bool __result)
  15. {
  16. if (__result) return;
  17. #if DEBUG
  18. API.Utility.Logging.Log($"Replacing float.TryParse method call with invariant call. Parsing: \"{s}\"");
  19. #endif
  20. __result = float.TryParse(s,
  21. NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign |
  22. NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent,
  23. NumberFormatInfo.InvariantInfo, out result);
  24. #if DEBUG
  25. API.Utility.Logging.Log($"Parsed \"{s}\" into {result}");
  26. #endif
  27. }
  28. [HarmonyTargetMethod]
  29. public static MethodBase Target()
  30. {
  31. return AccessTools.Method(typeof(float), "TryParse", new []{typeof(string), typeof(float).MakeByRefType()});
  32. }
  33. }
  34. [HarmonyPatch(typeof(float), "Parse", new Type[] {typeof(string)})]
  35. class Float_Parse_Patch
  36. {
  37. [HarmonyPrefix]
  38. public static bool BeforeMethodCall(string s, ref float __result)
  39. {
  40. #if DEBUG
  41. API.Utility.Logging.Log($"Replacing float.Parse method call with invariant call. Parsing: \"{s}\"");
  42. #endif
  43. bool success = float.TryParse(s,
  44. NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign |
  45. NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent,
  46. NumberFormatInfo.InvariantInfo, out __result);
  47. #if DEBUG
  48. API.Utility.Logging.Log($"Parsed \"{s}\" into {__result}");
  49. #endif
  50. return !success;
  51. }
  52. }
  53. }