-
Notifications
You must be signed in to change notification settings - Fork 66
/
AsyncContext.cs
206 lines (171 loc) · 6.09 KB
/
AsyncContext.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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using AltV.Net.Elements.Entities;
namespace AltV.Net.Async
{
public interface IAsyncContext : IAsyncDisposable
{
void Enqueue(Action action);
void RunOnMainThreadBlocking(Action action);
void RunOnMainThreadBlockingAndRunAll(Action action);
void RunAll();
bool CheckIfExists(IBaseObject baseObject);
bool CheckIfExists(IWorldObject worldObject);
bool CheckIfExists(IEntity entity);
bool CheckIfExistsOrCached(IBaseObject baseObject);
bool CheckIfExistsOrCached(IWorldObject worldObject);
bool CheckIfExistsOrCached(IEntity entity);
}
public class AsyncContext : IAsyncContext
{
public static IAsyncContext Create(bool throwOnExistsCheck = true, bool createRefAutomatically = true)
{
return new AsyncContext(throwOnExistsCheck, createRefAutomatically);
}
private readonly LinkedList<Action> actions;
private readonly LinkedList<IBaseObject> baseObjectRefs;
private readonly SemaphoreSlim semaphoreSlim;
private readonly bool throwOnExistsCheck;
private readonly bool createRefAutomatically;
private AsyncContext(bool throwOnExistsCheck, bool createRefAutomatically)
{
actions = new LinkedList<Action>();
baseObjectRefs = new LinkedList<IBaseObject>();
semaphoreSlim = new SemaphoreSlim(0, 1);
this.throwOnExistsCheck = throwOnExistsCheck;
this.createRefAutomatically = createRefAutomatically;
}
public void Enqueue(Action action)
{
actions.AddLast(action);
}
public void RunOnMainThreadBlocking(Action action)
{
if (throwOnExistsCheck)
{
AltAsync.RunOnMainThreadBlockingThrows(action, semaphoreSlim);
}
else
{
AltAsync.RunOnMainThreadBlocking(action, semaphoreSlim);
}
}
public void RunOnMainThreadBlockingAndRunAll(Action action)
{
RunOnMainThreadBlocking(() =>
{
if (actions.Count != 0)
{
// Thread safe linked list loop
var first = actions.First;
var current = first;
var last = actions.Last;
var done = false;
if (current == null) return; //empty list
do
{
current.Value();
if (current == last) done = true;
current = current.Next;
} while (!done && current != null);
actions.Clear();
}
action();
});
}
public bool CheckIfExists(IBaseObject baseObject)
{
if (baseObject.Exists) return true;
if (throwOnExistsCheck)
{
throw new BaseObjectRemovedException(baseObject);
}
return false;
}
public bool CheckIfExists(IWorldObject worldObject)
{
if (worldObject.Exists) return true;
if (throwOnExistsCheck)
{
throw new WorldObjectRemovedException(worldObject);
}
return false;
}
public bool CheckIfExists(IEntity entity)
{
if (entity.Exists) return true;
if (throwOnExistsCheck)
{
throw new EntityRemovedException(entity);
}
return false;
}
public bool CheckIfExistsOrCached(IBaseObject baseObject)
{
if (baseObject.Cached) return true;
return CheckIfExists(baseObject);
}
public bool CheckIfExistsOrCached(IWorldObject worldObject)
{
if (worldObject.Cached) return true;
return CheckIfExists(worldObject);
}
public bool CheckIfExistsOrCached(IEntity entity)
{
if (entity.Cached) return true;
return CheckIfExists(entity);
}
public void RunAll()
{
RunAll(false);
}
public void RunAll(bool dispose)
{
if (actions.Count == 0 && !dispose) return;
RunOnMainThreadBlocking(() =>
{
if (actions.Count != 0)
{
// Thread safe linked list loop
var first = actions.First;
var current = first;
var last = actions.Last;
var done = false;
if (current == null) return; //empty list
do
{
current.Value();
if (current == last) done = true;
current = current.Next;
} while (!done && current != null);
actions.Clear();
}
if (dispose)
{
var firstBaseObject = baseObjectRefs.First;
var currentBaseObject = firstBaseObject;
var lastBaseObject = baseObjectRefs.Last;
var doneBaseObject = false;
if (currentBaseObject != null) // check if empty
{
do
{
if (currentBaseObject == lastBaseObject) doneBaseObject = true;
currentBaseObject = currentBaseObject.Next;
} while (!doneBaseObject && currentBaseObject != null);
}
baseObjectRefs.Clear();
}
});
}
public ValueTask DisposeAsync()
{
RunAll(true);
semaphoreSlim.Dispose();
GC.SuppressFinalize(this);
return ValueTask.CompletedTask;
}
}
}