From 9868d30810d853c1f1e7744693d59d220d8ba9aa Mon Sep 17 00:00:00 2001 From: devbeni Date: Sat, 13 Dec 2025 23:55:28 +0100 Subject: [PATCH] =?UTF-8?q?na=20most=20tal=C3=A1n=20ez=20a=20geci=20mukodi?= =?UTF-8?q?k=3F=3F=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LoadSaveOverrides/LoadSaveLoadHooks.cs | 48 +++++++++++ Main.cs | 114 ++++++++++++++++++++++++- Packets/Lobby/SaveTransferPacket.cs | 6 +- ServerBrowser/ServerBrowser.cs | 2 +- 4 files changed, 164 insertions(+), 6 deletions(-) create mode 100644 LoadSaveOverrides/LoadSaveLoadHooks.cs diff --git a/LoadSaveOverrides/LoadSaveLoadHooks.cs b/LoadSaveOverrides/LoadSaveLoadHooks.cs new file mode 100644 index 0000000..34bdc69 --- /dev/null +++ b/LoadSaveOverrides/LoadSaveLoadHooks.cs @@ -0,0 +1,48 @@ +using Harmony; +using KCM.LoadSaveOverrides; +using System; +using System.IO; +using UnityEngine; + +namespace KCM +{ + public static class LoadSaveLoadAtPathHook + { + public static byte[] saveData = new byte[1]; + public static string lastLoadedPath = string.Empty; + } + + public static class LoadSaveLoadHook + { + public static byte[] saveBytes = null; + public static bool memoryStreamHook = false; + public static MultiplayerSaveContainer saveContainer = null; + } + + [HarmonyPatch(typeof(LoadSave), "LoadAtPath")] + public class LoadSaveLoadAtPathCaptureHook + { + public static void Prefix(string path) + { + try + { + if (string.IsNullOrEmpty(path)) + return; + + LoadSaveLoadAtPathHook.lastLoadedPath = path; + + if (!File.Exists(path)) + return; + + LoadSaveLoadAtPathHook.saveData = File.ReadAllBytes(path); + Main.Log($"Captured save bytes from: {path} ({LoadSaveLoadAtPathHook.saveData.Length} bytes)"); + } + catch (Exception ex) + { + Main.Log("Failed capturing save bytes from LoadSave.LoadAtPath"); + Main.Log(ex); + } + } + } +} + diff --git a/Main.cs b/Main.cs index 5664c52..79a9ef7 100644 --- a/Main.cs +++ b/Main.cs @@ -9,11 +9,11 @@ using KCM.Packets.State; using KCM.StateManagement.Observers; using KCM.StateManagement.Sync; using KCM.StateManagement.BuildingState; +using KCM.Enums; using Riptide; using Steamworks; using System; using System.Collections.Generic; -using System.Diagnostics; using System.Linq; using System.Reflection; using System.Text; @@ -74,7 +74,7 @@ namespace KCM if (helper != null) helper.Log(message ?? string.Empty); else - Debug.Log(message ?? string.Empty); + UnityEngine.Debug.Log(message ?? string.Empty); } public static void Log(Exception ex) @@ -104,6 +104,116 @@ namespace KCM UnityEngine.SceneManagement.SceneManager.LoadScene(scene); } + public static void TransitionTo(MenuState menuState) + { + try + { + if (GameState.inst != null && GameState.inst.mainMenuMode != null) + { + GameState.inst.SetNewMode(GameState.inst.mainMenuMode); + SetMainMenuState(GameState.inst.mainMenuMode, (int)menuState); + return; + } + } + catch (Exception ex) + { + Log(ex); + } + + TransitionTo(menuState.ToString()); + } + + private static void SetMainMenuState(object mainMenuMode, int menuStateValue) + { + if (mainMenuMode == null) + return; + + Type type = mainMenuMode.GetType(); + const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; + + string[] methodNames = new string[] + { + "SetMenuState", + "SetState", + "GoToState", + "GotoState", + "TransitionToState", + "TransitionTo", + }; + + MethodInfo[] methods = type.GetMethods(flags); + for (int i = 0; i < methodNames.Length; i++) + { + string name = methodNames[i]; + for (int j = 0; j < methods.Length; j++) + { + MethodInfo method = methods[j]; + if (method == null || method.Name != name) + continue; + + ParameterInfo[] parameters = method.GetParameters(); + if (parameters == null || parameters.Length != 1) + continue; + + Type paramType = parameters[0].ParameterType; + if (paramType == typeof(int)) + { + method.Invoke(mainMenuMode, new object[] { menuStateValue }); + return; + } + + if (paramType != null && paramType.IsEnum) + { + object enumValue = Enum.ToObject(paramType, menuStateValue); + method.Invoke(mainMenuMode, new object[] { enumValue }); + return; + } + } + } + + FieldInfo[] fields = type.GetFields(flags); + for (int i = 0; i < fields.Length; i++) + { + FieldInfo field = fields[i]; + if (field == null) + continue; + + if (field.FieldType == typeof(int)) + { + field.SetValue(mainMenuMode, menuStateValue); + return; + } + + if (field.FieldType != null && field.FieldType.IsEnum) + { + field.SetValue(mainMenuMode, Enum.ToObject(field.FieldType, menuStateValue)); + return; + } + } + + PropertyInfo[] properties = type.GetProperties(flags); + for (int i = 0; i < properties.Length; i++) + { + PropertyInfo prop = properties[i]; + if (prop == null || !prop.CanWrite) + continue; + + if (prop.PropertyType == typeof(int)) + { + prop.SetValue(mainMenuMode, menuStateValue, null); + return; + } + + if (prop.PropertyType != null && prop.PropertyType.IsEnum) + { + prop.SetValue(mainMenuMode, Enum.ToObject(prop.PropertyType, menuStateValue), null); + return; + } + } + + Log($"TransitionTo(MenuState) could not set state value={menuStateValue} on {type.FullName}"); + } + public static void ResetMultiplayerState() { // Reset multiplayer state diff --git a/Packets/Lobby/SaveTransferPacket.cs b/Packets/Lobby/SaveTransferPacket.cs index d3e1374..e941e96 100644 --- a/Packets/Lobby/SaveTransferPacket.cs +++ b/Packets/Lobby/SaveTransferPacket.cs @@ -103,15 +103,15 @@ namespace KCM.Packets.Lobby // Handle completed transfer here Main.helper.Log("Save Transfer complete!"); - LoadSaveLoadHook.saveBytes = saveData; - LoadSaveLoadHook.memoryStreamHook = true; + KCM.LoadSaveLoadHook.saveBytes = saveData; + KCM.LoadSaveLoadHook.memoryStreamHook = true; LoadSave.Load(); try { Main.SetMultiplayerSaveLoadInProgress(true); - LoadSaveLoadHook.saveContainer.Unpack(null); + KCM.LoadSaveLoadHook.saveContainer.Unpack(null); } finally { diff --git a/ServerBrowser/ServerBrowser.cs b/ServerBrowser/ServerBrowser.cs index f37ebd8..95bafcd 100644 --- a/ServerBrowser/ServerBrowser.cs +++ b/ServerBrowser/ServerBrowser.cs @@ -400,7 +400,7 @@ namespace KCM SfxSystem.PlayUiSelect(); - Main.TransitionTo(MenuState.Menu); + Main.TransitionTo(KCM.Enums.MenuState.Menu); });