Skip to content

Commit

Permalink
add size paramter for emits with message size
Browse files Browse the repository at this point in the history
  • Loading branch information
Doxoh committed Oct 21, 2023
1 parent afd853c commit 296dbd1
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 7 deletions.
34 changes: 34 additions & 0 deletions api/AltV.Net.Async/Elements/Entities/AsyncPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,40 @@ public void Emit(string eventName, params object[] args)
Marshal.FreeHGlobal(eventNamePtr);
}

public void Emit(string eventName, out ulong size, params object[] args)
{
var argsSize = args.Length;
var mValues = new MValueConst[argsSize];
MValueConstLockedNoRefs.CreateFromObjectsLocked(args, mValues);
var eventNamePtr = MemoryUtils.StringToHGlobalUtf8(eventName);

var mValuePointers = new IntPtr[argsSize];
for (var i = 0; i < argsSize; i++)
{
mValuePointers[i] = mValues[i].nativePointer;
}

unsafe
{
size = Alt.Core.Library.Shared.Core_GetMValueArgsSize(NativePointer, mValuePointers, argsSize);
}

lock (Player)
{
if (Player.Exists)
{
Alt.Core.TriggerClientEvent(Player, eventNamePtr, mValues);
}
}

for (var i = 0; i < argsSize; i++)
{
mValues[i].Dispose();
}

Marshal.FreeHGlobal(eventNamePtr);
}

