Fix some more float parsing bugs

This commit is contained in:
NGnius (Graham) 2021-09-01 00:31:13 -04:00
parent 29ac927b95
commit 175a569ea4
6 changed files with 108 additions and 16 deletions

View file

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

View file

@ -30,7 +30,7 @@ namespace CLre.Fixes
NumberFormatInfo.InvariantInfo, out result); NumberFormatInfo.InvariantInfo, out result);
#if DEBUG #if DEBUG
API.Utility.Logging.Log($"Parsed \"{s}\" into {result}"); API.Utility.Logging.Log($"Parsed \"{s}\" into {result} (successfully? {__result})\n{Environment.StackTrace}");
#endif #endif
} }

View file

@ -1,7 +1,8 @@
using System; /*using System;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection;
using HarmonyLib; using HarmonyLib;
namespace CLre.Fixes namespace CLre.Fixes
@ -22,6 +23,8 @@ namespace CLre.Fixes
|| File.Exists("whateverFloatsYourBoat")) || File.Exists("whateverFloatsYourBoat"))
{ {
Enabled = true; Enabled = true;
API.Utility.Logging.LogWarning("AntiStupidFloats fix enabled, this may cause issues");
API.Utility.Logging.MetaLog(args.ToString());
} }
else else
{ {
@ -41,30 +44,48 @@ namespace CLre.Fixes
[Bugfix(name = "AntiStupidFloats", [Bugfix(name = "AntiStupidFloats",
description = "Force language-agnostic float->string behaviour internally, like all programs should (DISABLED BY DEFAULT)", description = "Force language-agnostic float->string behaviour internally, like all programs should (DISABLED BY DEFAULT)",
component = BugfixType.HarmonyPatch, id = 10)] component = BugfixType.HarmonyPatch, id = 10)]
[HarmonyPatch(typeof(float), "ToString", new Type[] { })] [HarmonyPatch]
class Float_ToString0_Patch class Float_ToString0_Patch
{ {
[HarmonyPostfix] // prefix causes a crash for some reason... [HarmonyPostfix] // prefix causes a crash for some reason...
public static void AfterMethodCall(float __instance, ref string __result) public static void AfterMethodCall(ref float __instance, ref string __result)
{ {
#if DEBUG
API.Utility.Logging.MetaLog("Float_ToString0_Patch");
#endif
if (!FloatLanguageFix.Enabled) return; if (!FloatLanguageFix.Enabled) return;
API.Utility.Logging.LogWarning($"Intercepting float.ToString() to InvariantCulture equivalent\nStackTrace: {Environment.StackTrace}"); API.Utility.Logging.LogWarning($"Intercepting float.ToString() to InvariantCulture equivalent\nStackTrace: {Environment.StackTrace}");
__result = __instance.ToString(CultureInfo.InvariantCulture); __result = __instance.ToString(CultureInfo.InvariantCulture);
} }
[HarmonyTargetMethod]
public static MethodBase Target()
{
return AccessTools.Method(typeof(float), "ToString");
}
} }
[Bugfix(name = "EnchantmentTableFloatParseFix", [Bugfix(name = "AntiStupidFloats",
description = "Make all float parsing culture-invariant", description = "Force language-agnostic float->string behaviour internally, like all programs should (DISABLED BY DEFAULT)",
component = BugfixType.HarmonyPatch, id = 1)] component = BugfixType.HarmonyPatch, id = 10)]
[HarmonyPatch(typeof(float), "ToString", new Type[] { typeof(string)})] [HarmonyPatch]
class Float_ToString1_Patch class Float_ToString1_Patch
{ {
[HarmonyPostfix] [HarmonyPostfix]
public static void AfterMethodCall(string format, float __instance, ref string __result) public static void AfterMethodCall(ref string format, ref float __instance, ref string __result)
{ {
#if DEBUG
API.Utility.Logging.MetaLog("Float_ToString1_Patch");
#endif
if (!FloatLanguageFix.Enabled) return; if (!FloatLanguageFix.Enabled) return;
API.Utility.Logging.LogWarning($"Intercepting float.ToString(\"{format}\") to InvariantCulture equivalent\nStackTrace: {Environment.StackTrace}"); API.Utility.Logging.LogWarning($"Intercepting float.ToString(\"{format}\") to InvariantCulture equivalent\nStackTrace: {Environment.StackTrace}");
__result = __instance.ToString(format, CultureInfo.InvariantCulture); __result = __instance.ToString(format, CultureInfo.InvariantCulture);
} }
[HarmonyTargetMethod]
public static MethodBase Target()
{
return AccessTools.Method(typeof(float), "ToString", new[] {typeof(string)});
}
} }
} }*/

View file

