-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFsmHook.cs
More file actions
50 lines (46 loc) · 1.58 KB
/
Copy pathFsmHook.cs
File metadata and controls
50 lines (46 loc) · 1.58 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
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using HutongGames.PlayMaker;
using UnityEngine;
// GNU GPL 3.0
#pragma warning disable CS1591
namespace MSCLoader
{
[EditorBrowsable(EditorBrowsableState.Never)]
public class FsmHook
{
private class FsmHookAction : FsmStateAction
{
public Action hook;
public override void OnEnter()
{
hook?.Invoke();
Finish();
}
}
[EditorBrowsable(EditorBrowsableState.Never)]
public static void FsmInject(GameObject gameObject, string stateName, Action hook)
{
FsmState state = GetStateFromGameObject(gameObject, stateName);
if (state != null)
{
List<FsmStateAction> actions = new List<FsmStateAction>(state.Actions);
actions.Insert(0, new FsmHookAction { hook = hook });
state.Actions = actions.ToArray();
}
else ModConsole.LogError(string.Format("Cannot find state <b>{0}</b> in GameObject <b>{1}</b>", stateName, gameObject.name));
}
static FsmState GetStateFromGameObject(GameObject obj, string stateName)
{
PlayMakerFSM[] comps = obj.GetComponents<PlayMakerFSM>();
foreach (PlayMakerFSM playMakerFsm in comps)
{
FsmState state = playMakerFsm.FsmStates.FirstOrDefault(x => x.Name == stateName);
if (state != null) return state;
}
return null;
}
}
}