From 9905accbf442e11a6506e625cddb75046ce55ebd Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Mon, 7 Oct 2024 05:44:41 -0700 Subject: [PATCH] chore: allow hooking existing game server process --- Assets/Rivet/Editor/Task.cs | 1 - Assets/Rivet/Editor/TaskManager.cs | 28 +++++++++++++++---- Assets/Rivet/Editor/UI/Dock/Dock.cs | 10 +++++++ .../Editor/UI/Dock/Tabs/DevelopController.cs | 2 +- .../Editor/UI/Dock/Tabs/SettingsController.cs | 2 +- .../Editor/UI/TaskPanel/TaskPanelWindow.cs | 5 +++- 6 files changed, 38 insertions(+), 10 deletions(-) diff --git a/Assets/Rivet/Editor/Task.cs b/Assets/Rivet/Editor/Task.cs index cdd756c..22290b2 100644 --- a/Assets/Rivet/Editor/Task.cs +++ b/Assets/Rivet/Editor/Task.cs @@ -84,7 +84,6 @@ private void OnOutputEvent(JObject eventObj) private void OnLogEvent(string log) { - RivetLogger.Log($"TaskLog is null: {TaskLog == null}"); TaskLog?.Invoke(log, LogType.STDOUT); } diff --git a/Assets/Rivet/Editor/TaskManager.cs b/Assets/Rivet/Editor/TaskManager.cs index ccbd675..92e7413 100644 --- a/Assets/Rivet/Editor/TaskManager.cs +++ b/Assets/Rivet/Editor/TaskManager.cs @@ -38,12 +38,14 @@ public LogEntry(string message, LogType type) } public delegate Task GetStartConfigDelegate(); + public delegate Task GetHookConfigDelegate(); public delegate Task GetStopConfigDelegate(); public delegate TaskPanelWindow? GetTaskPanelDelegate(); public delegate void StateChangeHandler(bool running); // Config private GetStartConfigDelegate _getStartConfig; + private GetHookConfigDelegate? _getHookConfig; private GetStopConfigDelegate _getStopConfig; private GetTaskPanelDelegate _getTaskPanel; private bool _autoRestart = false; @@ -57,9 +59,16 @@ public LogEntry(string message, LogType type) private RivetTask? _stopTask; public List LogEntries = new(); - public TaskManager(string initMessage, GetStartConfigDelegate getStartConfig, GetStopConfigDelegate getStopConfig, GetTaskPanelDelegate getTaskPanel, bool autoRestart = false) + public bool IsRunning { + get { + return _task != null || _stopTask != null; + } + } + + public TaskManager(string initMessage, GetStartConfigDelegate getStartConfig, GetHookConfigDelegate? getHookConfig, GetStopConfigDelegate getStopConfig, GetTaskPanelDelegate getTaskPanel, bool autoRestart = false) { _getStartConfig = getStartConfig; + _getHookConfig = getHookConfig; _getStopConfig = getStopConfig; _getTaskPanel = getTaskPanel; _autoRestart = autoRestart; @@ -67,7 +76,7 @@ public TaskManager(string initMessage, GetStartConfigDelegate getStartConfig, Ge LogEntries.Add(new LogEntry(initMessage, LogType.META)); } - public async Task StartTask() + public async Task StartTask(bool hook = false) { lock (_taskLock) { @@ -77,10 +86,12 @@ public async Task StartTask() } // Kill old task - _ = StopTask(); + if (!hook) { + _ = StopTask(); + } // Start new task - var config = await _getStartConfig(); + var config = hook ? await _getHookConfig() : await _getStartConfig(); if (config == null) { RivetLogger.Log("No task config provided."); @@ -90,14 +101,17 @@ public async Task StartTask() { _task = new RivetTask(config.Value.Name, config.Value.Input); _task.TaskLog += OnTaskLog; + RivetLogger.Log($"Running {config.Value.Name}"); OnStateChange(); + RivetLogger.Log($"After state change {config.Value.Name}"); } - AddLogLine("Start", LogType.META); + AddLogLine(hook ? "Hook" : "Start", LogType.META); // Run task var output = await _task.RunAsync(); + RivetLogger.Log($"Finished {config.Value.Name}"); await OnTaskOutput(output, _task); } @@ -236,7 +250,9 @@ private void OnStateChange() // event right after the task is started (where IsRunning is set to // true). To fix this, we need to be able to hook in to state change // events on RivetTask. - StateChange.Invoke(_task != null); + var isRunning = IsRunning; + EditorApplication.delayCall += () => StateChange.Invoke(isRunning); + RivetLogger.Log($"State change: {IsRunning} ({_task != null} {_stopTask != null})"); } } } \ No newline at end of file diff --git a/Assets/Rivet/Editor/UI/Dock/Dock.cs b/Assets/Rivet/Editor/UI/Dock/Dock.cs index fe2d905..fad39bf 100644 --- a/Assets/Rivet/Editor/UI/Dock/Dock.cs +++ b/Assets/Rivet/Editor/UI/Dock/Dock.cs @@ -145,6 +145,14 @@ public void OnEnable() } }); }, + getHookConfig: () => + { + return Task.FromResult(new TaskConfig + { + Name = "game_server.hook", + Input = new JObject {} + }); + }, getStopConfig: () => { return Task.FromResult(new TaskConfig @@ -169,6 +177,7 @@ public void OnEnable() } }); }, + getHookConfig: null, getStopConfig: () => { return Task.FromResult(new TaskConfig @@ -196,6 +205,7 @@ public void OnEnable() // Auto-start backend _ = BackendManager.StartTask(); + _ = LocalGameServerManager.StartTask(hook: true); } public void OnDisable() diff --git a/Assets/Rivet/Editor/UI/Dock/Tabs/DevelopController.cs b/Assets/Rivet/Editor/UI/Dock/Tabs/DevelopController.cs index 1aa5c11..834e0d2 100644 --- a/Assets/Rivet/Editor/UI/Dock/Tabs/DevelopController.cs +++ b/Assets/Rivet/Editor/UI/Dock/Tabs/DevelopController.cs @@ -73,7 +73,7 @@ void InitUI() // Callbacks _dock.LocalGameServerManager.StateChange += OnLocalGameServerStateChange; - OnLocalGameServerStateChange(false); + OnLocalGameServerStateChange(_dock.LocalGameServerManager.IsRunning); _refreshButton.RegisterCallback(ev => { _ = plugin.Bootstrap(); }); diff --git a/Assets/Rivet/Editor/UI/Dock/Tabs/SettingsController.cs b/Assets/Rivet/Editor/UI/Dock/Tabs/SettingsController.cs index 4f03e3a..83aed51 100644 --- a/Assets/Rivet/Editor/UI/Dock/Tabs/SettingsController.cs +++ b/Assets/Rivet/Editor/UI/Dock/Tabs/SettingsController.cs @@ -72,7 +72,7 @@ void InitUI() // Callbacks _dock.BackendManager.StateChange += OnBackendStateChange; - OnBackendStateChange(false); + OnBackendStateChange(_dock.BackendManager.IsRunning); _signInButton.RegisterCallback(ev => { _ = _dock.StartSignIn(); }); _signOutButton.RegisterCallback(ev => { _ = _dock.SignOut(); }); diff --git a/Assets/Rivet/Editor/UI/TaskPanel/TaskPanelWindow.cs b/Assets/Rivet/Editor/UI/TaskPanel/TaskPanelWindow.cs index f95eedb..4aec766 100644 --- a/Assets/Rivet/Editor/UI/TaskPanel/TaskPanelWindow.cs +++ b/Assets/Rivet/Editor/UI/TaskPanel/TaskPanelWindow.cs @@ -56,7 +56,10 @@ public void CreateGUI() public void UpdateLogs() { // In case the singleton doesn't exist yet - _logListView.itemsSource = TaskManager?.LogEntries; + if (TaskManager != null) + { + _logListView.itemsSource = TaskManager.LogEntries; + } // Rebuild view _logListView.Rebuild();