-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
InteractionsService.cs
207 lines (183 loc) · 7.97 KB
/
InteractionsService.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
using AltV.Net.Elements.Entities;
namespace AltV.Net.Interactions;
public class InteractionsService: IDisposable
{
private readonly InteractionsServiceThread[] threads;
private readonly InteractionsServiceThread[] threadToTypeIndex;
private readonly Func<ulong, ulong, ulong> globalThreadMapping;
internal InteractionsService(IReadOnlySet<ulong> types,
IReadOnlyList<(ulong[], Grid)> threadIndexes, Func<ulong, ulong, ulong> globalThreadMapping)
{
if (globalThreadMapping == null)
{
var length = threadIndexes.Count;
threadToTypeIndex = new InteractionsServiceThread[types.Max() + 1];
threads = new InteractionsServiceThread[length];
this.globalThreadMapping = null;
var allocatedTypes = new HashSet<ulong>(types);
for (var threadIndex = 0; threadIndex < length; ++threadIndex)
{
var (typesForThread, grid) = threadIndexes[threadIndex];
if (typesForThread == null)
{
throw new ArgumentException(
"You have to use .AddThreadWithType when you don't use .AddGlobalThreadMapping");
}
foreach (var currThreadType in typesForThread)
{
if (types.Any(currType => currType == currThreadType)) continue;
throw new ArgumentException(
"You have to use .AddType to add a type. Missing type: " + currThreadType);
}
var currThread = new InteractionsServiceThread(threadIndex, typesForThread, grid);
foreach (var currThreadType in typesForThread)
{
if (!allocatedTypes.Remove(currThreadType))
{
throw new ArgumentException(
"You can only use one type in one thread. Type used in multiple threads: " + currThreadType);
}
threadToTypeIndex[currThreadType] = currThread;
}
threads[threadIndex] = currThread;
}
}
else
{
var length = threadIndexes.Count;
threadToTypeIndex = null;
threads = new InteractionsServiceThread[length];
this.globalThreadMapping = globalThreadMapping;
for (var threadIndex = 0; threadIndex < length; ++threadIndex)
{
var (typesForThread, grid) = threadIndexes[threadIndex];
if (typesForThread == null)
{
throw new ArgumentException(
"You have to use .AddThread when you use .AddGlobalThreadMapping");
}
var currThread = new InteractionsServiceThread(threadIndex, typesForThread, grid);
threads[threadIndex] = currThread;
}
}
}
public static InteractionsServiceBuilder CreateBuilder(InteractionsServiceOptions serviceOptions = null)
{
return new InteractionsServiceBuilder(serviceOptions ?? new InteractionsServiceOptions());
}
public void Add(IInteraction interaction)
{
if (threadToTypeIndex == null)
{
threads[globalThreadMapping(interaction.Type, interaction.Id)].Writer.TryWrite(new InteractionsServiceThreadEvent(0, interaction));
return;
}
threadToTypeIndex[interaction.Type].Writer.TryWrite(new InteractionsServiceThreadEvent(0, interaction));
}
public void Remove(IInteraction interaction)
{
if (threadToTypeIndex == null)
{
threads[globalThreadMapping(interaction.Type, interaction.Id)].Writer.TryWrite(new InteractionsServiceThreadEvent(1, interaction));
return;
}
threadToTypeIndex[interaction.Type].Writer.TryWrite(new InteractionsServiceThreadEvent(1, interaction));
}
public void Trigger(ulong type, IPlayer player, object argument = null/*in Vector3 position, int dimension*/)
{
if (threadToTypeIndex == null)
{
for (int i = 0, length = threads.Length; i < length; ++i) {
threads[i].Writer.TryWrite(new InteractionsServiceThreadEvent(2, (type, player, argument)));
}
return;
}
threadToTypeIndex[type].Writer.TryWrite(new InteractionsServiceThreadEvent(2, (type, player, argument)));
}
public void TriggerMultiple(IPlayer player, object argument, params ulong[] types)
{
var typesCount = types.Length;
InteractionsServiceThread firstThread = null;
HashSet<ulong> firstThreadTypes = null;
Dictionary<InteractionsServiceThread, HashSet<ulong>> threadsToTrigger = null;
// zero alloc in case of single thread types
for (var i = 0; i < typesCount; ++i)
{
var type = types[i];
var thread = threadToTypeIndex[type];
if (firstThread == null)
{
firstThread = thread;
firstThreadTypes = new HashSet<ulong>{type};
}
else if (firstThread != thread)
{
if (threadsToTrigger == null)
{
threadsToTrigger = new Dictionary<InteractionsServiceThread, HashSet<ulong>> {[thread] = new() {type}};
}
else
{
if (!threadsToTrigger.TryGetValue(thread, out var threadTypes))
{
threadTypes = new HashSet<ulong> {type};
threadsToTrigger[thread] = threadTypes;
}
threadTypes.Add(type);
}
}
else
{
firstThreadTypes.Add(type);
}
}
if (firstThread == null) return; // no types found
firstThread.Writer.TryWrite(new InteractionsServiceThreadEvent(4, ((IReadOnlySet<ulong>) firstThreadTypes, player, argument)));
if (threadsToTrigger == null) return; // no other threads to trigger
foreach (var (thread, threadTypes) in threadsToTrigger)
{
thread.Writer.TryWrite(new InteractionsServiceThreadEvent(4, ((IReadOnlySet<ulong>) threadTypes, player, argument)));
}
}
public void TriggerAll(IPlayer player, object argument)
{
for (int i = 0, length = threads.Length; i < length; ++i)
{
threads[i].Writer.TryWrite(new InteractionsServiceThreadEvent(5, (player, argument)));
}
}
public Task<IInteraction[]> Find(ulong type, Vector3 position, int dimension)
{
if (threadToTypeIndex == null)
{
return Task.Run(async () =>
{
var interactions = new List<IInteraction>();
for (int i = 0, length = threads.Length; i < length; ++i) {
var callback = new TaskCompletionSource<IInteraction[]>(TaskCreationOptions.RunContinuationsAsynchronously);
threads[i].Writer.TryWrite(new InteractionsServiceThreadEvent(3, (position, dimension, callback)));
interactions.AddRange(await callback.Task);
}
return interactions.ToArray();
});
}
var callback = new TaskCompletionSource<IInteraction[]>(TaskCreationOptions.RunContinuationsAsynchronously);
threadToTypeIndex[type].Writer.TryWrite(new InteractionsServiceThreadEvent(3, (position, dimension, callback)));
return callback.Task;
}
public void Dispose()
{
for (int i = 0, length = threads.Length; i < length; ++i) {
threads[i].Dispose();
threads[i] = null;
}
for (int i = 0, length = threadToTypeIndex.Length; i < length; ++i) {
threadToTypeIndex[i] = null;
}
}
}