Skip to content

Commit

Permalink
Added serverside Nui message capabilities, added json conversion meth…
Browse files Browse the repository at this point in the history
…ods.
  • Loading branch information
unknown committed Dec 6, 2020
1 parent a639da7 commit 71129e2
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 54 deletions.
137 changes: 118 additions & 19 deletions VinaFrameworkClient/Core/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
using CitizenFX.Core;
using CitizenFX.Core.Native;

using Newtonsoft.Json;

using VinaFrameworkClient.Shared;

namespace VinaFrameworkClient.Core
{
/// <summary>
Expand All @@ -25,6 +29,8 @@ public BaseClient()
modules = new List<Module>();
deadPlayers = new List<Player>();

EventHandlers[$"internal:{Name}:onServerNuiRequest"] += new Action<NuiRequest>(onServerNuiRequest);

Tick += initialize;
Tick += garbageCollect;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -281,11 +292,99 @@ protected void SetExport(string name, Delegate method)
Exports.Add(name, method);
}

/// <summary>
/// Send a message to this resource Nui.
/// </summary>
/// <param name="nuiRequest">The nui request object.</param>
public static void SendNuiActionData(NuiRequest nuiRequest)
{
try
{
string serializedQuery = JsonConvert.SerializeObject(nuiRequest, Formatting.Indented);

API.SendNuiMessage(serializedQuery);
}
catch (Exception exception)
{
LogError(exception, " in SendNuiActionData");
}
}

/// <summary>
/// Send a message to this resource Nui.
/// </summary>
/// <param name="action">The action name.</param>
/// <param name="data">Some data will be sent as a string.</param>
public static void SendNuiActionData(string action, dynamic data = null)
{
NuiRequest request = new NuiRequest(action, data);
SendNuiActionData(request);
}

/// <summary>
/// Serialize an object into a json string.
/// </summary>
/// <param name="obj">The object to convert into a string.</param>
/// <param name="formatting">The json format, indented or none by default.</param>
/// <returns>The converted json string.</returns>
public static string SerializeObject(dynamic obj, Formatting formatting = Formatting.None)
{
try
{
return JsonConvert.SerializeObject(obj, formatting);
}
catch (Exception exception)
{
LogError(exception, " in SerializeObject");
}

return "";
}

/// <summary>
/// Deserialize a json into a dynamic object.
/// </summary>
/// <param name="json">The json string to convert into an object.</param>
/// <returns>The converted object.</returns>
public static dynamic DeserializeObject(string json)
{
try
{
return JsonConvert.DeserializeObject(json);
}
catch (Exception exception)
{
LogError(exception, " in DeserializeObject");
}

return null;
}

/// <summary>
/// Deserialize an json into an object of a specific Type.
/// </summary>
/// <typeparam name="T">The type to convert the json to.</typeparam>
/// <param name="json">The json string to convert into an object.</param>
/// <returns>The converted object.</returns>
public static T DeserializeObject<T>(string json)
{
try
{
return JsonConvert.DeserializeObject<T>(json);
}
catch (Exception exception)
{
LogError(exception, $" in DeserializeObject with generic type {typeof(T).Name}");
}

return default(T);
}

/// <summary>
/// Log a message.
/// </summary>
/// <param name="message">The message to log.</param>
protected void Log(string message)
protected static void Log(string message)
{
Debug.WriteLine($"{DateTime.Now.ToLongTimeString()} [INFO] {BaseClient.ResourceName.ToUpper()}: {message}");
}
Expand All @@ -295,7 +394,7 @@ protected void Log(string message)
/// </summary>
/// <param name="exception">The Exception to log.</param>
/// <param name="prefix">Some text to add before the log message.</param>
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}");
Expand Down
32 changes: 0 additions & 32 deletions VinaFrameworkClient/Core/ModuleScript.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.Threading.Tasks;

using Newtonsoft.Json;

using CitizenFX.Core;
using CitizenFX.Core.Native;

namespace VinaFrameworkClient.Core
{
Expand Down Expand Up @@ -109,35 +106,6 @@ public void SetExport(string name, Delegate method)
Log($"Export {name} set!");
}

