Skip to content

Commit

Permalink
Merge pull request #359 from FabianTerhorst/rc
Browse files Browse the repository at this point in the history
Rc
  • Loading branch information
FabianTerhorst committed Oct 9, 2021
2 parents 926c5ed + 0cf1d37 commit 262cadd
Show file tree
Hide file tree
Showing 24 changed files with 3,216 additions and 58 deletions.
77 changes: 76 additions & 1 deletion api/AltV.Net.Async/ActionTickScheduler.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;

namespace AltV.Net.Async
Expand Down Expand Up @@ -160,10 +161,67 @@ public void Run()
}
}
}

private readonly struct ActionContainer7
{
private readonly Action action;

private readonly SemaphoreSlim semaphoreSlim;

public ActionContainer7(Action action, SemaphoreSlim semaphoreSlim)
{
this.action = action;
this.semaphoreSlim = semaphoreSlim;
}

public void Run()
{
try
{
action();
}
catch (Exception exception)
{
Console.WriteLine(exception);
}

semaphoreSlim.Release();
}
}

private struct ActionContainer8
{
private readonly Action action;

private readonly SemaphoreSlim semaphoreSlim;

public Exception Exception;

public ActionContainer8(Action action, SemaphoreSlim semaphoreSlim, Exception exception)
{
this.action = action;
this.semaphoreSlim = semaphoreSlim;
Exception = exception;
}

public void Run()
{
try
{
action();
}
catch (Exception exception)
{
Exception = exception;
}

semaphoreSlim.Release();
}
}

private int runs;

private readonly ConcurrentQueue<Action> actions = new ConcurrentQueue<Action>();
private readonly ConcurrentQueue<Action> actions = new ();

