Skip to content

Commit

Permalink
use of priority list for input update events too
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddio0141 committed Sep 1, 2023
1 parent 02db6c5 commit 7a25145
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
13 changes: 13 additions & 0 deletions UniTAS/Patcher/Implementations/MonoBehEventInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,17 @@ public void AddPriorityCallback(CallbackUpdate callbackUpdate, Action callback,

callbackList.Add(callback, (int)priority);
}

public void AddPriorityCallback(CallbackInputUpdate callbackUpdate, InputSystemEvents.InputUpdateCall callback,
CallbackPriority priority)
{
var callbackList = callbackUpdate switch
{
CallbackInputUpdate.InputUpdateActual => InputSystemEvents.InputUpdatesActual,
CallbackInputUpdate.InputUpdateUnconditional => InputSystemEvents.InputUpdatesUnconditional,
_ => throw new ArgumentOutOfRangeException(nameof(callbackUpdate), callbackUpdate, null)
};

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

public enum CallbackInputUpdate
{
InputUpdateActual,
InputUpdateUnconditional
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ public interface IUpdateEvents
event InputSystemEvents.InputUpdateCall OnInputUpdateUnconditional;

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

void AddPriorityCallback(CallbackInputUpdate callbackUpdate, InputSystemEvents.InputUpdateCall callback,
CallbackPriority priority);
}
25 changes: 17 additions & 8 deletions UniTAS/Patcher/Utils/InputSystemEvents.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using UniTAS.Patcher.Interfaces.Invoker;
using UniTAS.Patcher.Models;
using UniTAS.Patcher.Models.EventSubscribers;
using UnityEngine.InputSystem;

namespace UniTAS.Patcher.Utils;
Expand All @@ -12,13 +13,18 @@ public static class InputSystemEvents

public static event InputUpdateCall OnInputUpdateActual
{
add => InputUpdateActualCalls.Add(value);
remove => InputUpdateActualCalls.Remove(value);
add => InputUpdatesActual.Add(value, (int)CallbackPriority.Default);
remove => InputUpdatesActual.Remove(value);
}

public static event InputUpdateCall OnInputUpdateUnconditional;
public static event InputUpdateCall OnInputUpdateUnconditional
{
add => InputUpdatesUnconditional.Add(value, (int)CallbackPriority.Default);
remove => InputUpdatesUnconditional.Remove(value);
}

private static readonly List<InputUpdateCall> InputUpdateActualCalls = new();
public static readonly PriorityList<InputUpdateCall> InputUpdatesActual = new();
public static readonly PriorityList<InputUpdateCall> InputUpdatesUnconditional = new();

private static bool _usingMonoBehUpdate;
private static bool _usingMonoBehFixedUpdate;
Expand Down Expand Up @@ -149,13 +155,16 @@ public static void InputSystemChangeUpdate(InputSettings.UpdateMode updateMode)

private static void InputUpdate(bool fixedUpdate, bool newInputSystemUpdate)
{
OnInputUpdateUnconditional?.Invoke(fixedUpdate, newInputSystemUpdate);
for (var i = 0; i < InputUpdatesUnconditional.Count; i++)
{
InputUpdatesUnconditional[i](fixedUpdate, newInputSystemUpdate);
}

foreach (var update in InputUpdateActualCalls)
for (var i = 0; i < InputUpdatesActual.Count; i++)
{
if (MonoBehaviourController.PausedExecution ||
(!fixedUpdate && MonoBehaviourController.PausedUpdate)) continue;
update(fixedUpdate, newInputSystemUpdate);
InputUpdatesActual[i](fixedUpdate, newInputSystemUpdate);
}
}
}

0 comments on commit 7a25145

Please sign in to comment.