-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathStateMachineEXT.cs
40 lines (39 loc) · 2.2 KB
/
StateMachineEXT.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using Monocle;
using System;
using System.Collections;
using System.Reflection;
namespace Celeste.Mod.IsaGrabBag
{
internal static class StateMachineExt
{
/// <summary>
/// Adds a state to a StateMachine. Ripped straight from JaThePlayer lol
/// </summary>
/// <returns>The index of the new state</returns>
public static int AddState(this StateMachine machine, Func<int> onUpdate, Func<IEnumerator> coroutine = null, Action begin = null, Action end = null)
{
Action[] begins = (Action[])StateMachine_begins.GetValue(machine);
Func<int>[] updates = (Func<int>[])StateMachine_updates.GetValue(machine);
Action[] ends = (Action[])StateMachine_ends.GetValue(machine);
Func<IEnumerator>[] coroutines = (Func<IEnumerator>[])StateMachine_coroutines.GetValue(machine);
int nextIndex = begins.Length;
// Now let's expand the arrays
Array.Resize(ref begins, begins.Length + 1);
Array.Resize(ref updates, begins.Length + 1);
Array.Resize(ref ends, begins.Length + 1);
Array.Resize(ref coroutines, coroutines.Length + 1);
// Store the resized arrays back into the machine
StateMachine_begins.SetValue(machine, begins);
StateMachine_updates.SetValue(machine, updates);
StateMachine_ends.SetValue(machine, ends);
StateMachine_coroutines.SetValue(machine, coroutines);
// And now we add the new functions
machine.SetCallbacks(nextIndex, onUpdate, coroutine, begin, end);
return nextIndex;
}
private static FieldInfo StateMachine_begins = typeof(StateMachine).GetField("begins", BindingFlags.Instance | BindingFlags.NonPublic);
private static FieldInfo StateMachine_updates = typeof(StateMachine).GetField("updates", BindingFlags.Instance | BindingFlags.NonPublic);
private static FieldInfo StateMachine_ends = typeof(StateMachine).GetField("ends", BindingFlags.Instance | BindingFlags.NonPublic);
private static FieldInfo StateMachine_coroutines = typeof(StateMachine).GetField("coroutines", BindingFlags.Instance | BindingFlags.NonPublic);
}
}