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
3.1KB

  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Linq;
  5. using HarmonyLib;
  6. namespace CLre.Fixes
  7. {
  8. [Bugfix(name = "AntiStupidFloats",
  9. description = "Force language-agnostic float->string behaviour internally, like all programs should (DISABLED BY DEFAULT)",
  10. component = BugfixType.Initialiser, id = 10)]
  11. public static class FloatLanguageFix
  12. {
  13. public static bool Enabled { private set; get; } = false;
  14. public static void Init()
  15. {
  16. CultureInfo ci = CultureInfo.CurrentUICulture;
  17. if (ci.TwoLetterISOLanguageName.ToLower() != "en")
  18. {
  19. API.Utility.Logging.LogWarning($"CLre detected non-English language {ci.DisplayName} ({ci.Name}/{ci.TwoLetterISOLanguageName}). " +
  20. "CardLife works best with the Windows language set to English. " +
  21. "If you run into bugs, try launching CardLife with the launch options set to \"%command% --float-fix\", without quotes. " +
  22. "Please also report the issue to NGnius (ngniusness@gmail.com or NGnius#0864 on the CL Discord server), so he can fix the problem properly.");
  23. }
  24. string[] args = Environment.GetCommandLineArgs();
  25. if (args.Contains("-ff", StringComparer.InvariantCultureIgnoreCase)
  26. || args.Contains("--float-fix", StringComparer.InvariantCultureIgnoreCase)
  27. || File.Exists("floatFix.txt")
  28. || File.Exists("whateverFloatsYourBoat"))
  29. {
  30. Enabled = true;
  31. }
  32. }
  33. }
  34. [Bugfix(name = "EnchantmentTableFloatParseFix",
  35. description = "Make all float parsing culture-invariant",
  36. component = BugfixType.HarmonyPatch, id = 1)]
  37. [HarmonyPatch(typeof(float), "ToString", new Type[] { })]
  38. class Float_ToString0_Patch
  39. {
  40. [HarmonyPostfix] // prefix causes a crash for some reason...
  41. public static void AfterMethodCall(float __instance, ref string __result)
  42. {
  43. if (!FloatLanguageFix.Enabled) return;
  44. API.Utility.Logging.LogWarning($"Intercepting float.ToString() to InvariantCulture equivalent\nStackTrace: {Environment.StackTrace}");
  45. __result = __instance.ToString(CultureInfo.InvariantCulture);
  46. }
  47. }
  48. [Bugfix(name = "EnchantmentTableFloatParseFix",
  49. description = "Make all float parsing culture-invariant",
  50. component = BugfixType.HarmonyPatch, id = 1)]
  51. [HarmonyPatch(typeof(float), "ToString", new Type[] { typeof(string)})]
  52. class Float_ToString1_Patch
  53. {
  54. [HarmonyPostfix]
  55. public static void AfterMethodCall(string format, float __instance, ref string __result)
  56. {
  57. if (!FloatLanguageFix.Enabled) return;
  58. API.Utility.Logging.LogWarning($"Intercepting float.ToString(\"{format}\") to InvariantCulture equivalent\nStackTrace: {Environment.StackTrace}");
  59. __result = __instance.ToString(format, CultureInfo.InvariantCulture);
  60. }
  61. }
  62. }