Skip to content

Commit

Permalink
Attempt to add streamsyncedmetachange event to server-side.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDutchDev committed Dec 26, 2024
1 parent 6394eef commit b363dda
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 0 deletions.
14 changes: 14 additions & 0 deletions api/AltV.Net.Async/AsyncCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ internal readonly AsyncEventHandler<PlayerChangeVehicleSeatAsyncDelegate>
internal readonly AsyncEventHandler<MetaDataChangeAsyncDelegate> SyncedMetaDataChangeAsyncEventHandler =
new(EventType.SYNCED_META_CHANGE);

internal readonly AsyncEventHandler<MetaDataChangeAsyncDelegate> StreamSyncedMetaDataChangeAsyncEventHandler =
new(EventType.STREAM_SYNCED_META_CHANGE);

internal readonly AsyncEventHandler<ColShapeAsyncDelegate> ColShapeAsyncEventHandler =
new(EventType.COLSHAPE_EVENT);

Expand Down Expand Up @@ -602,6 +605,17 @@ await SyncedMetaDataChangeAsyncEventHandler.CallAsync(@delegate =>
});
}

public override void OnStreamSyncedMetaDataChangeEvent(IEntity entity, string key, object value)
{
base.OnStreamSyncedMetaDataChangeEvent(entity, key, value);
if (!StreamSyncedMetaDataChangeAsyncEventHandler.HasEvents()) return;
Task.Run(async () =>
{
await StreamSyncedMetaDataChangeAsyncEventHandler.CallAsync(@delegate =>
@delegate(entity, key, value));
});
}

