From d2dff09bc391fd41651eb590ec69fe1fd0073c19 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Sun, 29 Aug 2021 17:16:05 -0400 Subject: [PATCH] Add extreme fix for float<->string conversion inconsistencies, because MS designed it poorly and FJ devs didn't know that --- CLre/CLre.cs | 1 + CLre/Fixes/FloatLanguageFix.cs | 66 ++++++++++++++++++++- CLre/Fixes/InventoryPanelScrollEngineFix.cs | 2 +- 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/CLre/CLre.cs b/CLre/CLre.cs index 582e742..5aede5f 100644 --- a/CLre/CLre.cs +++ b/CLre/CLre.cs @@ -52,6 +52,7 @@ namespace CLre Fixes.MiniScreenHelper.Init(); Fixes.UnderStructureCollider.Init(); Fixes.TerrainModifyReset.Init(); + Fixes.FloatLanguageFix.Init(); // API init API.Synergy.ClientHandshakeEngine.Init(); diff --git a/CLre/Fixes/FloatLanguageFix.cs b/CLre/Fixes/FloatLanguageFix.cs index 9b781fd..8e3165a 100644 --- a/CLre/Fixes/FloatLanguageFix.cs +++ b/CLre/Fixes/FloatLanguageFix.cs @@ -1,4 +1,66 @@ -$HEADER$namespace $NAMESPACE$ +using System; +using System.Globalization; +using System.IO; +using System.Linq; +using HarmonyLib; + +namespace CLre.Fixes { - public class $CLASS$ {$END$} + [Bugfix(name = "AntiStupidFloats", + description = "Force language-agnostic float->string behaviour internally, like all programs should (DISABLED BY DEFAULT)", + component = BugfixType.Initialiser, id = 10)] + public static class FloatLanguageFix + { + public static bool Enabled { private set; get; } = false; + + public static void Init() + { + CultureInfo ci = CultureInfo.CurrentUICulture; + if (ci.TwoLetterISOLanguageName.ToLower() != "en") + { + API.Utility.Logging.LogWarning($"CLre detected non-English language {ci.DisplayName} ({ci.Name}/{ci.TwoLetterISOLanguageName}). " + + "CardLife works best with the Windows language set to English. " + + "If you run into bugs, try launching CardLife with the launch options set to \"%command% --float-fix\", without quotes. " + + "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."); + } + string[] args = Environment.GetCommandLineArgs(); + if (args.Contains("-ff", StringComparer.InvariantCultureIgnoreCase) + || args.Contains("--float-fix", StringComparer.InvariantCultureIgnoreCase) + || File.Exists("floatFix.txt") + || File.Exists("whateverFloatsYourBoat")) + { + Enabled = true; + } + } + } + + [Bugfix(name = "EnchantmentTableFloatParseFix", + description = "Make all float parsing culture-invariant", + component = BugfixType.HarmonyPatch, id = 1)] + [HarmonyPatch(typeof(float), "ToString", new Type[] { })] + class Float_ToString0_Patch + { + [HarmonyPostfix] // prefix causes a crash for some reason... + public static void AfterMethodCall(float __instance, ref string __result) + { + if (!FloatLanguageFix.Enabled) return; + API.Utility.Logging.LogWarning($"Intercepting float.ToString() to InvariantCulture equivalent\nStackTrace: {Environment.StackTrace}"); + __result = __instance.ToString(CultureInfo.InvariantCulture); + } + } + + [Bugfix(name = "EnchantmentTableFloatParseFix", + description = "Make all float parsing culture-invariant", + component = BugfixType.HarmonyPatch, id = 1)] + [HarmonyPatch(typeof(float), "ToString", new Type[] { typeof(string)})] + class Float_ToString1_Patch + { + [HarmonyPostfix] + public static void AfterMethodCall(string format, float __instance, ref string __result) + { + if (!FloatLanguageFix.Enabled) return; + API.Utility.Logging.LogWarning($"Intercepting float.ToString(\"{format}\") to InvariantCulture equivalent\nStackTrace: {Environment.StackTrace}"); + __result = __instance.ToString(format, CultureInfo.InvariantCulture); + } + } } \ No newline at end of file diff --git a/CLre/Fixes/InventoryPanelScrollEngineFix.cs b/CLre/Fixes/InventoryPanelScrollEngineFix.cs index ed8db71..b3744f1 100644 --- a/CLre/Fixes/InventoryPanelScrollEngineFix.cs +++ b/CLre/Fixes/InventoryPanelScrollEngineFix.cs @@ -32,7 +32,7 @@ namespace CLre.Fixes public static MethodBase Target() { MethodInfo methodtopatch = AccessTools.Method("Game.UI.InventoryPanelScrollEngine:ScrollPanelByMouseWheel"); - if (null == methodtopatch) API.Utility.Logging.MetaLog("Intercepting Game.UI.InventoryPanelScrollEngine:ScrollPanelByMouseWheel() failed"); + //if (null == methodtopatch) API.Utility.Logging.MetaLog("Intercepting Game.UI.InventoryPanelScrollEngine:ScrollPanelByMouseWheel() failed"); return methodtopatch; } }