From c66f2a3579b952e3a20687bf697b53a6c2bd384e Mon Sep 17 00:00:00 2001 From: Niels <7136117+TheDutchDev@users.noreply.github.com> Date: Tue, 24 Dec 2024 01:21:37 +0100 Subject: [PATCH] Attempt to add streamsyncedmetachange event to server-side. --- api/AltV.Net.Async/AsyncCore.cs | 14 +++++++++ api/AltV.Net.CApi/Native/AltV.Resource.cs | 4 +++ api/AltV.Net/Alt.Events.cs | 5 +++ api/AltV.Net/Alt.RegisterEvents.cs | 14 +++++++++ api/AltV.Net/CSharpResourceImpl.cs | 4 +++ api/AltV.Net/Core.Events.cs | 37 +++++++++++++++++++++++ api/AltV.Net/ModuleWrapper.cs | 6 ++++ api/AltV.Net/ScriptEventType.cs | 1 + 8 files changed, 85 insertions(+) diff --git a/api/AltV.Net.Async/AsyncCore.cs b/api/AltV.Net.Async/AsyncCore.cs index 2dbacf858..432852af0 100644 --- a/api/AltV.Net.Async/AsyncCore.cs +++ b/api/AltV.Net.Async/AsyncCore.cs @@ -108,6 +108,9 @@ internal readonly AsyncEventHandler internal readonly AsyncEventHandler SyncedMetaDataChangeAsyncEventHandler = new(EventType.SYNCED_META_CHANGE); + internal readonly AsyncEventHandler StreamSyncedMetaDataChangeAsyncEventHandler = + new(EventType.STREAM_SYNCED_META_CHANGE); + internal readonly AsyncEventHandler ColShapeAsyncEventHandler = new(EventType.COLSHAPE_EVENT); @@ -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); diff --git a/api/AltV.Net.CApi/Native/AltV.Resource.cs b/api/AltV.Net.CApi/Native/AltV.Resource.cs index f3cf4dac4..872b38cab 100644 --- a/api/AltV.Net.CApi/Native/AltV.Resource.cs +++ b/api/AltV.Net.CApi/Native/AltV.Resource.cs @@ -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); diff --git a/api/AltV.Net/Alt.Events.cs b/api/AltV.Net/Alt.Events.cs index c49bcfafe..e7366bb8a 100644 --- a/api/AltV.Net/Alt.Events.cs +++ b/api/AltV.Net/Alt.Events.cs @@ -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 { diff --git a/api/AltV.Net/Alt.RegisterEvents.cs b/api/AltV.Net/Alt.RegisterEvents.cs index 7d213ed42..787459b7e 100644 --- a/api/AltV.Net/Alt.RegisterEvents.cs +++ b/api/AltV.Net/Alt.RegisterEvents.cs @@ -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, diff --git a/api/AltV.Net/CSharpResourceImpl.cs b/api/AltV.Net/CSharpResourceImpl.cs index b2803c30f..661acfda2 100644 --- a/api/AltV.Net/CSharpResourceImpl.cs +++ b/api/AltV.Net/CSharpResourceImpl.cs @@ -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); diff --git a/api/AltV.Net/Core.Events.cs b/api/AltV.Net/Core.Events.cs index 7aa37724b..018e26a66 100644 --- a/api/AltV.Net/Core.Events.cs +++ b/api/AltV.Net/Core.Events.cs @@ -86,6 +86,9 @@ public partial class Core internal readonly IEventHandler SyncedMetaDataChangeEventHandler = new HashSetEventHandler(EventType.SYNCED_META_CHANGE); + + internal readonly IEventHandler StreamSyncedMetaDataChangeEventHandler = + new HashSetEventHandler(EventType.STREAM_SYNCED_META_CHANGE); internal readonly IEventHandler ColShapeEventHandler = new HashSetEventHandler(EventType.COLSHAPE_EVENT); @@ -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) { diff --git a/api/AltV.Net/ModuleWrapper.cs b/api/AltV.Net/ModuleWrapper.cs index 7685e1ae3..992db4a88 100644 --- a/api/AltV.Net/ModuleWrapper.cs +++ b/api/AltV.Net/ModuleWrapper.cs @@ -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) { diff --git a/api/AltV.Net/ScriptEventType.cs b/api/AltV.Net/ScriptEventType.cs index 2053cf95a..9240befce 100644 --- a/api/AltV.Net/ScriptEventType.cs +++ b/api/AltV.Net/ScriptEventType.cs @@ -24,6 +24,7 @@ public enum ScriptEventType ConsoleCommand, MetaDataChange, SyncedMetaDataChange, + StreamSyncedMetaDataChange, ColShape, WeaponDamage, VehicleDestroy,