public ushort EmitRPC(string name, params object[] args)
{
var size = args.Length;
Expand Down
4 changes: 2 additions & 2 deletions api/AltV.Net.CApi/Libraries/ClientLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ public unsafe interface IClientLibrary

public unsafe class ClientLibrary : IClientLibrary
{
public readonly uint Methods = 1735;
public readonly uint Methods = 1737;
public delegate* unmanaged[Cdecl]<nint, nint, void> Audio_AddOutput { get; }
public delegate* unmanaged[Cdecl]<nint, nint> Audio_GetBaseObject { get; }
public delegate* unmanaged[Cdecl]<nint, double> Audio_GetCurrentTime { get; }
Expand Down Expand Up @@ -3512,7 +3512,7 @@ private IntPtr GetUnmanagedPtr<T>(IDictionary<ulong, IntPtr> funcTable, ulong ha
public ClientLibrary(Dictionary<ulong, IntPtr> funcTable)
{
if (!funcTable.TryGetValue(0, out var capiHash)) Outdated = true;
else if (capiHash == IntPtr.Zero || *(ulong*)capiHash != 4677548953854785821UL) Outdated = true;
else if (capiHash == IntPtr.Zero || *(ulong*)capiHash != 16677003916101455049UL) Outdated = true;
Audio_AddOutput = (delegate* unmanaged[Cdecl]<nint, nint, void>) GetUnmanagedPtr<Audio_AddOutputDelegate>(funcTable, 9914412815391408844UL, Audio_AddOutputFallback);
Audio_GetBaseObject = (delegate* unmanaged[Cdecl]<nint, nint>) GetUnmanagedPtr<Audio_GetBaseObjectDelegate>(funcTable, 6330360502401226894UL, Audio_GetBaseObjectFallback);
Audio_GetCurrentTime = (delegate* unmanaged[Cdecl]<nint, double>) GetUnmanagedPtr<Audio_GetCurrentTimeDelegate>(funcTable, 2944324482134975819UL, Audio_GetCurrentTimeFallback);
Expand Down
4 changes: 2 additions & 2 deletions api/AltV.Net.CApi/Libraries/ServerLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ public unsafe interface IServerLibrary

public unsafe class ServerLibrary : IServerLibrary
{
public readonly uint Methods = 1735;
public readonly uint Methods = 1737;
public delegate* unmanaged[Cdecl]<nint, nint, void> BaseObject_DeleteSyncedMetaData { get; }
public delegate* unmanaged[Cdecl]<nint, nint[], nint[], ulong, void> BaseObject_SetMultipleSyncedMetaData { get; }
public delegate* unmanaged[Cdecl]<nint, nint, nint, void> BaseObject_SetSyncedMetaData { get; }
Expand Down Expand Up @@ -1884,7 +1884,7 @@ private IntPtr GetUnmanagedPtr<T>(IDictionary<ulong, IntPtr> funcTable, ulong ha
public ServerLibrary(Dictionary<ulong, IntPtr> funcTable)
{
if (!funcTable.TryGetValue(0, out var capiHash)) Outdated = true;
else if (capiHash == IntPtr.Zero || *(ulong*)capiHash != 4677548953854785821UL) Outdated = true;
else if (capiHash == IntPtr.Zero || *(ulong*)capiHash != 16677003916101455049UL) Outdated = true;
BaseObject_DeleteSyncedMetaData = (delegate* unmanaged[Cdecl]<nint, nint, void>) GetUnmanagedPtr<BaseObject_DeleteSyncedMetaDataDelegate>(funcTable, 8228424877092269355UL, BaseObject_DeleteSyncedMetaDataFallback);
BaseObject_SetMultipleSyncedMetaData = (delegate* unmanaged[Cdecl]<nint, nint[], nint[], ulong, void>) GetUnmanagedPtr<BaseObject_SetMultipleSyncedMetaDataDelegate>(funcTable, 1390762125822890831UL, BaseObject_SetMultipleSyncedMetaDataFallback);
BaseObject_SetSyncedMetaData = (delegate* unmanaged[Cdecl]<nint, nint, nint, void>) GetUnmanagedPtr<BaseObject_SetSyncedMetaDataDelegate>(funcTable, 8002999088966424231UL, BaseObject_SetSyncedMetaDataFallback);
Expand Down
14 changes: 12 additions & 2 deletions api/AltV.Net.CApi/Libraries/SharedLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ public unsafe interface ISharedLibrary
public delegate* unmanaged[Cdecl]<byte> Core_GetEventTypeSize { get; }
public delegate* unmanaged[Cdecl]<nint, ulong*, nint> Core_GetMarkers { get; }
public delegate* unmanaged[Cdecl]<nint, nint, nint> Core_GetMetaData { get; }
public delegate* unmanaged[Cdecl]<nint, nint[], int, ulong> Core_GetMValueArgsSize { get; }
public delegate* unmanaged[Cdecl]<nint, nint, ulong> Core_GetMValueSize { get; }
public delegate* unmanaged[Cdecl]<nint, int> Core_GetNetTime { get; }
public delegate* unmanaged[Cdecl]<nint, ulong*, nint> Core_GetNetworkObjects { get; }
public delegate* unmanaged[Cdecl]<nint, ulong*, nint> Core_GetPeds { get; }
Expand Down Expand Up @@ -410,7 +412,7 @@ public unsafe interface ISharedLibrary

public unsafe class SharedLibrary : ISharedLibrary
{
public readonly uint Methods = 1735;
public readonly uint Methods = 1737;
public delegate* unmanaged[Cdecl]<nint, uint> Audio_GetID { get; }
public delegate* unmanaged[Cdecl]<nint, uint> AudioAttachedOutput_GetID { get; }
public delegate* unmanaged[Cdecl]<nint, uint> AudioFilter_GetID { get; }
Expand Down Expand Up @@ -590,6 +592,8 @@ public unsafe class SharedLibrary : ISharedLibrary
public delegate* unmanaged[Cdecl]<byte> Core_GetEventTypeSize { get; }
public delegate* unmanaged[Cdecl]<nint, ulong*, nint> Core_GetMarkers { get; }
public delegate* unmanaged[Cdecl]<nint, nint, nint> Core_GetMetaData { get; }
public delegate* unmanaged[Cdecl]<nint, nint[], int, ulong> Core_GetMValueArgsSize { get; }
public delegate* unmanaged[Cdecl]<nint, nint, ulong> Core_GetMValueSize { get; }
public delegate* unmanaged[Cdecl]<nint, int> Core_GetNetTime { get; }
public delegate* unmanaged[Cdecl]<nint, ulong*, nint> Core_GetNetworkObjects { get; }
public delegate* unmanaged[Cdecl]<nint, ulong*, nint> Core_GetPeds { get; }
Expand Down Expand Up @@ -1165,6 +1169,10 @@ public unsafe class SharedLibrary : ISharedLibrary
private static nint Core_GetMarkersFallback(nint _core, ulong* _size) => throw new Exceptions.OutdatedSdkException("Core_GetMarkers", "Core_GetMarkers SDK method is outdated. Please update your module nuget");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate nint Core_GetMetaDataDelegate(nint _core, nint _key);
private static nint Core_GetMetaDataFallback(nint _core, nint _key) => throw new Exceptions.OutdatedSdkException("Core_GetMetaData", "Core_GetMetaData SDK method is outdated. Please update your module nuget");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate ulong Core_GetMValueArgsSizeDelegate(nint _core, nint[] args, int _size);
private static ulong Core_GetMValueArgsSizeFallback(nint _core, nint[] args, int _size) => throw new Exceptions.OutdatedSdkException("Core_GetMValueArgsSize", "Core_GetMValueArgsSize SDK method is outdated. Please update your module nuget");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate ulong Core_GetMValueSizeDelegate(nint _core, nint _args);
private static ulong Core_GetMValueSizeFallback(nint _core, nint _args) => throw new Exceptions.OutdatedSdkException("Core_GetMValueSize", "Core_GetMValueSize SDK method is outdated. Please update your module nuget");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate int Core_GetNetTimeDelegate(nint _core);
private static int Core_GetNetTimeFallback(nint _core) => throw new Exceptions.OutdatedSdkException("Core_GetNetTime", "Core_GetNetTime SDK method is outdated. Please update your module nuget");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate nint Core_GetNetworkObjectsDelegate(nint _core, ulong* _size);
Expand Down Expand Up @@ -1608,7 +1616,7 @@ private IntPtr GetUnmanagedPtr<T>(IDictionary<ulong, IntPtr> funcTable, ulong ha
public SharedLibrary(Dictionary<ulong, IntPtr> funcTable)
{
if (!funcTable.TryGetValue(0, out var capiHash)) Outdated = true;
else if (capiHash == IntPtr.Zero || *(ulong*)capiHash != 4677548953854785821UL) Outdated = true;
else if (capiHash == IntPtr.Zero || *(ulong*)capiHash != 16677003916101455049UL) Outdated = true;
Audio_GetID = (delegate* unmanaged[Cdecl]<nint, uint>) GetUnmanagedPtr<Audio_GetIDDelegate>(funcTable, 4464042055475980737UL, Audio_GetIDFallback);
AudioAttachedOutput_GetID = (delegate* unmanaged[Cdecl]<nint, uint>) GetUnmanagedPtr<AudioAttachedOutput_GetIDDelegate>(funcTable, 17725794901805112189UL, AudioAttachedOutput_GetIDFallback);
AudioFilter_GetID = (delegate* unmanaged[Cdecl]<nint, uint>) GetUnmanagedPtr<AudioFilter_GetIDDelegate>(funcTable, 8824535635529306325UL, AudioFilter_GetIDFallback);
Expand Down Expand Up @@ -1788,6 +1796,8 @@ public SharedLibrary(Dictionary<ulong, IntPtr> funcTable)
Core_GetEventTypeSize = (delegate* unmanaged[Cdecl]<byte>) GetUnmanagedPtr<Core_GetEventTypeSizeDelegate>(funcTable, 13737530370025977174UL, Core_GetEventTypeSizeFallback);
Core_GetMarkers = (delegate* unmanaged[Cdecl]<nint, ulong*, nint>) GetUnmanagedPtr<Core_GetMarkersDelegate>(funcTable, 7482854450085275693UL, Core_GetMarkersFallback);
Core_GetMetaData = (delegate* unmanaged[Cdecl]<nint, nint, nint>) GetUnmanagedPtr<Core_GetMetaDataDelegate>(funcTable, 2139798095052897524UL, Core_GetMetaDataFallback);
Core_GetMValueArgsSize = (delegate* unmanaged[Cdecl]<nint, nint[], int, ulong>) GetUnmanagedPtr<Core_GetMValueArgsSizeDelegate>(funcTable, 2001253713694863162UL, Core_GetMValueArgsSizeFallback);
Core_GetMValueSize = (delegate* unmanaged[Cdecl]<nint, nint, ulong>) GetUnmanagedPtr<Core_GetMValueSizeDelegate>(funcTable, 3166577616070768315UL, Core_GetMValueSizeFallback);
Core_GetNetTime = (delegate* unmanaged[Cdecl]<nint, int>) GetUnmanagedPtr<Core_GetNetTimeDelegate>(funcTable, 15652019729912249391UL, Core_GetNetTimeFallback);
Core_GetNetworkObjects = (delegate* unmanaged[Cdecl]<nint, ulong*, nint>) GetUnmanagedPtr<Core_GetNetworkObjectsDelegate>(funcTable, 8454955647873390265UL, Core_GetNetworkObjectsFallback);
Core_GetPeds = (delegate* unmanaged[Cdecl]<nint, ulong*, nint>) GetUnmanagedPtr<Core_GetPedsDelegate>(funcTable, 5411021830103603795UL, Core_GetPedsFallback);
Expand Down
30 changes: 30 additions & 0 deletions api/AltV.Net.Mock/MockCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ public void TriggerClientEventForAll(string eventName, params object[] args)
throw new NotImplementedException();
}

public void TriggerClientEventForAll(string eventName, out ulong size, params object[] args)
{
throw new NotImplementedException();
}

public void TriggerClientEventForSome(IPlayer[] clients, IntPtr eventNamePtr, MValueConst[] args)
{
throw new NotImplementedException();
Expand Down Expand Up @@ -248,6 +253,11 @@ public void TriggerClientEventForSome(IPlayer[] clients, string eventName, param
throw new NotImplementedException();
}

public void TriggerClientEventForSome(IPlayer[] clients, string eventName, out ulong size, params object[] args)
{
throw new NotImplementedException();
}

public void TriggerClientEventUnreliable(IPlayer player, IntPtr eventNamePtr, MValueConst[] args)
{
throw new NotImplementedException();
Expand Down Expand Up @@ -308,6 +318,11 @@ public void TriggerClientEventUnreliableForAll(string eventName, params object[]
throw new NotImplementedException();
}

public void TriggerClientEventUnreliableForAll(string eventNamePtr, out ulong size, params object[] args)
{
throw new NotImplementedException();
}

public void TriggerClientEventUnreliableForSome(IPlayer[] clients, IntPtr eventNamePtr, MValueConst[] args)
{
throw new NotImplementedException();
Expand Down Expand Up @@ -338,6 +353,11 @@ public void TriggerClientEventUnreliableForSome(IPlayer[] clients, string eventN
throw new NotImplementedException();
}

public void TriggerClientEventUnreliableForSome(IPlayer[] clients, string eventName, out ulong size, params object[] args)
{
throw new NotImplementedException();
}

public void TriggerClientRPCAnswer(IPlayer target, ushort answerId, object answer, string error)
{
throw new NotImplementedException();
Expand Down Expand Up @@ -742,6 +762,11 @@ public void TriggerClientEvent(IPlayer player, string eventName, params object[]
throw new NotImplementedException();
}

public void TriggerClientEvent(IPlayer player, string eventName, out ulong size, params object[] args)
{
throw new NotImplementedException();
}

public void TriggerClientEventForAll(IntPtr eventNamePtr, MValueConst[] args)
{
throw new NotImplementedException();
Expand Down Expand Up @@ -853,6 +878,11 @@ public void TriggerLocalEvent(string eventName, params object[] args)
throw new NotImplementedException();
}

public void TriggerLocalEvent(string eventName, out ulong size, params object[] args)
{
throw new NotImplementedException();
}

public VoiceConnectionState GetVoiceConnectionState()
{
throw new NotImplementedException();
Expand Down
1 change: 1 addition & 0 deletions api/AltV.Net.Shared/ISharedCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ void CreateMValueDict(out MValueConst mValue, string[] keys, MValueConst[] val,
void TriggerLocalEvent(IntPtr eventNamePtr, params object[] args);

void TriggerLocalEvent(string eventName, params object[] args);
void TriggerLocalEvent(string eventName, out ulong size, params object[] args);
#endregion

VoiceConnectionState GetVoiceConnectionState();
Expand Down
25 changes: 25 additions & 0 deletions api/AltV.Net.Shared/SharedCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,31 @@ public void TriggerLocalEvent(string eventName, params object[] args)
mValues[i].Dispose();
}
}

public void TriggerLocalEvent(string eventName, out ulong size, params object[] args)
{
if (args == null) throw new ArgumentException("Arguments array should not be null.");
var argsSize = args.Length;
var mValues = new MValueConst[argsSize];
CreateMValues(mValues, args);
var mValuePointers = new IntPtr[argsSize];
for (var i = 0; i < argsSize; i++)
{
mValuePointers[i] = mValues[i].nativePointer;
}

unsafe
{
size = Library.Shared.Core_GetMValueArgsSize(NativePointer, mValuePointers, argsSize);
}

TriggerLocalEvent(eventName, mValues);
for (var i = 0; i < argsSize; i++)
{
mValues[i].Dispose();
}
}

#endregion


Expand Down
13 changes: 13 additions & 0 deletions api/AltV.Net/Alt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,32 @@ public static partial class Alt
public static bool IsDebug => Core.IsDebug;

public static void Emit(string eventName, params object[] args) => Core.TriggerLocalEvent(eventName, args);
public static void Emit(string eventName, out ulong size, params object[] args) => Core.TriggerLocalEvent(eventName, out size, args);

public static void EmitAllClients(string eventName, params object[] args) =>
Core.TriggerClientEventForAll(eventName, args);

public static void EmitAllClients(string eventName, out ulong size, params object[] args) =>
Core.TriggerClientEventForAll(eventName, out size, args);

public static void EmitClients(IPlayer[] clients, string eventName, params object[] args) =>
Core.TriggerClientEventForSome(clients, eventName, args);

public static void EmitClients(IPlayer[] clients, string eventName, out ulong size, params object[] args) =>
Core.TriggerClientEventForSome(clients, eventName, out size, args);

public static void EmitEventUnreliableAllClients(string eventName, params object[] args) =>
Core.TriggerClientEventUnreliableForAll(eventName, args);

public static void EmitEventUnreliableAllClients(string eventName, out ulong size, params object[] args) =>
Core.TriggerClientEventUnreliableForAll(eventName, out size, args);

public static void EmitUnreliableClients(IPlayer[] clients, string eventName, params object[] args) =>
Core.TriggerClientEventUnreliableForSome(clients, eventName, args);

public static void EmitUnreliableClients(IPlayer[] clients, string eventName, out ulong size, params object[] args) =>
Core.TriggerClientEventUnreliableForSome(clients, eventName, out size, args);

public static IEnumerable<string> GetRegisteredClientEvents() => Core.GetRegisteredClientEvents();
public static IEnumerable<string> GetRegisteredServerEvents() => Core.GetRegisteredServerEvents();

Expand Down
Loading

0 comments on commit 296dbd1

Please sign in to comment.