Skip to content

Commit

Permalink
feat(server): Add VehicleBadge setter
Browse files Browse the repository at this point in the history
  • Loading branch information
Doxoh committed Dec 26, 2023
1 parent 78e2840 commit ac4fbbf
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 23 deletions.
19 changes: 19 additions & 0 deletions api/AltV.Net.Async/Elements/Entities/AsyncVehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using AltV.Net.CApi.Data;
using AltV.Net.Data;
using AltV.Net.Elements.Entities;
using AltV.Net.Enums;
Expand Down Expand Up @@ -1993,6 +1994,24 @@ public List<PlayerSeat> Passengers
}
}

public void SetBage(string textureDictionary, string texture, VehicleBadgePosition[] vehicleBadgePosition)
{
lock (Vehicle)
{
if (!AsyncContext.CheckIfExistsOrCachedNullable(Vehicle)) return;
Vehicle.SetBage(textureDictionary, texture, vehicleBadgePosition);
}
}

public void SetBage(uint textureDictionary, uint texture, VehicleBadgePosition[] vehicleBadgePosition)
{
lock (Vehicle)
{
if (!AsyncContext.CheckIfExistsOrCachedNullable(Vehicle)) return;
Vehicle.SetBage(textureDictionary, texture, vehicleBadgePosition);
}
}

[Obsolete("Use new async API instead")]
public IVehicle ToAsync(IAsyncContext asyncContext)
{
Expand Down
33 changes: 17 additions & 16 deletions api/AltV.Net.CApi.Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ private static string GetCMethodDelegateType(CMethod method)
var args = string.Join("", method.Params.Select(p => p.Type + ", "));
return $"delegate* unmanaged[Cdecl{noGc}]<{args}{method.ReturnType}>";
}

private static string GetCMethodArgs(CMethod method)
{
return string.Join(", ", method.Params.Select(p => p.Type + " " + p.Name));
}

public static void Generate()
{
var libOutputPath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)!, "../../../../AltV.Net.CApi/Libraries");
var libOutputPath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)!, "../../../../AltV.Net.CApi/Libraries");
var tableOutputPath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)!, "../../../../../runtime/c-api/func_table.cpp");

var tableHashes = new StringBuilder();
Expand All @@ -40,31 +40,32 @@ public static void Generate()
collisionFound = true;
Console.WriteLine("Colliding methods: " + string.Join(",", collision.Select(e => e.Name)));
}

if (collisionFound) throw new Exception("Collision found!");

var capiHash = FnvHash.Generate(string.Join(";", parsedMethods.Select(e => e.Hash)));

foreach (var group in parsedMethods.OrderBy(e => e.Name).GroupBy(e => e.Target))
{
#region C# bindings
var target = group.Key.ForceCapitalize();

var methods = string.Join("\n", group.Where(e => !e.OnlyManual)
.Select(e => $" public {GetCMethodDelegateType(e)} {e.Name} {{ get; }}"));

// todo add docs link to the exception
var fallbacks = string.Join("\n", group.Where(e => !e.OnlyManual)
.Select(e => $" [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate {e.ReturnType} {e.Name}Delegate({GetCMethodArgs(e)});\n"
+ $" private static {e.ReturnType} {e.Name}Fallback({GetCMethodArgs(e)}) => throw new Exceptions.OutdatedSdkException(\"{e.Name}\", \"{e.Name} SDK method is outdated. Please update your module nuget\");"));

var loads = string.Join("\n", group.Where(e => !e.OnlyManual)
.Select(e => $" {e.Name} = ({GetCMethodDelegateType(e)}) GetUnmanagedPtr<{e.Name}Delegate>(funcTable, {e.Hash}UL, {e.Name}Fallback);"));

var output = new StringBuilder();

output.Append("// ReSharper disable InconsistentNaming\n");
output.Append("using AltV.Net.Data;\n");
output.Append("using AltV.Net.CApi.Data;\n");
output.Append("using System.Numerics;\n");
output.Append("using System.Runtime.InteropServices;\n");
output.Append("using AltV.Net.Elements.Args;\n");
Expand Down Expand Up @@ -100,21 +101,21 @@ public static void Generate()

File.WriteAllText(Path.Combine(libOutputPath, $"{target}Library.cs"), output.ToString());
#endregion

#region Func table

if (group.Key != "SHARED")
{
tableHashes.Append($" #ifdef ALT_{group.Key}_API\n");
tablePointers.Append($" #ifdef ALT_{group.Key}_API\n");
}

foreach (var e in group)
{
tableHashes.Append($" {e.Hash}UL,\n");
tablePointers.Append($" (void*) {e.Name},\n");
}

if (group.Key != "SHARED")
{
tableHashes.Append($" #endif\n");
Expand All @@ -125,13 +126,13 @@ public static void Generate()

var table = new StringBuilder();
table.Append("#include \"func_table.h\"\n\n");

table.Append($"inline uint64_t capiHash = {capiHash}UL;\n");
table.Append("inline uint64_t capiHashes[] = {\n");
table.Append(" 0,\n");
table.Append(tableHashes);
table.Append("};\n\n");

table.Append("inline void* capiPointers[] = {\n");
table.Append(" (void*) &capiHash,\n");
table.Append(tablePointers);
Expand All @@ -145,10 +146,10 @@ public static void Generate()
table.Append(" };\n");
table.Append(" return &data;\n");
table.Append("}");

File.WriteAllText(tableOutputPath, table.ToString());
}
}

