-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathReflectionAPI.cs
193 lines (167 loc) · 6.33 KB
/
ReflectionAPI.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using UnityEngine;
namespace ChenPipi.CodeExecutor.Examples
{
/// <summary>
/// CodeExecutor 反射 API 工具
/// </summary>
public static class ReflectionAPI
{
#region Propeties
private static Assembly s_CodeExecutorAssembly = null;
public static Assembly CodeExecutorAssembly
{
get
{
if (s_CodeExecutorAssembly != null)
{
return s_CodeExecutorAssembly;
}
try
{
return (s_CodeExecutorAssembly = Assembly.Load("ChenPipi.CodeExecutor.Editor"));
}
catch (Exception e)
{
Debug.LogError(e);
return null;
}
}
}
private static Type s_CodeExecutorManagerType = null;
public static Type CodeExecutorManagerType
{
get
{
if (s_CodeExecutorManagerType != null)
{
return s_CodeExecutorManagerType;
}
try
{
return (s_CodeExecutorManagerType = CodeExecutorAssembly?.GetType("ChenPipi.CodeExecutor.Editor.CodeExecutorManager", true));
}
catch (Exception e)
{
Debug.LogError(e);
return null;
}
}
}
private static Type s_ExecutionModeType = null;
public static Type ExecutionModeType
{
get
{
if (s_ExecutionModeType != null)
{
return s_ExecutionModeType;
}
try
{
return (s_ExecutionModeType = CodeExecutorAssembly?.GetType("ChenPipi.CodeExecutor.Editor.CodeExecutorManager+ExecutionMode", true));
}
catch (Exception e)
{
Debug.LogError(e);
return null;
}
}
}
#endregion
/// <summary>
/// 注册执行模式
/// </summary>
/// <param name="modeName">模式名称</param>
/// <param name="executor">代码执行器</param>
/// <param name="modeDesc">模式描述</param>
public static void RegisterExecMode(string modeName, Func<string, object[]> executor, string modeDesc = "")
{
// ChenPipi.CodeExecutor.Editor.CodeExecutorManager.RegisterExecMode(new ChenPipi.CodeExecutor.Editor.CodeExecutorManager.ExecutionMode
// {
// name = modeName,
// desc = modeDesc,
// executor = executor,
// });
if (CodeExecutorAssembly == null)
{
Debug.LogError("[CodeExecutorInjectorCSharp] Unable to reach \'CodeExecutor\'!");
return;
}
// 获取注册函数 RegisterExecMode
MethodInfo registerExecModeMethod = CodeExecutorManagerType.GetMethod("RegisterExecMode", BindingFlags.Public | BindingFlags.Static);
// 注册执行模式
{
object executionMode = Activator.CreateInstance(ExecutionModeType);
{
// 名称
FieldInfo nameFieldInfo = ExecutionModeType.GetField("name");
nameFieldInfo.SetValue(executionMode, modeName);
// 名称
FieldInfo descFieldInfo = ExecutionModeType.GetField("desc");
descFieldInfo.SetValue(executionMode, modeDesc);
// 代码执行函数
FieldInfo executorFieldInfo = ExecutionModeType.GetField("executor");
executorFieldInfo.SetValue(executionMode, executor);
}
registerExecModeMethod!.Invoke(null, new object[] { executionMode, });
}
}
/// <summary>
/// 取消注册所有执行模式
/// </summary>
public static void UnregisterAllExecModes()
{
// ChenPipi.CodeExecutor.Editor.CodeExecutorManager.UnregisterAllExecModes();
if (CodeExecutorAssembly == null)
{
Debug.LogError("[CodeExecutorInjectorCSharp] Unable to reach \'CodeExecutor\'!");
return;
}
// 获取并执行反注册函数 UnregisterAllExecModes
MethodInfo unregisterAllExecModesMethod = CodeExecutorManagerType.GetMethod("UnregisterAllExecModes", BindingFlags.Public | BindingFlags.Static);
unregisterAllExecModesMethod!.Invoke(null, null);
}
/// <summary>
/// 是否有指定名称的执行模式
/// </summary>
/// <param name="modeName"></param>
/// <returns></returns>
public static bool HasExecMode(string modeName)
{
// return ChenPipi.CodeExecutor.Editor.CodeExecutorManager.HasExecMode(modeName);
if (CodeExecutorAssembly == null)
{
Debug.LogError("[CodeExecutorInjectorCSharp] Unable to reach \'CodeExecutor\'!");
return false;
}
// 获取并执行反注册函数 UnregisterAllExecModes
MethodInfo unregisterAllExecModesMethod = CodeExecutorManagerType.GetMethod("HasExecMode", BindingFlags.Public | BindingFlags.Static);
return (bool)unregisterAllExecModesMethod!.Invoke(null, new object[] { modeName });
}
#region List Extension
private static string Join<T>(this IList<T> list, string separator = "", bool ignoreEmptyOrNull = true)
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < list.Count; ++i)
{
if (ignoreEmptyOrNull && string.IsNullOrEmpty(list[i].ToString()))
{
continue;
}
builder.Append(list[i]);
if (i < list.Count - 1)
{
builder.Append(separator);
}
}
return builder.ToString();
}
#endregion
}
}
#endif