-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRoutine.cs
597 lines (518 loc) · 15 KB
/
Routine.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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//MIT License
//
//Copyright (c) 2018 Tom Blind
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
namespace Routines
{
public class Routine
{
public delegate void ExceptionHandlerDelegate(System.Exception e, Object context = null);
public interface IWrappedArray {}
//Use to safely perform actions on a routine
public struct Handle
{
private Routine routine;
private ulong id;
public bool IsDone
{
get
{
return (routine == null || id != routine.id || routine.IsDone);
}
}
public Routine Get()
{
return IsDone ? null : routine;
}
public Handle(Routine routine, ulong id)
{
this.routine = routine;
this.id = id;
}
public void Stop()
{
if (routine != null && id == routine.id)
{
routine.Stop();
}
}
}
private class WrappedArray : IWrappedArray
{
public IEnumerable yieldables;
public bool isAny;
}
private class RoutineException: System.Exception
{
public RoutineException() {}
public RoutineException(string message) : base(message) {}
public RoutineException(string message, System.Exception inner) : base(message, inner) {}
}
private class Resumer : IResumer
{
public ulong steppingId;
public Routine routine;
public bool called;
public bool IsAlive
{
get
{
return (steppingId != noId && steppingId == routine.id);
}
}
public void Resume(object result)
{
Assert.IsFalse(called);
called = true;
if (steppingId == routine.id)
{
routine.returnValue = result;
routine.Finish(null);
}
Release();
}
public void Throw(System.Exception error)
{
Assert.IsFalse(called);
called = true;
if (steppingId == routine.id)
{
routine.Finish(routine.CreateException(error.Message, error));
}
Release();
}
public void Release()
{
resumerPool.Push(this);
}
public void Reset()
{
steppingId = noId;
routine = null;
called = false;
}
}
private class ImmediateResumable : Resumable
{
public override void Run(IResumer resumer)
{
resumer.Resume();
}
}
private const ulong noId = ulong.MaxValue;
private const int maxIterations = 999999; //Maximum times a routine will step without yielding (to prevent lockup from infinite loops)
private ulong id = noId;
private IEnumerator enumerator = null;
private Routine parent = null;
private Object context = null;
private ExceptionHandlerDelegate exceptionHandler = null;
private List<Routine> children = new List<Routine>();
private List<object> yieldedArray = new List<object>();
private bool arrayWasYielded = false;
private bool finishOnAny = false;
private bool isStepping = false;
private object returnValue = null;
private RoutineException error = null;
private System.Action onStop;
private static Stack<Routine> pool = new Stack<Routine>();
private static IResumable resumeImmediately = new ImmediateResumable();
private static ulong nextId = 0;
private static Stack<Resumer> resumerPool = new Stack<Resumer>();
private static Stack<WrappedArray> wrappedArrayPool = new Stack<WrappedArray>();
private static Stack<Routine> steppingStack = new Stack<Routine>();
private static object currentReturnValue = null;
private static List<object> currentAllReturnValues = new List<object>();
public bool IsDone { get { return id == noId; } }
public System.Exception Error { get { return error; } }
public void Start(IEnumerator enumerator, Object context = null, ExceptionHandlerDelegate exceptionHandler = null, System.Action onStop = null)
{
Stop();
Setup(enumerator, null, context, exceptionHandler, onStop);
Step();
}
public void Start(IEnumerable enumerable, Object context = null, ExceptionHandlerDelegate exceptionHandler = null, System.Action onStop = null)
{
Stop();
Setup(enumerable.GetEnumerator(), null, context, exceptionHandler, onStop);
Step();
}
public void Stop()
{
ClearChildren();
yieldedArray.Clear();
arrayWasYielded = false;
finishOnAny = false;
isStepping = false;
id = noId;
var oldOnStop = onStop;
onStop = null;
if (oldOnStop != null)
{
oldOnStop();
}
}
public void Reset()
{
Stop();
enumerator = null;
parent = null;
returnValue = null;
error = null;
context = null;
exceptionHandler = null;
}
public Handle GetHandle()
{
return new Handle(this, id);
}
//Get new routine from pool
public static Routine Create()
{
return (pool.Count > 0) ? pool.Pop() : new Routine();
}
//Release routine back to pool
public static void Release(ref Routine routine)
{
routine.Reset();
pool.Push(routine);
routine = null;
}
//Retrieve the result of the last finished routine
public static T GetResult<T>()
{
return (T)currentReturnValue;
}
public static object GetResult()
{
return currentReturnValue;
}
public static List<object> GetAllResults()
{
return currentAllReturnValues;
}
//Set the result of the currently stepping routine
public static void SetResult(object value)
{
Assert.IsTrue(steppingStack.Count > 0);
steppingStack.Peek().returnValue = value;
}
//Wrap multiple enumerators to be yielded.
//Routine won't resume until all have finished.
//Result will be a List continaing results from all routines. DO NOT alter that list.
public static IWrappedArray All(params IEnumerator[] yieldables)
{
var allArray = (wrappedArrayPool.Count > 0) ? wrappedArrayPool.Pop() : new WrappedArray();
allArray.yieldables = yieldables;
allArray.isAny = false;
return allArray;
}
public static IWrappedArray All(IEnumerable yieldables)
{
var allArray = (wrappedArrayPool.Count > 0) ? wrappedArrayPool.Pop() : new WrappedArray();
allArray.yieldables = yieldables;
allArray.isAny = false;
return allArray;
}
//Wrap multiple enumerators to be yielded.
//Routine will resume when first one finishes.
//Result will be value from that first finished routine.
public static IWrappedArray Any(params IEnumerator[] yieldables)
{
var anyArray = (wrappedArrayPool.Count > 0) ? wrappedArrayPool.Pop() : new WrappedArray();
anyArray.yieldables = yieldables;
anyArray.isAny = true;
return anyArray;
}
public static IWrappedArray Any(IEnumerable yieldables)
{
var anyArray = (wrappedArrayPool.Count > 0) ? wrappedArrayPool.Pop() : new WrappedArray();
anyArray.yieldables = yieldables;
anyArray.isAny = true;
return anyArray;
}
//Dummy resumer that resumes immediately upon yielding
public static IEnumerator ResumeImmediately()
{
return resumeImmediately;
}
private Routine() {} //Use Routine.Create()
private void Setup(IEnumerator enumerator, Routine parent, Object context, ExceptionHandlerDelegate exceptionHandler, System.Action onStop = null)
{
id = nextId++;
Assert.IsTrue(nextId != ulong.MaxValue);
returnValue = null;
if (parent != null)
{
this.parent = parent;
}
this.enumerator = enumerator;
this.context = context;
this.exceptionHandler = exceptionHandler ?? Debug.LogException;
this.onStop = onStop;
}
private void ClearChildren()
{
foreach (var child in children)
{
child.Reset();
pool.Push(child);
}
children.Clear();
}
private void Finish(RoutineException error)
{
this.error = error;
Stop();
if (parent != null && !parent.isStepping)
{
parent.Step();
}
}
private void Step()
{
Assert.IsTrue(!isStepping);
isStepping = true;
//Used to detect that routine was killed during step
var steppingId = id;
//Running IResumable
if (enumerator is IResumable)
{
var resumer = (resumerPool.Count > 0) ? resumerPool.Pop() : new Resumer();
resumer.called = false;
resumer.routine = this;
resumer.steppingId = steppingId;
var resumable = enumerator as IResumable;
resumable.Run(resumer);
//In case of suicide or immediate finish
if (steppingId != id)
{
return;
}
}
//Running enumerator
else
{
int itCount;
for (itCount = 0; itCount < Routine.maxIterations; ++itCount)
{
currentReturnValue = null;
currentAllReturnValues.Clear();
//Bail out if any children are yielding:
//Any array: stop if all children are yielding
if (finishOnAny)
{
var childIsDone = true;
for (int i = 0, l = children.Count; i < l; ++i)
{
var child = children[i];
if (child.error != null)
{
Finish(child.error);
return;
}
childIsDone = child.IsDone;
if (childIsDone)
{
currentReturnValue = child.returnValue;
break;
}
}
if (!childIsDone)
{
break;
}
}
//Single or all-array: stop if any children are yielding
else
{
var childIsYielding = false;
for (int i = 0, l = children.Count; i < l; ++i)
{
var child = children[i];
if (child.error != null)
{
Finish(child.error);
return;
}
childIsYielding = !child.IsDone;
if (childIsYielding)
{
break;
}
}
if (childIsYielding)
{
break;
}
//Collect return values from children
if (arrayWasYielded)
{
for (int i = 0, l = children.Count; i < l; ++i)
{
currentAllReturnValues.Add(children[i].returnValue);
}
currentReturnValue = currentAllReturnValues;
}
else if (children.Count > 0)
{
Assert.IsTrue(children.Count == 1);
currentReturnValue = children[0].returnValue;
}
}
//Stop and clear children
ClearChildren();
//Step this routine
steppingStack.Push(this);
bool done;
RoutineException stepError = null;
try
{
done = !enumerator.MoveNext();
}
catch (System.Exception e)
{
stepError = CreateException(e.Message, e);
exceptionHandler(stepError, context);
done = true;
}
steppingStack.Pop();
//Check for suicide
if (steppingId != id)
{
return;
}
//Prevent GetResult() from giving back something when it shouldn't
currentReturnValue = null;
currentAllReturnValues.Clear();
//Routine finished?
if (done)
{
Finish(stepError);
return;
}
arrayWasYielded = false;
finishOnAny = false;
//Check for yielded array
var result = enumerator.Current;
if (result is WrappedArray)
{
var wrappedArray = result as WrappedArray;
var arr = wrappedArray.yieldables;
finishOnAny = wrappedArray.isAny;
wrappedArrayPool.Push(wrappedArray);
if (arr == null)
{
Finish(CreateException("yieldables not set in WrappedArray"));
return;
}
arrayWasYielded = true;
//Copy array contents in case one of contained routines messes with it when stepped
Assert.IsTrue(yieldedArray.Count == 0);
foreach (var element in arr)
{
yieldedArray.Add(element);
}
for (int i = 0, l = yieldedArray.Count; i < l; ++i)
{
var yieldedValue = yieldedArray[i];
if (yieldedValue is IEnumerable)
{
yieldedValue = (yieldedValue as IEnumerable).GetEnumerator();
}
else if (!(yieldedValue is IEnumerator))
{
Finish(CreateException(string.Format("yielded value [{0}] is not an IEnumerator: {1}", i, yieldedValue)));
return;
}
var child = CreateChild(yieldedValue as IEnumerator);
//Check for parenticide
if (steppingId != id)
{
return;
}
//Exit if any child completes in any-array mode
if (finishOnAny && child.IsDone)
{
break;
}
}
yieldedArray.Clear();
}
//Single runable yielded: create child routine
else if (result is IEnumerable) //Check for IEnumerable before IEnumerator, as the object could be both (Linq) and we want to treat it as the former if so
{
CreateChild((result as IEnumerable).GetEnumerator());
//Check for parenticide
if (steppingId != id)
{
return;
}
}
else if (result is IEnumerator)
{
CreateChild(result as IEnumerator);
//Check for parenticide
if (steppingId != id)
{
return;
}
}
//Something not-runable was returned
else
{
throw CreateException(string.Format("yielded value is not an IEnumerator or IEnumerable: {0}", result));
}
}
if (itCount == maxIterations)
{
throw CreateException("Infinite loop in routine!");
}
}
isStepping = false;
}
private Routine CreateChild(IEnumerator enumerator)
{
var child = Create();
child.Setup(enumerator, this, context, exceptionHandler);
children.Add(child);
child.Step();
return child;
}
private RoutineException CreateException(string message, System.Exception inner = null)
{
var stack = new List<string>();
stack.Add(message);
stack.Add("Routine stack:");
var routine = this;
do
{
stack.Add(string.Format("{0}] {1}", stack.Count - 2, routine.enumerator.ToString()));
routine = routine.parent;
}
while (routine != null);
message = string.Join("\n", stack.ToArray());
return (inner != null) ? new RoutineException(message, inner) : new RoutineException(message);
}
}
}