Skip to content

Commit

Permalink
mark all entity constructor as obsolete and add all Alt.Create*
Browse files Browse the repository at this point in the history
  • Loading branch information
Doxoh committed May 28, 2023
1 parent 3c75c30 commit 5e4440d
Show file tree
Hide file tree
Showing 37 changed files with 288 additions and 274 deletions.
16 changes: 16 additions & 0 deletions api/AltV.Net.Async/AltAsync.Marker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Threading.Tasks;
using AltV.Net.Data;
using AltV.Net.Elements.Entities;
using AltV.Net.Enums;
using AltV.Net.Shared.Enums;

namespace AltV.Net.Async;

public partial class AltAsync
{
public static Task<IMarker> CreateMarker(MarkerType type, Position pos, Rgba color) =>
AltVAsync.Schedule(() => Alt.CreateMarker(null, type, pos, color));

public static Task<IMarker> CreateMarker(IPlayer player, MarkerType type, Position pos, Rgba color) =>
AltVAsync.Schedule(() => Alt.CreateMarker(player, type, pos, color));
}
15 changes: 15 additions & 0 deletions api/AltV.Net.Async/AltAsync.NetworkObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Threading.Tasks;
using AltV.Net.Data;
using AltV.Net.Elements.Entities;

namespace AltV.Net.Async
{
public partial class AltAsync
{
public static Task<INetworkObject> CreateNetworkObject(uint model, Position position, Rotation rotation, byte alpha = 255, byte textureVariation = 0, ushort lodDistance = 100) =>
AltVAsync.Schedule(() => Alt.CreateNetworkObject(model, position, rotation, alpha, textureVariation, lodDistance));

public static Task<INetworkObject> CreateNetworkObject(string model, Position position, Rotation rotation, byte alpha = 255, byte textureVariation = 0, ushort lodDistance = 100) =>
AltVAsync.Schedule(() => Alt.CreateNetworkObject(Alt.Hash(model), position, rotation, alpha, textureVariation, lodDistance));
}
}
12 changes: 0 additions & 12 deletions api/AltV.Net.Async/Elements/Entities/AsyncMarker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@ public AsyncMarker(ICore core, IntPtr nativePointer, uint id) : this(new Marker(
{
}

public AsyncMarker(ICore core, MarkerType type, Position pos, Rgba color) :
this(core, core.CreateMarkerEntity(out var id, null, type, pos, color), id)
{
core.PoolManager.Marker.Add(this);
}

public AsyncMarker(ICore core, IPlayer player, MarkerType type, Position pos, Rgba color) : this(core,
core.CreateMarkerEntity(out var id, player, type, pos, color), id)
{
core.PoolManager.Marker.Add(this);
}

[Obsolete("Use new async API instead")]
public IMarker ToAsync(IAsyncContext asyncContext)
{
Expand Down
6 changes: 0 additions & 6 deletions api/AltV.Net.Async/Elements/Entities/AsyncNetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ public AsyncNetworkObject(ICore core, IntPtr nativePointer, uint id) : this(new
{
}

public AsyncNetworkObject(ICore core, uint model, Position position, Rotation rotation, byte alpha = 255, byte textureVariation = 0, ushort lodDistance = 100) : this(
core, core.CreateNetworkObjectEntity(out var id, model, position, rotation, alpha, textureVariation, lodDistance), id)
{
core.PoolManager.NetworkObject.Add(this);
}

public byte Alpha
{
get
Expand Down
6 changes: 0 additions & 6 deletions api/AltV.Net.Async/Elements/Entities/AsyncPed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ public AsyncPed(ICore core, IntPtr nativePointer, uint id) : this(new Ped(core,
{
}

public AsyncPed(ICore core, uint model, Position position, Rotation rotation) : this(
core, core.CreatePedEntity(out var id, model, position, rotation), id)
{
core.PoolManager.Ped.Add(this);
}

[Obsolete("Use new async API instead")]
public IPed ToAsync(IAsyncContext asyncContext)
{
Expand Down
1 change: 1 addition & 0 deletions api/AltV.Net.Async/Elements/Entities/AsyncVehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ public AsyncVehicle(ICore core, IntPtr nativePointer, uint id) : this(new Vehicl
{
}

[Obsolete("Use AltAsync.CreateVehicle instead")]
public AsyncVehicle(ICore core, uint model, Position position, Rotation rotation) : this(
core, core.CreateVehicleEntity(out var id, model, position, rotation), id)
{
Expand Down
6 changes: 0 additions & 6 deletions api/AltV.Net.Async/Elements/Entities/AsyncVirtualEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ public AsyncVirtualEntity(ICore core, IntPtr nativePointer, uint id) : this(new
{
}

public AsyncVirtualEntity(ICore core, IVirtualEntityGroup group, Position position, uint streamingDistance, Dictionary<string, object> data) : this(
core, core.CreateVirtualEntityEntity(out var id, group, position, streamingDistance, data), id)
{
core.PoolManager.VirtualEntity.Add(this);
}

public uint StreamingDistance
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ public AsyncVirtualEntityGroup(ICore core, IntPtr nativePointer, uint id) : this
{
}

public AsyncVirtualEntityGroup(ICore core, uint maxEntitiesInStream) : this(
core, core.CreateVirtualEntityGroupEntity(out var id, maxEntitiesInStream), id)
{
core.PoolManager.VirtualEntityGroup.Add(this);
}

public uint Id => VirtualEntityGroup.Id;
public uint MaxEntitiesInStream
{
Expand Down
36 changes: 28 additions & 8 deletions api/AltV.Net.Client/Alt.Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using AltV.Net.Client.Elements.Interfaces;
using AltV.Net.Data;
using AltV.Net.Elements.Entities;
using AltV.Net.Enums;
using AltV.Net.Shared.Enums;

namespace AltV.Net.Client
{
Expand All @@ -21,15 +23,33 @@ public partial class Alt
public static ICheckpoint CreateCheckpoint(CheckpointType type, Vector3 pos, Vector3 nextPos, float radius,
float height, Rgba color, uint streamingDistance) => Core.CreateCheckpoint(type, pos, nextPos, radius, height, color, streamingDistance);

public static IVirtualEntityGroup CreateVirtualEntityGroup(uint streamingDistance)
{
return new VirtualEntityGroup(Core, streamingDistance);
}
public static IVirtualEntityGroup CreateVirtualEntityGroup(uint streamingDistance) => Core.CreateVirtualEntityGroup(streamingDistance);

public static IVirtualEntity CreateVirtualEntity(IVirtualEntityGroup group, Position position,
uint streamingDistance, Dictionary<string, object> dataDict)
{
return new VirtualEntity(Core, group, position, streamingDistance, dataDict);
}
uint streamingDistance, Dictionary<string, object> dataDict) => Core.CreateVirtualEntity(group, position, streamingDistance, dataDict);

public static ILocalPed CreateLocalPed(uint modelHash, int dimension, Position position, Rotation rotation,
bool useStreaming, uint streamingDistance) =>
Core.CreateLocalPed(modelHash, dimension, position, rotation, useStreaming, streamingDistance);

public static ILocalPed CreateLocalPed(PedModel modelHash, int dimension, Position position, Rotation rotation,
bool useStreaming, uint streamingDistance) =>
Core.CreateLocalPed((uint)modelHash, dimension, position, rotation, useStreaming, streamingDistance);

public static ILocalVehicle CreateLocalVehicle(uint modelHash, int dimension, Position position, Rotation rotation, bool useStreaming, uint streamingDistance) =>
Core.CreateLocalVehicle(modelHash, dimension, position, rotation, useStreaming, streamingDistance);

public static ILocalVehicle CreateLocalVehicle(VehicleModel modelHash, int dimension, Position position, Rotation rotation, bool useStreaming, uint streamingDistance) =>
Core.CreateLocalVehicle((uint)modelHash, dimension, position, rotation, useStreaming, streamingDistance);

public static IMarker CreateMarker(MarkerType type, Position pos, Rgba color, bool useStreaming,
uint streamingDistance) => Core.CreateMarker(type, pos, color, useStreaming, streamingDistance);

public static ITextLabel CreateTextLabel(string name, string fontName, float fontSize, float scale,
Position pos,
Rotation rot, Rgba color, float outlineWidth, Rgba outlineColor, bool useStreaming,
uint streamingDistance) => Core.CreateTextLabel(name, fontName, fontSize, scale, pos,
rot, color, outlineWidth, outlineColor, useStreaming, streamingDistance);

}
}
119 changes: 74 additions & 45 deletions api/AltV.Net.Client/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -654,104 +654,133 @@ public IReadOnlyCollection<IObject> GetAllObjects()
}
}

public IntPtr CreateTextLabelPtr(out uint id, string name, string fontName, float fontSize, float scale,
Position pos, Rotation rot, Rgba color, float outlineWidth, Rgba outlineColor, bool useStreaming, uint streamingDistance)
public IBlip GetBlipByGameId(uint gameId)
{
unsafe
{
var blipPtr = Library.Client.Core_GetBlipByGameID(NativePointer, gameId);

if (blipPtr == IntPtr.Zero) return null;
return PoolManager.Blip.GetOrCreate(this, blipPtr);
}
}

public IWorldObject GetWorldObjectByScriptID(BaseObjectType type, uint scriptId)
{
unsafe
{
var wordlObjectPtr = Library.Client.Core_GetWorldObjectByScriptID(NativePointer, scriptId);
if (wordlObjectPtr == IntPtr.Zero) return null;
return (IWorldObject) PoolManager.Get(wordlObjectPtr, type);
}
}

public IVirtualEntityGroup CreateVirtualEntityGroup(uint streamingDistance)
{
unsafe
{
CheckIfCallIsValid();

uint pId = default;
var namePtr = MemoryUtils.StringToHGlobalUtf8(name);
var fontSizePtr = MemoryUtils.StringToHGlobalUtf8(fontName);
var textLabelMarker = Library.Client.Core_CreateTextLabel(NativePointer, namePtr, fontSizePtr, fontSize, scale, pos, rot, color, outlineWidth, outlineColor,
useStreaming ? (byte)1:(byte)0, streamingDistance, Resource.NativePointer, &pId);
id = pId;
Marshal.FreeHGlobal(namePtr);
Marshal.FreeHGlobal(fontSizePtr);
return textLabelMarker;
var ptr = Library.Shared.Core_CreateVirtualEntityGroup(NativePointer, streamingDistance, &pId);
if (ptr == IntPtr.Zero) return null;
return PoolManager.VirtualEntityGroup.GetOrCreate(this, ptr, pId);
}
}

public IntPtr CreateVirtualEntityEntity(out uint id, IVirtualEntityGroup group, Position position, uint streamingDistance,
public IVirtualEntity CreateVirtualEntity(IVirtualEntityGroup group, Position position, uint streamingDistance,
Dictionary<string, object> dataDict)
{
unsafe
{
CheckIfCallIsValid();

var data = new Dictionary<IntPtr, MValueConst>();

foreach (var dataValue in dataDict)
var keys = new IntPtr[dataDict.Count];
var values = new IntPtr[dataDict.Count];

for (var i = 0; i < dataDict.Count; i++)
{
var stringPtr = AltNative.StringUtils.StringToHGlobalUtf8(dataValue.Key);
Alt.Core.CreateMValue(out var mValue, dataValue);
var stringPtr = AltNative.StringUtils.StringToHGlobalUtf8(dataDict.ElementAt(i).Key);
Alt.Core.CreateMValue(out var mValue, dataDict.ElementAt(i).Value);
keys[i] = stringPtr;
values[i] = mValue.nativePointer;
data.Add(stringPtr, mValue);
}

uint pId = default;
var ptr = Library.Shared.Core_CreateVirtualEntity(NativePointer, group.NativePointer, position, streamingDistance, data.Keys.ToArray(), data.Values.Select(x => x.nativePointer).ToArray(), (uint)data.Count, &pId);
id = pId;
var ptr = Library.Shared.Core_CreateVirtualEntity(NativePointer, group.VirtualEntityGroupNativePointer, position, streamingDistance, keys, values, (uint)data.Count, &pId);

foreach (var dataValue in data)
{
dataValue.Value.Dispose();
Marshal.FreeHGlobal(dataValue.Key);
}

return ptr;
if (ptr == IntPtr.Zero) return null;
return PoolManager.VirtualEntity.GetOrCreate(this, ptr, pId);
}
}

public IntPtr CreateVirtualEntityGroupEntity(out uint id, uint maxEntitiesInStream)
public ILocalPed CreateLocalPed(uint modelHash, int dimension, Position position, Rotation rotation, bool useStreaming,
uint streamingDistance)
{
unsafe
{
CheckIfCallIsValid();

uint pId = default;
var ptr = Library.Shared.Core_CreateVirtualEntityGroup(NativePointer, maxEntitiesInStream, &pId);
id = pId;
return ptr;
var ptr = Library.Client.Core_CreateLocalPed(NativePointer, modelHash, dimension, position, rotation,
useStreaming ? (byte)1 : (byte)0, streamingDistance, Resource.NativePointer, &pId);
if (ptr == IntPtr.Zero) return null;
return PoolManager.LocalPed.GetOrCreate(this, ptr, pId);
}
}

public IntPtr CreateLocalVehiclePtr(out uint id, uint modelHash, int dimension, Position position, Rotation rotation,
bool useStreaming, uint streamingDistance)
public ILocalVehicle CreateLocalVehicle(uint modelHash, int dimension, Position position, Rotation rotation, bool useStreaming,
uint streamingDistance)
{
unsafe
{
CheckIfCallIsValid();

uint pId = default;
var ptr = Library.Client.Core_CreateLocalVehicle(NativePointer, modelHash, dimension, position, rotation, useStreaming ? (byte)1:(byte)0, streamingDistance, Resource.NativePointer, &pId);
id = pId;
return ptr;
var ptr = Library.Client.Core_CreateLocalVehicle(NativePointer, modelHash, dimension, position, rotation,
useStreaming ? (byte)1 : (byte)0, streamingDistance, Resource.NativePointer, &pId);
if (ptr == IntPtr.Zero) return null;
return PoolManager.LocalVehicle.GetOrCreate(this, ptr, pId);
}
}

public IntPtr CreateLocalPedPtr(out uint id, uint modelHash, int dimension, Position position, Rotation rotation,
bool useStreaming, uint streamingDistance)
public IMarker CreateMarker(MarkerType type, Position pos, Rgba color, bool useStreaming, uint streamingDistance)
{
unsafe
{
uint pId = default;
var ptr = Library.Client.Core_CreateLocalPed(NativePointer, modelHash, dimension, position, rotation, useStreaming ? (byte)1:(byte)0, streamingDistance, Resource.NativePointer, &pId);
id = pId;
return ptr;
var ptr = Library.Client.Core_CreateMarker_Client(NativePointer, (byte)type, pos, color,
useStreaming ? (byte)1 : (byte)0, streamingDistance, Resource.NativePointer, &pId);
if (ptr == IntPtr.Zero) return null;
return PoolManager.Marker.GetOrCreate(this, ptr, pId);
}
}

public IBlip GetBlipByGameId(uint gameId)
public ITextLabel CreateTextLabel(string name, string fontName, float fontSize, float scale, Position pos, Rotation rot,
Rgba color, float outlineWidth, Rgba outlineColor, bool useStreaming, uint streamingDistance)
{
unsafe
{
var blipPtr = Library.Client.Core_GetBlipByGameID(NativePointer, gameId);
uint pId = default;
var namePtr = MemoryUtils.StringToHGlobalUtf8(name);
var fontSizePtr = MemoryUtils.StringToHGlobalUtf8(fontName);

if (blipPtr == IntPtr.Zero) return null;
return PoolManager.Blip.GetOrCreate(this, blipPtr);
}
}
var ptr = Library.Client.Core_CreateTextLabel(NativePointer, namePtr, fontSizePtr, fontSize, scale, pos, rot, color, outlineWidth, outlineColor,
useStreaming ? (byte)1:(byte)0, streamingDistance, Resource.NativePointer, &pId);

public IWorldObject GetWorldObjectByScriptID(BaseObjectType type, uint scriptId)
{
unsafe
{
var wordlObjectPtr = Library.Client.Core_GetWorldObjectByScriptID(NativePointer, scriptId);
if (wordlObjectPtr == IntPtr.Zero) return null;
return (IWorldObject) PoolManager.Get(wordlObjectPtr, type);
Marshal.FreeHGlobal(namePtr);
Marshal.FreeHGlobal(fontSizePtr);

if (ptr == IntPtr.Zero) return null;
return PoolManager.TextLabel.GetOrCreate(this, ptr, pId);
}
}

Expand Down
1 change: 1 addition & 0 deletions api/AltV.Net.Client/Elements/Entities/Audio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public Audio(ICore core, IntPtr audioNativePointer, uint id) : base(core, GetBas
AudioNativePointer = audioNativePointer;
}

[Obsolete("Use Alt.CreateAudio instead")]
public Audio(ICore core, string source, float volume, uint category, bool frontend) : this(core, core.CreateAudioPtr(out var id,source, volume, category, frontend), id)
{
core.PoolManager.Audio.Add(this);
Expand Down
3 changes: 3 additions & 0 deletions api/AltV.Net.Client/Elements/Entities/Blip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -705,16 +705,19 @@ public Blip(ICore core, IntPtr nativePointer, uint id) : base(core, GetWorldObje
BlipNativePointer = nativePointer;
}

[Obsolete("Use Alt.CreatePointBlip instead")]
public Blip(ICore core, Position position) : this(core, core.CreatePointBlipPtr(out var id, position), id)
{
core.PoolManager.Blip.Add(this);
}

[Obsolete("Use Alt.CreateRadiusBlip instead")]
public Blip(ICore core, Position position, float radius) : this(core, core.CreateRadiusBlipPtr(out var id, position, radius), id)
{
core.PoolManager.Blip.Add(this);
}

[Obsolete("Use Alt.CreateAreaBlip instead")]
public Blip(ICore core, Position position, int width, int height) : this(core, core.CreateAreaBlipPtr(out var id, position, width, height), id)
{
core.PoolManager.Blip.Add(this);
Expand Down
1 change: 1 addition & 0 deletions api/AltV.Net.Client/Elements/Entities/Checkpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public Checkpoint(ICore core, IntPtr nativePointer, uint id) : base(core, GetCol
CheckpointNativePointer = nativePointer;
}

[Obsolete("Use Alt.CreateCheckpoint instead")]
public Checkpoint(ICore core, CheckpointType type, Vector3 pos, Vector3 nextPos, float radius, float height, Rgba color, uint streamingDistance)
: this(core, core.CreateCheckpointPtr(out var id, type, pos, nextPos, radius, height, color, streamingDistance), id)
{
Expand Down
1 change: 1 addition & 0 deletions api/AltV.Net.Client/Elements/Entities/HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public HttpClient(ICore core, IntPtr httpClientNativePointer, uint id) : base(co
HttpClientNativePointer = httpClientNativePointer;
}

[Obsolete("Use Alt.CreateHttpClient instead")]
public HttpClient(ICore core) : this(core, core.CreateHttpClientPtr(out var id), id)
{
core.PoolManager.HttpClient.Add(this);
Expand Down
Loading

0 comments on commit 5e4440d

Please sign in to comment.