44using Dalamud . Game . Text ;
55using Dalamud . Game . Text . SeStringHandling ;
66using Dalamud . Plugin . Services ;
7+ using NLua ;
78using SomethingNeedDoing . Core . Events ;
89using SomethingNeedDoing . Core . Interfaces ;
910using SomethingNeedDoing . LuaMacro ;
1011using SomethingNeedDoing . LuaMacro . Wrappers ;
1112using SomethingNeedDoing . NativeMacro ;
12- using System . Collections . Concurrent ;
1313using System . Text . RegularExpressions ;
1414using System . Threading ;
1515using System . Threading . Tasks ;
@@ -20,8 +20,8 @@ namespace SomethingNeedDoing.Managers;
2020/// </summary>
2121public class MacroScheduler : IMacroScheduler , IDisposable
2222{
23- private readonly ConcurrentDictionary < string , IMacroEngine > _enginesByMacroId = [ ] ;
24- private readonly ConcurrentDictionary < string , MacroExecutionState > _macroStates = [ ] ;
23+ private readonly Dictionary < string , MacroExecutionState > _macroStates = [ ] ;
24+ private readonly Dictionary < string , IMacroEngine > _enginesByMacroId = [ ] ;
2525 private readonly Dictionary < string , AutoRetainerApi > _arApis = [ ] ;
2626 private readonly Dictionary < string , AddonEventConfig > _addonEvents = [ ] ;
2727 private readonly MacroHierarchyManager _macroHierarchy = new ( ) ;
@@ -54,6 +54,7 @@ public MacroScheduler(NativeMacroEngine nativeEngine, NLuaMacroEngine luaEngine,
5454 _nativeEngine . MacroError += OnEngineError ;
5555 _luaEngine . MacroError += OnEngineError ;
5656 _triggerEventManager . TriggerEventOccurred += OnTriggerEventOccurred ;
57+ _triggerEventManager . FunctionExecutionRequested += OnFunctionExecutionRequested ;
5758
5859 _nativeEngine . MacroControlRequested += OnMacroControlRequested ;
5960 _luaEngine . MacroControlRequested += OnMacroControlRequested ;
@@ -355,7 +356,7 @@ public async void StopMacro(string macroId)
355356 /// <param name="macroId">The ID of the macro to clean up.</param>
356357 public void CleanupMacro ( string macroId )
357358 {
358- if ( _macroStates . TryRemove ( macroId , out var state ) )
359+ if ( _macroStates . Remove ( macroId , out var state ) )
359360 {
360361 if ( state . Macro is ConfigMacro configMacro )
361362 _triggerEventManager . UnregisterAllTriggers ( configMacro ) ;
@@ -366,7 +367,7 @@ public void CleanupMacro(string macroId)
366367 state . Macro . StateChanged -= OnMacroStateChanged ;
367368 }
368369
369- _enginesByMacroId . TryRemove ( macroId , out _ ) ;
370+ _enginesByMacroId . Remove ( macroId ) ;
370371 }
371372
372373 /// <inheritdoc/>
@@ -551,15 +552,15 @@ private void OnMacroStateChanged(object? sender, MacroStateChangedEventArgs e)
551552 tempMacro . StateChanged -= OnMacroStateChanged ;
552553 }
553554
554- if ( _macroStates . TryRemove ( e . MacroId , out var state ) )
555+ if ( _macroStates . Remove ( e . MacroId , out var state ) )
555556 {
556557 UnregisterFunctionTriggers ( state . Macro ) ;
557558 state . CancellationSource . Cancel ( ) ;
558559 state . CancellationSource . Dispose ( ) ;
559560 state . Macro . StateChanged -= OnMacroStateChanged ;
560561 }
561562
562- _enginesByMacroId . TryRemove ( e . MacroId , out _ ) ;
563+ _enginesByMacroId . Remove ( e . MacroId ) ;
563564 }
564565 }
565566
@@ -583,19 +584,39 @@ private void OnTriggerEventOccurred(object? sender, TriggerEventArgs e)
583584
584585 _ = StartMacro ( macro , e ) ;
585586 }
586- else
587- Svc . Log . Warning ( $ "[{ nameof ( MacroScheduler ) } ] Could not find parent macro { parts [ 0 ] } for temporary macro { macro . Id } ") ;
588587 }
589588 else
590589 {
591- // don't let an infinte loop of it starting itself happen
592- if ( macro . State == MacroState . Running )
590+ _ = StartMacro ( macro , e ) ;
591+ }
592+ }
593+ }
594+
595+ /// <summary>
596+ /// Handles function execution requests from trigger events.
597+ /// </summary>
598+ /// <param name="sender">The sender of the event.</param>
599+ /// <param name="e">The function execution request event arguments.</param>
600+ private void OnFunctionExecutionRequested ( object ? sender , FunctionExecutionRequestedEventArgs e )
601+ {
602+ try
603+ {
604+ if ( GetEngineForMacro ( e . MacroId ) is NLuaMacroEngine nluaEngine && nluaEngine . GetLuaEnvironment ( e . MacroId ) is Lua lua )
605+ {
606+ if ( C . GetMacro ( e . MacroId ) is { State : MacroState . Running } macro )
593607 {
594- Svc . Log . Verbose ( $ "[ { nameof ( MacroScheduler ) } ] Macro { macro . Id } is already running, skipping trigger ") ;
595- return ;
608+ Svc . Log . Verbose ( $ "Executing function { e . FunctionName } in macro { macro . Name } ") ;
609+ lua . DoString ( $ " { e . FunctionName } ()" ) ; // call in the parent's lua state
596610 }
597- _ = StartMacro ( macro , e ) ;
611+ else
612+ Svc . Log . Debug ( $ "Skipping function { e . FunctionName } for stopped macro { e . MacroId } ") ;
598613 }
614+ else
615+ Svc . Log . Warning ( $ "Could not find active Lua environment for macro { e . MacroId } ") ;
616+ }
617+ catch ( Exception ex )
618+ {
619+ Svc . Log . Error ( $ "Error executing function { e . FunctionName } for macro { e . MacroId } : { ex } ") ;
599620 }
600621 }
601622
@@ -795,4 +816,7 @@ public void Dispose()
795816
796817 /// <inheritdoc/>
797818 public void ExecuteCleanup ( string macroId , string reason = "Manual" ) => _cleanupManager . ExecuteCleanup ( macroId , reason ) ;
819+
820+ /// <inheritdoc/>
821+ public IMacroEngine ? GetEngineForMacro ( string macroId ) => _enginesByMacroId . TryGetValue ( macroId , out var engine ) ? engine : null ;
798822}
0 commit comments