Skip to content

Commit

Permalink
feat: run game server from within the plugin (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Jul 26, 2024
1 parent d42e808 commit f7bea53
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 12 deletions.
2 changes: 1 addition & 1 deletion addons/rivet/devtools/dock/elements/buttons_bar.gd
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ func _select_button(curr: int) -> void:


func set_current_button(button: int) -> void:
_select_button(button)
_select_button(button)
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ func _select_menu_item(idx: int) -> void:
func _on_plugin_bootstrapped() -> void:
disabled = false
_update_menu_button(namespaces)
_select_menu_item(0)
_select_menu_item(0)
94 changes: 94 additions & 0 deletions addons/rivet/devtools/dock/playtest_tab.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@

const ButtonsBar = preload("elements/buttons_bar.gd")

# Namespaces
@onready var namespace_description: RichTextLabel = %NamespaceDescription
@onready var buttons_bar: ButtonsBar = %ButtonsBar
@onready var warning: RichTextLabel = %WarningLabel
@onready var error: RichTextLabel = %ErrorLabel
@onready var deploy_button: Button = %DeployButton
@onready var namespace_selector = %AuthNamespaceSelector

# Game Server
var game_server_poll_timer: Timer
var game_server_pid: int = -1
@onready var game_server_start_button: Button = %GameServerStartButton
@onready var game_server_stop_button: Button = %GameServerStopButton
@onready var game_server_restart_button: Button = %GameServerRestartButton
@onready var game_server_status_label: Label = %GameServerStatusLabel
@onready var game_server_show_logs: CheckBox = %GameServerShowLogs

func _ready() -> void:
if get_tree().edited_scene_root == self:
return # This is the scene opened in the editor!

namespace_description.add_theme_font_override(&"mono_font", get_theme_font(&"output_source_mono", &"EditorFonts"))
namespace_description.add_theme_font_override(&"bold_font", get_theme_font(&"bold", &"EditorFonts"))
namespace_description.add_theme_stylebox_override(&"normal", get_theme_stylebox(&"bg", &"AssetLib"))
Expand Down Expand Up @@ -40,6 +51,15 @@ func _ready() -> void:
deploy_button.pressed.connect(_on_deploy_button_pressed)
buttons_bar.selected.connect(_on_buttons_bar_selected)

game_server_poll_timer = Timer.new()
game_server_poll_timer.wait_time = 0.5
game_server_poll_timer.paused = true
game_server_poll_timer.autostart = true
game_server_poll_timer.timeout.connect(_poll_game_server_status)
add_child(game_server_poll_timer)

_poll_game_server_status()

func _on_namespace_selector_item_selected(id: int) -> void:
_update_warnings()

Expand Down Expand Up @@ -117,3 +137,77 @@ func _on_deploy_button_pressed() -> void:
owner.change_tab(1)
owner.deploy_tab.namespace_selector.current_value = namespace_selector.current_value
owner.deploy_tab.namespace_selector.selected = namespace_selector.selected


func _on_game_server_start_pressed():
start_server()


func _on_game_server_stop_pressed():
stop_server()

func _on_game_server_restart_pressed():
start_server()

# MARK: Game Server
func start_server():
if game_server_pid != -1:
RivetPluginBridge.log("Restarting server, old pid %s" % game_server_pid)
stop_server()

if game_server_show_logs.button_pressed:
# Running with logs does not have a PID we can kill
game_server_pid = -1

# Run via Rivet CLI to show the terminal. Get the PID from the process
# the Rivet CLI spawned.
var full_args = ["sidekick", "show-term", "--", OS.get_executable_path()]
full_args.append_array(_server_run_args())
var result = RivetPluginBridge.get_plugin().cli.run_and_wait_sync(full_args)
if result.exit_code != 0 or !("Ok" in result.output):
RivetPluginBridge.display_cli_error(self, result)
return
RivetPluginBridge.log("Started server with logs")
else:
# Run natively without terminal
game_server_pid = OS.create_process(OS.get_executable_path(), _server_run_args())
RivetPluginBridge.log("Started server without logs %s" % game_server_pid)

_poll_game_server_status()

func stop_server():
if game_server_pid != -1:
RivetPluginBridge.log("Stopped serer %s" % game_server_pid)
OS.kill(game_server_pid)
game_server_pid = -1
_poll_game_server_status()
else:
RivetPluginBridge.log("Server not running")

func _server_run_args() -> PackedStringArray:
var project_path = ProjectSettings.globalize_path("res://")
return ["--path", project_path, "--headless", "--", "--server"]

## Checks if the server process is still running.
func _poll_game_server_status():
# Check if server still running
if game_server_pid != -1 and !OS.is_process_running(game_server_pid):
RivetPluginBridge.log("Server process exited %s" % game_server_pid)
game_server_pid = -1

# Update stop button
if game_server_pid != -1:
game_server_poll_timer.paused = false
game_server_status_label.text = "Game server running (pid %s)" % game_server_pid
game_server_start_button.visible = false
game_server_stop_button.visible = true
game_server_restart_button.visible = true
game_server_status_label.visible = true
game_server_show_logs.visible = false
else:
game_server_poll_timer.paused = true
game_server_start_button.visible = true
game_server_stop_button.visible = false
game_server_restart_button.visible = false
game_server_status_label.visible = false
game_server_show_logs.visible = true
64 changes: 56 additions & 8 deletions addons/rivet/devtools/dock/playtest_tab.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=4 format=3 uid="uid://b17eqs0bmrncs"]
[gd_scene load_steps=4 format=3 uid="uid://bl27qv24us70m"]

