diff options
Diffstat (limited to 'client')
-rw-r--r-- | client/menu/play.gd | 1 | ||||
-rw-r--r-- | client/project.godot | 1 | ||||
-rw-r--r-- | client/server_list.gd | 48 | ||||
-rw-r--r-- | client/service/discover.gd | 31 | ||||
-rw-r--r-- | client/service/discover.gd.uid | 1 | ||||
-rw-r--r-- | client/service/service.gd | 8 | ||||
-rw-r--r-- | client/settings.gd | 4 |
7 files changed, 53 insertions, 41 deletions
diff --git a/client/menu/play.gd b/client/menu/play.gd index 184b3b2f..2633163e 100644 --- a/client/menu/play.gd +++ b/client/menu/play.gd @@ -56,6 +56,7 @@ func _ready(): Global.set_profile("registry_asked", true) Global.save_settings() Global.save_profile() + ServerList.start() func update_server_list(lists: Array[Array]): diff --git a/client/project.godot b/client/project.godot index c9146766..e3bbec06 100644 --- a/client/project.godot +++ b/client/project.godot @@ -27,6 +27,7 @@ InputManager="*res://menu/settings/input/input_manager.gd" ServerList="*res://server_list.gd" Server="*res://service/server.gd" Editor="*res://service/editor.gd" +Discover="*res://service/discover.gd" [debug] diff --git a/client/server_list.gd b/client/server_list.gd index c3fc1f27..57c6e89f 100644 --- a/client/server_list.gd +++ b/client/server_list.gd @@ -35,9 +35,6 @@ var loading := false var mdns := HTTPRequest.new() var reg := HTTPRequest.new() -var thread: Thread -var pid: int = -1 - # Fallback to http, since there seems to be a problem related to mbed tls in Godot. # See: https://github.com/godotengine/godot/issues/96103 var using_http_fallback := false @@ -65,33 +62,19 @@ func _ready() -> void: add_child(timeout) mdns.request_completed.connect(_on_request_completed.bind(Registry.MDNS)) reg.request_completed.connect(_on_request_completed.bind(Registry.GLOBAL)) - if Global.get_setting("online.use_discovery"): - thread = Thread.new() - thread.start(func(): - # SAFETY: reading/writing arrays/dictionaries is allowed as long as - # the size is not changed. - # cf.: https://docs.godotengine.org/en/4.3/tutorials/performance/thread_safe_apis.html#gdscript-arrays-dictionaries - var res = OS.create_process(get_discovery_path(), [], false) - # SAFETY: since this is not read until the thread stops, this should - # probably be safe without synchronisation :) - pid = res - ) - -static func get_discovery_path() -> String: - var path: String = Global.get_setting("online.discovery_binary") - if path != "": - return path - else: - return "hurrycurry-discover" func fetch_server_list(registry: Registry) -> void: match registry: Registry.MDNS: - mdns.request(MDNS_URL, HEADERS) + if Global.get_setting("online.use_discover"): + match Discover.state: + Service.State.STOPPED: Discover.start() + Service.State.RUNNING: mdns.request(MDNS_URL, HEADERS) Registry.GLOBAL: - var url: String = Global.get_setting("online.registry_url") - url = url.replace("https:", "http:") if using_http_fallback else url - reg.request(url + "/v1/list", HEADERS) + if Global.get_setting("online.use_registry"): + var url: String = Global.get_setting("online.registry_url") + url = url.replace("https:", "http:") if using_http_fallback else url + reg.request(url + "/v1/list", HEADERS) loading = true update_loading.emit(true) @@ -116,12 +99,10 @@ func _on_request_completed(result: int, _response_code: int, _headers: PackedStr func start() -> void: timeout.stop() timeout.start() - if Global.get_setting("online.use_discovery"): - mdns_timer.start() - fetch_server_list(Registry.MDNS) - if Global.get_setting("online.use_registry"): - reg_timer.start() - fetch_server_list(Registry.GLOBAL) + mdns_timer.start() + fetch_server_list(Registry.MDNS) + reg_timer.start() + fetch_server_list(Registry.GLOBAL) func stop() -> void: timeout.stop() @@ -131,8 +112,3 @@ func stop() -> void: func one_shot() -> void: start() stop() - -func _exit_tree(): - if thread != null: thread.wait_to_finish() - if pid != -1: - OS.kill(pid) diff --git a/client/service/discover.gd b/client/service/discover.gd new file mode 100644 index 00000000..43fe5f38 --- /dev/null +++ b/client/service/discover.gd @@ -0,0 +1,31 @@ +# Hurry Curry! - a game about cooking +# Copyright 2025 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 DiscoverService +extends Service + +func name(): + return "Discover" + +func arguments(): + return [] + +func exe_path() -> String: + var path: String = Global.get_setting("online.discover_binary") + if path != "": return path + else: return "hurrycurry-discover" + +func test_port(): + return 27033 diff --git a/client/service/discover.gd.uid b/client/service/discover.gd.uid new file mode 100644 index 00000000..d7ce87a2 --- /dev/null +++ b/client/service/discover.gd.uid @@ -0,0 +1 @@ +uid://bppjsmlmnm33p diff --git a/client/service/service.gd b/client/service/service.gd index f19364c7..9695714f 100644 --- a/client/service/service.gd +++ b/client/service/service.gd @@ -56,6 +56,7 @@ func start(): if state != State.STOPPED and state != State.FAILED: push_error(name() + " can't be started") return + print(name() + ": Starting...") state = State.STARTING thread = Thread.new() thread.start(_server_exec) @@ -64,13 +65,14 @@ func stop(): if state != State.RUNNING: push_error(name() + " can't be stopped") return + print(name() + ": Stopping...") OS.kill(pid) func _test_server(): var output = [] - print(name() + ": Binary path is " + exe_path()) + print(name() + ": Testing executable " + exe_path()) thread_result = OS.execute(exe_path(), ["-v"], output, true, false) - print(output) + print(name() + ": Version = " + output[0].strip_edges()) sem.post() func _server_exec(): @@ -118,5 +120,5 @@ func _process(_delta): pid = null func _exit_tree(): - if pid != null: OS.kill(pid) + if state == State.RUNNING: stop() if thread != null: thread.wait_to_finish() diff --git a/client/settings.gd b/client/settings.gd index ddecea64..00b02c24 100644 --- a/client/settings.gd +++ b/client/settings.gd @@ -77,8 +77,8 @@ static func get_root(): SettingsCategory.new("online", [ ToggleSetting.new("use_registry", false), TextSetting.new("registry_url", "https://hurrycurry-registry.metamuffin.org/"), - ToggleSetting.new("use_discovery", true), - TextSetting.new("discovery_binary", ""), + ToggleSetting.new("use_discover", true), + TextSetting.new("discover_binary", ""), ]) ]) |