using System; using System.Globalization; using System.IO; using System.Linq; using HarmonyLib; namespace CLre.Fixes { [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() { 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; } else { 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) or on Trello (https://trello.com/b/EGKEpfBF/cardlife-bugs), so the problem can be fixed properly."); } } } } [Bugfix(name = "AntiStupidFloats", description = "Force language-agnostic float->string behaviour internally, like all programs should (DISABLED BY DEFAULT)", component = BugfixType.HarmonyPatch, id = 10)] [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); } } }