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.

91 lines
3.9KB

  1. /*using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using HarmonyLib;
  7. namespace CLre.Fixes
  8. {
  9. [Bugfix(name = "AntiStupidFloats",
  10. description = "Force language-agnostic float->string behaviour internally, like all programs should (DISABLED BY DEFAULT)",
  11. component = BugfixType.Initialiser, id = 10)]
  12. public static class FloatLanguageFix
  13. {
  14. public static bool Enabled { private set; get; } = false;
  15. public static void Init()
  16. {
  17. string[] args = Environment.GetCommandLineArgs();
  18. if (args.Contains("-ff", StringComparer.InvariantCultureIgnoreCase)
  19. || args.Contains("--float-fix", StringComparer.InvariantCultureIgnoreCase)
  20. || File.Exists("floatFix.txt")
  21. || File.Exists("whateverFloatsYourBoat"))
  22. {
  23. Enabled = true;
  24. API.Utility.Logging.LogWarning("AntiStupidFloats fix enabled, this may cause issues");
  25. API.Utility.Logging.MetaLog(args.ToString());
  26. }
  27. else
  28. {
  29. CultureInfo ci = CultureInfo.CurrentUICulture;
  30. if (ci.TwoLetterISOLanguageName.ToLower() != "en")
  31. {
  32. API.Utility.Logging.LogWarning(
  33. $"CLre detected non-English language {ci.DisplayName} ({ci.Name}/{ci.TwoLetterISOLanguageName}). " +
  34. "CardLife works best with the Windows language set to English. " +
  35. "If you run into bugs, try launching CardLife with the launch options set to \"%command% --float-fix\", without quotes. " +
  36. "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.");
  37. }
  38. }
  39. }
  40. }
  41. [Bugfix(name = "AntiStupidFloats",
  42. description = "Force language-agnostic float->string behaviour internally, like all programs should (DISABLED BY DEFAULT)",
  43. component = BugfixType.HarmonyPatch, id = 10)]
  44. [HarmonyPatch]
  45. class Float_ToString0_Patch
  46. {
  47. [HarmonyPostfix] // prefix causes a crash for some reason...
  48. public static void AfterMethodCall(ref float __instance, ref string __result)
  49. {
  50. #if DEBUG
  51. API.Utility.Logging.MetaLog("Float_ToString0_Patch");
  52. #endif
  53. if (!FloatLanguageFix.Enabled) return;
  54. API.Utility.Logging.LogWarning($"Intercepting float.ToString() to InvariantCulture equivalent\nStackTrace: {Environment.StackTrace}");
  55. __result = __instance.ToString(CultureInfo.InvariantCulture);
  56. }
  57. [HarmonyTargetMethod]
  58. public static MethodBase Target()
  59. {
  60. return AccessTools.Method(typeof(float), "ToString");
  61. }
  62. }
  63. [Bugfix(name = "AntiStupidFloats",
  64. description = "Force language-agnostic float->string behaviour internally, like all programs should (DISABLED BY DEFAULT)",
  65. component = BugfixType.HarmonyPatch, id = 10)]
  66. [HarmonyPatch]
  67. class Float_ToString1_Patch
  68. {
  69. [HarmonyPostfix]
  70. public static void AfterMethodCall(ref string format, ref float __instance, ref string __result)
  71. {
  72. #if DEBUG
  73. API.Utility.Logging.MetaLog("Float_ToString1_Patch");
  74. #endif
  75. if (!FloatLanguageFix.Enabled) return;
  76. API.Utility.Logging.LogWarning($"Intercepting float.ToString(\"{format}\") to InvariantCulture equivalent\nStackTrace: {Environment.StackTrace}");
  77. __result = __instance.ToString(format, CultureInfo.InvariantCulture);
  78. }
  79. [HarmonyTargetMethod]
  80. public static MethodBase Target()
  81. {
  82. return AccessTools.Method(typeof(float), "ToString", new[] {typeof(string)});
  83. }
  84. }
  85. }*/