public ActionTickScheduler()
{
Expand All @@ -178,6 +236,23 @@ public void Schedule(Action<object> action, object state)
{
actions.Enqueue(new ActionContainer2(action, state).Run);
}

public void ScheduleBlocking(Action action, SemaphoreSlim semaphoreSlim)
{
actions.Enqueue(new ActionContainer7(action, semaphoreSlim).Run);
semaphoreSlim.Wait();
}

public void ScheduleBlockingThrows(Action action, SemaphoreSlim semaphoreSlim)
{
var container = new ActionContainer8(action, semaphoreSlim, null);
actions.Enqueue(container.Run);
semaphoreSlim.Wait();
if (container.Exception != null)
{
throw container.Exception;
}
}

public Task ScheduleTask(Action action)
{
Expand Down
1 change: 1 addition & 0 deletions api/AltV.Net.Async/AltAsync.Player.cs
@@ -1,6 +1,7 @@
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using AltV.Net.Async.Elements.Entities;
using AltV.Net.Data;
using AltV.Net.Elements.Entities;
using AltV.Net.Elements.Args;
Expand Down
100 changes: 100 additions & 0 deletions api/AltV.Net.Async/AltAsync.ToAsync.cs
@@ -0,0 +1,100 @@
using AltV.Net.Async.Elements.Entities;
using AltV.Net.Elements.Entities;

namespace AltV.Net.Async
{
public static partial class AltAsync
{
public static IPlayer ToAsync(this IPlayer player, IAsyncContext asyncContext) =>
asyncContext.CreateRef(player) ? new AsyncPlayer<IPlayer>(player, asyncContext) : null;

public static IVehicle ToAsync(this IVehicle vehicle, IAsyncContext asyncContext) =>
asyncContext.CreateRef(vehicle) ? new AsyncVehicle<IVehicle>(vehicle, asyncContext) : null;

public static ICheckpoint ToAsync(this ICheckpoint checkpoint, IAsyncContext asyncContext) =>
asyncContext.CreateRef(checkpoint) ? new AsyncCheckpoint<ICheckpoint>(checkpoint, asyncContext) : null;

public static IColShape ToAsync(this IColShape colShape, IAsyncContext asyncContext) =>
asyncContext.CreateRef(colShape) ? new AsyncColShape<IColShape>(colShape, asyncContext) : null;

public static IBlip ToAsync(this IBlip blip, IAsyncContext asyncContext) =>
asyncContext.CreateRef(blip) ? new AsyncBlip<IBlip>(blip, asyncContext) : null;

public static IVoiceChannel ToAsync(this IVoiceChannel voiceChannel, IAsyncContext asyncContext) =>
asyncContext.CreateRef(voiceChannel) ? new AsyncVoiceChannel<IVoiceChannel>(voiceChannel, asyncContext) : null;

public static bool TryToAsync(this IPlayer thisValue, IAsyncContext asyncContext, out IPlayer player)
{
if (!asyncContext.CreateRef(thisValue, true))
{
player = default;
return false;
}

player = new AsyncPlayer<IPlayer>(thisValue, asyncContext);
return true;
}

public static bool TryToAsync(this IVehicle thisValue, IAsyncContext asyncContext, out IVehicle vehicle)
{
if (!asyncContext.CreateRef(thisValue, true))
{
vehicle = default;
return false;
}

vehicle = new AsyncVehicle<IVehicle>(thisValue, asyncContext);
return true;
}

public static bool TryToAsync(this ICheckpoint thisValue, IAsyncContext asyncContext,
out ICheckpoint checkpoint)
{
if (!asyncContext.CreateRef(thisValue, true))
{
checkpoint = default;
return false;
}

checkpoint = new AsyncCheckpoint<ICheckpoint>(thisValue, asyncContext);
return true;
}

public static bool TryToAsync(this IColShape thisValue, IAsyncContext asyncContext, out IColShape colShape)
{
if (!asyncContext.CreateRef(thisValue, true))
{
colShape = default;
return false;
}

colShape = new AsyncColShape<IColShape>(thisValue, asyncContext);
return true;
}

public static bool TryToAsync(this IBlip thisValue, IAsyncContext asyncContext, out IBlip blip)
{
if (!asyncContext.CreateRef(thisValue, true))
{
blip = default;
return false;
}

blip = new AsyncBlip<IBlip>(thisValue, asyncContext);
return true;
}

public static bool TryToAsync(this IVoiceChannel thisValue, IAsyncContext asyncContext,
out IVoiceChannel voiceChannel)
{
if (!asyncContext.CreateRef(thisValue, true))
{
voiceChannel = default;
return false;
}

voiceChannel = new AsyncVoiceChannel<IVoiceChannel>(thisValue, asyncContext);
return true;
}
}
}
13 changes: 13 additions & 0 deletions api/AltV.Net.Async/AltAsync.cs
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using AltV.Net.Async.Events;
using AltV.Net.Elements.Args;
Expand Down Expand Up @@ -230,6 +231,18 @@ public static Task Do(Action action)
CheckIfAsyncResource();
return AltVAsync.Schedule(action);
}

public static void RunOnMainThreadBlocking(Action action, SemaphoreSlim semaphoreSlim)
{
CheckIfAsyncResource();
AltVAsync.ScheduleBlocking(action, semaphoreSlim);
}

public static void RunOnMainThreadBlockingThrows(Action action, SemaphoreSlim semaphoreSlim)
{
CheckIfAsyncResource();
AltVAsync.ScheduleBlockingThrows(action, semaphoreSlim);
}

public static Task Do(Action<object> action, object value)
{
Expand Down
24 changes: 24 additions & 0 deletions api/AltV.Net.Async/AltVAsync.cs
Expand Up @@ -49,6 +49,30 @@ internal Task Schedule(Action action)

return scheduler.ScheduleTask(action);
}

internal void ScheduleBlocking(Action action, SemaphoreSlim semaphoreSlim)
{
var currThread = Thread.CurrentThread;
if (currThread == mainThread || currThread == TickThread)
{
action();
return;
}

scheduler.ScheduleBlocking(action, semaphoreSlim);
}

internal void ScheduleBlockingThrows(Action action, SemaphoreSlim semaphoreSlim)
{
var currThread = Thread.CurrentThread;
if (currThread == mainThread || currThread == TickThread)
{
action();
return;
}

scheduler.ScheduleBlockingThrows(action, semaphoreSlim);
}

internal Task Schedule(Action<object> action, object value)
{
Expand Down

0 comments on commit 262cadd

Please sign in to comment.