Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#43 implement streamed entities property #44

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions api/AltV.Net.Async/Elements/Entities/AsyncPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -136,6 +137,18 @@ public long DiscordId
}
}

public List<StreamedEntityDistance> StreamedEntities
{
get
{
lock (Player)
{
if (!AsyncContext.CheckIfExistsOrCachedNullable(Player)) return default;
return Player.StreamedEntities;
}
}
}

public ushort Health
{
get
Expand Down
1 change: 1 addition & 0 deletions api/AltV.Net.CApi.Generator/TypeRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,6 @@ public static class TypeRegistry
{ "ClrAmmoFlags*", "nint" },
{ "ClrDecoration**", "nint" },
{ "aabb_t&", "AABB*" },
{ "uint16_t[]", "ushort[]" },
Doxoh marked this conversation as resolved.
Show resolved Hide resolved
};
}
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 @@ -938,7 +938,7 @@ public unsafe interface IClientLibrary

public unsafe class ClientLibrary : IClientLibrary
{
public readonly uint Methods = 1811;
public readonly uint Methods = 1813;
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 @@ -3717,7 +3717,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 != 2737407539021183714UL) Outdated = true;
else if (capiHash == IntPtr.Zero || *(ulong*)capiHash != 14078434553092505847UL) 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
18 changes: 14 additions & 4 deletions api/AltV.Net.CApi/Libraries/ServerLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ public unsafe interface IServerLibrary
public delegate* unmanaged[Cdecl]<nint, byte> Player_GetSendNames { get; }
public delegate* unmanaged[Cdecl]<nint, int*, nint> Player_GetSocialClubName { get; }
public delegate* unmanaged[Cdecl]<nint, ulong> Player_GetSocialID { get; }
public delegate* unmanaged[Cdecl]<nint, nint*, byte[], ushort[], uint, void> Player_GetStreamedEntities { get; }
public delegate* unmanaged[Cdecl]<nint, uint> Player_GetStreamedEntitiesCount { get; }
public delegate* unmanaged[Cdecl]<nint, uint, ushort> Player_GetWeaponAmmo { get; }
public delegate* unmanaged[Cdecl]<nint, ulong> Player_GetWeaponCount { get; }
public delegate* unmanaged[Cdecl]<nint, nint*, uint*, void> Player_GetWeapons { get; }
Expand Down Expand Up @@ -490,7 +492,7 @@ public unsafe interface IServerLibrary

