Skip to content

Commit

Permalink
Implement streamed entities
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDutchDev committed Dec 26, 2024
1 parent 6394eef commit b8ccc92
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 9 deletions.
21 changes: 21 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 @@ -123,6 +124,8 @@ public long DiscordId
}
}
}

public List<StreamedEntityDistance> StreamedEntityDistances => GetStreamedEntities();

public ushort Health
{
Expand Down Expand Up @@ -941,6 +944,24 @@ public WeaponData[] GetWeapons()
}
}

public uint GetStreamedEntitiesCount()
{
lock (Player)
{
if (!AsyncContext.CheckIfExistsNullable(Player)) return default;
return Player.GetStreamedEntitiesCount();
}
}

public List<StreamedEntityDistance> GetStreamedEntities()
{
lock (Player)
{
if (!AsyncContext.CheckIfExistsNullable(Player)) return default;
return Player.GetStreamedEntities();
}
}

public void ClearBloodDamage()
{
lock (Player)
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[]" },
};
}
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 = 1809;
public readonly uint Methods = 1811;
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 != 10075699596264467259UL) Outdated = true;
else if (capiHash == IntPtr.Zero || *(ulong*)capiHash != 13612278072387064778UL) 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 @@ -206,6 +206,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*, ushort[], byte[], 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 @@ -488,7 +490,7 @@ public unsafe interface IServerLibrary

public unsafe class ServerLibrary : IServerLibrary
{
public readonly uint Methods = 1809;
public readonly uint Methods = 1811;
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 @@ -684,6 +686,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*, ushort[], byte[], 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 @@ -1352,6 +1356,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, ushort[] distances, byte[] types, uint _size);
private static void Player_GetStreamedEntitiesFallback(nint _player, nint* _entities, ushort[] distances, byte[] types, 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 @@ -1378,8 +1386,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 @@ -1917,7 +1925,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 != 10075699596264467259UL) Outdated = true;
else if (capiHash == IntPtr.Zero || *(ulong*)capiHash != 13612278072387064778UL) 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 @@ -2113,6 +2121,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*, ushort[], byte[], uint, void>) GetUnmanagedPtr<Player_GetStreamedEntitiesDelegate>(funcTable, 16539572063395996721UL, 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 = 1809;
public readonly uint Methods = 1811;
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 != 10075699596264467259UL) Outdated = true;
else if (capiHash == IntPtr.Zero || *(ulong*)capiHash != 13612278072387064778UL) 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);
15 changes: 15 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 @@ -40,6 +41,8 @@ public interface IPlayer : ISharedPlayer, IEntity

long DiscordId { get; }

List<StreamedEntityDistance> StreamedEntityDistances { get; }

/// <summary>
/// Gets and Sets the players health
/// </summary>
Expand Down Expand Up @@ -251,6 +254,18 @@ void SetDateTime(int day, int month, int year, int hour,
/// <returns></returns>
WeaponData[] GetWeapons();

/// <summary>
/// Returns the amount of streamed entities
/// </summary>
/// <returns></returns>
uint GetStreamedEntitiesCount();

/// <summary>
/// Returns a list of streamed entities
/// </summary>
/// <returns></returns>
List<StreamedEntityDistance> GetStreamedEntities();

/// <summary>
/// Clears the blood damage of the player
/// </summary>
Expand Down
42 changes: 42 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 @@ -562,6 +565,8 @@ public long DiscordId
}
}

public List<StreamedEntityDistance> StreamedEntityDistances => GetStreamedEntities();

public ushort Health
{
get
Expand Down Expand Up @@ -1240,6 +1245,43 @@ public WeaponData[] GetWeapons()
}
}

public uint GetStreamedEntitiesCount()
{
unsafe
{
CheckIfEntityExists();
return Core.Library.Server.Player_GetStreamedEntitiesCount(PlayerNativePointer);
}
}

public List<StreamedEntityDistance> GetStreamedEntities()
{
unsafe
{
CheckIfEntityExists();

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, distances, types, 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 void Kick(string reason)
{
unsafe
Expand Down
2 changes: 1 addition & 1 deletion runtime

0 comments on commit b8ccc92

Please sign in to comment.