@ -22,7 +22,7 @@ namespace CLre.Fixes
CustomLoggerThread_CreateGameObject_Patch.allowed = true; CustomLoggerThread_CreateGameObject_Patch.allowed = true;
CustomLoggerThread.CreateGameObject(); CustomLoggerThread.CreateGameObject();
CustomLoggerThread_CreateGameObject_Patch.allowed = false; CustomLoggerThread_CreateGameObject_Patch.allowed = false;
API.Utility.Logging.Log($"Completed early log init"); API.Utility.Logging.Log("Completed early log init");
//System.IO.File.WriteAllText("InitLogSooner.log", $"Done at " + System.DateTime.Now.ToString()); //System.IO.File.WriteAllText("InitLogSooner.log", $"Done at " + System.DateTime.Now.ToString());
} }
catch (Exception e) catch (Exception e)
@ -65,10 +65,12 @@ namespace CLre.Fixes
FieldInfo quitThreadField = AccessTools.Field(typeof(CustomLoggerThread), "_quitThread"); FieldInfo quitThreadField = AccessTools.Field(typeof(CustomLoggerThread), "_quitThread");
MethodInfo flushLoggerMethod = AccessTools.Method(typeof(CustomLoggerThread), "FlushLogger"); MethodInfo flushLoggerMethod = AccessTools.Method(typeof(CustomLoggerThread), "FlushLogger");
Flusher flushLogger = (Flusher) Delegate.CreateDelegate(typeof(Action), null, flushLoggerMethod); Flusher flushLogger = API.Utility.Reflection.BuildDelegate<Flusher>(flushLoggerMethod, null);
//Flusher flushLogger = (Flusher) Delegate.CreateDelegate(typeof(Action), null, flushLoggerMethod);
MethodInfo forceFlushMethod = AccessTools.Method("CustomLogger:ForceFlush"); MethodInfo forceFlushMethod = AccessTools.Method("CustomLogger:ForceFlush");
Flusher forceFlush = (Flusher) Delegate.CreateDelegate(typeof(Action), null, forceFlushMethod); Flusher forceFlush = API.Utility.Reflection.BuildDelegate<Flusher>(forceFlushMethod, null);
//Flusher forceFlush = (Flusher) Delegate.CreateDelegate(typeof(Action), null, forceFlushMethod);
Thread.MemoryBarrier(); Thread.MemoryBarrier();
IsLogStarted = true; IsLogStarted = true;

View file

@ -0,0 +1,69 @@
using System;
using System.Reflection;
using HarmonyLib;
using SQL;
using Svelto.DataStructures;
namespace CLre.Fixes
{
public class OfflineGameTimeSavingFloatFix
{
}
[Bugfix(name = "OfflineGameTimeSavingFloatFix",
description = "Make game time save properly for everyone, even when floats contain a comma",
component = BugfixType.HarmonyPatch, id = 11)]
[HarmonyPatch]
class OfflineSaveServerGameTimeRequest_DoRequest_Patch
{
private const string QUERY = "INSERT OR REPLACE INTO gametime (gameId, gameTime) VALUES (@GameId, @GameTime)";
[HarmonyPrefix]
public static bool BeforeMethodCall(object __instance, ref object ____dependency)
{
//API.Utility.Logging.Log("Intercepting OfflineSaveServerGameTimeRequest.DoRequest");
//if (____dependency == null) return true;
#if DEBUG
API.Utility.Logging.Log(
"Replacing OfflineSaveServerGameTimeRequest.DoRequest SQL query with safer alternative");
#endif
// TODO optimise
ISQL sql = Traverse.Create(__instance).Property<ISQL>("sql").Value;
Traverse dep = Traverse.Create(____dependency);
// populate params
FasterList<SQLParam> sqlParams = new FasterList<SQLParam>();
sqlParams.Add(new SQLParam("@GameId", dep.Field<long>("gameId").Value));
sqlParams.Add(new SQLParam("@GameTime", dep.Field<double>("gameTime").Value));
// actually perform query
sql.ExecuteSaveQuery(QUERY, OnComplete, OnError, sqlParams);
#if DEBUG
API.Utility.Logging.Log("Executed corrected game time saving SQL query");
#endif
return false;
}
private static void OnComplete(int numberRowsEdited)
{
#if DEBUG
API.Utility.Logging.Log(
$"Completed OfflineSaveServerGameTimeRequest_DoRequest_Patch SQL Query ({numberRowsEdited} rows)");
#endif
}
private static void OnError(Exception e)
{
#if DEBUG
API.Utility.Logging.LogError(
$"Error in OfflineSaveServerGameTimeRequest_DoRequest_Patch SQL Query: {e}\n{e.StackTrace}");
#endif
}
[HarmonyTargetMethod]
public static MethodBase Target()
{
return AccessTools.Method(
"Requests.ServerSaving.ServerLoginTime.OfflineSaveServerGameTimeRequest:DoRequest");
}
}
}

View file

@ -23,7 +23,7 @@ namespace CLre.Fixes
[HarmonyPrefix] [HarmonyPrefix]
public static bool BeforeMethodCall(object __instance, object ____dependency) public static bool BeforeMethodCall(object __instance, object ____dependency)
{ {
API.Utility.Logging.Log("Intercepting OfflineSavePlayerSpawnPointRequest.DoRequest"); //API.Utility.Logging.Log("Intercepting OfflineSavePlayerSpawnPointRequest.DoRequest");
if (____dependency == null) return true; if (____dependency == null) return true;
#if DEBUG #if DEBUG
API.Utility.Logging.Log("Replacing OfflineSavePlayerSpawnPointRequest.DoRequest SQL squery with safer alternative"); API.Utility.Logging.Log("Replacing OfflineSavePlayerSpawnPointRequest.DoRequest SQL squery with safer alternative");