Skip to content

Commit

Permalink
add missing async events
Browse files Browse the repository at this point in the history
  • Loading branch information
Doxoh committed Oct 10, 2023
1 parent a568866 commit 36959cc
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 10 deletions.
108 changes: 108 additions & 0 deletions api/AltV.Net.Async/AltAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,114 @@ public static event PlayerSpawnAsyncDelegate OnPlayerSpawn
remove => Core.PlayerSpawnAsyncEventHandler.Remove(value);
}

public static event RequestSyncedSceneAsyncEventDelegate OnRequestSyncedScene
{
add => Core.RequestSyncedSceneAsyncEventHandler.Add(value);
remove => Core.RequestSyncedSceneAsyncEventHandler.Remove(value);
}

public static event StartSyncedSceneAsyncEventDelegate OnStartSyncedScene
{
add => Core.StartSyncedSceneAsyncEventHandler.Add(value);
remove => Core.StartSyncedSceneAsyncEventHandler.Remove(value);
}

public static event StopSyncedSceneAsyncEventDelegate OnStopSyncedScene
{
add => Core.StopSyncedSceneAsyncEventHandler.Add(value);
remove => Core.StopSyncedSceneAsyncEventHandler.Remove(value);
}

public static event UpdateSyncedSceneAsyncEventDelegate OnUpdateSyncedScene
{
add => Core.UpdateSyncedSceneAsyncEventHandler.Add(value);
remove => Core.UpdateSyncedSceneAsyncEventHandler.Remove(value);
}

public static event ClientRequestObjectAsyncEventDelegate OnClientRequestObject
{
add => Core.ClientRequestObjectAsyncEventHandler.Add(value);
remove => Core.ClientRequestObjectAsyncEventHandler.Remove(value);
}

public static event ClientDeleteObjectAsyncEventDelegate OnClientDeleteObject
{
add => Core.ClientDeleteObjectAsyncEventHandler.Add(value);
remove => Core.ClientDeleteObjectAsyncEventHandler.Remove(value);
}

public static event GivePedScriptedTaskAsyncDelegate OnGivePedScriptedTask
{
add => Core.GivePedScriptedTaskAsyncEventHandler.Add(value);
remove => Core.GivePedScriptedTaskAsyncEventHandler.Remove(value);
}

public static event PedDamageAsyncDelegate OnPedDamage
{
add => Core.PedDamageAsyncEventHandler.Add(value);
remove => Core.PedDamageAsyncEventHandler.Remove(value);
}

public static event PedDeadAsyncDelegate OnPedDead
{
add => Core.PedDeadAsyncEventHandler.Add(value);
remove => Core.PedDeadAsyncEventHandler.Remove(value);
}

public static event PedHealAsyncDelegate OnPedHeal
{
add => Core.PedHealAsyncEventHandler.Add(value);
remove => Core.PedHealAsyncEventHandler.Remove(value);
}

public static event PlayerStartTalkingAsyncDelegate OnPlayerStartTalking
{
add => Core.PlayerStartTalkingAsyncEventHandler.Add(value);
remove => Core.PlayerStartTalkingAsyncEventHandler.Remove(value);
}

public static event PlayerStopTalkingAsyncDelegate OnPlayerStopTalking
{
add => Core.PlayerStopTalkingAsyncEventHandler.Add(value);
remove => Core.PlayerStopTalkingAsyncEventHandler.Remove(value);
}

public static event ScriptRpcAsyncDelegate OnScriptRpc
{
add => Core.ScriptRpcAsyncEventHandler.Add(value);
remove => Core.ScriptRpcAsyncEventHandler.Remove(value);
}

public static event ScriptRpcAnswerAsyncDelegate OnScriptRpcAnswer
{
add => Core.ScriptRpcAnswerAsyncEventHandler.Add(value);
remove => Core.ScriptRpcAnswerAsyncEventHandler.Remove(value);
}

public static event PlayerConnectDeniedAsyncDelegate OnPlayerConnectDenied
{
add => Core.PlayerConnectDeniedAsyncEventHandler.Add(value);
remove => Core.PlayerConnectDeniedAsyncEventHandler.Remove(value);
}

public static event ResourceEventAsyncDelegate OnResourceStart
{
add => Core.ResourceStartAsyncEventHandler.Add(value);
remove => Core.ResourceStartAsyncEventHandler.Remove(value);
}

public static event ResourceEventAsyncDelegate OnResourceStop
{
add => Core.ResourceStopAsyncEventHandler.Add(value);
remove => Core.ResourceStopAsyncEventHandler.Remove(value);
}

