Skip to content

Commit

Permalink
chore: add game server & backend panel (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Aug 13, 2024
1 parent 77e6324 commit 865b967
Show file tree
Hide file tree
Showing 22 changed files with 533 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .opengb/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Do not edit this file directly.
//
// Generated at 2024-07-29T07:22:45.889Z
// Generated at 2024-07-30T15:15:37.842Z
import { Runtime } from "/Users/nathan/rivet/plugin-unity/.opengb/runtime/packages/runtime/src/mod.ts";
import { dependencyCaseConversionMap } from "/Users/nathan/rivet/plugin-unity/.opengb/dependencyCaseConversion.ts";
import { actorCaseConversionMap } from "/Users/nathan/rivet/plugin-unity/.opengb/actorCaseConversion.ts";
Expand Down
3 changes: 2 additions & 1 deletion .opengb/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
}
},
"rivet": {}
}
},
"runtime": {}
},
"registries": {
"default": {
Expand Down
2 changes: 1 addition & 1 deletion .opengb/runtime_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Do not edit this file directly.
//
// Generated at 2024-07-29T07:22:45.889Z
// Generated at 2024-07-30T15:15:37.842Z
import {
BuildRuntime,
Config,
Expand Down
185 changes: 185 additions & 0 deletions Assets/Rivet/Editor/TaskManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Codice.Client.BaseCommands;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Rivet.Editor.UI.TaskPanel;
using UnityEditor;

namespace Rivet.Editor
{
public struct TaskConfig
{
public string Name;
public JObject Input;
}


/// <summary>
/// Persistent task that can be stopped and restarted with logs.
/// </summary>
public class TaskManager
{
const int MAX_LOG_LEN = 10_000;

public enum LogType { META, STDOUT, STDERR }

public struct LogEntry
{
public string Message;
public LogType Type;

public LogEntry(string message, LogType type)
{
Message = message;
Type = type;
}
}

public delegate Task<TaskConfig?> GetTaskConfigDelegate();
public delegate TaskPanelWindow? GetTaskPanelDelegate();
public delegate void StateChangeHandler(bool running);

// Config
private GetTaskConfigDelegate _getTaskConfig;
private GetTaskPanelDelegate _getTaskPanel;
private bool _autoRestart = false;
private bool _taskStopping = false;

// Events
public event StateChangeHandler StateChange;

// State
private readonly object _taskLock = new();
private RivetTask _task;
public List<LogEntry> LogEntries = new();

public TaskManager(string initMessage, GetTaskConfigDelegate getTaskConfig, GetTaskPanelDelegate getTaskPanel, bool autoRestart = false)
{
_getTaskConfig = getTaskConfig;
_getTaskPanel = getTaskPanel;
_autoRestart = autoRestart;

LogEntries.Add(new LogEntry(initMessage, LogType.META));
}

public async Task StartTask(bool restart = true)
{
lock (_taskLock)
{
// Do nothing if already stopping another task
if (_taskStopping)
return;

// Do nothing if task already running
if (!restart && _task != null)
return;
}

// Kill old task
StopTask();

// Start task
var config = await _getTaskConfig();
if (config == null)
{
RivetLogger.Log("No task config provided.");
}
lock (_taskLock)
{
_task = new RivetTask(config.Value.Name, config.Value.Input);
_taskStopping = false;
_task.OnLog += OnTaskLog;
OnStateChange();
}

AddLogLine("Start", LogType.META);

// Run task
var output = await _task.RunAsync();
lock (_taskLock)
{
_task = null;
OnStateChange();
}

// Log output
switch (output)
{
case ResultOk<JObject> ok:
AddLogLine($"Exited with {ok.Data.ToString(Formatting.None)}", LogType.META);
break;
case ResultErr<JObject> err:
AddLogLine($"Task error: {err.Message}", LogType.META);
break;
}

// TODO: Re-enable task restarting
// // Restart if task was not stopped
// bool shouldRestart;
// lock (_taskLock) shouldRestart = _autoRestart && _task == null && !_taskStopping;
// if (shouldRestart)
// {
// AddLogLine("Restarting in 2 seconds", LogType.META);
// await Task.Delay(2000);
// await StartTask();
// }
}

public void StopTask()
{
lock (_taskLock)
{
if (_task != null)
{
_taskStopping = true;
_task.Kill();
_task = null;

AddLogLine("Stop", LogType.META);

OnStateChange();
}
}
}

private void OnTaskLog(string message, RivetTask.LogType type)
{
LogType logType = type switch
{
RivetTask.LogType.STDOUT => LogType.STDOUT,
RivetTask.LogType.STDERR => LogType.STDERR,
_ => LogType.META
};
AddLogLine(message, logType);
}

public void AddLogLine(string message, LogType type)
{
LogEntries.Add(new LogEntry(message, type));
if (LogEntries.Count > MAX_LOG_LEN)
{
LogEntries.RemoveRange(0, LogEntries.Count - MAX_LOG_LEN);
}

// This might not be called on the main thread
EditorApplication.delayCall += () => _getTaskPanel()?.UpdateLogs();
}

public void ClearLogs()
{
LogEntries.Clear();
_getTaskPanel()?.UpdateLogs();
}

private void OnStateChange()
{
// HACK: Ignore _task.IsRunning because we can't hook in to the
// 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);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Rivet/Editor/TaskManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 13 additions & 8 deletions Assets/Rivet/Editor/UI/RivetPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using Rivet.UI.Screens;
using Rivet.Editor.UI.TaskPanel;

namespace Rivet.Editor.UI
{
Expand All @@ -17,6 +18,8 @@ public enum Screen

public class RivetPlugin : EditorWindow
{
public static RivetPlugin? Singleton;

[SerializeField]
private VisualTreeAsset m_VisualTreeAsset;

Expand All @@ -27,18 +30,20 @@ public class RivetPlugin : EditorWindow
private VisualElement _screenLogin;
private VisualElement _screenMain;

private LoginController _loginController;
private MainController _mainController;
public LoginController LoginController;
public MainController MainController;

[MenuItem("Window/UI Toolkit/RivetPlugin")]
public static void ShowExample()
[MenuItem("Window/Rivet/Rivet", false, 20)]
public static void ShowPlugin()
{
RivetPlugin wnd = GetWindow<RivetPlugin>();
wnd.titleContent = new GUIContent("RivetPlugin");
RivetPlugin window = GetWindow<RivetPlugin>();
window.titleContent = new GUIContent("Rivet");
}

public void CreateGUI()
{
Singleton = this;

PluginSettings.LoadSettings();

m_VisualTreeAsset.CloneTree(rootVisualElement);
Expand All @@ -54,8 +59,8 @@ public void CreateGUI()
rootVisualElement.Q(className: "discordButton").RegisterCallback<ClickEvent>((ev) => Application.OpenURL("https://rivet.gg/discord"));
rootVisualElement.Q(className: "feedbackButton").RegisterCallback<ClickEvent>((ev) => Application.OpenURL("https://hub.rivet.gg/?modal=feedback&utm=unity"));

_loginController = new LoginController(this, _screenLogin);
_mainController = new MainController(this, _screenMain);
LoginController = new LoginController(this, _screenLogin);
MainController = new MainController(this, _screenMain);

SetScreen(Screen.Login);
}
Expand Down
Loading

0 comments on commit 865b967

Please sign in to comment.