/// <summary>
/// Send a message to this resource Nui.
/// </summary>
/// <param name="nuiRequest">The nui request object.</param>
public void SendNuiActionData(NuiRequest nuiRequest)
{
try
{
string serializedQuery = JsonConvert.SerializeObject(nuiRequest, Formatting.Indented);

API.SendNuiMessage(serializedQuery);
}
catch (Exception exception)
{
LogError(exception, " in SendNuiActionData");
}
}

/// <summary>
/// Send a message to this resource Nui.
/// </summary>
/// <param name="action">The action name.</param>
/// <param name="data">Some data will be sent as a string.</param>
public void SendNuiActionData(string action, dynamic data = null)
{
NuiRequest request = new NuiRequest(action, data);
SendNuiActionData(request);
}

/// <summary>
/// Log a message.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion VinaFrameworkClient/Shared/NuiRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace VinaFrameworkClient.Core
namespace VinaFrameworkClient.Shared
{
public class NuiRequest
{
Expand Down
86 changes: 84 additions & 2 deletions VinaFrameworkServer/Core/BaseServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
using CitizenFX.Core;
using CitizenFX.Core.Native;

using Newtonsoft.Json;

using VinaFrameworkClient.Shared;

namespace VinaFrameworkServer.Core
{
/// <summary>
Expand Down Expand Up @@ -267,11 +271,89 @@ protected void SetExport(string name, Delegate method)
Exports.Add(name, method);
}

/// <summary>
/// Send a nui message to a player client.
/// </summary>
/// <param name="player">The player to send to.</param>
/// <param name="nuiRequest">The nui request to send.</param>
public static void SendNuiActionDataTo(Player player, NuiRequest nuiRequest)
{
TriggerClientEvent(player, $"internal:{ResourceName}:onServerNuiRequest", nuiRequest);
}

/// <summary>
/// Send a nui message to all player clients.
/// </summary>
/// <param name="nuiRequest">The nui request to send to all players.</param>
public static void SendNuiActionDataToAll(NuiRequest nuiRequest)
{
TriggerClientEvent($"internal:{ResourceName}:onServerNuiRequest", nuiRequest);
}

/// <summary>
/// Serialize an object into a json string.
/// </summary>
/// <param name="obj">The object to convert into a string.</param>
/// <param name="formatting">The json format, indented or none by default.</param>
/// <returns>The converted json string.</returns>
public static string SerializeObject(dynamic obj, Formatting formatting = Formatting.None)
{
try
{
return JsonConvert.SerializeObject(obj, formatting);
}
catch (Exception exception)
{
LogError(exception, " in SerializeObject");
}

return "";
}

/// <summary>
/// Deserialize a json into a dynamic object.
/// </summary>
/// <param name="json">The json string to convert into an object.</param>
/// <returns>The converted object.</returns>
public static dynamic DeserializeObject(string json)
{
try
{
return JsonConvert.DeserializeObject(json);
}
catch (Exception exception)
{
LogError(exception, " in DeserializeObject");
}

return null;
}

/// <summary>
/// Deserialize an json into an object of a specific Type.
/// </summary>
/// <typeparam name="T">The type to convert the json to.</typeparam>
/// <param name="json">The json string to convert into an object.</param>
/// <returns>The converted object.</returns>
public static T DeserializeObject<T>(string json)
{
try
{
return JsonConvert.DeserializeObject<T>(json);
}
catch (Exception exception)
{
LogError(exception, $" in DeserializeObject with generic type {typeof(T).Name}");
}

return default(T);
}

/// <summary>
/// Log a message.
/// </summary>
/// <param name="message">The message to log.</param>
protected void Log(string message)
protected static void Log(string message)
{
Debug.WriteLine($"{DateTime.Now.ToLongTimeString()} [INFO] {BaseServer.ResourceName.ToUpper()}: {message}");
}
Expand All @@ -281,7 +363,7 @@ protected void Log(string message)
/// </summary>
/// <param name="exception">The Exception to log.</param>
/// <param name="prefix">Some text to add before the log message.</param>
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}");
Expand Down

0 comments on commit 71129e2

Please sign in to comment.