From cf76acccf3de26bfda180e248296094e7b5cd1e6 Mon Sep 17 00:00:00 2001 From: devbeni Date: Sat, 13 Dec 2025 23:56:51 +0100 Subject: [PATCH] =?UTF-8?q?tal=C3=A1n=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LoadSaveOverrides/LoadSaveLoadHooks.cs | 48 +++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/LoadSaveOverrides/LoadSaveLoadHooks.cs b/LoadSaveOverrides/LoadSaveLoadHooks.cs index 34bdc69..0cbfb2d 100644 --- a/LoadSaveOverrides/LoadSaveLoadHooks.cs +++ b/LoadSaveOverrides/LoadSaveLoadHooks.cs @@ -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); + } } } -