using ABI_RC.Core.Base; using ABI.Scripting.CVRSTL.Common; using JetBrains.Annotations; using NAK.LuaNetVars; using MoonSharp.Interpreter; namespace NAK.LuaNetVars.Modules; [PublicAPI] // Indicates that this class is used and should not be considered unused public class LuaNetModule : BaseScriptedStaticWrapper { public const string MODULE_ID = "NetworkModule"; private LuaNetVarController _controller; public LuaNetModule(CVRLuaContext context) : base(context) { _controller = context.behaviour.AddComponentIfMissing(); } internal static object RegisterUserData(Script script, CVRLuaContext context) { // Register the LuaNetModule type with MoonSharp UserData.RegisterType(InteropAccessMode.Default, MODULE_ID); return new LuaNetModule(context); } #region Module Instance Methods /// /// Registers a network variable that can be synchronized over the network. /// /// The name of the variable to register. public void RegisterNetworkVar(string varName) { CheckIfCanAccessMethod(nameof(RegisterNetworkVar), false, CVRLuaEnvironmentContext.CLIENT, CVRLuaObjectContext.ALL_BUT_EVENTS, CVRLuaOwnerContext.ANY); if (_controller == null) { LuaNetVarsMod.Logger.Error("LuaNetVarController is null."); return; } _controller.RegisterNetworkVar(varName); } /// /// Registers a callback function to be called when the specified network variable changes. /// /// The name of the variable to watch. /// The Lua function to call when the variable changes. public void RegisterNotifyCallback(string varName, DynValue callback) { CheckIfCanAccessMethod(nameof(RegisterNotifyCallback), false, CVRLuaEnvironmentContext.CLIENT, CVRLuaObjectContext.ALL_BUT_EVENTS, CVRLuaOwnerContext.ANY); if (_controller == null) { LuaNetVarsMod.Logger.Error("LuaNetVarController is null."); return; } _controller.RegisterNotifyCallback(varName, callback); } /// /// Registers a callback function to be called when the specified event is received. /// /// The name of the event to watch. /// The Lua function to call when the event occurs. public void RegisterEventCallback(string eventName, DynValue callback) { CheckIfCanAccessMethod(nameof(RegisterEventCallback), false, CVRLuaEnvironmentContext.CLIENT, CVRLuaObjectContext.ALL_BUT_EVENTS, CVRLuaOwnerContext.ANY); if (_controller == null) { LuaNetVarsMod.Logger.Error("LuaNetVarController is null."); return; } _controller.RegisterEventCallback(eventName, callback); } /// /// Sends a Lua event to other clients. /// /// The name of the event to send. /// Optional arguments to send with the event. public void SendLuaEvent(string eventName, params DynValue[] args) { CheckIfCanAccessMethod(nameof(SendLuaEvent), false, CVRLuaEnvironmentContext.CLIENT, CVRLuaObjectContext.ALL_BUT_EVENTS, CVRLuaOwnerContext.ANY); if (_controller == null) { LuaNetVarsMod.Logger.Error("LuaNetVarController is null."); return; } _controller.SendLuaEvent(eventName, args); } /// /// Checks if the current client is the owner of the synchronized object. /// public bool IsSyncOwner() { CheckIfCanAccessMethod(nameof(IsSyncOwner), false, CVRLuaEnvironmentContext.CLIENT, CVRLuaObjectContext.ALL_BUT_EVENTS, CVRLuaOwnerContext.ANY); if (_controller == null) { LuaNetVarsMod.Logger.Error("LuaNetVarController is null."); return false; } return _controller.IsSyncOwner(); } public string GetSyncOwner() { CheckIfCanAccessMethod(nameof(GetSyncOwner), false, CVRLuaEnvironmentContext.CLIENT, CVRLuaObjectContext.ALL_BUT_EVENTS, CVRLuaOwnerContext.ANY); if (_controller == null) { LuaNetVarsMod.Logger.Error("LuaNetVarController is null."); return string.Empty; } return _controller.GetSyncOwner(); } #endregion }