Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions FxEvents/FxEvents.Client/EventDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
global using CitizenFX.Core;
using CitizenFX.FiveM.Native;
using CitizenFX.Shared.Native;
using FxEvents.EventSystem;
using FxEvents.Shared;
using Logger;
Expand All @@ -19,7 +19,7 @@ public EventDispatcher()
{
Logger = new Log();
Instance = this;
string debugMode = Natives.GetResourceMetadata(Natives.GetCurrentResourceName(), "fxevents_debug_mode", 0);
string debugMode = Natives.GetResourceMetadata((CString)Natives.GetCurrentResourceName(), "fxevents_debug_mode", 0);
Debug = debugMode == "yes" || debugMode == "true" || Convert.ToInt32(debugMode) > 0;
}

Expand Down Expand Up @@ -71,14 +71,14 @@ public static void Initalize(string inboundEvent, string outboundEvent, string s
}

/// <summary>
/// registra un evento client (TriggerEvent)
/// Register an Event (TriggerEvent)
/// </summary>
/// <param name="eventName">Nome evento</param>
/// <param name="action">Azione legata all'evento</param>
internal async void AddEventHandler(string eventName, Delegate action)
/// <param name="name">Event Name</param>
/// <param name="action">Event-related action</param>
internal async void AddEventHandler(string eventName, DynFunc action)
{
while (!Initialized) await BaseScript.Delay(0);
EventHandlers[eventName].Add(Func.Create(action));
EventHandlers[eventName].Add(action, Binding.All);
}

