forked from FabianTerhorst/coreclr-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RefContext.cs
131 lines (111 loc) · 3.8 KB
/
RefContext.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
using System;
using System.Collections.Generic;
using AltV.Net.Elements.Entities;
using AltV.Net.Shared.Elements.Entities;
namespace AltV.Net
{
public interface IRefContext : IDisposable
{
public bool CheckIfExists(ISharedBaseObject baseObject);
public bool CheckIfExists(IWorldObject worldObject);
public bool CheckIfExists(IEntity entity);
bool CreateRef(ISharedBaseObject baseObject, bool safe = false);
}
public class RefContext : IRefContext
{
public static IRefContext Create(bool throwOnExistsCheck = true)
{
return new RefContext(throwOnExistsCheck);
}
private readonly LinkedList<ISharedBaseObject> baseObjectRefs;
private readonly bool throwOnExistsCheck;
private RefContext(bool throwOnExistsCheck)
{
baseObjectRefs = new LinkedList<ISharedBaseObject>();
this.throwOnExistsCheck = throwOnExistsCheck;
}
public bool CreateRef(ISharedBaseObject baseObject, bool safe)
{
if (baseObject == null) return false;
try
{
if (!baseObject.AddRef())
{
// Check safe to prevent throw on TryToAsync calls
if (!safe && throwOnExistsCheck)
{
throw baseObject switch
{
IEntity entity => new EntityRemovedException(entity),
IWorldObject worldObject => new WorldObjectRemovedException(worldObject),
_ => new BaseObjectRemovedException(baseObject)
};
}
return false;
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
return false;
}
Alt.CoreImpl.CountUpRefForCurrentThread(baseObject);
baseObjectRefs.AddLast(baseObject);
return true;
}
public bool CheckIfExists(ISharedBaseObject 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 void Dispose()
{
// Thread safe linked list loop
var first = baseObjectRefs.First;
var current = first;
var last = baseObjectRefs.Last;
var done = false;
if (current != null) // check if empty
{
do
{
try
{
current.Value.RemoveRef();
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
Alt.CoreImpl.CountDownRefForCurrentThread(current.Value);
if (current == last) done = true;
current = current.Next;
} while (!done && current != null);
}
baseObjectRefs.Clear();
GC.SuppressFinalize(this);
}
}
}