[LuaNetworkVariables] Remove exp stuff, fix context properties

This commit is contained in:
NotAKidoS 2025-04-14 18:11:23 -05:00
parent ece15e0dfc
commit e85c1e2f25
10 changed files with 95 additions and 514 deletions

View file

@ -1,6 +1,6 @@
using MoonSharp.Interpreter;
using ABI_RC.Core.Networking;
using MoonSharp.Interpreter;
using ABI_RC.Core.Player;
using ABI_RC.Core.Savior;
namespace NAK.LuaNetVars;
@ -12,9 +12,9 @@ public struct LuaEventContext
private double TimeSinceLastInvoke { get; set; }
private bool IsLocal { get; set; }
public static LuaEventContext Create(string senderId, DateTime lastInvokeTime)
public static LuaEventContext Create(bool isLocal, string senderId, DateTime lastInvokeTime)
{
var playerName = CVRPlayerManager.Instance.TryGetPlayerName(senderId);
var playerName = isLocal ? AuthManager.Username : CVRPlayerManager.Instance.TryGetPlayerName(senderId);
return new LuaEventContext
{
@ -22,7 +22,7 @@ public struct LuaEventContext
SenderName = playerName ?? "Unknown",
LastInvokeTime = lastInvokeTime,
TimeSinceLastInvoke = (DateTime.Now - lastInvokeTime).TotalSeconds,
IsLocal = senderId == MetaPort.Instance.ownerId
IsLocal = isLocal
};
}
@ -30,11 +30,11 @@ public struct LuaEventContext
{
Table table = new(script)
{
["senderId"] = SenderId,
["senderName"] = SenderName,
["lastInvokeTime"] = LastInvokeTime.ToUniversalTime().ToString("O"),
["timeSinceLastInvoke"] = TimeSinceLastInvoke,
["isLocal"] = IsLocal
["SenderId"] = SenderId,
["SenderName"] = SenderName,
["LastInvokeTime"] = LastInvokeTime.ToUniversalTime().ToString("O"),
["TimeSinceLastInvoke"] = TimeSinceLastInvoke,
["IsLocal"] = IsLocal
};
return table;
}

View file

@ -5,6 +5,7 @@ using ABI.CCK.Components;
using ABI.Scripting.CVRSTL.Common;
using MoonSharp.Interpreter;
using UnityEngine;
using Coroutine = UnityEngine.Coroutine;
namespace NAK.LuaNetVars;
@ -26,21 +27,23 @@ public partial class LuaNetVarController : MonoBehaviour
private bool _requestInitialSync;
private CVRSpawnable _spawnable;
private CVRObjectSync _objectSync;
private bool _isInitialized;
private Coroutine _syncCoroutine;
#region Unity Events
private void Awake()
{
if (!Initialize())
return;
// TODO: a manager script should be in charge of this
// TODO: disabling object will kill coroutine
StartCoroutine(SendVariableUpdatesCoroutine());
}
=> _isInitialized = Initialize();
private void OnDestroy()
=> Cleanup();
private void OnEnable()
=> StartStopVariableUpdatesCoroutine(true);
private void OnDisable()
=> StartStopVariableUpdatesCoroutine(false);
#endregion Unity Events
@ -102,9 +105,16 @@ public partial class LuaNetVarController : MonoBehaviour
_hashes.Remove(_uniquePathHash);
}
private void StartStopVariableUpdatesCoroutine(bool start)
{
if (_syncCoroutine != null) StopCoroutine(_syncCoroutine);
_syncCoroutine = null;
if (start) _syncCoroutine = StartCoroutine(SendVariableUpdatesCoroutine());
}
private System.Collections.IEnumerator SendVariableUpdatesCoroutine()
{
while (true)
while (isActiveAndEnabled)
{
yield return new WaitForSeconds(0.1f);
if (IsSyncOwner()) SendVariableUpdates();

View file

@ -1,7 +1,6 @@
using ABI_RC.Core.Savior;
using ABI_RC.Systems.ModNetwork;
using MoonSharp.Interpreter;
using Unity.Services.Authentication.Internal;
namespace NAK.LuaNetVars
{
@ -66,7 +65,7 @@ namespace NAK.LuaNetVars
msg.Read(out int argsCount);
DateTime lastInvokeTime = _eventTracker.GetLastInvokeTimeForSender(eventName, senderId);
LuaEventContext context = LuaEventContext.Create(senderId, lastInvokeTime);
LuaEventContext context = LuaEventContext.Create(false, senderId, lastInvokeTime);
// Update tracking
_eventTracker.UpdateInvokeTime(eventName, senderId);
@ -187,7 +186,7 @@ namespace NAK.LuaNetVars
{
string senderId = MetaPort.Instance.ownerId;
DateTime lastInvokeTime = _eventTracker.GetLastInvokeTimeForSender(eventName, senderId);
LuaEventContext context = LuaEventContext.Create(senderId, lastInvokeTime);
LuaEventContext context = LuaEventContext.Create(true, senderId, lastInvokeTime);
// Update tracking
_eventTracker.UpdateInvokeTime(eventName, senderId);
@ -209,6 +208,32 @@ namespace NAK.LuaNetVars
modMsg.Send();
}
internal void SendLuaEventToUser(string eventName, string userId, DynValue[] args)
{
string senderId = MetaPort.Instance.ownerId;
DateTime lastInvokeTime = _eventTracker.GetLastInvokeTimeForSender(eventName, senderId);
LuaEventContext context = LuaEventContext.Create(true, senderId, lastInvokeTime);
// Update tracking
_eventTracker.UpdateInvokeTime(eventName, senderId);
var argsWithContext = new DynValue[args.Length + 1];
argsWithContext[0] = DynValue.FromObject(_luaClientBehaviour.script, context.ToLuaTable(_luaClientBehaviour.script));
Array.Copy(args, 0, argsWithContext, 1, args.Length);
InvokeLuaEvent(eventName, argsWithContext);
using ModNetworkMessage modMsg = new(ModNetworkID, userId);
modMsg.Write((byte)MessageType.LuaEvent);
modMsg.Write(eventName);
modMsg.Write(args.Length);
foreach (DynValue arg in args)
SerializeDynValue(modMsg, arg);
modMsg.Send();
}
#endregion
}
}