public unsafe class ServerLibrary : IServerLibrary
{
public readonly uint Methods = 1811;
public readonly uint Methods = 1813;
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 @@ -688,6 +690,8 @@ public unsafe class ServerLibrary : IServerLibrary
public delegate* unmanaged[Cdecl]<nint, byte> Player_GetSendNames { get; }
public delegate* unmanaged[Cdecl]<nint, int*, nint> Player_GetSocialClubName { get; }
public delegate* unmanaged[Cdecl]<nint, ulong> Player_GetSocialID { get; }
public delegate* unmanaged[Cdecl]<nint, nint*, byte[], ushort[], uint, void> Player_GetStreamedEntities { get; }
public delegate* unmanaged[Cdecl]<nint, uint> Player_GetStreamedEntitiesCount { get; }
public delegate* unmanaged[Cdecl]<nint, uint, ushort> Player_GetWeaponAmmo { get; }
public delegate* unmanaged[Cdecl]<nint, ulong> Player_GetWeaponCount { get; }
public delegate* unmanaged[Cdecl]<nint, nint*, uint*, void> Player_GetWeapons { get; }
Expand Down Expand Up @@ -1360,6 +1364,10 @@ public unsafe class ServerLibrary : IServerLibrary
private static nint Player_GetSocialClubNameFallback(nint _player, int* _size) => throw new Exceptions.OutdatedSdkException("Player_GetSocialClubName", "Player_GetSocialClubName SDK method is outdated. Please update your module nuget");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate ulong Player_GetSocialIDDelegate(nint _player);
private static ulong Player_GetSocialIDFallback(nint _player) => throw new Exceptions.OutdatedSdkException("Player_GetSocialID", "Player_GetSocialID SDK method is outdated. Please update your module nuget");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void Player_GetStreamedEntitiesDelegate(nint _player, nint* _entities, byte[] types, ushort[] distances, uint _size);
private static void Player_GetStreamedEntitiesFallback(nint _player, nint* _entities, byte[] types, ushort[] distances, uint _size) => throw new Exceptions.OutdatedSdkException("Player_GetStreamedEntities", "Player_GetStreamedEntities SDK method is outdated. Please update your module nuget");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate uint Player_GetStreamedEntitiesCountDelegate(nint _player);
private static uint Player_GetStreamedEntitiesCountFallback(nint _player) => throw new Exceptions.OutdatedSdkException("Player_GetStreamedEntitiesCount", "Player_GetStreamedEntitiesCount SDK method is outdated. Please update your module nuget");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate ushort Player_GetWeaponAmmoDelegate(nint _player, uint _weaponHash);
private static ushort Player_GetWeaponAmmoFallback(nint _player, uint _weaponHash) => throw new Exceptions.OutdatedSdkException("Player_GetWeaponAmmo", "Player_GetWeaponAmmo SDK method is outdated. Please update your module nuget");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate ulong Player_GetWeaponCountDelegate(nint _player);
Expand All @@ -1386,8 +1394,8 @@ public unsafe class ServerLibrary : IServerLibrary
private static byte Player_IsNetworkOwnershipDisabledFallback(nint _player) => throw new Exceptions.OutdatedSdkException("Player_IsNetworkOwnershipDisabled", "Player_IsNetworkOwnershipDisabled SDK method is outdated. Please update your module nuget");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate byte Player_IsStealthyDelegate(nint _player);
private static byte Player_IsStealthyFallback(nint _player) => throw new Exceptions.OutdatedSdkException("Player_IsStealthy", "Player_IsStealthy SDK method is outdated. Please update your module nuget");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate byte Player_IsSuperJumpEnabledDelegate(nint _playere);
private static byte Player_IsSuperJumpEnabledFallback(nint _playere) => throw new Exceptions.OutdatedSdkException("Player_IsSuperJumpEnabled", "Player_IsSuperJumpEnabled SDK method is outdated. Please update your module nuget");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate byte Player_IsSuperJumpEnabledDelegate(nint _player);
private static byte Player_IsSuperJumpEnabledFallback(nint _player) => throw new Exceptions.OutdatedSdkException("Player_IsSuperJumpEnabled", "Player_IsSuperJumpEnabled SDK method is outdated. Please update your module nuget");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void Player_KickDelegate(nint _player, nint _reason);
private static void Player_KickFallback(nint _player, nint _reason) => throw new Exceptions.OutdatedSdkException("Player_Kick", "Player_Kick SDK method is outdated. Please update your module nuget");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void Player_PlayAmbientSpeechDelegate(nint _player, nint _speechName, nint _speechParam, uint _speechDictHash);
Expand Down Expand Up @@ -1925,7 +1933,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 != 2737407539021183714UL) Outdated = true;
else if (capiHash == IntPtr.Zero || *(ulong*)capiHash != 14078434553092505847UL) 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 Expand Up @@ -2123,6 +2131,8 @@ public ServerLibrary(Dictionary<ulong, IntPtr> funcTable)
Player_GetSendNames = (delegate* unmanaged[Cdecl]<nint, byte>) GetUnmanagedPtr<Player_GetSendNamesDelegate>(funcTable, 7490273379384857895UL, Player_GetSendNamesFallback);
Player_GetSocialClubName = (delegate* unmanaged[Cdecl]<nint, int*, nint>) GetUnmanagedPtr<Player_GetSocialClubNameDelegate>(funcTable, 17452312619664438538UL, Player_GetSocialClubNameFallback);
Player_GetSocialID = (delegate* unmanaged[Cdecl]<nint, ulong>) GetUnmanagedPtr<Player_GetSocialIDDelegate>(funcTable, 17807664466527734655UL, Player_GetSocialIDFallback);
Player_GetStreamedEntities = (delegate* unmanaged[Cdecl]<nint, nint*, byte[], ushort[], uint, void>) GetUnmanagedPtr<Player_GetStreamedEntitiesDelegate>(funcTable, 17370306352290181349UL, Player_GetStreamedEntitiesFallback);
Player_GetStreamedEntitiesCount = (delegate* unmanaged[Cdecl]<nint, uint>) GetUnmanagedPtr<Player_GetStreamedEntitiesCountDelegate>(funcTable, 13915984405046061551UL, Player_GetStreamedEntitiesCountFallback);
Player_GetWeaponAmmo = (delegate* unmanaged[Cdecl]<nint, uint, ushort>) GetUnmanagedPtr<Player_GetWeaponAmmoDelegate>(funcTable, 2928381910125083497UL, Player_GetWeaponAmmoFallback);
Player_GetWeaponCount = (delegate* unmanaged[Cdecl]<nint, ulong>) GetUnmanagedPtr<Player_GetWeaponCountDelegate>(funcTable, 17600594564491002166UL, Player_GetWeaponCountFallback);
Player_GetWeapons = (delegate* unmanaged[Cdecl]<nint, nint*, uint*, void>) GetUnmanagedPtr<Player_GetWeaponsDelegate>(funcTable, 3618744060322552484UL, Player_GetWeaponsFallback);
Expand Down
4 changes: 2 additions & 2 deletions api/AltV.Net.CApi/Libraries/SharedLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public unsafe interface ISharedLibrary