public override void OnColShapeEvent(IColShape colShape, IWorldObject entity, bool state)
{
base.OnColShapeEvent(colShape, entity, state);
Expand Down
4 changes: 4 additions & 0 deletions api/AltV.Net.CApi/Native/AltV.Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ internal static extern void CSharpResourceImpl_SetMetaChangeDelegate(IntPtr reso
internal static extern void CSharpResourceImpl_SetSyncedMetaChangeDelegate(IntPtr resource,
MetaChangeDelegate @delegate);

[DllImport(DllName, CallingConvention = NativeCallingConvention)]
internal static extern void CSharpResourceImpl_SetStreamSyncedMetaChangeDelegate(IntPtr resource,
MetaChangeDelegate @delegate);

[DllImport(DllName, CallingConvention = NativeCallingConvention)]
internal static extern void CSharpResourceImpl_SetColShapeDelegate(IntPtr resource,
ColShapeDelegate @delegate);
Expand Down
5 changes: 5 additions & 0 deletions api/AltV.Net/Alt.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ public static event MetaDataChangeDelegate OnSyncedMetaDataChange
add => ((Core)CoreImpl).SyncedMetaDataChangeEventHandler.Add(value);
remove => ((Core)CoreImpl).SyncedMetaDataChangeEventHandler.Remove(value);
}
public static event MetaDataChangeDelegate OnStreamSyncedMetaDataChange
{
add => ((Core)CoreImpl).StreamSyncedMetaDataChangeEventHandler.Add(value);
remove => ((Core)CoreImpl).StreamSyncedMetaDataChangeEventHandler.Remove(value);
}

public static event ColShapeDelegate OnColShape
{
Expand Down
14 changes: 14 additions & 0 deletions api/AltV.Net/Alt.RegisterEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,20 @@ public static void RegisterEvents(object target)
};
break;
}
case ScriptEventType.StreamSyncedMetaDataChange:
{
scriptFunction = ScriptFunction.Create(eventMethodDelegate,
new[] { typeof(IEntity), typeof(string), typeof(object) });
if (scriptFunction == null) return;
OnStreamSyncedMetaDataChange += (entity, key, value) =>
{
scriptFunction.Set(entity);
scriptFunction.Set(key);
scriptFunction.Set(value);
scriptFunction.Call();
};
break;
}
case ScriptEventType.ColShape:
{
scriptFunction = ScriptFunction.Create(eventMethodDelegate,
Expand Down
4 changes: 4 additions & 0 deletions api/AltV.Net/CSharpResourceImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ internal void SetDelegates(AltNative.Resource.MainDelegate onStart)
handles.AddFirst(GCHandle.Alloc(onSyncedMetaDataChange));
AltNative.Resource.CSharpResourceImpl_SetSyncedMetaChangeDelegate(NativePointer, onSyncedMetaDataChange);

AltNative.Resource.MetaChangeDelegate onStreamSyncedMetaDataChange = ModuleWrapper.OnStreamSyncedMetaDataChange;
handles.AddFirst(GCHandle.Alloc(onStreamSyncedMetaDataChange));
AltNative.Resource.CSharpResourceImpl_SetStreamSyncedMetaChangeDelegate(NativePointer, onStreamSyncedMetaDataChange);

AltNative.Resource.ColShapeDelegate onColShape = ModuleWrapper.OnColShape;
handles.AddFirst(GCHandle.Alloc(onColShape));
AltNative.Resource.CSharpResourceImpl_SetColShapeDelegate(NativePointer, onColShape);
Expand Down
37 changes: 37 additions & 0 deletions api/AltV.Net/Core.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public partial class Core

internal readonly IEventHandler<MetaDataChangeDelegate> SyncedMetaDataChangeEventHandler =
new HashSetEventHandler<MetaDataChangeDelegate>(EventType.SYNCED_META_CHANGE);

internal readonly IEventHandler<MetaDataChangeDelegate> StreamSyncedMetaDataChangeEventHandler =
new HashSetEventHandler<MetaDataChangeDelegate>(EventType.STREAM_SYNCED_META_CHANGE);

internal readonly IEventHandler<ColShapeDelegate> ColShapeEventHandler =
new HashSetEventHandler<ColShapeDelegate>(EventType.COLSHAPE_EVENT);
Expand Down Expand Up @@ -961,6 +964,40 @@ public virtual void OnSyncedMetaDataChangeEvent(IEntity entity, string key, obje
}
}

public void OnStreamSyncedMetaDataChange(IntPtr entityPointer, BaseObjectType entityType, string key,
IntPtr value)
{
var entity = (IEntity)PoolManager.Get(entityPointer, entityType);
if (entity is null)
{
Console.WriteLine("OnStreamSyncedMetaDataChange Invalid entity " + entityPointer + " " + entityType + " " +
key + " " + value);
return;
}

OnStreamSyncedMetaDataChangeEvent(entity, key, new MValueConst(this, value).ToObject());
}

public virtual void OnStreamSyncedMetaDataChangeEvent(IEntity entity, string key, object value)
{
if (!StreamSyncedMetaDataChangeEventHandler.HasEvents()) return;
foreach (var eventHandler in StreamSyncedMetaDataChangeEventHandler.GetEvents())
{
try
{
eventHandler(entity, key, value);
}
catch (TargetInvocationException exception)
{
Alt.Log("exception at event:" + "OnStreamSyncedMetaDataChangeEvent" + ":" + exception.InnerException);
}
catch (Exception exception)
{
Alt.Log("exception at event:" + "OnStreamSyncedMetaDataChangeEvent" + ":" + exception);
}
}
}

public void OnColShape(IntPtr colShapePointer, IntPtr targetEntityPointer, BaseObjectType entityType,
bool state)
{
Expand Down
6 changes: 6 additions & 0 deletions api/AltV.Net/ModuleWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ public static void OnSyncedMetaDataChange(IntPtr entityPointer, BaseObjectType e
_core.OnSyncedMetaDataChange(entityPointer, entityType, key, value);
}

public static void OnStreamSyncedMetaDataChange(IntPtr entityPointer, BaseObjectType entityType, string key,
IntPtr value)
{
_core.OnStreamSyncedMetaDataChange(entityPointer, entityType, key, value);
}

public static void OnColShape(IntPtr colShapePointer, IntPtr targetEntityPointer, BaseObjectType entityType,
byte state)
{
Expand Down
1 change: 1 addition & 0 deletions api/AltV.Net/ScriptEventType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public enum ScriptEventType
ConsoleCommand,
MetaDataChange,
SyncedMetaDataChange,
StreamSyncedMetaDataChange,
ColShape,
WeaponDamage,
VehicleDestroy,
Expand Down

0 comments on commit b363dda

Please sign in to comment.