This commit is contained in:
2025-12-13 23:56:51 +01:00
parent 9868d30810
commit cf76acccf3

View File

@@ -1,7 +1,6 @@
using Harmony;
using KCM.LoadSaveOverrides;
using System;
using System.IO;
using UnityEngine;
namespace KCM
@@ -31,10 +30,10 @@ namespace KCM
LoadSaveLoadAtPathHook.lastLoadedPath = path;
if (!File.Exists(path))
if (!FileExists(path))
return;
LoadSaveLoadAtPathHook.saveData = File.ReadAllBytes(path);
LoadSaveLoadAtPathHook.saveData = ReadAllBytes(path);
Main.Log($"Captured save bytes from: {path} ({LoadSaveLoadAtPathHook.saveData.Length} bytes)");
}
catch (Exception ex)
@@ -43,6 +42,47 @@ namespace KCM
Main.Log(ex);
}
}
private static bool FileExists(string path)
{
object result = InvokeSystemIoFile("Exists", new Type[] { typeof(string) }, new object[] { path });
return result is bool && (bool)result;
}
private static byte[] ReadAllBytes(string path)
{
object result = InvokeSystemIoFile("ReadAllBytes", new Type[] { typeof(string) }, new object[] { path });
return result as byte[];
}
private static object InvokeSystemIoFile(string methodName, Type[] parameterTypes, object[] args)
{
// Avoid direct references to System.IO in IL (some mod loaders forbid it).
const string typeName = "System.IO.File";
Type fileType = Type.GetType(typeName) ?? Type.GetType(typeName + ", mscorlib");
if (fileType == null)
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
for (int i = 0; i < assemblies.Length && fileType == null; i++)
{
try
{
fileType = assemblies[i].GetType(typeName, false);
}
catch
{
}
}
}
if (fileType == null)
return null;
var method = fileType.GetMethod(methodName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static, null, parameterTypes, null);
if (method == null)
return null;
return method.Invoke(null, args);
}
}
}