/
GetStaticProperty.cs
executable file
·143 lines (109 loc) · 3.25 KB
/
GetStaticProperty.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// (c) Copyright HutongGames, LLC 2010-2020. All rights reserved.
// License: Attribution 4.0 International(CC BY 4.0)
/*--- __ECO__ __PLAYMAKER__ __ACTION__ ---*/
using System;
using System.Reflection;
using UnityEngine;
using System.Collections.Generic;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.ScriptControl)]
[Tooltip("Get a public static property from a class.")]
public class GetStaticProperty : FsmStateAction
{
[Tooltip("Full path to the class that contains the static method.")]
[RequiredField]
public FsmString className;
[Tooltip("The public static property to set.")]
[RequiredField]
public FsmString propertyName;
[Tooltip("Property Value. NOTE: must match the property's type")]
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmVar propertyValue;
[Tooltip("Repeat every frame.")]
public bool everyFrame;
private Type cachedType;
private string cachedClassName;
private string cachedPropertyName;
private FieldInfo cachedFieldInfo;
private object[] parametersArray;
private string errorString;
public override void Reset()
{
className = null;
propertyName = null;
propertyValue = null;
everyFrame = false;
}
public override void OnEnter ()
{
DoGetStaticValue ();
if (!everyFrame) {
Finish ();
}
}
public override void OnUpdate ()
{
DoGetStaticValue ();
}
private void DoGetStaticValue ()
{
if (className == null || string.IsNullOrEmpty (className.Value)) {
Finish ();
return;
}
if (cachedClassName != className.Value || cachedPropertyName != propertyName.Value) {
errorString = string.Empty;
if (!DoCache ()) {
Debug.LogError (errorString);
Finish ();
return;
}
}
propertyValue.SetValue(cachedFieldInfo.GetValue(null));
}
private bool DoCache ()
{
cachedType = ReflectionUtils.GetGlobalType (className.Value);
if (cachedType == null) {
errorString += "Class is invalid: " + className.Value + "\n";
Finish ();
return false;
}
cachedClassName = className.Value;
#if NETFX_CORE
cachedPropertyInfo = cachedType.GetTypeInfo().GetDeclaredField(propertyName.Value);
#else
cachedFieldInfo = cachedType.GetField (propertyName.Value, BindingFlags.Public |
BindingFlags.Static |
BindingFlags.FlattenHierarchy);
#endif
if (cachedFieldInfo == null) {
errorString += "Invalid Property Name: " + propertyName.Value + "\n";
Finish ();
return false;
}
cachedPropertyName = propertyName.Value;
return true;
}
public override string ErrorCheck()
{
errorString = string.Empty;
DoCache();
if (!string.IsNullOrEmpty(errorString))
{
return errorString;
}
if (ReferenceEquals(cachedFieldInfo.FieldType, typeof(void)))
{
return "Class not found";
}
else if (!ReferenceEquals(cachedFieldInfo.FieldType,propertyValue.RealType))
{
return "propertyValue is of the wrong type.\nIt should be of type: " + cachedFieldInfo.FieldType;
}
return string.Empty;
}
}
}