public unsafe class SharedLibrary : ISharedLibrary
{
public readonly uint Methods = 1811;
public readonly uint Methods = 1813;
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 @@ -1669,7 +1669,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 != 2737407539021183714UL) Outdated = true;
else if (capiHash == IntPtr.Zero || *(ulong*)capiHash != 14078434553092505847UL) 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
10 changes: 10 additions & 0 deletions api/AltV.Net/Data/StreamedEntityDistance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using AltV.Net.Elements.Entities;

namespace AltV.Net.Data;

/// <summary>
/// Streamed entity
/// </summary>
/// <param name="Entity">The entity</param>
/// <param name="Distance">The distance between the player and this entity</param>
public record StreamedEntityDistance(IEntity Entity, int Distance);
3 changes: 3 additions & 0 deletions api/AltV.Net/Elements/Entities/IPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using AltV.Net.Data;
using AltV.Net.Elements.Args;
Expand Down Expand Up @@ -42,6 +43,8 @@ public interface IPlayer : ISharedPlayer, IEntity

long DiscordId { get; }

List<StreamedEntityDistance> StreamedEntities { get; }

/// <summary>
/// Gets and Sets the players health
/// </summary>
Expand Down
34 changes: 34 additions & 0 deletions api/AltV.Net/Elements/Entities/Player.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.InteropServices;
using AltV.Net.CApi;
using AltV.Net.Data;
using AltV.Net.Elements.Args;
using AltV.Net.Elements.Pools;
using AltV.Net.Enums;
using AltV.Net.Shared.Elements.Entities;
using AltV.Net.Shared.Utils;
Expand Down Expand Up @@ -576,6 +579,37 @@ public long DiscordId
}
}

public List<StreamedEntityDistance> StreamedEntities
{
get
{
unsafe
{
CheckIfEntityExistsOrCached();

var entitiesCount = Core.Library.Server.Player_GetStreamedEntitiesCount(PlayerNativePointer);

var pointers = IntPtr.Zero;
var distances = new ushort[entitiesCount];
var types = new byte[entitiesCount];
Core.Library.Server.Player_GetStreamedEntities(PlayerNativePointer, &pointers, types, distances, entitiesCount);

var entityPtrArray = new IntPtr[entitiesCount];
Marshal.Copy(pointers, entityPtrArray, 0, (int) entitiesCount);
Core.Library.Shared.FreeVoidPointerArray(pointers);

var streamedEntities = new List<StreamedEntityDistance>((int) entitiesCount);
for (ulong i = 0; i < entitiesCount; i++)
{
var entity = (IEntity) Core.PoolManager.GetOrCreate(Core, entityPtrArray[i], (BaseObjectType) types[i]);
streamedEntities.Add(new StreamedEntityDistance(entity, distances[i]));
}

return streamedEntities;
}
}
}

public ushort Health
{
get
Expand Down
2 changes: 1 addition & 1 deletion runtime