forked from Jackal-Celeste/JackalHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateMachineExt.cs
More file actions
37 lines (32 loc) · 1.69 KB
/
StateMachineExt.cs
File metadata and controls
37 lines (32 loc) · 1.69 KB
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
using System;
using System.Collections;
using System.Reflection;
using Monocle;
namespace Celeste.Mod.JackalHelper
{
public static class StateMachineExt
{
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);
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;
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);
StateMachine_begins.SetValue(machine, begins);
StateMachine_updates.SetValue(machine, updates);
StateMachine_ends.SetValue(machine, ends);
StateMachine_coroutines.SetValue(machine, coroutines);
machine.SetCallbacks(nextIndex, onUpdate, coroutine, begin, end);
return nextIndex;
}
}
}