-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathActionWindow.cs
More file actions
319 lines (257 loc) · 10.9 KB
/
ActionWindow.cs
File metadata and controls
319 lines (257 loc) · 10.9 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using NeuroSdk.Il2Cpp;
using NeuroSdk.Internal;
using NeuroSdk.Messages.Outgoing;
using NeuroSdk.Websocket;
using UnityEngine;
namespace NeuroSdk.Actions
{
/// <summary>
/// A wrapper class around the concept of an action window, which handles sending context, registering actions, forcing actions and unregistering the actions afterwards.
/// </summary>
#pragma warning disable CS0618 // Type or member is obsolete
[RegisterInIl2Cpp]
#pragma warning restore CS0618 // Type or member is obsolete
public sealed class ActionWindow : MonoBehaviour
{
#region Creation
private static bool _isCreatedCorrectly = false;
/// <summary>
/// Creates a new ActionWindow. If the parent is destroyed, this ActionWindow will be automatically ended.
/// </summary>
public static ActionWindow Create(GameObject parent)
{
try
{
_isCreatedCorrectly = true;
return parent.AddComponent<ActionWindow>();
}
finally
{
_isCreatedCorrectly = false;
}
}
private void Awake()
{
if (!_isCreatedCorrectly)
{
Debug.LogError("ActionWindow should be created using Create method. This ActionWindow was either created with AddComponent or with Instantiate.");
Destroy(this);
}
}
#endregion
#region State
public enum State
{
Building,
Registered,
Forced,
Ended
}
public State CurrentState { get; private set; } = State.Building;
private bool ValidateFrozen()
{
if (CurrentState != State.Building)
{
Debug.LogError("Cannot mutate ActionWindow after it has been registered.");
return false;
}
return true;
}
/// <summary>
/// Register this ActionWindow, sending an actions register to the websocket and making this window immutable.
/// </summary>
public void Register()
{
if (CurrentState != State.Building)
{
Debug.LogError("Cannot register an ActionWindow multiple times.");
return;
}
if (_actions.Count == 0)
{
Debug.LogError("Cannot register an ActionWindow with no actions.");
return;
}
if (_contextMessage is not (null or ""))
Context.Send(_contextMessage, _contextSilent!.Value);
NeuroActionHandler.RegisterActions(_actions);
CurrentState = State.Registered;
}
#endregion
#region Context
private string? _contextMessage;
private bool? _contextSilent;
/// <summary>
/// Set a context message to be sent alongside the action register.
/// </summary>
/// <returns>The <see cref="ActionWindow"/> itself for chaining.</returns>
public ActionWindow SetContext(string message, bool silent = false)
{
if (!ValidateFrozen()) return this;
_contextMessage = message;
_contextSilent = silent;
return this;
}
#endregion
#region Actions
private readonly List<INeuroAction> _actions = new();
/// <summary>
/// Add a new action to the list of possible actions that Neuro can pick from
/// </summary>
/// <returns>The <see cref="ActionWindow"/> itself for chaining.</returns>
[Il2CppHide]
public ActionWindow AddAction(INeuroAction action)
{
if (!ValidateFrozen()) return this;
if (action.ActionWindow != null)
{
if (action.ActionWindow != this)
{
Debug.LogError($"Cannot add action {action.Name} to this ActionWindow because it is already included in another ActionWindow.");
}
return this;
}
if (!action.CanAddToActionWindow(this)) return this;
if (_actions.Any(a => a.Name == action.Name))
{
Debug.LogError($"Cannot add two actions with the same name to the same ActionWindow. Triggered by {action.Name}");
return this;
}
action.SetActionWindow(this);
_actions.Add(action);
return this;
}
#endregion
#region Forcing
private Func<bool>? _shouldForceFunc;
private Func<string>? _forceQueryGetter;
private Func<string?>? _forceStateGetter;
private bool? _forceEphemeralContext;
private ActionsForce.Priority? _forcePriority;
/// <summary>
/// Specify a condition under which the actions should be forced.
/// </summary>
/// <param name="shouldForce">When this returns true, the actions will be forced.</param>
/// <param name="queryGetter">A getter for the query of the action force, invoked at force-time.</param>
/// <param name="stateGetter">A getter for the state of the action force, invoked at force-time.</param>
/// <param name="ephemeralContext">If true, the query and state won't be remembered after the action force is finished.</param>
/// <param name="priority">Priority of the action force. Defaults to low.</param>
/// <returns>The <see cref="ActionWindow"/> itself for chaining.</returns>
[Il2CppHide]
public ActionWindow SetForce(Func<bool> shouldForce, Func<string> queryGetter, Func<string?> stateGetter, bool ephemeralContext = false, ActionsForce.Priority priority = ActionsForce.Priority.Low)
{
if (!ValidateFrozen()) return this;
_shouldForceFunc = shouldForce;
_forceQueryGetter = queryGetter;
_forceStateGetter = stateGetter;
_forceEphemeralContext = ephemeralContext;
_forcePriority = priority;
return this;
}
/// <summary>
/// Specify a condition under which the actions should be forced.
/// </summary>
/// <param name="shouldForce">When this returns true, the actions will be forced.</param>
/// <param name="ephemeralContext">If true, the query and state won't be remembered after the action force is finished.</param>
/// <returns>The <see cref="ActionWindow"/> itself for chaining.</returns>
[Il2CppHide]
public ActionWindow SetForce(Func<bool> shouldForce, string query, string? state, bool ephemeralContext = false, ActionsForce.Priority priority = ActionsForce.Priority.Low)
=> SetForce(shouldForce, () => query, () => state, ephemeralContext, priority);
/// <summary>
/// Specify a time in seconds after which the actions should be forced.
/// </summary>
/// <param name="queryGetter">A getter for the query of the action force, invoked at force-time.</param>
/// <param name="stateGetter">A getter for the state of the action force, invoked at force-time.</param>
/// <param name="ephemeralContext">If true, the query and state won't be remembered after the action force is finished.</param>
/// <returns>The <see cref="ActionWindow"/> itself for chaining.</returns>
[Il2CppHide]
public ActionWindow SetForce(float afterSeconds, Func<string> queryGetter, Func<string?> stateGetter, bool ephemeralContext = false, ActionsForce.Priority priority = ActionsForce.Priority.Low)
{
float time = afterSeconds;
return SetForce(shouldForce, queryGetter, stateGetter, ephemeralContext, priority);
bool shouldForce()
{
// called every update
time -= Time.deltaTime;
return time <= 0;
}
}
/// <summary>
/// Specify a time in seconds after which the actions should be forced.
/// </summary>
/// <param name="ephemeralContext">If true, the query and state won't be remembered after the action force is finished.</param>
/// <returns>The <see cref="ActionWindow"/> itself for chaining.</returns>
public ActionWindow SetForce(float afterSeconds, string query, string? state, bool ephemeralContext = false, ActionsForce.Priority priority = ActionsForce.Priority.Low)
=> SetForce(afterSeconds, () => query, () => state, ephemeralContext, priority);
public void Force()
{
if (CurrentState != State.Registered) return;
CurrentState = State.Forced;
_shouldForceFunc = null;
WebsocketConnection.Instance!.Send(new ActionsForce(_forceQueryGetter!(), _forceStateGetter!(), _forceEphemeralContext, _forcePriority ?? ActionsForce.Priority.Low, _actions));
}
#endregion
#region Ending
private Func<bool>? _shouldEndFunc;
/// <summary>
/// Specify a condition under which the actions should be unregistered and this window closed.
/// </summary>
/// <param name="shouldEnd">When this returns true, the actions will be unregistered.</param>
/// <returns>The <see cref="ActionWindow"/> itself for chaining.</returns>
[Il2CppHide]
public ActionWindow SetEnd(Func<bool> shouldEnd)
{
if (!ValidateFrozen()) return this;
_shouldEndFunc = shouldEnd;
return this;
}
/// <summary>
/// Specify a time in seconds after which the actions should be unregistered and this window closed.
/// </summary>
/// <returns>The <see cref="ActionWindow"/> itself for chaining.</returns>
public ActionWindow SetEnd(float afterSeconds)
{
float time = afterSeconds;
return SetEnd(shouldEnd);
bool shouldEnd()
{
// called every update
time -= Time.deltaTime;
return time <= 0;
}
}
private void OnDestroy()
{
if (CurrentState == State.Ended) return;
End();
}
public void End()
{
if (CurrentState >= State.Ended) return;
NeuroActionHandler.UnregisterActions(_actions);
_shouldForceFunc = null;
_shouldEndFunc = null;
CurrentState = State.Ended;
Destroy(this);
}
#endregion
#region Handling
private void Update()
{
if (CurrentState != State.Registered) return;
if (_shouldForceFunc != null && _shouldForceFunc())
{
Force();
}
if (_shouldEndFunc != null && _shouldEndFunc())
{
End();
}
}
#endregion
}
}