public static event ResourceEventAsyncDelegate OnResourceError
{
add => Core.ResourceErrorAsyncEventHandler.Add(value);
remove => Core.ResourceErrorAsyncEventHandler.Remove(value);
}

public static async void Log(string message)
{
var messagePtr = AltNative.StringUtils.StringToHGlobalUtf8(message);
Expand Down
164 changes: 160 additions & 4 deletions api/AltV.Net.Async/AsyncCore.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.Loader;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -11,9 +9,8 @@
using AltV.Net.Data;
using AltV.Net.Elements.Entities;
using AltV.Net.Elements.Args;
using AltV.Net.Events;
using AltV.Net.Shared.Elements.Entities;
using AltV.Net.Shared.Events;
using AltV.Net.Types;

namespace AltV.Net.Async
{
Expand Down Expand Up @@ -41,6 +38,18 @@ public override IEnumerable<string> GetRegisteredServerEvents()
internal readonly AsyncEventHandler<PlayerConnectAsyncDelegate> PlayerConnectAsyncEventHandler =
new(EventType.PLAYER_CONNECT);

internal readonly AsyncEventHandler<PlayerConnectDeniedAsyncDelegate> PlayerConnectDeniedAsyncEventHandler =
new(EventType.PLAYER_CONNECT_DENIED);

internal readonly AsyncEventHandler<ResourceEventAsyncDelegate> ResourceStartAsyncEventHandler =
new(EventType.RESOURCE_START);

internal readonly AsyncEventHandler<ResourceEventAsyncDelegate> ResourceStopAsyncEventHandler =
new(EventType.RESOURCE_STOP);

internal readonly AsyncEventHandler<ResourceEventAsyncDelegate> ResourceErrorAsyncEventHandler =
new(EventType.RESOURCE_ERROR);

internal readonly AsyncEventHandler<PlayerDamageAsyncDelegate> PlayerDamageAsyncEventHandler =
new(EventType.PLAYER_DAMAGE);

Expand Down Expand Up @@ -171,6 +180,29 @@ internal readonly AsyncEventHandler<PlayerChangeVehicleSeatAsyncDelegate>
internal readonly AsyncEventHandler<GivePedScriptedTaskAsyncDelegate> GivePedScriptedTaskAsyncEventHandler =
new(EventType.GIVE_PED_SCRIPTED_TASK);

internal readonly AsyncEventHandler<PedDamageAsyncDelegate> PedDamageAsyncEventHandler =
new(EventType.PED_DAMAGE);

internal readonly AsyncEventHandler<PedDeadAsyncDelegate> PedDeadAsyncEventHandler =
new(EventType.PED_DEATH);

internal readonly AsyncEventHandler<PedHealAsyncDelegate> PedHealAsyncEventHandler =
new(EventType.PED_HEAL);

internal readonly AsyncEventHandler<PlayerStartTalkingAsyncDelegate> PlayerStartTalkingAsyncEventHandler =
new(EventType.PLAYER_START_TALKING);

internal readonly AsyncEventHandler<PlayerStopTalkingAsyncDelegate> PlayerStopTalkingAsyncEventHandler =
new(EventType.PLAYER_STOP_TALKING);

internal readonly AsyncEventHandler<ScriptRpcAsyncDelegate> ScriptRpcAsyncEventHandler =
new(EventType.SCRIPT_RPC_EVENT);

internal readonly AsyncEventHandler<ScriptRpcAnswerAsyncDelegate> ScriptRpcAnswerAsyncEventHandler =
new(EventType.SCRIPT_RPC_ANSWER_EVENT);



public AsyncCore(IntPtr nativePointer, IntPtr resourcePointer, AssemblyLoadContext assemblyLoadContext, ILibrary library, IPoolManager poolManager,
INativeResourcePool nativeResourcePool) : base(nativePointer, resourcePointer, assemblyLoadContext, library, poolManager, nativeResourcePool)
{
Expand Down Expand Up @@ -836,6 +868,130 @@ public override void OnGivePedScriptedTaskEvent(IntPtr eventPointer, IPlayer sou
});
}

public override void OnPlayerConnectDeniedEvent(PlayerConnectDeniedReason reason, string name, string ip, ulong passwordHash,
bool isDebug, string branch, uint majorVersion, string cdnUrl, long discordId)
{
base.OnPlayerConnectDeniedEvent(reason, name, ip, passwordHash, isDebug, branch, majorVersion, cdnUrl, discordId);

if (!PlayerConnectDeniedAsyncEventHandler.HasEvents()) return;
Task.Run(async () =>
{
await PlayerConnectDeniedAsyncEventHandler.CallAsync(@delegate => @delegate(reason, name, ip, passwordHash, isDebug, branch, majorVersion, cdnUrl, discordId));
});
}

public override void OnResourceStartEvent(INativeResource resource)
{
base.OnResourceStartEvent(resource);

if (!ResourceStartAsyncEventHandler.HasEvents()) return;
Task.Run(async () =>
{
await ResourceStartAsyncEventHandler.CallAsync(@delegate => @delegate(resource));
});
}

public override void OnResourceStopEvent(INativeResource resource)
{
base.OnResourceStopEvent(resource);

if (!ResourceStopAsyncEventHandler.HasEvents()) return;
Task.Run(async () =>
{
await ResourceStopAsyncEventHandler.CallAsync(@delegate => @delegate(resource));
});
}

public override void OnResourceErrorEvent(INativeResource resource)
{
base.OnResourceErrorEvent(resource);

if (!ResourceErrorAsyncEventHandler.HasEvents()) return;
Task.Run(async () =>
{
await ResourceErrorAsyncEventHandler.CallAsync(@delegate => @delegate(resource));
});
}

public override void OnPedDamageEvent(IPed ped, IEntity attacker, uint weapon, ushort healthDamage, ushort armourDamage)
{
base.OnPedDamageEvent(ped, attacker, weapon, healthDamage, armourDamage);

if (!PedDamageAsyncEventHandler.HasEvents()) return;
Task.Run(async () =>
{
await PedDamageAsyncEventHandler.CallAsync(@delegate => @delegate(ped, attacker, weapon, healthDamage, armourDamage));
});
}

public override void OnPedDeathEvent(IPed ped, IEntity killer, uint weapon)
{
base.OnPedDeathEvent(ped, killer, weapon);

if (!PedDeadAsyncEventHandler.HasEvents()) return;
Task.Run(async () =>
{
await PedDeadAsyncEventHandler.CallAsync(@delegate => @delegate(ped, killer, weapon));
});
}

public override void OnPedHealEvent(IPed ped, ushort oldHealth, ushort newHealth, ushort oldArmour, ushort newArmour)
{
base.OnPedHealEvent(ped, oldHealth, newHealth, oldArmour, newArmour);

if (!PedHealAsyncEventHandler.HasEvents()) return;
Task.Run(async () =>
{
await PedHealAsyncEventHandler.CallAsync(@delegate => @delegate(ped, oldHealth, newHealth, oldArmour, newArmour));
});
}

public override void OnPlayerStartTalkingEvent(IPlayer player)
{
base.OnPlayerStartTalkingEvent(player);

if (!PlayerStartTalkingAsyncEventHandler.HasEvents()) return;
Task.Run(async () =>
{
await PlayerStartTalkingAsyncEventHandler.CallAsync(@delegate => @delegate(player));
});
}

public override void OnPlayerStopTalkingEvent(IPlayer player)
{
base.OnPlayerStopTalkingEvent(player);

if (!PlayerStopTalkingAsyncEventHandler.HasEvents()) return;
Task.Run(async () =>
{
await PlayerStopTalkingAsyncEventHandler.CallAsync(@delegate => @delegate(player));
});
}

public override void OnScriptRPCEvent(IntPtr eventpointer, IPlayer target, string name, IntPtr[] args, ushort answerId)
{
base.OnScriptRPCEvent(eventpointer, target, name, args, answerId);

if (!ScriptRpcAsyncEventHandler.HasEvents()) return;
Task.Run(async () =>
{
var mValues = MValueConst.CreateFrom(this, args);
var clientScriptRPCEvent = new ScriptRpcEvent(this, eventpointer);
await ScriptRpcAsyncEventHandler.CallAsync(@delegate => @delegate(clientScriptRPCEvent, target, name, mValues.Select(x => x.ToObject()).ToArray(), answerId));
});
}

public override void OnScriptAnswerRPCEvent(IPlayer target, ushort answerid, IntPtr mValue, string answererror)
{
base.OnScriptAnswerRPCEvent(target, answerid, mValue, answererror);

if (!ScriptRpcAnswerAsyncEventHandler.HasEvents()) return;
Task.Run(async () =>
{
await ScriptRpcAnswerAsyncEventHandler.CallAsync(@delegate => @delegate(target, answerid, mValue, answererror));
});
}

public new Function OnClient(string eventName, Function function)
{
if (function == null)
Expand Down
18 changes: 18 additions & 0 deletions api/AltV.Net.Async/Events/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using AltV.Net.Data;
using AltV.Net.Elements.Entities;
using AltV.Net.Shared.Elements.Entities;
using AltV.Net.Types;

namespace AltV.Net.Async.Events
Expand All @@ -12,6 +13,11 @@ namespace AltV.Net.Async.Events

public delegate Task PlayerConnectAsyncDelegate(IPlayer player, string reason);

public delegate Task PlayerConnectDeniedAsyncDelegate(PlayerConnectDeniedReason reason, string name, string ip,
ulong passwordHash, bool isDebug, string branch, uint majorVersion, string cdnUrl, long discordId);

public delegate Task ResourceEventAsyncDelegate(INativeResource resource);

public delegate Task PlayerDamageAsyncDelegate(IPlayer player, IEntity attacker, ushort oldHealth, ushort oldArmor,
ushort oldMaxHealth, ushort oldMaxArmor, uint weapon, ushort healthDamage, ushort armourDamage);

Expand Down Expand Up @@ -98,4 +104,16 @@ public delegate Task WeaponDamageAsyncDelegate(IPlayer player, IEntity target, u
public delegate Task ClientDeleteObjectAsyncEventDelegate(IPlayer target);

public delegate Task GivePedScriptedTaskAsyncDelegate(IPlayer source, IPed target, uint taskType);

public delegate Task PedDamageAsyncDelegate(IPed ped, IEntity attacker, uint weapon, ushort healthDamage, ushort armourDamage);

public delegate Task PedDeadAsyncDelegate(IPed ped, IEntity killer, uint weapon);

public delegate Task PedHealAsyncDelegate(IPed ped, ushort oldHealth, ushort newHealth, ushort oldArmour,
ushort newArmour);
public delegate Task PlayerStartTalkingAsyncDelegate(IPlayer Player);
public delegate Task PlayerStopTalkingAsyncDelegate(IPlayer Player);

public delegate Task ScriptRpcAsyncDelegate(IScriptRPCEvent scriptRpcEvent, IPlayer target, string name, object[] args, ushort answerId);
public delegate Task ScriptRpcAnswerAsyncDelegate(IPlayer target, ushort answerId, object answer, string answerError);
}
10 changes: 5 additions & 5 deletions api/AltV.Net/Core.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,15 @@ public virtual void OnPlayerConnectEvent(IPlayer player, string reason)
}
}

