|
| 1 | +using SomethingNeedDoing.Core.Interfaces; |
| 2 | +using System.Text.RegularExpressions; |
| 3 | + |
| 4 | +namespace SomethingNeedDoing.Managers; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Manages cleanup functions for macros when they stop execution. |
| 8 | +/// </summary> |
| 9 | +public class CleanupManager : IDisposable |
| 10 | +{ |
| 11 | + private readonly Dictionary<string, List<string>> _cleanupFunctionsByMacroId = []; |
| 12 | + private readonly Dictionary<string, IMacro> _macrosById = []; |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Event raised when a cleanup function should be executed. |
| 16 | + /// </summary> |
| 17 | + public event EventHandler<CleanupFunctionEventArgs>? CleanupFunctionRequested; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Registers cleanup functions for a macro. |
| 21 | + /// </summary> |
| 22 | + public void RegisterCleanupFunctions(IMacro macro) |
| 23 | + { |
| 24 | + if (macro is TemporaryMacro) return; |
| 25 | + |
| 26 | + var cleanupFunctions = new List<string>(); |
| 27 | + |
| 28 | + if (macro.Type == MacroType.Lua) |
| 29 | + { |
| 30 | + var patterns = new[] |
| 31 | + { |
| 32 | + @"function\s+(OnCleanup|OnDispose|OnStop|OnEnd)\s*\(", |
| 33 | + @"function\s+(Cleanup|Dispose|Stop|End)\s*\(" |
| 34 | + }; |
| 35 | + |
| 36 | + foreach (var pattern in patterns) |
| 37 | + { |
| 38 | + var matches = Regex.Matches(macro.Content, pattern, RegexOptions.IgnoreCase); |
| 39 | + foreach (Match match in matches) |
| 40 | + { |
| 41 | + var functionName = match.Groups[1].Value; |
| 42 | + cleanupFunctions.Add(functionName); |
| 43 | + Svc.Log.Debug($"[{nameof(CleanupManager)}] Found cleanup function {functionName} in macro {macro.Name}"); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if (cleanupFunctions.Count > 0) |
| 49 | + { |
| 50 | + _cleanupFunctionsByMacroId[macro.Id] = cleanupFunctions; |
| 51 | + _macrosById[macro.Id] = macro; |
| 52 | + Svc.Log.Debug($"[{nameof(CleanupManager)}] Registered {cleanupFunctions.Count} cleanup functions for macro {macro.Name}"); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Unregisters cleanup functions for a macro. |
| 58 | + /// </summary> |
| 59 | + public void UnregisterCleanupFunctions(IMacro macro) |
| 60 | + { |
| 61 | + if (macro is TemporaryMacro) return; |
| 62 | + |
| 63 | + if (_cleanupFunctionsByMacroId.Remove(macro.Id)) |
| 64 | + { |
| 65 | + _macrosById.Remove(macro.Id); |
| 66 | + Svc.Log.Debug($"[{nameof(CleanupManager)}] Unregistered cleanup functions for macro {macro.Name}"); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Executes cleanup functions for a macro. |
| 72 | + /// </summary> |
| 73 | + public void ExecuteCleanup(string macroId, string reason = "Stopped") |
| 74 | + { |
| 75 | + if (!_cleanupFunctionsByMacroId.TryGetValue(macroId, out var cleanupFunctions) || !_macrosById.TryGetValue(macroId, out var macro)) |
| 76 | + return; |
| 77 | + |
| 78 | + Svc.Log.Info($"[{nameof(CleanupManager)}] Executing {cleanupFunctions.Count} cleanup functions for macro {macro.Name} (reason: {reason})"); |
| 79 | + |
| 80 | + foreach (var functionName in cleanupFunctions) |
| 81 | + { |
| 82 | + try |
| 83 | + { |
| 84 | + string functionContent; |
| 85 | + if (macro.Type == MacroType.Lua) |
| 86 | + { |
| 87 | + var match = Regex.Match(macro.Content, $@"function\s+{functionName}\s*\([^)]*\)\s*\n(.*?)\n\s*end", RegexOptions.Singleline | RegexOptions.IgnoreCase); |
| 88 | + if (!match.Success) |
| 89 | + { |
| 90 | + Svc.Log.Warning($"[{nameof(CleanupManager)}] Could not find function {functionName} in macro {macro.Name}"); |
| 91 | + continue; |
| 92 | + } |
| 93 | + functionContent = match.Groups[1].Value.Trim(); |
| 94 | + } |
| 95 | + else |
| 96 | + throw new NotSupportedException($"Cleanup functions are not supported for macro type {macro.Type}"); |
| 97 | + |
| 98 | + var tempMacroId = $"{macro.Id}_cleanup_{functionName}_{Guid.NewGuid()}"; |
| 99 | + var tempMacro = new TemporaryMacro(functionContent, tempMacroId) |
| 100 | + { |
| 101 | + Name = $"{macro.Name} - {functionName} (Cleanup)", |
| 102 | + Type = macro.Type |
| 103 | + }; |
| 104 | + |
| 105 | + Svc.Log.Debug($"[{nameof(CleanupManager)}] Created cleanup temporary macro {tempMacro.Id} for function {functionName}"); |
| 106 | + CleanupFunctionRequested?.Invoke(this, new CleanupFunctionEventArgs(tempMacro, functionName, reason)); |
| 107 | + } |
| 108 | + catch (Exception ex) |
| 109 | + { |
| 110 | + Svc.Log.Error(ex, $"[{nameof(CleanupManager)}] Error executing cleanup function {functionName} for macro {macro.Name}"); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Gets all registered cleanup functions for a macro. |
| 117 | + /// </summary> |
| 118 | + public IReadOnlyList<string> GetCleanupFunctions(string macroId) |
| 119 | + => _cleanupFunctionsByMacroId.TryGetValue(macroId, out var functions) ? functions.AsReadOnly() : Array.Empty<string>(); |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Checks if a macro has any registered cleanup functions. |
| 123 | + /// </summary> |
| 124 | + public bool HasCleanupFunctions(string macroId) => _cleanupFunctionsByMacroId.ContainsKey(macroId); |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Disposes of the cleanup manager. |
| 128 | + /// </summary> |
| 129 | + public void Dispose() |
| 130 | + { |
| 131 | + _cleanupFunctionsByMacroId.Clear(); |
| 132 | + _macrosById.Clear(); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/// <summary> |
| 137 | +/// Event arguments for cleanup function execution. |
| 138 | +/// </summary> |
| 139 | +/// <param name="tempMacro">The temporary macro containing the cleanup function.</param> |
| 140 | +/// <param name="functionName">The name of the cleanup function.</param> |
| 141 | +/// <param name="reason">The reason for cleanup execution.</param> |
| 142 | +public class CleanupFunctionEventArgs(IMacro tempMacro, string functionName, string reason) : EventArgs |
| 143 | +{ |
| 144 | + /// <summary> |
| 145 | + /// Gets the temporary macro containing the cleanup function. |
| 146 | + /// </summary> |
| 147 | + public IMacro TempMacro { get; } = tempMacro; |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// Gets the name of the cleanup function. |
| 151 | + /// </summary> |
| 152 | + public string FunctionName { get; } = functionName; |
| 153 | + |
| 154 | + /// <summary> |
| 155 | + /// Gets the reason for cleanup execution. |
| 156 | + /// </summary> |
| 157 | + public string Reason { get; } = reason; |
| 158 | +} |
0 commit comments