Skip to content

Commit

Permalink
Merge branch 'master' into docs
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeTwentyThree committed Jul 9, 2023
2 parents 2815b40 + 4c4cded commit 307debf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Nautilus/Handlers/SaveDataHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Nautilus.Json;
using Nautilus.Json;
using Nautilus.Utility;

namespace Nautilus.Handlers;
Expand All @@ -18,7 +18,7 @@ public static class SaveDataHandler
{
T cache = new();

SaveUtils.RegisterOnLoadEvent(() => cache.Load());
SaveUtils.RegisterOnStartLoadingEvent(() => cache.Load());
SaveUtils.RegisterOnSaveEvent(() => cache.Save());

return cache;
Expand Down
7 changes: 5 additions & 2 deletions Nautilus/Patchers/SaveUtilsPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ internal class SaveUtilsPatcher
private static readonly List<Action> oneTimeUseOnQuitEvents = new();

internal static Action OnSaveEvents;
internal static Action OnLoadEvents;
internal static Action OnFinishLoadingEvents;
internal static Action OnStartLoadingEvents;
internal static Action OnQuitEvents;

public static void Patch(Harmony harmony)
Expand Down Expand Up @@ -57,12 +58,14 @@ internal static void InvokeSaveEvents()

internal static IEnumerator InvokeLoadEvents(IEnumerator enumerator)
{
OnStartLoadingEvents?.Invoke();

while (enumerator.MoveNext())
{
yield return enumerator.Current;
}

OnLoadEvents?.Invoke();
OnFinishLoadingEvents?.Invoke();

if (oneTimeUseOnLoadEvents.Count > 0)
{
Expand Down
34 changes: 27 additions & 7 deletions Nautilus/Utility/SaveUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Nautilus.Patchers;

namespace Nautilus.Utility;
Expand Down Expand Up @@ -26,11 +26,21 @@ public static void RegisterOnSaveEvent(Action onSaveAction)

/// <summary>
/// Registers a simple <see cref="Action"/> method to invoke the <c>first time</c> the player loads a saved game via the in game menu.
/// This is only invoked after the game (including most objects around the player) has FULLY loaded. For an earlier alternative, see <see cref="RegisterOnStartLoadingEvent"/>.
/// </summary>
/// <param name="onLoadAction">The method to invoke. This action will not be invoked a second time.</param>
public static void RegisterOnLoadEvent(Action onLoadAction)
/// <param name="onFinishLoadingAction">The method to invoke. This action will not be invoked a second time.</param>
public static void RegisterOnFinishLoadingEvent(Action onFinishLoadingAction)
{
SaveUtilsPatcher.OnLoadEvents += onLoadAction;
SaveUtilsPatcher.OnFinishLoadingEvents += onFinishLoadingAction;
}

/// <summary>
/// Registers a simple <see cref="Action"/> method to invoke immediately after the <c>first time</c> the player loads a saved game via the in game menu.
/// </summary>
/// <param name="onStartLoadingAction">The method to invoke. This action will not be invoked a second time.</param>
public static void RegisterOnStartLoadingEvent(Action onStartLoadingAction)
{
SaveUtilsPatcher.OnStartLoadingEvents += onStartLoadingAction;
}

/// <summary>
Expand All @@ -53,13 +63,23 @@ public static void UnregisterOnSaveEvent(Action onSaveAction)
}

/// <summary>
/// Removes a method previously added through <see cref="RegisterOnLoadEvent(Action)"/> so it is no longer invoked when loading the game.<para/>
/// Removes a method previously added through <see cref="RegisterOnFinishLoadingEvent(Action)"/> so it is no longer invoked when loading the game.<para/>
/// If you plan on using this, do not register an anonymous method.
/// </summary>
/// <param name="onLoadAction">The method invoked.</param>
public static void UnregisterOnFinishLoadingEvent(Action onLoadAction)
{
SaveUtilsPatcher.OnFinishLoadingEvents -= onLoadAction;
}

/// <summary>
/// Removes a method previously added through <see cref="RegisterOnStartLoadingEvent(Action)"/> so it is no longer invoked when loading the game.<para/>
/// If you plan on using this, do not register an anonymous method.
/// </summary>
/// <param name="onLoadAction">The method invoked.</param>
public static void UnregisterOnLoadEvent(Action onLoadAction)
public static void UnregisterOnStartLoadingEvent(Action onLoadAction)
{
SaveUtilsPatcher.OnLoadEvents -= onLoadAction;
SaveUtilsPatcher.OnStartLoadingEvents -= onLoadAction;
}

/// <summary>
Expand Down

0 comments on commit 307debf

Please sign in to comment.