public void onPlayerConnectDenied(PlayerConnectDeniedReason reason, string name, string ip, ulong passwordHash,
public void OnPlayerConnectDenied(PlayerConnectDeniedReason reason, string name, string ip, ulong passwordHash,
bool isDebug, string branch, uint majorVersion, string cdnUrl, long discordId)
{
onPlayerConnectDeniedEvent(reason, name, ip, passwordHash, isDebug, branch, majorVersion, cdnUrl,
OnPlayerConnectDeniedEvent(reason, name, ip, passwordHash, isDebug, branch, majorVersion, cdnUrl,
discordId);
}


public virtual void onPlayerConnectDeniedEvent(PlayerConnectDeniedReason reason, string name, string ip,
public virtual void OnPlayerConnectDeniedEvent(PlayerConnectDeniedReason reason, string name, string ip,
ulong passwordHash, bool isDebug, string branch, uint majorVersion, string cdnUrl, long discordId)
{
foreach (var @delegate in PlayerConnectDeniedEventHandler.GetEvents())
Expand All @@ -273,11 +273,11 @@ public virtual void onPlayerConnectDeniedEvent(PlayerConnectDeniedReason reason,
}
catch (TargetInvocationException exception)
{
Alt.Log("exception at event:" + "onPlayerConnectDeniedEvent" + ":" + exception.InnerException);
Alt.Log("exception at event:" + "OnPlayerConnectDeniedEvent" + ":" + exception.InnerException);
}
catch (Exception exception)
{
Alt.Log("exception at event:" + "onPlayerConnectDeniedEvent" + ":" + exception);
Alt.Log("exception at event:" + "OnPlayerConnectDeniedEvent" + ":" + exception);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion api/AltV.Net/ModuleWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public static void OnPlayerDimensionChange(IntPtr player, int oldDimension, int

public static void OnPlayerConnectDenied(PlayerConnectDeniedReason reason, string name, string ip, ulong passwordHash, byte isDebug, string branch, uint majorVersion, string cdnUrl, long discordId)
{
_core.onPlayerConnectDenied(reason, name, ip, passwordHash, isDebug == 1, branch, majorVersion, cdnUrl,
_core.OnPlayerConnectDenied(reason, name, ip, passwordHash, isDebug == 1, branch, majorVersion, cdnUrl,
discordId);
}

Expand Down

0 comments on commit 36959cc

Please sign in to comment.