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.

70 lines
3.2KB

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