public static void Main()
{
Generate();
Expand Down
2 changes: 2 additions & 0 deletions api/AltV.Net.CApi.Generator/TypeRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public static class TypeRegistry
{ "rotation_t", "Rotation" },
{ "alt::Rotation", "Rotation" },

{ "vehicleBadgePosition_t[]", "VehicleBadgePosition[]" },

{ "cloth_t&", "Cloth*" },
{ "cloth_t", "Cloth" },
{ "dlccloth_t&", "DlcCloth*" },
Expand Down
39 changes: 39 additions & 0 deletions api/AltV.Net.CApi/Data/VehicleBadgePosition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Numerics;
using System.Runtime.InteropServices;

namespace AltV.Net.CApi.Data;

[StructLayout(LayoutKind.Sequential)]
public struct VehicleBadgePosition : IEquatable<VehicleBadgePosition>
{
public VehicleBadgePosition(bool active, byte alpha, float size, short boneIndex, Vector3 offset, Vector3 direction, Vector3 side)
{
Active = active ? (byte)1 : (byte)0;
Alpha = alpha;
Size = size;
BoneIndex = boneIndex;
Offset = offset;
Direction = direction;
Side = side;
}

public byte Active { get; set; } = 0;
public byte Alpha { get; set; } = 255;
public float Size { get; set; } = 1f;
public short BoneIndex { get; set; } = 0;
public Vector3 Offset { get; set; } = new(0, 0, 0);
public Vector3 Direction { get; set; } = new(0, 0, 0);
public Vector3 Side { get; set; } = new(0, 0, 0);

public bool Equals(VehicleBadgePosition other)
{
return Active == other.Active && Alpha == other.Alpha && Size.Equals(other.Size) && BoneIndex == other.BoneIndex && Offset.Equals(other.Offset) && Direction.Equals(other.Direction) && Side.Equals(other.Side);
}

public override bool Equals(object? obj)
{
return obj is VehicleBadgePosition other && Equals(other);
}

public override int GetHashCode() => HashCode.Combine(Active.GetHashCode(), Alpha.GetHashCode(), Size.GetHashCode(), BoneIndex.GetHashCode(), Offset.GetHashCode(), Direction.GetHashCode(), Side.GetHashCode());
}
5 changes: 3 additions & 2 deletions api/AltV.Net.CApi/Libraries/ClientLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ReSharper disable InconsistentNaming
using AltV.Net.Data;
using AltV.Net.CApi.Data;
using System.Numerics;
using System.Runtime.InteropServices;
using AltV.Net.Elements.Args;
Expand Down Expand Up @@ -889,7 +890,7 @@ public unsafe interface IClientLibrary

public unsafe class ClientLibrary : IClientLibrary
{
public readonly uint Methods = 1755;
public readonly uint Methods = 1756;
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 @@ -3524,7 +3525,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 != 554522947139118248UL) Outdated = true;
else if (capiHash == IntPtr.Zero || *(ulong*)capiHash != 9602013249028565151UL) 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
10 changes: 8 additions & 2 deletions api/AltV.Net.CApi/Libraries/ServerLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ReSharper disable InconsistentNaming
using AltV.Net.Data;
using AltV.Net.CApi.Data;
using System.Numerics;
using System.Runtime.InteropServices;
using AltV.Net.Elements.Args;
Expand Down Expand Up @@ -381,6 +382,7 @@ public unsafe interface IServerLibrary
public delegate* unmanaged[Cdecl]<nint, void> Vehicle_Repair { get; }
public delegate* unmanaged[Cdecl]<nint, byte, float, void> Vehicle_SetArmoredWindowHealth { get; }
public delegate* unmanaged[Cdecl]<nint, byte, byte, void> Vehicle_SetArmoredWindowShootCount { get; }
public delegate* unmanaged[Cdecl]<nint, uint, uint, VehicleBadgePosition[], ushort, void> Vehicle_SetBadge { get; }
public delegate* unmanaged[Cdecl]<nint, byte, void> Vehicle_SetBoatAnchor { get; }
public delegate* unmanaged[Cdecl]<nint, uint, void> Vehicle_SetBodyAdditionalHealth { get; }
public delegate* unmanaged[Cdecl]<nint, uint, void> Vehicle_SetBodyHealth { get; }
Expand Down Expand Up @@ -482,7 +484,7 @@ public unsafe interface IServerLibrary

public unsafe class ServerLibrary : IServerLibrary
{
public readonly uint Methods = 1755;
public readonly uint Methods = 1756;
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 @@ -854,6 +856,7 @@ public unsafe class ServerLibrary : IServerLibrary
public delegate* unmanaged[Cdecl]<nint, void> Vehicle_Repair { get; }
public delegate* unmanaged[Cdecl]<nint, byte, float, void> Vehicle_SetArmoredWindowHealth { get; }
public delegate* unmanaged[Cdecl]<nint, byte, byte, void> Vehicle_SetArmoredWindowShootCount { get; }
public delegate* unmanaged[Cdecl]<nint, uint, uint, VehicleBadgePosition[], ushort, void> Vehicle_SetBadge { get; }
public delegate* unmanaged[Cdecl]<nint, byte, void> Vehicle_SetBoatAnchor { get; }
public delegate* unmanaged[Cdecl]<nint, uint, void> Vehicle_SetBodyAdditionalHealth { get; }
public delegate* unmanaged[Cdecl]<nint, uint, void> Vehicle_SetBodyHealth { get; }
Expand Down Expand Up @@ -1693,6 +1696,8 @@ public unsafe class ServerLibrary : IServerLibrary
private static void Vehicle_SetArmoredWindowHealthFallback(nint _vehicle, byte _windowId, float _health) => throw new Exceptions.OutdatedSdkException("Vehicle_SetArmoredWindowHealth", "Vehicle_SetArmoredWindowHealth SDK method is outdated. Please update your module nuget");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void Vehicle_SetArmoredWindowShootCountDelegate(nint _vehicle, byte _windowId, byte _count);
private static void Vehicle_SetArmoredWindowShootCountFallback(nint _vehicle, byte _windowId, byte _count) => throw new Exceptions.OutdatedSdkException("Vehicle_SetArmoredWindowShootCount", "Vehicle_SetArmoredWindowShootCount SDK method is outdated. Please update your module nuget");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void Vehicle_SetBadgeDelegate(nint _vehicle, uint _textureDictionary, uint _texture, VehicleBadgePosition[] vehicleBadgePosition, ushort _size);
private static void Vehicle_SetBadgeFallback(nint _vehicle, uint _textureDictionary, uint _texture, VehicleBadgePosition[] vehicleBadgePosition, ushort _size) => throw new Exceptions.OutdatedSdkException("Vehicle_SetBadge", "Vehicle_SetBadge SDK method is outdated. Please update your module nuget");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void Vehicle_SetBoatAnchorDelegate(nint _vehicle, byte _state);
private static void Vehicle_SetBoatAnchorFallback(nint _vehicle, byte _state) => throw new Exceptions.OutdatedSdkException("Vehicle_SetBoatAnchor", "Vehicle_SetBoatAnchor SDK method is outdated. Please update your module nuget");
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void Vehicle_SetBodyAdditionalHealthDelegate(nint _vehicle, uint _health);
Expand Down Expand Up @@ -1896,7 +1901,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 != 554522947139118248UL) Outdated = true;
else if (capiHash == IntPtr.Zero || *(ulong*)capiHash != 9602013249028565151UL) 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 @@ -2268,6 +2273,7 @@ public ServerLibrary(Dictionary<ulong, IntPtr> funcTable)
Vehicle_Repair = (delegate* unmanaged[Cdecl]<nint, void>) GetUnmanagedPtr<Vehicle_RepairDelegate>(funcTable, 277481303661922113UL, Vehicle_RepairFallback);
Vehicle_SetArmoredWindowHealth = (delegate* unmanaged[Cdecl]<nint, byte, float, void>) GetUnmanagedPtr<Vehicle_SetArmoredWindowHealthDelegate>(funcTable, 1070345202824576095UL, Vehicle_SetArmoredWindowHealthFallback);
Vehicle_SetArmoredWindowShootCount = (delegate* unmanaged[Cdecl]<nint, byte, byte, void>) GetUnmanagedPtr<Vehicle_SetArmoredWindowShootCountDelegate>(funcTable, 4149223353503655708UL, Vehicle_SetArmoredWindowShootCountFallback);
Vehicle_SetBadge = (delegate* unmanaged[Cdecl]<nint, uint, uint, VehicleBadgePosition[], ushort, void>) GetUnmanagedPtr<Vehicle_SetBadgeDelegate>(funcTable, 15010482901293452804UL, Vehicle_SetBadgeFallback);
Vehicle_SetBoatAnchor = (delegate* unmanaged[Cdecl]<nint, byte, void>) GetUnmanagedPtr<Vehicle_SetBoatAnchorDelegate>(funcTable, 16890059088943800731UL, Vehicle_SetBoatAnchorFallback);
Vehicle_SetBodyAdditionalHealth = (delegate* unmanaged[Cdecl]<nint, uint, void>) GetUnmanagedPtr<Vehicle_SetBodyAdditionalHealthDelegate>(funcTable, 5545167983491514394UL, Vehicle_SetBodyAdditionalHealthFallback);
Vehicle_SetBodyHealth = (delegate* unmanaged[Cdecl]<nint, uint, void>) GetUnmanagedPtr<Vehicle_SetBodyHealthDelegate>(funcTable, 13734895793996634557UL, Vehicle_SetBodyHealthFallback);
Expand Down
5 changes: 3 additions & 2 deletions api/AltV.Net.CApi/Libraries/SharedLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ReSharper disable InconsistentNaming
using AltV.Net.Data;
using AltV.Net.CApi.Data;
using System.Numerics;
using System.Runtime.InteropServices;
using AltV.Net.Elements.Args;
Expand Down Expand Up @@ -424,7 +425,7 @@ public unsafe interface ISharedLibrary

public unsafe class SharedLibrary : ISharedLibrary
{
public readonly uint Methods = 1755;
public readonly uint Methods = 1756;
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 @@ -1664,7 +1665,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 != 554522947139118248UL) Outdated = true;
else if (capiHash == IntPtr.Zero || *(ulong*)capiHash != 9602013249028565151UL) 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
4 changes: 4 additions & 0 deletions api/AltV.Net/Elements/Entities/IVehicle.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using AltV.Net.CApi.Data;
using AltV.Net.Data;
using AltV.Net.Enums;
using AltV.Net.Shared.Elements.Entities;
Expand Down Expand Up @@ -854,5 +855,8 @@ NumberPlateStyle GetNumberPlateStyleExt() =>
float BrakeLevel { get; }

List<PlayerSeat> Passengers { get; }

void SetBage(string textureDictionary, string texture, VehicleBadgePosition[] vehicleBadgePosition);
void SetBage(uint textureDictionary, uint texture, VehicleBadgePosition[] vehicleBadgePosition);
}
}
14 changes: 14 additions & 0 deletions api/AltV.Net/Elements/Entities/Vehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.InteropServices;
using AltV.Net.CApi.Data;
using AltV.Net.Data;
using AltV.Net.Enums;
using AltV.Net.Native;
Expand Down Expand Up @@ -2085,6 +2086,19 @@ public List<PlayerSeat> Passengers
}
}

public void SetBage(string textureDictionary, string texture, VehicleBadgePosition[] vehicleBadgePosition)
{
SetBage(Alt.Hash(textureDictionary), Alt.Hash(texture), vehicleBadgePosition);
}

public void SetBage(uint textureDictionary, uint texture, VehicleBadgePosition[] vehicleBadgePosition)
{
unsafe
{
Core.Library.Server.Vehicle_SetBadge(VehicleNativePointer, textureDictionary, texture, vehicleBadgePosition, (ushort)vehicleBadgePosition.Length);
}
}

public override void SetCached(IntPtr cachedVehicle)
{
this.VehicleNativePointer = cachedVehicle;
Expand Down
2 changes: 1 addition & 1 deletion runtime

0 comments on commit ac4fbbf

Please sign in to comment.