mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-01-01 18:23:32 +00:00
Added LUA timer module
This commit is contained in:
parent
094b7abfc4
commit
b91ad6fcdd
@ -518,6 +518,8 @@
|
|||||||
<Compile Include="Profiles\Lua\Modules\LuaLayerModule.cs" />
|
<Compile Include="Profiles\Lua\Modules\LuaLayerModule.cs" />
|
||||||
<Compile Include="Profiles\Lua\Modules\LuaMouseModule.cs" />
|
<Compile Include="Profiles\Lua\Modules\LuaMouseModule.cs" />
|
||||||
<Compile Include="Profiles\Lua\Modules\LuaProfileModule.cs" />
|
<Compile Include="Profiles\Lua\Modules\LuaProfileModule.cs" />
|
||||||
|
<Compile Include="Profiles\Lua\Modules\Timer\LuaTimer.cs" />
|
||||||
|
<Compile Include="Profiles\Lua\Modules\LuaTimerModule.cs" />
|
||||||
<Compile Include="Profiles\Lua\Wrappers\LuaDrawWrapper.cs" />
|
<Compile Include="Profiles\Lua\Wrappers\LuaDrawWrapper.cs" />
|
||||||
<Compile Include="Profiles\Lua\Wrappers\LuaLayerWrapper.cs" />
|
<Compile Include="Profiles\Lua\Wrappers\LuaLayerWrapper.cs" />
|
||||||
<Compile Include="Profiles\ProfileModel.cs" />
|
<Compile Include="Profiles\ProfileModel.cs" />
|
||||||
|
|||||||
@ -21,8 +21,8 @@ namespace Artemis.Managers
|
|||||||
private readonly DeviceManager _deviceManager;
|
private readonly DeviceManager _deviceManager;
|
||||||
private readonly IKernel _kernel;
|
private readonly IKernel _kernel;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private List<LuaModule> _luaModules;
|
|
||||||
private readonly Script _luaScript;
|
private readonly Script _luaScript;
|
||||||
|
private List<LuaModule> _luaModules;
|
||||||
private FileSystemWatcher _watcher;
|
private FileSystemWatcher _watcher;
|
||||||
|
|
||||||
public LuaManager(IKernel kernel, ILogger logger, DeviceManager deviceManager)
|
public LuaManager(IKernel kernel, ILogger logger, DeviceManager deviceManager)
|
||||||
@ -52,17 +52,15 @@ namespace Artemis.Managers
|
|||||||
|
|
||||||
// Get new instances of all modules
|
// Get new instances of all modules
|
||||||
_luaModules = _kernel.Get<List<LuaModule>>();
|
_luaModules = _kernel.Get<List<LuaModule>>();
|
||||||
ProfileModule = (LuaProfileModule)_luaModules.First(m => m.ModuleName == "Profile");
|
ProfileModule = (LuaProfileModule) _luaModules.First(m => m.ModuleName == "Profile");
|
||||||
EventsModule = (LuaEventsModule)_luaModules.First(m => m.ModuleName == "Events");
|
EventsModule = (LuaEventsModule) _luaModules.First(m => m.ModuleName == "Events");
|
||||||
|
|
||||||
// Setup new state
|
// Setup new state
|
||||||
_luaScript.Options.DebugPrint = LuaPrint;
|
_luaScript.Options.DebugPrint = LuaPrint;
|
||||||
|
|
||||||
// Insert each module into the script's globals
|
// Insert each module into the script's globals
|
||||||
foreach (var luaModule in _luaModules)
|
foreach (var luaModule in _luaModules)
|
||||||
{
|
|
||||||
_luaScript.Globals[luaModule.ModuleName] = luaModule;
|
_luaScript.Globals[luaModule.ModuleName] = luaModule;
|
||||||
}
|
|
||||||
|
|
||||||
// If there is no LUA script, don't bother executing the string
|
// If there is no LUA script, don't bother executing the string
|
||||||
if (ProfileModel.LuaScript.IsNullOrEmpty())
|
if (ProfileModel.LuaScript.IsNullOrEmpty())
|
||||||
@ -74,6 +72,7 @@ namespace Artemis.Managers
|
|||||||
{
|
{
|
||||||
lock (_luaScript)
|
lock (_luaScript)
|
||||||
{
|
{
|
||||||
|
UpdateLuaSource(ProfileModel);
|
||||||
_luaScript.DoString(ProfileModel.LuaScript);
|
_luaScript.DoString(ProfileModel.LuaScript);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -116,7 +115,6 @@ namespace Artemis.Managers
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (EventsModule != null)
|
if (EventsModule != null)
|
||||||
{
|
|
||||||
lock (EventsModule.InvokeLock)
|
lock (EventsModule.InvokeLock)
|
||||||
{
|
{
|
||||||
lock (_luaScript)
|
lock (_luaScript)
|
||||||
@ -124,14 +122,52 @@ namespace Artemis.Managers
|
|||||||
_luaScript.DoString("");
|
_luaScript.DoString("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
lock (_luaScript)
|
lock (_luaScript)
|
||||||
{
|
{
|
||||||
_luaScript.DoString("");
|
_luaScript.DoString("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Safely call a function on the active script
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="function"></param>
|
||||||
|
/// <param name="args"></param>
|
||||||
|
public void Call(DynValue function, DynValue[] args = null)
|
||||||
|
{
|
||||||
|
if (EventsModule == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
lock (EventsModule.InvokeLock)
|
||||||
|
{
|
||||||
|
lock (_luaScript)
|
||||||
|
{
|
||||||
|
if (args != null)
|
||||||
|
_luaScript.Call(function, args);
|
||||||
|
else
|
||||||
|
_luaScript.Call(function);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (ArgumentException e)
|
||||||
|
{
|
||||||
|
_logger.Error("[{0}-LUA]: Error: {1}", ProfileModel.Name, e.Message);
|
||||||
|
}
|
||||||
|
catch (InternalErrorException e)
|
||||||
|
{
|
||||||
|
_logger.Error("[{0}-LUA]: Error: {1}", ProfileModel.Name, e.DecoratedMessage);
|
||||||
|
}
|
||||||
|
catch (SyntaxErrorException e)
|
||||||
|
{
|
||||||
|
_logger.Error("[{0}-LUA]: Error: {1}", ProfileModel.Name, e.DecoratedMessage);
|
||||||
|
}
|
||||||
|
catch (ScriptRuntimeException e)
|
||||||
|
{
|
||||||
|
_logger.Error("[{0}-LUA]: Error: {1}", ProfileModel.Name, e.DecoratedMessage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Private lua functions
|
#region Private lua functions
|
||||||
@ -143,6 +179,20 @@ namespace Artemis.Managers
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Updates a profile's LUA script to be compatible with the latest version of Artemis, if needed.
|
||||||
|
/// This function obviously won't fix completely custom profiles but it'll fix copied LUA.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="profileModel"></param>
|
||||||
|
private static void UpdateLuaSource(ProfileModel profileModel)
|
||||||
|
{
|
||||||
|
// 1.7.1.0 - Events cleanup
|
||||||
|
profileModel.LuaScript = profileModel.LuaScript.Replace("function updateHandler(profile, eventArgs)",
|
||||||
|
"function updateHandler(eventArgs)");
|
||||||
|
profileModel.LuaScript = profileModel.LuaScript.Replace("function drawHandler(profile, eventArgs)",
|
||||||
|
"function drawHandler(eventArgs)");
|
||||||
|
}
|
||||||
|
|
||||||
#region Editor
|
#region Editor
|
||||||
|
|
||||||
public void OpenEditor()
|
public void OpenEditor()
|
||||||
|
|||||||
43
Artemis/Artemis/Profiles/Lua/Modules/LuaTimerModule.cs
Normal file
43
Artemis/Artemis/Profiles/Lua/Modules/LuaTimerModule.cs
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using Artemis.Managers;
|
||||||
|
using Artemis.Profiles.Lua.Modules.Timer;
|
||||||
|
using MoonSharp.Interpreter;
|
||||||
|
|
||||||
|
namespace Artemis.Profiles.Lua.Modules
|
||||||
|
{
|
||||||
|
[MoonSharpUserData]
|
||||||
|
public class LuaTimerModule : LuaModule
|
||||||
|
{
|
||||||
|
private readonly List<LuaTimer> _timers;
|
||||||
|
|
||||||
|
public LuaTimerModule(LuaManager luaManager) : base(luaManager)
|
||||||
|
{
|
||||||
|
_timers = new List<LuaTimer>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ModuleName => "Timer";
|
||||||
|
|
||||||
|
public override void Dispose()
|
||||||
|
{
|
||||||
|
foreach (var luaTimer in _timers)
|
||||||
|
luaTimer.Dispose();
|
||||||
|
|
||||||
|
_timers.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public LuaTimer SetTimer(DynValue function, int interval, int timesToExecute, params DynValue[] args)
|
||||||
|
{
|
||||||
|
var luaTimer = new LuaTimer(this, function, interval, timesToExecute, args);
|
||||||
|
_timers.Add(luaTimer);
|
||||||
|
return luaTimer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveTimer(LuaTimer luaTimer)
|
||||||
|
{
|
||||||
|
if (_timers.Contains(luaTimer))
|
||||||
|
_timers.Remove(luaTimer);
|
||||||
|
|
||||||
|
luaTimer.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
59
Artemis/Artemis/Profiles/Lua/Modules/Timer/LuaTimer.cs
Normal file
59
Artemis/Artemis/Profiles/Lua/Modules/Timer/LuaTimer.cs
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
using System;
|
||||||
|
using System.Timers;
|
||||||
|
using MoonSharp.Interpreter;
|
||||||
|
|
||||||
|
namespace Artemis.Profiles.Lua.Modules.Timer
|
||||||
|
{
|
||||||
|
[MoonSharpUserData]
|
||||||
|
public class LuaTimer : IDisposable
|
||||||
|
{
|
||||||
|
private readonly DynValue[] _args;
|
||||||
|
private readonly DynValue _function;
|
||||||
|
private readonly System.Timers.Timer _timer;
|
||||||
|
private readonly LuaTimerModule _timerModule;
|
||||||
|
private readonly int _timesToExecute;
|
||||||
|
private int _timesExecuted;
|
||||||
|
|
||||||
|
public LuaTimer(LuaTimerModule timerModule, DynValue function, int interval, int timesToExecute,
|
||||||
|
params DynValue[] args)
|
||||||
|
{
|
||||||
|
_timerModule = timerModule;
|
||||||
|
_function = function;
|
||||||
|
_timesToExecute = timesToExecute;
|
||||||
|
_args = args;
|
||||||
|
_timesExecuted = 0;
|
||||||
|
|
||||||
|
// Setup timer
|
||||||
|
_timer = new System.Timers.Timer(interval);
|
||||||
|
_timer.Elapsed += TimerOnElapsed;
|
||||||
|
_timer.Start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_timer?.Stop();
|
||||||
|
_timer?.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Stop()
|
||||||
|
{
|
||||||
|
_timerModule.RemoveTimer(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void TimerOnElapsed(object sender, ElapsedEventArgs e)
|
||||||
|
{
|
||||||
|
if (_args != null)
|
||||||
|
_timerModule.LuaManager.Call(_function, _args);
|
||||||
|
else
|
||||||
|
_timerModule.LuaManager.Call(_function);
|
||||||
|
|
||||||
|
// Don't keep track of execution if times is set to 0 (infinite)
|
||||||
|
if (_timesToExecute <= 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
_timesExecuted++;
|
||||||
|
if (_timesExecuted >= _timesToExecute)
|
||||||
|
_timerModule.RemoveTimer(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -14,7 +14,7 @@
|
|||||||
-- changes are applied to the profile and the script is restarted.
|
-- changes are applied to the profile and the script is restarted.
|
||||||
|
|
||||||
-- This event is raised after every profile update, before drawing.
|
-- This event is raised after every profile update, before drawing.
|
||||||
function updateHandler(profile, eventArgs)
|
function updateHandler(eventArgs)
|
||||||
-- In this example we only want to update once per frame when the keyboard is
|
-- In this example we only want to update once per frame when the keyboard is
|
||||||
-- updated. If you don't do this the updateHandler will trigger on every
|
-- updated. If you don't do this the updateHandler will trigger on every
|
||||||
-- device's update.
|
-- device's update.
|
||||||
@ -26,7 +26,7 @@ function updateHandler(profile, eventArgs)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- This event is raised after every profile draw, after updating.
|
-- This event is raised after every profile draw, after updating.
|
||||||
function drawHandler(profile, eventArgs)
|
function drawHandler(eventArgs)
|
||||||
-- In this example we only want to draw to the keyboard. Each device has it's
|
-- In this example we only want to draw to the keyboard. Each device has it's
|
||||||
-- own drawing event
|
-- own drawing event
|
||||||
if eventArgs.DeviceType != "keyboard" then
|
if eventArgs.DeviceType != "keyboard" then
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user