123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- using System;
- using System.Linq;
- using UnityEditor;
- using UnityEngine;
- using LLM.Editor.Data;
- using System.Reflection;
- using LLM.Editor.Commands;
- using System.Collections.Generic;
- namespace LLM.Editor.Core
- {
- /// <summary>
- /// Responsible for finding and executing commands from the session queue.
- /// </summary>
- [InitializeOnLoad]
- public static class CommandExecutor
- {
- private static List<CommandData> _commandQueue;
- private static CommandContext _currentContext;
- public static Action OnQueueUpdated;
- static CommandExecutor()
- {
- // This will run when the editor loads, including after a recompile.
- EditorApplication.delayCall += Initialize;
- }
- private static void Initialize()
- {
- Debug.Log("[CommandExecutor] Initializing...");
- if (SessionManager.HasActiveSession())
- {
- _commandQueue = SessionManager.LoadCommandQueue();
- if (!_commandQueue.Any()) return;
- Debug.Log($"[CommandExecutor] Resuming session with {_commandQueue.Count} commands in queue.");
- _currentContext = new CommandContext();
- OnQueueUpdated?.Invoke();
- }
- else
- {
- _commandQueue = new List<CommandData>();
- }
- }
- public static void SetQueue(List<CommandData> commands)
- {
- _commandQueue = commands;
- _currentContext = new CommandContext();
- SessionManager.SaveCommandQueue(_commandQueue);
- OnQueueUpdated?.Invoke();
- }
- public static bool HasPendingCommands() => _commandQueue != null && _commandQueue.Any();
-
- public static CommandData GetNextCommand() => HasPendingCommands() ? _commandQueue.First() : null;
- public static void ExecuteNextCommand()
- {
- if (!HasPendingCommands())
- {
- Debug.LogWarning("[CommandExecutor] No commands to execute.");
- return;
- }
- var commandData = _commandQueue.First();
- _commandQueue.RemoveAt(0);
- try
- {
- var commandInstance = CreateCommandInstance(commandData);
- if (commandInstance != null)
- {
- Debug.Log($"[CommandExecutor] Executing: {commandData.commandName}");
- commandInstance.Execute(_currentContext);
- }
- else
- {
- Debug.LogError($"[CommandExecutor] Could not create instance for command: {commandData.commandName}");
- }
- }
- catch(Exception e)
- {
- Debug.LogError($"[CommandExecutor] Failed to execute command '{commandData.commandName}'. Error: {e.Message}");
- }
-
- // Save the modified queue
- SessionManager.SaveCommandQueue(_commandQueue);
- OnQueueUpdated?.Invoke();
- }
- private static ICommand CreateCommandInstance(CommandData data)
- {
- // Use reflection to find the command class in the Commands namespace
- // This makes the system extensible without needing a giant switch statement.
- var commandClassName = $"{data.commandName}Command";
- var type = Assembly.GetExecutingAssembly().GetTypes()
- .FirstOrDefault(t => t.Namespace == "LLM.Editor.Commands" && t.Name == commandClassName);
- if (type != null)
- {
- // Assumes commands have a constructor that takes a single string (the JSON parameters)
- return (ICommand)Activator.CreateInstance(type, data.jsonData);
- }
- Debug.LogError($"[CommandExecutor] Command type '{commandClassName}' not found.");
- return null;
- }
- }
- }
|