-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathAsyncContextExtensions.cs
68 lines (60 loc) · 2.5 KB
/
AsyncContextExtensions.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
using System;
using System.Threading;
using AltV.Net.Elements.Entities;
namespace AltV.Net.Async
{
public static class AsyncContextExtensions
{
private static bool CheckIfExists(IBaseObject baseObject)
{
if (baseObject.Exists) return true;
if (Alt.ThrowIfEntityDoesNotExist) throw new BaseObjectRemovedException(baseObject);
return false;
}
private static bool CheckIfExistsOrCached(IBaseObject baseObject)
{
if (baseObject.Cached) return true;
return CheckIfExists(baseObject);
}
public static bool CheckIfExistsNullable(this IAsyncContext context, IBaseObject baseObject)
{
if (context == null) return CheckIfExists(baseObject);
return context.CheckIfExists(baseObject);
}
public static bool CheckIfExistsNullable(this IAsyncContext context, IWorldObject worldObject)
{
if (context == null) return CheckIfExists(worldObject);
return context.CheckIfExists(worldObject);
}
public static bool CheckIfExistsNullable(this IAsyncContext context, IEntity entity)
{
if (context == null) return CheckIfExists(entity);
return context.CheckIfExists(entity);
}
public static bool CheckIfExistsOrCachedNullable(this IAsyncContext context, IBaseObject baseObject)
{
if (context == null) return CheckIfExistsOrCached(baseObject);
return context.CheckIfExistsOrCached(baseObject);
}
public static bool CheckIfExistsOrCachedNullable(this IAsyncContext context, IWorldObject worldObject)
{
if (context == null) return CheckIfExistsOrCached(worldObject);
return context.CheckIfExistsOrCached(worldObject);
}
public static bool CheckIfExistsOrCachedNullable(this IAsyncContext context, IEntity entity)
{
if (context == null) return CheckIfExistsOrCached(entity);
return context.CheckIfExistsOrCached(entity);
}
public static void RunOnMainThreadBlockingNullable(this IAsyncContext context, Action action)
{
if (context == null)
{
using var semaphore = new SemaphoreSlim(0, 1);
AltAsync.RunOnMainThreadBlocking(action, semaphore);
return;
}
context.RunOnMainThreadBlocking(action);
}
}
}