forked from altmp/coreclr-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntity.cs
371 lines (299 loc) · 10.5 KB
/
Entity.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
namespace AltV.Net.EntitySync
{
public class Entity : IEntity
{
public ulong Id { get; }
public ulong Type { get; }
public (ulong, ulong) HashKey { get; }
private Vector3 position;
public Vector3 Position
{
get => position;
set => SetPositionInternal(value);
}
private bool exists = false;
public bool Exists => exists;
private bool positionState = false;
private Vector3 newPosition;
private int dimension;
public int Dimension
{
get => dimension;
set => SetDimensionInternal(value);
}
private bool dimensionState = false;
private int newDimension;
private uint range;
public uint Range
{
get => range;
set => SetRangeInternal(value);
}
public uint MigrationDistance { get; }
public uint RangeSquared { get; private set; }
private bool rangeState = false;
private uint newRange;
public IClient TempNetOwner { get; set; } = null;
public IClient NetOwner { get; set; } = null;
public float NetOwnerRange { get; set; } = float.MaxValue;
public float TempNetOwnerRange { get; set; } = float.MaxValue;
public float LastStreamInRange { get; set; } = -1;
public int StartingXIndex { get; set; }
public int StoppingXIndex { get; set; }
public int StartingYIndex { get; set; }
public int StoppingYIndex { get; set; }
private readonly object propertiesMutex = new object();
private readonly IDictionary<string, object> data;
private readonly IDictionary<string, object> threadLocalData;
public IDictionary<string, object> ThreadLocalData => threadLocalData;
public EntityDataSnapshot DataSnapshot { get; }
/// <summary>
/// List of clients that have the entity created.
/// </summary>
private readonly HashSet<IClient> clients = new ();
public Entity(ulong type, Vector3 position, int dimension, uint range) : this(
AltEntitySync.IdProvider.GetNext(), type,
position, dimension, range, range / 2, new Dictionary<string, object>())
{
}
public Entity(ulong type, Vector3 position, int dimension, uint range, uint migrationDistance) : this(
AltEntitySync.IdProvider.GetNext(), type,
position, dimension, range, migrationDistance, new Dictionary<string, object>())
{
}
public Entity(ulong type, Vector3 position, int dimension, uint range, IDictionary<string, object> data) : this(
AltEntitySync.IdProvider.GetNext(), type,
position, dimension, range, range / 2, data)
{
}
public Entity(ulong type, Vector3 position, int dimension, uint range, uint migrationDistance,
IDictionary<string, object> data) : this(
AltEntitySync.IdProvider.GetNext(), type,
position, dimension, range, migrationDistance, data)
{
}
internal Entity(ulong id, ulong type, Vector3 position, int dimension, uint range,
IDictionary<string, object> data) : this(id, type, position, dimension, range, range / 2, data)
{
}
internal Entity(ulong id, ulong type, Vector3 position, int dimension, uint range,
uint migrationDistance, IDictionary<string, object> data)
{
Id = id;
Type = type;
HashKey = (id, type);
this.position = position;
this.dimension = dimension;
this.range = range;
RangeSquared = range * range;
this.data = data;
DataSnapshot = new EntityDataSnapshot(this);
threadLocalData = new Dictionary<string, object>(data);
if (migrationDistance > range)
{
throw new ArgumentException("MigrationDistance should not be larger then range:" + migrationDistance +
"<=" + range + " = false");
}
MigrationDistance = migrationDistance;
}
public void SetData(string key, object value)
{
lock (data)
{
data[key] = value;
}
AltEntitySync.EntitySyncServer.UpdateEntityData(this, key, value);
}
public void ResetData(string key)
{
lock (data)
{
data.Remove(key);
}
AltEntitySync.EntitySyncServer.ResetEntityData(this, key);
}
public bool TryGetData(string key, out object value)
{
lock (data)
{
return data.TryGetValue(key, out value);
}
}
public ICollection<string> GetDataKeys()
{
lock (data)
{
return data.Keys;
}
}
public bool TryGetData<T>(string key, out T value)
{
lock (data)
{
if (!data.TryGetValue(key, out var currValue))
{
value = default;
return false;
}
try
{
value = (T)Convert.ChangeType(currValue, typeof(T));
return true;
}
catch
{
if (currValue is T cast)
{
value = cast;
return true;
}
value = default;
return false;
}
}
}
/// <summary>
/// Tries to add a client to the list of clients that created this entity.
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
public bool TryAddClient(IClient client)
{
return clients.Add(client);
}
public bool RemoveClient(IClient client)
{
return clients.Remove(client);
}
public HashSet<IClient> GetClients()
{
return clients;
}
public void SetPositionInternal(Vector3 currNewPosition)
{
lock (propertiesMutex)
{
positionState = true;
newPosition = currNewPosition;
}
AltEntitySync.EntitySyncServer.UpdateEntity(this);
}
public void SetDimensionInternal(int currNewDimension)
{
lock (propertiesMutex)
{
dimensionState = true;
newDimension = currNewDimension;
}
AltEntitySync.EntitySyncServer.UpdateEntity(this);
}
public void SetRangeInternal(uint currNewRange)
{
lock (propertiesMutex)
{
rangeState = true;
newRange = currNewRange;
}
AltEntitySync.EntitySyncServer.UpdateEntity(this);
}
public (bool, bool, bool) TrySetPropertiesComputing(out Vector3 currOldPosition, out uint currOldRange,
out int currOldDimension, out Vector3 currNewPosition, out uint currNewRange,
out int currNewDimension)
{
lock (propertiesMutex)
{
var newPositionFound = positionState;
var newRangeFound = rangeState;
var newDimensionFound = dimensionState;
if (!positionState)
{
currOldPosition = default;
currNewPosition = default;
}
else
{
currOldPosition = position;
currNewPosition = newPosition;
positionState = false;
position = newPosition;
}
if (!rangeState)
{
currOldRange = default;
currNewRange = default;
}
else
{
currOldRange = range;
currNewRange = newRange;
rangeState = false;
range = newRange;
RangeSquared = range * range;
}
if (!dimensionState)
{
currOldDimension = default;
currNewDimension = default;
}
else
{
currOldDimension = dimension;
currNewDimension = newDimension;
dimensionState = false;
dimension = newDimension;
}
return ValueTuple.Create(newPositionFound, newRangeFound, newDimensionFound);
}
}
public void SetThreadLocalData(string key, object value)
{
threadLocalData[key] = value;
}
public void ResetThreadLocalData(string key)
{
threadLocalData.Remove(key);
}
public bool TryGetThreadLocalData(string key, out object value)
{
return threadLocalData.TryGetValue(key, out value);
}
public virtual byte[] Serialize(IEnumerable<string> changedKeys)
{
using var m = new MemoryStream();
using (var writer = new BinaryWriter(m))
{
writer.Write(Id);
writer.Write(Type);
writer.Write(position.X);
writer.Write(position.Y);
writer.Write(position.Z);
writer.Write(Range);
//TODO: serialize data depending on changedKeys
}
return m.ToArray();
}
public void SetExistsInternal(bool state)
{
exists = state;
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
public override bool Equals(object? obj)
{
if (!(obj is Entity entity)) return false;
if (entity.Id != Id) return false;
if (entity.Type != Type) return false;
if (entity.dimension != dimension) return false;
if (entity.range != range) return false;
if (entity.position != position) return false;
if (entity.data != data) return false;
return true;
}
}
}