FxEvents an advanced event subsystem for FiveM
A C# advanced library to handle Events and Callbacks both client ↔ server
With FxEvents you can send and request strongly typized values between client and server using an advanced event handling process.
Signatures for each client are encrypted and using the provided binary serialization to it’s 2x times faster than Newtonsoft.Json, also you can hide event values from malicious clients!
To make it work you only need to add FXEvents.Client.dll
or FXEvents.Server.dll
and Newtonsoft.Json.dll
(In case of json serialization) to your resource.
No need of any external library for binary serialization, the event system uses the internal MsgPack.dll provided with fivem itself!!
GitHub Page: GitHub - manups4e/FxEvents: An advanced event subsystem for FiveM
Download: Release First public release · manups4e/FxEvents · GitHub
Usage examples:
To Debug your resource:
To debug simply add this line in your resource fxmanifest.lua, FXEvents will debug your events and callbacks, displaying the time (milliseconds) they take to complete.
fxevents_debug_mode 'true'
Remember to remove or comment this line in production!
To mount an event:
// It can be declared also as normal events EventDispatcher.Mount("eventName", new Action<ISource, type1, type2>(MyMethod));
// public void MyMethod(ISource param, type1 param1, type2 param2) { ...code }
EventDispatcher.Mount("eventName", new Action<ISource, type1, type2>((source, val1, val2) =>
{
// code to be used inside the event.
// ISource is the optional Interface provided that handles clients triggering the event.. is like the "[FromSource] Player player" parameter but can be derived and handled as you want! So you can directly load any User / Player / Client / whatever type that handles the player for you!
// Clientside is the same thing without the ISource parameter
}));
To trigger an event
The library only works in client ↔ server communication… for the moment same side events are not working but the feature will be added in the future!
// clientside
EventDispatcher.Send("eventName", params);
// serverside
EventDispatcher.Send(Player, "eventName", params);
EventDispatcher.Send(List<Player>, "eventName", params);
EventDispatcher.Send(ISource, "eventName", params);
EventDispatcher.Send(List<ISource>, "eventName", params);
To trigger a callback
Mounting it
EventDispatcher.Mount("eventName", new Func<ISource, type1, type2, Task<returnType>>(async (source, val1, val2) =>
{
// code to be used inside the event.
// ISource is the optional Interface provided that handles clients triggering the event.. is like the "[FromSource] Player player" parameter but can be derived and handled as you want! So you can directly load any User / Player / Client / whatever type that handles the player for you!
// Clientside is the same thing without the ClientId parameter
return val3
}));
Calling it
// clientside
type param = await EventDispatcher.Get<type>("eventName", params);
// serverside
type param = await EventDispatcher.Get<type>(ClientId, "eventName", params);
type param = await EventDispatcher.Get<type>(Player, "eventName", params);
Callbacks can be called serverside too because it might happen that the server needs info from certain clients and this will help you doing it.
The library comes with some goodies to help with customization and debugging serialization printing.
ToJson()
You need Newtonsoft.Json to make this work!!
string text = param.ToJson();
FromJson()
You need Newtonsoft.Json to make this work!!
type value = jsonText.FromJson<type>();
ToBytes()
byte[] bytes = param.ToBytes();
FromBytes()
type value = bytes.FromBytes<type>();
BytesToString
byte[] bytes = param.ToBytes();
string txt = bytes.BytesToString();
StringToBytes
byte[] bytes = txt.StringToBytes();
type value = bytes.FromBytes<type>();
FXEvents comes also with a Logger class that can be used to give “Info”, “Debug”, “Warning”, “Error” and “Fatal” messages.
FxEvents has reached its 3.0.0 version and it’s gonna bring most of the V2 features into mono V1 + extras.
Features:
- New EventHub Class: Streamline your event management with our newly introduced EventHub class.
- Enhanced Encryption:
- Internally: Improved encryption between Client and Server using robust public/private key mechanisms.
- Externally: Updated public API methods featuring SHA-256 encryption for enhanced security.
- Advanced Event Management: Experience a new event management system that emulates the functionality of V2, without the previous bugs.
- Event Attributes: Simplify event and callback definitions using the
[FxEvent("Event name")]
attribute. - Optimized Callback Handling: Enjoy faster response times with enhanced callback handling, allowing callbacks from Server to Client and back as an extra.
- Improved Serialization: Updated MsgPack serialization code enhances performance and reduces binary data size.
- Native ValueTuple Support: Full native support for
ValueTuple
in .NET Framework, eliminating the need for external NuGet libraries and enabling MsgPack serialization. - New EventHub initialization : Initializing EventHub now only requires you to call Initialize() method without parameters… encrypted event parameters are now SHA-256 encrypted based on resource name
Features taken from mono V2:
- Introduced MsgPack native internal type conversion (thanks @thorium for your MsgPack and dedication to us… you’re the only element that deserved my respect)
- Introduced mono V2 event binding
FxEvent
attribute binds to All by default and can be changed in attribute constructorEventDispatcher
legacy Mount method binds automatically to AllEventHub
Mount method requires to choose a binding
- Introduced a basic Anti-Tamper system that uses a server event (
fxevents:tamperingprotection
) with parameters [source
,endpoint
,TamperType
]- TamperType can be
REPEATED_MESSAGE_ID
: someone tried to send the same event or a new event with an used IDEDITED_ENCRYPTED_DATA
: someone altered the encrypted data trying to change values making it impossible to decryptREQUESTED_NEW_PUBLIC_KEY
: a client tried to request a new ID to change the encryption and thus edit the encryption
- TamperType can be
Some pictures:
As you can see GimmeAll2 and Gimmeall5 are not called as the event triggered is a local event.
Binding makes it so Remote events are only available to client/server communication and Local events only to same side communication while All is available to both and None makes the event invisible to any call possible.
Same approach applies to callbacks but only 1 can be registered.
Some neat internal conversion to handle your events and encryption the most fluid as possible.
Beware: if a conversion can’t be made, a throw is launched to avoid data corruption!
Code is accessible | Yes |
Subscription-based | No |
Lines (approximately) | a lot but never enough |
Requirements | Client: .Net Framework 4.5.2 / Server: .Net Standard 2.0 |
Support | Yes |
A huge thanks to @lacoL for being a friend in the toughest of times