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.

66 lines
2.4KB

  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. [Bugfix(name = "EnchantmentTableFloatParseFix",
  11. description = "Make all float parsing culture-invariant",
  12. more = "https://trello.com/c/qawBFb7P/1-enchantment-table-fails-to-work",
  13. component = BugfixType.HarmonyPatch, id = 1)]
  14. [HarmonyPatch]
  15. class Float_TryParse_Patch
  16. {
  17. [HarmonyPostfix]
  18. public static void AfterMethodCall(string s, ref float result, ref bool __result)
  19. {
  20. if (__result) return;
  21. #if DEBUG
  22. API.Utility.Logging.Log($"Replacing float.TryParse method call with invariant call. Parsing: \"{s}\"");
  23. #endif
  24. __result = float.TryParse(s,
  25. NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign |
  26. NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent,
  27. NumberFormatInfo.InvariantInfo, out result);
  28. #if DEBUG
  29. API.Utility.Logging.Log($"Parsed \"{s}\" into {result} (successfully? {__result})\n{Environment.StackTrace}");
  30. #endif
  31. }
  32. [HarmonyTargetMethod]
  33. public static MethodBase Target()
  34. {
  35. return AccessTools.Method(typeof(float), "TryParse", new []{typeof(string), typeof(float).MakeByRefType()});
  36. }
  37. }
  38. [Bugfix(name = "EnchantmentTableFloatParseFix",
  39. description = "Make all float parsing culture-invariant",
  40. component = BugfixType.HarmonyPatch, id = 1)]
  41. [HarmonyPatch(typeof(float), "Parse", new Type[] {typeof(string)})]
  42. class Float_Parse_Patch
  43. {
  44. [HarmonyPrefix]
  45. public static bool BeforeMethodCall(string s, ref float __result)
  46. {
  47. #if DEBUG
  48. API.Utility.Logging.Log($"Replacing float.Parse method call with invariant call. Parsing: \"{s}\"");
  49. #endif
  50. bool success = float.TryParse(s,
  51. NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign |
  52. NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent,
  53. NumberFormatInfo.InvariantInfo, out __result);
  54. #if DEBUG
  55. API.Utility.Logging.Log($"Parsed \"{s}\" into {__result} (successfully? {success})");
  56. #endif
  57. return !success;
  58. }
  59. }
  60. }