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 972d0c7 commit 67969a6
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 1 deletion.
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 @@ -135,6 +136,8 @@ public long DiscordId
}
}
}

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

public ushort Health
{
Expand Down Expand Up @@ -953,6 +956,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[]" },
};
}
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 @@ -42,6 +43,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 @@ -253,6 +256,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 @@ -576,6 +579,8 @@ public long DiscordId
}
}

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

public ushort Health
{
get
Expand Down Expand Up @@ -1254,6 +1259,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

0 comments on commit 67969a6

Please sign in to comment.