public static void Send(string endpoint, params object[] args)
Expand All @@ -99,7 +99,7 @@ public static async Coroutine<T> Get<T>(string endpoint, params object[] args)
}
return await Events.Get<T>(endpoint, args);
}
public static void Mount(string endpoint, Delegate @delegate)
public static void Mount(string endpoint, DynFunc @delegate)
{
if (!Initialized)
{
Expand Down
30 changes: 16 additions & 14 deletions FxEvents/FxEvents.Client/EventSystem/ClientGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,9 @@ public ClientGateway()

internal void AddEvents()
{
EventDispatcher.Instance.AddEventHandler(InboundPipeline, new Action<byte[]>(async serialized =>
{
try
{
await ProcessInboundAsync(new ServerId().Handle, serialized);
}
catch (Exception ex)
{
Logger.Error("InboundPipeline:" + ex.ToString());
}
}));
EventDispatcher.Instance.AddEventHandler(InboundPipeline, Func.Create<Remote, byte[]>(OnInboundPipelineHandler));

EventDispatcher.Instance.AddEventHandler(OutboundPipeline, new Action<byte[]>(serialized =>
EventDispatcher.Instance.AddEventHandler(OutboundPipeline, Func.Create<byte[]>(serialized =>
{
try
{
Expand All @@ -49,16 +39,28 @@ internal void AddEvents()
}
}));

EventDispatcher.Instance.AddEventHandler(SignaturePipeline, new Action<string>(signature => _signature = signature));
EventDispatcher.Instance.AddEventHandler(SignaturePipeline, Func.Create<string>(signature => _signature = signature));
Events.TriggerServerEvent(SignaturePipeline);
}

private async void OnInboundPipelineHandler([Source] Remote remote, byte[] serialized)
{
try
{
await ProcessInboundAsync(remote, serialized);
}
catch (Exception ex)
{
Logger.Error("InboundPipeline:" + ex.ToString());
}
}

public async Coroutine PrepareAsync(string pipeline, int source, IMessage message)
{
if (string.IsNullOrWhiteSpace(_signature))
{
StopwatchUtil stopwatch = StopwatchUtil.StartNew();
while (_signature == null) await BaseScript.Delay(0);
while (_signature == null) await BaseScript.Yield();
if (EventDispatcher.Debug)
{
Logger.Debug($"[{message}] Halted {stopwatch.Elapsed.TotalMilliseconds}ms due to signature retrieval.");
Expand Down
14 changes: 4 additions & 10 deletions FxEvents/FxEvents.Client/FxEvents.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>FxEvents.Client</RootNamespace>
<AssemblyName>FxEvents.Client</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFramework>net452</TargetFramework>
<TargetFrameworks>net462;net472;net48</TargetFrameworks>
<LangVersion>latest</LangVersion>
<BaseOutputPath>..\CompiledLibs\Client</BaseOutputPath>
<Title>FxEvents.Client</Title>
Expand All @@ -35,7 +34,7 @@
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<AssemblyVersion></AssemblyVersion>
<FileVersion></FileVersion>
<Version>1.0.2</Version>
<Version>2.0.0-beta3-monov2</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug FiveM' Or '$(Configuration)' == 'Debug RedM'">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -54,13 +53,8 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="CitizenFX.Core">
<HintPath>L:\FiveM_gioco\FiveM.app\citizen\clr2\lib\mono\4.5\v2\CitizenFX.Core.dll</HintPath>
</Reference>
<Reference Include="CitizenFX.FiveM">
<HintPath>L:\FiveM_gioco\FiveM.app\citizen\clr2\lib\mono\4.5\v2\CitizenFX.FiveM.dll</HintPath>
</Reference>
<Reference Include="CitizenFX.FiveM.Native">
<HintPath>L:\FiveM_gioco\FiveM.app\citizen\clr2\lib\mono\4.5\v2\Native\CitizenFX.FiveM.Native.dll</HintPath>
<HintPath>..\dependencies\FiveM\Client\CitizenFX.Core.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="MsgPack">
<HintPath>..\FiveMMsgPack\MsgPack.dll</HintPath>
Expand Down
15 changes: 8 additions & 7 deletions FxEvents/FxEvents.Server/EventDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
global using CitizenFX.Core;
global using CitizenFX.Server.Native;
using CitizenFX.Server;
using FxEvents.EventSystem;
using FxEvents.Shared;
using FxEvents.Shared.EventSubsystem;
Expand All @@ -25,7 +26,7 @@ public EventDispatcher()
GetPlayers = new PlayerList();
Logger = new Log();
Instance = this;
string debugMode = Natives.GetResourceMetadata(Natives.GetCurrentResourceName(), "fxevents_debug_mode", 0);
string debugMode = Natives.GetResourceMetadata((CString)Natives.GetCurrentResourceName(), "fxevents_debug_mode", 0);
Debug = debugMode == "yes" || debugMode == "true" || Convert.ToInt32(debugMode) > 0;
}

Expand Down Expand Up @@ -77,14 +78,14 @@ public static void Initalize(string inboundEvent, string outboundEvent, string s
}

/// <summary>
/// registra un evento (TriggerEvent)
/// Register an Event (TriggerEvent)
/// </summary>
/// <param name="name">Nome evento</param>
/// <param name="action">Azione legata all'evento</param>
internal async void AddEventHandler(string eventName, Delegate action)
/// <param name="name">Event Name</param>
/// <param name="action">Event-related action</param>
internal async void AddEventHandler(string eventName, DynFunc action)
{
while (!Initialized) await Delay(0);
EventHandlers[eventName].Add(Func.Create(action));
EventHandlers[eventName].Add(action, Binding.All);
}

public static void Send(Player player, string endpoint, params object[] args)
Expand Down Expand Up @@ -141,7 +142,7 @@ public static Coroutine<T> Get<T>(ISource client, string endpoint, params object
}
return Events.Get<T>(client.Handle, endpoint, args);
}
public static void Mount(string endpoint, Delegate @delegate)
public static void Mount(string endpoint, DynFunc @delegate)
{
if (!Initialized)
{
Expand Down
30 changes: 17 additions & 13 deletions FxEvents/FxEvents.Server/EventSystem/ServerGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using Player = CitizenFX.Server.Player;

namespace FxEvents.EventSystem
{
Expand All @@ -27,9 +28,9 @@ public ServerGateway()

internal void AddEvents()
{
EventDispatcher.Instance.AddEventHandler(SignaturePipeline, new Action<Remote>(GetSignature));
EventDispatcher.Instance.AddEventHandler(InboundPipeline, new Action<Remote, byte[]>(Inbound));
EventDispatcher.Instance.AddEventHandler(OutboundPipeline, new Action<Remote, byte[]>(Outbound));
EventDispatcher.Instance.AddEventHandler(SignaturePipeline, Func.Create<Player>(GetSignature));
EventDispatcher.Instance.AddEventHandler(InboundPipeline, Func.Create<Remote, byte[]>(Inbound));
EventDispatcher.Instance.AddEventHandler(OutboundPipeline, Func.Create<Player, byte[]>(Outbound));
}

public void Push(string pipeline, int source, byte[] buffer)
Expand All @@ -40,12 +41,11 @@ public void Push(string pipeline, int source, byte[] buffer)
Events.TriggerAllClientsEvent(pipeline, buffer);
}


private void GetSignature(Remote source)
private void GetSignature([Source] Player source)
{
try
{
int client = int.Parse(source.ToString().Substring(7, source.ToString().Length - 1));
int client = source.Handle;

if (_signatures.ContainsKey(client))
{
Expand All @@ -63,21 +63,25 @@ private void GetSignature(Remote source)
string signature = BitConverter.ToString(holder).Replace("-", "").ToLower();

_signatures.Add(client, signature);
Events.TriggerClientEvent(SignaturePipeline, EventDispatcher.Instance.GetPlayers[client], signature);
Events.TriggerClientEvent(SignaturePipeline, source, signature);
}
catch (Exception ex)
{
Logger.Error(ex.ToString());
}
}

private async void Inbound(Remote source, byte[] buffer)
private async void Inbound([Source] Remote source, byte[] buffer)
{
try
{
int client = int.Parse(source.ToString().Substring(7, source.ToString().Length - 1));
int client = ((Player)source).Handle;

if (!_signatures.TryGetValue(client, out string signature)) return;
if (!_signatures.TryGetValue(client, out string signature))
{
Logger.Error($"Client {(string)Natives.GetPlayerName("" + client)}[{client}] tried sending an event without a signature.");
return;
}

using SerializationContext context = new SerializationContext(InboundPipeline, null, Serialization, buffer);

Expand All @@ -88,7 +92,7 @@ private async void Inbound(Remote source, byte[] buffer)

try
{
await ProcessInboundAsync(message, client);
await ProcessInboundAsync(message, source);
}
catch (TimeoutException)
{
Expand All @@ -112,11 +116,11 @@ public bool VerifySignature(int source, IMessage message, string signature)
return false;
}

private void Outbound(Remote source, byte[] buffer)
private void Outbound([Source] Player source, byte[] buffer)
{
try
{
int client = int.Parse(source.ToString().Substring(7, source.ToString().Length - 1));
int client = source.Handle;

if (!_signatures.TryGetValue(client, out string signature)) return;

Expand Down
10 changes: 6 additions & 4 deletions FxEvents/FxEvents.Server/FxEvents.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<AssemblyTitle>FxEvents Server</AssemblyTitle>
<Description>FxEvents an advanced event subsystem for FiveM C# Resources.</Description>
<Description>FxEvents an advanced event subsystem for CFX C# Resources.</Description>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Title>FxEvents.Server</Title>
Expand All @@ -19,7 +19,7 @@
<Configurations>Debug;Release</Configurations>
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<Version>1.0.2</Version>
<Version>2.0.0-beta3-monov2</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand All @@ -41,10 +41,12 @@
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" ExcludeAssets="Compile" GeneratePathProperty="true" />
<Reference Include="CitizenFX.Core">
<HintPath>L:\FiveM\ManuGamemode\citizen\clr2\lib\mono\4.5\v2\CitizenFX.Core.dll</HintPath>
<HintPath>..\dependencies\FiveM\Server\CitizenFX.Core.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="CitizenFX.Server">
<HintPath>L:\FiveM\ManuGamemode\citizen\clr2\lib\mono\4.5\v2\CitizenFX.Server.dll</HintPath>
<HintPath>..\dependencies\FiveM\Server\CitizenFX.Server.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="MsgPack">
<HintPath>..\FiveMMsgPack\MsgPack.dll</HintPath>
Expand Down
Loading