From 130fac884678889f1608d5d0706d38c29cb6ac90 Mon Sep 17 00:00:00 2001 From: Toni Macaroni <62687014+ToniMacaroni@users.noreply.github.com> Date: Tue, 27 Feb 2024 00:06:58 +0100 Subject: [PATCH] fix saving callbacks --- CHANGELOG.md | 2 +- SonsSdk/MainInitializer.cs | 2 - SonsSdk/SdkEvents.cs | 105 ++++++++++++++++++++++++------------- 3 files changed, 71 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0eb4b2..ab28a89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ - Updated for SOTF 1.0 - Option to instantly open the inventory without animations. - Temporarily removed hooks. -- Readded the `goto` command. +- Readded the `goto` and `addcharacter` command. **Experimental version. Please use with caution** \ No newline at end of file diff --git a/SonsSdk/MainInitializer.cs b/SonsSdk/MainInitializer.cs index 489faf4..4fcf204 100644 --- a/SonsSdk/MainInitializer.cs +++ b/SonsSdk/MainInitializer.cs @@ -62,8 +62,6 @@ private static IEnumerator InitCoro() SonsUiTools.Init(); SonsSaveTools.Init(); - SdkEvents.RegisterSaveGameManager(); - SdkEvents.OnSdkInitialized.Invoke(); } diff --git a/SonsSdk/SdkEvents.cs b/SonsSdk/SdkEvents.cs index 99499cd..b8c3fd6 100644 --- a/SonsSdk/SdkEvents.cs +++ b/SonsSdk/SdkEvents.cs @@ -125,6 +125,7 @@ internal static void Init() RenderPipelineManager.endContextRendering += (Il2CppSystem.Action>)OnEndContextRendering; Patches.Patch(); + SavingCallbackPatches.Patch(); _isInitialized = true; } @@ -148,16 +149,6 @@ private static void OnSceneWasInitialized(int sceneIdx, string sceneName) } } - internal static void RegisterSaveGameManager() - { - //TODO: 1.0 Update, why are they broken??? - - //SaveGameManager.RegisterBeforeLoadCallback((Action)OnBeforeLoadSave); - //SaveGameManager.RegisterAfterLoadCallback((Action)OnAfterLoadSave); - //SaveGameManager.RegisterBeforeSaveCallback((Action)OnBeforeSave); - //SaveGameManager.RegisterAfterSaveCallback((Action)OnAfterSave); - } - private static void OnUpdateInternal() { ModInputCache.CheckAll(); @@ -214,31 +205,6 @@ private static void SonsEventsOnGameStart(object o) private static void OnEndContextRendering(ScriptableRenderContext context, Il2CppSystem.Collections.Generic.List cameras){ OnCameraRender.Invoke(context, cameras); } - - private static void OnBeforeSave() - { - RLog.Debug($"ON_BEFORE_SAVE_LOADING"); - BeforeSaveLoading.Invoke(); - } - - private static void OnAfterSave(bool savePlayerOnly) - { - RLog.Debug($"ON_AFTER_SAVE_LOADING"); - AfterSaveLoading.Invoke(savePlayerOnly); - } - - private static void OnBeforeLoadSave() - { - RLog.Debug($"ON_BEFORE_LOAD_SAVE"); - BeforeLoadSave.Invoke(); - GameState.LastLoadedSaveId = GameSetupManager.GetSelectedSaveId(); - } - - private static void OnAfterLoadSave() - { - RLog.Debug($"ON_AFTER_LOAD_SAVE"); - AfterLoadSave.Invoke(); - } private static void OnGameActivation() { @@ -277,4 +243,73 @@ private static void RemoveWorldSimActor(WorldSimActor removeActor) OnWorldSimActorRemoved.Invoke(removeActor); } } + + public class SavingCallbackPatches + { + [HarmonyPatch(typeof(SaveGameManager), nameof(SaveGameManager.Load), typeof(string), typeof(SaveGameType))] + [HarmonyPrefix] + public static void BeforeLoad(string dir, SaveGameType saveGameType) + { + if (!SaveGameManager.HasInstance) + { + RLog.Error("SaveGameManager not initialized, aborting load callback."); + return; + } + + RLog.Msg($"Loading savegame from {dir} of type {saveGameType}"); + + GameState.LastLoadedSaveId = GameSetupManager.GetSelectedSaveId(); + BeforeLoadSave.Invoke(); + } + + [HarmonyPatch(typeof(SaveGameManager), nameof(SaveGameManager.Load), typeof(string), typeof(SaveGameType))] + [HarmonyPostfix] + public static void AfterLoad(string dir, SaveGameType saveGameType) + { + if (!SaveGameManager.HasInstance) + { + RLog.Error("SaveGameManager not initialized, aborting load callback."); + return; + } + + RLog.Msg($"AfterLoad"); + + AfterLoadSave.Invoke(); + } + + [HarmonyPatch(typeof(SaveGameManager), nameof(SaveGameManager.Save), typeof(string), typeof(string), typeof(bool))] + [HarmonyPrefix] + public static void BeforeSave(string dir, string gameName, bool savePlayerOnly) + { + if (!SaveGameManager.HasInstance) + { + RLog.Error("SaveGameManager not initialized, aborting save callback."); + return; + } + + RLog.Msg($"Saving savegame to {dir} with name {gameName} (SavePlayerOnly:{savePlayerOnly})"); + + BeforeSaveLoading.Invoke(); + } + + [HarmonyPatch(typeof(SaveGameManager), nameof(SaveGameManager.Save), typeof(string), typeof(string), typeof(bool))] + [HarmonyPrefix] + public static void AfterSave(string dir, string gameName, bool savePlayerOnly) + { + if (!SaveGameManager.HasInstance) + { + RLog.Error("SaveGameManager not initialized, aborting save callback."); + return; + } + + RLog.Msg($"AfterSave"); + + AfterSaveLoading.Invoke(savePlayerOnly); + } + + public static void Patch() + { + Core.HarmonyInstance.PatchAll(typeof(SavingCallbackPatches)); + } + } } \ No newline at end of file