forked from Elem8100/MapleStoryDB2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoroutine.cs
176 lines (155 loc) · 4.92 KB
/
Coroutine.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using IE = System.Collections.IEnumerator;
namespace WzComparerR2.MapRender
{
class CoroutineManager : GameComponent
{
public CoroutineManager(Game game) : base(game)
{
this.Enabled = true;
this.runList = new LinkedList<Coroutine>();
this.preAdd = new List<Coroutine>();
this.preRemove = new List<Coroutine>();
}
public GameTime GameTime { get; private set; }
private LinkedList<Coroutine> runList;
private List<Coroutine> preAdd;
private List<Coroutine> preRemove;
private bool isUpdating;
public override void Update(GameTime gameTime)
{
this.GameTime = gameTime;
this.isUpdating = true;
LinkedListNode<Coroutine> node = null;
while ((node = (node == null ? runList.First : node.Next)) != null)
{
preAdd.Clear();
preRemove.Clear();
bool removeMe = true;
var coroutine = node.Value;
while (coroutine != null)
{
if (coroutine.Enumerator != null)
{
bool hasNext = coroutine.Enumerator.MoveNext();
if (hasNext)
{
removeMe = false;
var value = coroutine.Enumerator.Current;
if (value == null)
{
//跳到下次update
}
else if (value is YieldCoroutine)
{
node.Value = ((YieldCoroutine)value);
}
else if (value is Coroutine)
{
//栈执行
var nextCoroutine = (Coroutine)value;
nextCoroutine.Prev = coroutine;
node.Value = nextCoroutine;
preAdd.Remove(nextCoroutine);
}
else
{
//其他奇妙类型 忽略
}
break;
}
}
coroutine = coroutine.Prev;
}
foreach (var c in preRemove)
{
runList.Remove(c);
}
foreach (var c in preAdd)
{
runList.AddLast(c);
}
if (removeMe)
{
var nextNode = node.Previous;
runList.Remove(node);
node = nextNode;
}
}
this.isUpdating = false;
}
public Coroutine StartCoroutine(IE ie)
{
var coroutine = new Coroutine() { Enumerator = ie };
if (this.isUpdating)
{
preAdd.Add(coroutine);
}
else
{
this.runList.AddLast(coroutine);
}
return coroutine;
}
public Coroutine Yield(IE ie)
{
return new YieldCoroutine() { Enumerator = ie };
}
public Coroutine Yield(Coroutine coroutine)
{
return new YieldCoroutine() { Enumerator = coroutine.Enumerator };
}
public void StopCoroutine(Coroutine coroutine)
{
if (this.isUpdating)
{
preRemove.Add(coroutine);
}
else
{
this.runList.Remove(coroutine);
}
}
}
class Coroutine
{
public IE Enumerator { get; set; }
public Coroutine Prev { get; set; }
}
sealed class YieldCoroutine : Coroutine
{
}
sealed class WaitTaskCompletedCoroutine : Coroutine
{
public WaitTaskCompletedCoroutine(Task task)
{
this.Task = task;
this.Enumerator = this.GetEnumerator();
}
public Task Task { get; set; }
private IE GetEnumerator()
{
while (!Task.IsCompleted)
{
yield return null;
}
if (Task.IsFaulted)
{
PluginBase.PluginManager.LogError("MapRender", Task.Exception, "Coroutine Error: ");
#if DEBUG
if (Debugger.IsAttached)
{
var ex = Task.Exception;
Debugger.Break();
}
#endif
}
}
}
}