From 71129e2f401afe30f39ef338bd543cdb489db53a Mon Sep 17 00:00:00 2001
From: unknown <_ __ _>
Date: Sun, 6 Dec 2020 13:58:08 -0500
Subject: [PATCH] Added serverside Nui message capabilities, added json
conversion methods.
---
VinaFrameworkClient/Core/BaseClient.cs | 137 +++++++++++++++++++----
VinaFrameworkClient/Core/ModuleScript.cs | 32 ------
VinaFrameworkClient/Shared/NuiRequest.cs | 2 +-
VinaFrameworkServer/Core/BaseServer.cs | 86 +++++++++++++-
4 files changed, 203 insertions(+), 54 deletions(-)
diff --git a/VinaFrameworkClient/Core/BaseClient.cs b/VinaFrameworkClient/Core/BaseClient.cs
index 9ae655b..75d30a9 100644
--- a/VinaFrameworkClient/Core/BaseClient.cs
+++ b/VinaFrameworkClient/Core/BaseClient.cs
@@ -5,6 +5,10 @@
using CitizenFX.Core;
using CitizenFX.Core.Native;
+using Newtonsoft.Json;
+
+using VinaFrameworkClient.Shared;
+
namespace VinaFrameworkClient.Core
{
///
@@ -25,6 +29,8 @@ public BaseClient()
modules = new List();
deadPlayers = new List();
+ EventHandlers[$"internal:{Name}:onServerNuiRequest"] += new Action(onServerNuiRequest);
+
Tick += initialize;
Tick += garbageCollect;
}
@@ -75,6 +81,14 @@ protected bool UsePlayerDeadResurectWatcher
#endregion
#region BASE EVENTS
+ private void onServerNuiRequest(NuiRequest request)
+ {
+ SendNuiActionData(request);
+ }
+
+ #endregion
+ #region SERVER TICKS
+
private async Task initialize()
{
Tick -= initialize;
@@ -86,6 +100,20 @@ private async Task initialize()
TriggerServerEvent($"internal:{Name}:onPlayerClientInitialized");
}
+ private async Task garbageCollect()
+ {
+ if (!UseGarbageCollector)
+ {
+ await Delay(1);
+ return;
+ }
+
+ await Delay(60000);
+
+ GC.Collect();
+ GC.WaitForPendingFinalizers();
+ }
+
private async Task playerDeadResurectWatcher()
{
await Delay(250); // run 4 times per second, experimental
@@ -113,23 +141,6 @@ private async Task playerDeadResurectWatcher()
}
}
- #endregion
- #region SERVER TICKS
-
- private async Task garbageCollect()
- {
- if (!UseGarbageCollector)
- {
- await Delay(1);
- return;
- }
-
- await Delay(60000);
-
- GC.Collect();
- GC.WaitForPendingFinalizers();
- }
-
#endregion
#region CLIENT METHODS
@@ -281,11 +292,99 @@ protected void SetExport(string name, Delegate method)
Exports.Add(name, method);
}
+ ///
+ /// Send a message to this resource Nui.
+ ///
+ /// The nui request object.
+ public static void SendNuiActionData(NuiRequest nuiRequest)
+ {
+ try
+ {
+ string serializedQuery = JsonConvert.SerializeObject(nuiRequest, Formatting.Indented);
+
+ API.SendNuiMessage(serializedQuery);
+ }
+ catch (Exception exception)
+ {
+ LogError(exception, " in SendNuiActionData");
+ }
+ }
+
+ ///
+ /// Send a message to this resource Nui.
+ ///
+ /// The action name.
+ /// Some data will be sent as a string.
+ public static void SendNuiActionData(string action, dynamic data = null)
+ {
+ NuiRequest request = new NuiRequest(action, data);
+ SendNuiActionData(request);
+ }
+
+ ///
+ /// Serialize an object into a json string.
+ ///
+ /// The object to convert into a string.
+ /// The json format, indented or none by default.
+ /// The converted json string.
+ public static string SerializeObject(dynamic obj, Formatting formatting = Formatting.None)
+ {
+ try
+ {
+ return JsonConvert.SerializeObject(obj, formatting);
+ }
+ catch (Exception exception)
+ {
+ LogError(exception, " in SerializeObject");
+ }
+
+ return "";
+ }
+
+ ///
+ /// Deserialize a json into a dynamic object.
+ ///
+ /// The json string to convert into an object.
+ /// The converted object.
+ public static dynamic DeserializeObject(string json)
+ {
+ try
+ {
+ return JsonConvert.DeserializeObject(json);
+ }
+ catch (Exception exception)
+ {
+ LogError(exception, " in DeserializeObject");
+ }
+
+ return null;
+ }
+
+ ///
+ /// Deserialize an json into an object of a specific Type.
+ ///
+ /// The type to convert the json to.
+ /// The json string to convert into an object.
+ /// The converted object.
+ public static T DeserializeObject(string json)
+ {
+ try
+ {
+ return JsonConvert.DeserializeObject(json);
+ }
+ catch (Exception exception)
+ {
+ LogError(exception, $" in DeserializeObject with generic type {typeof(T).Name}");
+ }
+
+ return default(T);
+ }
+
///
/// Log a message.
///
/// The message to log.
- protected void Log(string message)
+ protected static void Log(string message)
{
Debug.WriteLine($"{DateTime.Now.ToLongTimeString()} [INFO] {BaseClient.ResourceName.ToUpper()}: {message}");
}
@@ -295,7 +394,7 @@ protected void Log(string message)
///
/// The Exception to log.
/// Some text to add before the log message.
- protected void LogError(Exception exception, string prefix = "")
+ protected static void LogError(Exception exception, string prefix = "")
{
string pre = (prefix != "") ? prefix : "";
Debug.WriteLine($"{DateTime.Now.ToLongTimeString()} [ERROR] {BaseClient.ResourceName.ToUpper()}{pre}: {exception.Message}\n{exception.StackTrace}");
diff --git a/VinaFrameworkClient/Core/ModuleScript.cs b/VinaFrameworkClient/Core/ModuleScript.cs
index 04576a1..3b80779 100644
--- a/VinaFrameworkClient/Core/ModuleScript.cs
+++ b/VinaFrameworkClient/Core/ModuleScript.cs
@@ -1,10 +1,7 @@
using System;
using System.Threading.Tasks;
-using Newtonsoft.Json;
-
using CitizenFX.Core;
-using CitizenFX.Core.Native;
namespace VinaFrameworkClient.Core
{
@@ -109,35 +106,6 @@ public void SetExport(string name, Delegate method)
Log($"Export {name} set!");
}
- ///
- /// Send a message to this resource Nui.
- ///
- /// The nui request object.
- public void SendNuiActionData(NuiRequest nuiRequest)
- {
- try
- {
- string serializedQuery = JsonConvert.SerializeObject(nuiRequest, Formatting.Indented);
-
- API.SendNuiMessage(serializedQuery);
- }
- catch (Exception exception)
- {
- LogError(exception, " in SendNuiActionData");
- }
- }
-
- ///
- /// Send a message to this resource Nui.
- ///
- /// The action name.
- /// Some data will be sent as a string.
- public void SendNuiActionData(string action, dynamic data = null)
- {
- NuiRequest request = new NuiRequest(action, data);
- SendNuiActionData(request);
- }
-
///
/// Log a message.
///
diff --git a/VinaFrameworkClient/Shared/NuiRequest.cs b/VinaFrameworkClient/Shared/NuiRequest.cs
index 5849c87..cb7ddfb 100644
--- a/VinaFrameworkClient/Shared/NuiRequest.cs
+++ b/VinaFrameworkClient/Shared/NuiRequest.cs
@@ -1,4 +1,4 @@
-namespace VinaFrameworkClient.Core
+namespace VinaFrameworkClient.Shared
{
public class NuiRequest
{
diff --git a/VinaFrameworkServer/Core/BaseServer.cs b/VinaFrameworkServer/Core/BaseServer.cs
index 4379f4c..e9ceedb 100644
--- a/VinaFrameworkServer/Core/BaseServer.cs
+++ b/VinaFrameworkServer/Core/BaseServer.cs
@@ -5,6 +5,10 @@
using CitizenFX.Core;
using CitizenFX.Core.Native;
+using Newtonsoft.Json;
+
+using VinaFrameworkClient.Shared;
+
namespace VinaFrameworkServer.Core
{
///
@@ -267,11 +271,89 @@ protected void SetExport(string name, Delegate method)
Exports.Add(name, method);
}
+ ///
+ /// Send a nui message to a player client.
+ ///
+ /// The player to send to.
+ /// The nui request to send.
+ public static void SendNuiActionDataTo(Player player, NuiRequest nuiRequest)
+ {
+ TriggerClientEvent(player, $"internal:{ResourceName}:onServerNuiRequest", nuiRequest);
+ }
+
+ ///
+ /// Send a nui message to all player clients.
+ ///
+ /// The nui request to send to all players.
+ public static void SendNuiActionDataToAll(NuiRequest nuiRequest)
+ {
+ TriggerClientEvent($"internal:{ResourceName}:onServerNuiRequest", nuiRequest);
+ }
+
+ ///
+ /// Serialize an object into a json string.
+ ///
+ /// The object to convert into a string.
+ /// The json format, indented or none by default.
+ /// The converted json string.
+ public static string SerializeObject(dynamic obj, Formatting formatting = Formatting.None)
+ {
+ try
+ {
+ return JsonConvert.SerializeObject(obj, formatting);
+ }
+ catch (Exception exception)
+ {
+ LogError(exception, " in SerializeObject");
+ }
+
+ return "";
+ }
+
+ ///
+ /// Deserialize a json into a dynamic object.
+ ///
+ /// The json string to convert into an object.
+ /// The converted object.
+ public static dynamic DeserializeObject(string json)
+ {
+ try
+ {
+ return JsonConvert.DeserializeObject(json);
+ }
+ catch (Exception exception)
+ {
+ LogError(exception, " in DeserializeObject");
+ }
+
+ return null;
+ }
+
+ ///
+ /// Deserialize an json into an object of a specific Type.
+ ///
+ /// The type to convert the json to.
+ /// The json string to convert into an object.
+ /// The converted object.
+ public static T DeserializeObject(string json)
+ {
+ try
+ {
+ return JsonConvert.DeserializeObject(json);
+ }
+ catch (Exception exception)
+ {
+ LogError(exception, $" in DeserializeObject with generic type {typeof(T).Name}");
+ }
+
+ return default(T);
+ }
+
///
/// Log a message.
///
/// The message to log.
- protected void Log(string message)
+ protected static void Log(string message)
{
Debug.WriteLine($"{DateTime.Now.ToLongTimeString()} [INFO] {BaseServer.ResourceName.ToUpper()}: {message}");
}
@@ -281,7 +363,7 @@ protected void Log(string message)
///
/// The Exception to log.
/// Some text to add before the log message.
- protected void LogError(Exception exception, string prefix = "")
+ protected static void LogError(Exception exception, string prefix = "")
{
string pre = (prefix != "") ? prefix : "";
Debug.WriteLine($"{DateTime.Now.ToLongTimeString()} [ERROR] {BaseServer.ResourceName.ToUpper()}{pre}: {exception.Message}\n{exception.StackTrace}");