summaryrefslogtreecommitdiff
path: root/client/service
diff options
context:
space:
mode:
Diffstat (limited to 'client/service')
-rw-r--r--client/service/editor.gd31
-rw-r--r--client/service/server.gd44
-rw-r--r--client/service/service.gd122
3 files changed, 197 insertions, 0 deletions
diff --git a/client/service/editor.gd b/client/service/editor.gd
new file mode 100644
index 00000000..c600b715
--- /dev/null
+++ b/client/service/editor.gd
@@ -0,0 +1,31 @@
+# Hurry Curry! - a game about cooking
+# Copyright 2024 metamuffin
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, version 3 of the License only.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+#
+class_name EditorService
+extends Service
+
+func name():
+ return "Editor"
+
+func arguments():
+ return []
+
+func exe_path() -> String:
+ var path: String = Global.get_setting("server.editor_binary_path")
+ if path != "": return path
+ else: return "hurrycurry-editor"
+
+func test_port():
+ return 27035
diff --git a/client/service/server.gd b/client/service/server.gd
new file mode 100644
index 00000000..b2db04de
--- /dev/null
+++ b/client/service/server.gd
@@ -0,0 +1,44 @@
+# Hurry Curry! - a game about cooking
+# Copyright 2024 metamuffin
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, version 3 of the License only.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+#
+class_name ServerService
+extends Service
+
+func name():
+ return "Server"
+
+func arguments():
+ var args = []
+ if Global.get_setting("server.data_path") != "":
+ args.push_back("--data-dir")
+ args.push_back(Global.get_setting("server.data_path"))
+ if Global.get_setting("server.name") != "":
+ args.push_back("--server-name")
+ args.push_back(Global.get_setting("server.name"))
+ if Global.get_setting("server.mdns"):
+ args.push_back("--mdns")
+ if Global.get_setting("server.upnp"):
+ args.push_back("--upnp")
+ if Global.get_setting("server.register"):
+ args.push_back("--register")
+ return args
+
+func exe_path() -> String:
+ var path: String = Global.get_setting("server.binary_path")
+ if path != "": return path
+ else: return "hurrycurry-server"
+
+func test_port():
+ return 27032
diff --git a/client/service/service.gd b/client/service/service.gd
new file mode 100644
index 00000000..f19364c7
--- /dev/null
+++ b/client/service/service.gd
@@ -0,0 +1,122 @@
+# Hurry Curry! - a game about cooking
+# Copyright 2024 metamuffin
+# Copyright 2024 nokoe
+# Copyright 2024 tpart
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, version 3 of the License only.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+#
+class_name Service
+extends Node
+
+
+func name():
+ return "Unknown"
+func arguments():
+ return []
+func exe_path():
+ return "false"
+func test_port():
+ return 25565
+
+var thread = null
+var pid = null
+
+var state = State.TESTING
+enum State {
+ TESTING,
+ UNAVAILABLE,
+ FAILED,
+ STOPPED,
+ STARTING,
+ RUNNING,
+}
+
+var sem = Semaphore.new()
+var thread_result = null;
+
+func _ready():
+ state = State.TESTING
+ thread = Thread.new()
+ thread.start(_test_server)
+
+func test():
+ pass
+
+func start():
+ if state != State.STOPPED and state != State.FAILED:
+ push_error(name() + " can't be started")
+ return
+ state = State.STARTING
+ thread = Thread.new()
+ thread.start(_server_exec)
+
+func stop():
+ if state != State.RUNNING:
+ push_error(name() + " can't be stopped")
+ return
+ OS.kill(pid)
+
+func _test_server():
+ var output = []
+ print(name() + ": Binary path is " + exe_path())
+ thread_result = OS.execute(exe_path(), ["-v"], output, true, false)
+ print(output)
+ sem.post()
+
+func _server_exec():
+ var args = arguments()
+ thread_result = OS.create_process(exe_path(), args, false)
+ if thread_result >= 0:
+ var ok = false
+ while not ok:
+ var conn = StreamPeerTCP.new()
+ if conn.connect_to_host("127.0.0.1", test_port()) == OK:
+ while conn.poll() == OK:
+ if conn.get_status() == StreamPeerTCP.STATUS_ERROR: break
+ elif conn.get_status() == StreamPeerTCP.STATUS_CONNECTED: ok = true; break
+ OS.delay_msec(10)
+ OS.delay_msec(500 if not ok else 50)
+ if !OS.is_process_running(thread_result):
+ thread_result = -1
+ break
+ sem.post()
+
+func _process(_delta):
+ match state:
+ State.TESTING:
+ if sem.try_wait():
+ print(name() + ": Test result=", thread_result)
+ if thread_result == 0: state = State.STOPPED
+ else: state = State.UNAVAILABLE
+ thread.wait_to_finish()
+ thread = null
+ State.STARTING:
+ if sem.try_wait():
+ if thread_result >= 0:
+ state = State.RUNNING
+ pid = thread_result
+ print(name() + ": Started pid=", thread_result)
+ else:
+ state = State.FAILED
+ print(name() + ": Failed")
+ thread.wait_to_finish()
+ thread = null
+ State.RUNNING:
+ if not OS.is_process_running(pid):
+ print(name() + ": Stopped")
+ state = State.STOPPED
+ pid = null
+
+func _exit_tree():
+ if pid != null: OS.kill(pid)
+ if thread != null: thread.wait_to_finish()