Browse Source

Add extreme fix for float<->string conversion inconsistencies, because MS designed it poorly and FJ devs didn't know that

tags/v0.0.3
NGnius (Graham) 2 years ago
parent
commit
d2dff09bc3
3 changed files with 66 additions and 3 deletions
  1. +1
    -0
      CLre/CLre.cs
  2. +64
    -2
      CLre/Fixes/FloatLanguageFix.cs
  3. +1
    -1
      CLre/Fixes/InventoryPanelScrollEngineFix.cs

+ 1
- 0
CLre/CLre.cs View File

@@ -52,6 +52,7 @@ namespace CLre
Fixes.MiniScreenHelper.Init();
Fixes.UnderStructureCollider.Init();
Fixes.TerrainModifyReset.Init();
Fixes.FloatLanguageFix.Init();
// API init
API.Synergy.ClientHandshakeEngine.Init();


+ 64
- 2
CLre/Fixes/FloatLanguageFix.cs View File

@@ -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);
}
}
}

+ 1
- 1
CLre/Fixes/InventoryPanelScrollEngineFix.cs View File

@@ -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;
}
}


Loading…
Cancel
Save