[ext_resource type="Script" path="res://addons/rivet/devtools/dock/playtest_tab.gd" id="1_4p5p5"]
[ext_resource type="PackedScene" uid="uid://bogw8dj8rr202" path="res://addons/rivet/devtools/dock/elements/namespace_menu_button.tscn" id="1_brqcl"]
Expand Down Expand Up @@ -30,18 +30,18 @@ mouse_default_cursor_shape = 2
toggle_mode = true
text = "Rivet servers"

[node name="VBoxContainer" type="VBoxContainer" parent="PlaytestContent"]
[node name="Namepsace" type="VBoxContainer" parent="PlaytestContent"]
layout_mode = 2

[node name="Namespace" type="Label" parent="PlaytestContent/VBoxContainer"]
[node name="Namespace" type="Label" parent="PlaytestContent/Namepsace"]
layout_mode = 2
size_flags_horizontal = 3
text = "Namespace"

[node name="AuthNamespaceSelector" parent="PlaytestContent/VBoxContainer" instance=ExtResource("1_brqcl")]
[node name="AuthNamespaceSelector" parent="PlaytestContent/Namepsace" instance=ExtResource("1_brqcl")]
layout_mode = 2

[node name="NamespaceDescription" type="RichTextLabel" parent="PlaytestContent/VBoxContainer"]
[node name="NamespaceDescription" type="RichTextLabel" parent="PlaytestContent/Namepsace"]
unique_name_in_owner = true
layout_mode = 2
bbcode_enabled = true
Expand All @@ -50,23 +50,71 @@ fit_content = true
scroll_active = false
autowrap_mode = 2

[node name="WarningLabel" type="RichTextLabel" parent="PlaytestContent/VBoxContainer"]
[node name="WarningLabel" type="RichTextLabel" parent="PlaytestContent/Namepsace"]
unique_name_in_owner = true
layout_mode = 2
text = " Please make sure that you’re using the same version that’s deployed.
Using different versions can cause some unforeseen issues."
fit_content = true
scroll_active = false

[node name="ErrorLabel" type="RichTextLabel" parent="PlaytestContent/VBoxContainer"]
[node name="ErrorLabel" type="RichTextLabel" parent="PlaytestContent/Namepsace"]
unique_name_in_owner = true
layout_mode = 2
text = " Before you can test online, you need to deploy your current game version to Rivet.
In order to deploy current game version to the cloud, click the button below."
fit_content = true
scroll_active = false

[node name="DeployButton" type="Button" parent="PlaytestContent/VBoxContainer"]
[node name="DeployButton" type="Button" parent="PlaytestContent/Namepsace"]
unique_name_in_owner = true
layout_mode = 2
text = "Deploy"

[node name="HSeparator" type="HSeparator" parent="PlaytestContent"]
layout_mode = 2

[node name="Game Server" type="VBoxContainer" parent="PlaytestContent"]
layout_mode = 2

[node name="GameServer" type="Label" parent="PlaytestContent/Game Server"]
layout_mode = 2
size_flags_horizontal = 3
text = "Game Server"

[node name="HBoxContainer" type="HBoxContainer" parent="PlaytestContent/Game Server"]
layout_mode = 2
alignment = 1

[node name="GameServerStartButton" type="Button" parent="PlaytestContent/Game Server/HBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 3
text = "Start"

[node name="GameServerStopButton" type="Button" parent="PlaytestContent/Game Server/HBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 3
text = "Stop"

[node name="GameServerRestartButton" type="Button" parent="PlaytestContent/Game Server/HBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 3
text = "Restart
"

[node name="GameServerStatusLabel" type="Label" parent="PlaytestContent/Game Server"]
unique_name_in_owner = true
layout_mode = 2
text = "Game server not running"

[node name="GameServerShowLogs" type="CheckBox" parent="PlaytestContent/Game Server"]
unique_name_in_owner = true
layout_mode = 2
text = "Show Server Logs"

[connection signal="pressed" from="PlaytestContent/Game Server/HBoxContainer/GameServerStartButton" to="." method="_on_game_server_start_pressed"]
[connection signal="pressed" from="PlaytestContent/Game Server/HBoxContainer/GameServerStopButton" to="." method="_on_game_server_stop_pressed"]
[connection signal="pressed" from="PlaytestContent/Game Server/HBoxContainer/GameServerRestartButton" to="." method="_on_game_server_restart_pressed"]
7 changes: 5 additions & 2 deletions addons/rivet/devtools/rivet_cli.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extends RefCounted
##
## @experimental

const REQUIRED_RIVET_CLI_VERSION = "v1.3.1"
const REQUIRED_RIVET_CLI_VERSION = "v1.4.0"

const _RivetEditorSettings = preload("rivet_editor_settings.gd")
const _RivetThread = preload("rivet_thread.gd")
Expand All @@ -25,9 +25,12 @@ func run_and_wait(args: PackedStringArray) -> _RivetCliOutput:
var thread: _RivetThread = _RivetThread.new(_run.bind(args))
return await thread.wait_to_finish()

func run(args:PackedStringArray) -> _RivetThread:
func run(args: PackedStringArray) -> _RivetThread:
return _RivetThread.new(_run.bind(args))

func run_and_wait_sync(args: PackedStringArray) -> _RivetCliOutput:
return _run(args)

func get_bin_dir() -> String:
var home_path: String = OS.get_environment("USERPROFILE") if OS.get_name() == "Windows" else OS.get_environment("HOME")

Expand Down
2 changes: 2 additions & 0 deletions script.command
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
cd "/Users/nathan/rivet/plugin-godot" && /Applications/Godot_mono.app/Contents/MacOS/Godot --path /Users/nathan/rivet/plugin-godot/ --headless -- --server && rm "/Users/nathan/rivet/plugin-godot/script.command"

0 comments on commit f7bea53

Please sign in to comment.