Skip to content

Commit

Permalink
MonoBehaviour events can be added with a priority
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddio0141 committed Sep 1, 2023
1 parent f986104 commit d18afba
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 42 deletions.
3 changes: 1 addition & 2 deletions UniTAS/Patcher.Tests/KernelUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using UniTAS.Patcher.Interfaces.GUI;
using UniTAS.Patcher.Interfaces.Movie;
using UniTAS.Patcher.Models.Customization;
using UniTAS.Patcher.Models.DependencyInjection;
using UniTAS.Patcher.Models.GlobalHotkeyListener;
using UniTAS.Patcher.Models.GUI;
using UniTAS.Patcher.Models.UnitySafeWrappers.SceneManagement;
Expand Down Expand Up @@ -119,7 +118,7 @@ public void ResetStaticFields()
}
}

[Singleton(RegisterPriority.FirstUpdateSkipOnRestart, IncludeDifferentAssembly = true)]
[Singleton(IncludeDifferentAssembly = true)]
public class TestPriority : IOnPreUpdatesActual
{
public void PreUpdateActual()
Expand Down
39 changes: 39 additions & 0 deletions UniTAS/Patcher/Implementations/MonoBehEventInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using UniTAS.Patcher.Interfaces.DependencyInjection;
using UniTAS.Patcher.Interfaces.Events.MonoBehaviourEvents.DontRunIfPaused;
using UniTAS.Patcher.Interfaces.Events.MonoBehaviourEvents.RunEvenPaused;
using UniTAS.Patcher.Models.EventSubscribers;
using UniTAS.Patcher.Services;
using UniTAS.Patcher.Services.EventSubscribers;
using UniTAS.Patcher.Utils;
Expand Down Expand Up @@ -203,9 +204,47 @@ public event InputSystemEvents.InputUpdateCall OnInputUpdateActual
remove => InputSystemEvents.OnInputUpdateActual -= value;
}

public event InputSystemEvents.InputUpdateCall OnInputUpdateUnconditional
{
add => InputSystemEvents.OnInputUpdateUnconditional += value;
remove => InputSystemEvents.OnInputUpdateUnconditional -= value;
}

public event Action OnPreUpdatesActual
{
add => MonoBehaviourEvents.OnPreUpdateActual += value;
remove => MonoBehaviourEvents.OnPreUpdateActual -= value;
}

public event Action OnPreUpdatesUnconditional
{
add => MonoBehaviourEvents.OnPreUpdateUnconditional += value;
remove => MonoBehaviourEvents.OnPreUpdateUnconditional -= value;
}

public void AddPriorityCallback(CallbackUpdate callbackUpdate, Action callback, CallbackPriority priority)
{
var callbackList = callbackUpdate switch
{
CallbackUpdate.AwakeActual => MonoBehaviourEvents.AwakesActual,
CallbackUpdate.AwakeUnconditional => MonoBehaviourEvents.AwakesUnconditional,
CallbackUpdate.StartActual => MonoBehaviourEvents.StartsActual,
CallbackUpdate.StartUnconditional => MonoBehaviourEvents.StartsUnconditional,
CallbackUpdate.EnableActual => MonoBehaviourEvents.EnablesActual,
CallbackUpdate.EnableUnconditional => MonoBehaviourEvents.EnablesUnconditional,
CallbackUpdate.PreUpdateActual => MonoBehaviourEvents.PreUpdatesActual,
CallbackUpdate.PreUpdateUnconditional => MonoBehaviourEvents.PreUpdatesUnconditional,
CallbackUpdate.UpdateActual => MonoBehaviourEvents.UpdatesActual,
CallbackUpdate.UpdateUnconditional => MonoBehaviourEvents.UpdatesUnconditional,
CallbackUpdate.FixedUpdateActual => MonoBehaviourEvents.FixedUpdatesActual,
CallbackUpdate.FixedUpdateUnconditional => MonoBehaviourEvents.FixedUpdatesUnconditional,
CallbackUpdate.GUIActual => MonoBehaviourEvents.GUIsActual,
CallbackUpdate.GUIUnconditional => MonoBehaviourEvents.GUIsUnconditional,
CallbackUpdate.LastUpdateActual => MonoBehaviourEvents.LastUpdatesActual,
CallbackUpdate.LastUpdateUnconditional => MonoBehaviourEvents.LastUpdatesUnconditional,
_ => throw new ArgumentOutOfRangeException(nameof(callbackUpdate), callbackUpdate, null)
};

callbackList.Add(callback, (int)priority);
}
}
8 changes: 8 additions & 0 deletions UniTAS/Patcher/Models/EventSubscribers/CallbackPriority.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace UniTAS.Patcher.Models.EventSubscribers;

public enum CallbackPriority
{
// the higher you place it, the lower the value, the earlier it gets invoked
FirstUpdateSkipOnRestart,
Default
}
21 changes: 21 additions & 0 deletions UniTAS/Patcher/Models/EventSubscribers/CallbackUpdate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace UniTAS.Patcher.Models.EventSubscribers;

public enum CallbackUpdate
{
AwakeUnconditional,
AwakeActual,
StartUnconditional,
StartActual,
EnableUnconditional,
EnableActual,
PreUpdateUnconditional,
PreUpdateActual,
UpdateUnconditional,
UpdateActual,
FixedUpdateUnconditional,
FixedUpdateActual,
GUIUnconditional,
GUIActual,
LastUpdateUnconditional,
LastUpdateActual
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using UniTAS.Patcher.Models.EventSubscribers;
using UniTAS.Patcher.Utils;

namespace UniTAS.Patcher.Services.EventSubscribers;
Expand All @@ -19,5 +20,9 @@ public interface IUpdateEvents
event Action OnLastUpdateUnconditional;
event Action OnLastUpdateActual;
event Action OnPreUpdatesActual;
event Action OnPreUpdatesUnconditional;
event InputSystemEvents.InputUpdateCall OnInputUpdateActual;
event InputSystemEvents.InputUpdateCall OnInputUpdateUnconditional;

void AddPriorityCallback(CallbackUpdate callbackUpdate, Action callback, CallbackPriority priority);
}
Loading

0 comments on commit d18afba

Please sign in to comment.