Skip to content

Commit

Permalink
fix saving callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
ToniMacaroni committed Feb 26, 2024
1 parent 923e7b5 commit 130fac8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 38 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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**
2 changes: 0 additions & 2 deletions SonsSdk/MainInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ private static IEnumerator InitCoro()
SonsUiTools.Init();
SonsSaveTools.Init();

SdkEvents.RegisterSaveGameManager();

SdkEvents.OnSdkInitialized.Invoke();
}

Expand Down
105 changes: 70 additions & 35 deletions SonsSdk/SdkEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ internal static void Init()
RenderPipelineManager.endContextRendering += (Il2CppSystem.Action<ScriptableRenderContext, Il2CppSystem.Collections.Generic.List<Camera>>)OnEndContextRendering;

Patches.Patch();
SavingCallbackPatches.Patch();

_isInitialized = true;
}
Expand All @@ -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<bool>)OnAfterSave);
}

private static void OnUpdateInternal()
{
ModInputCache.CheckAll();
Expand Down Expand Up @@ -214,31 +205,6 @@ private static void SonsEventsOnGameStart(object o)
private static void OnEndContextRendering(ScriptableRenderContext context, Il2CppSystem.Collections.Generic.List<Camera> 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()
{
Expand Down Expand Up @@ -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));
}
}
}

0 comments on commit 130fac8

Please sign in to comment.