From d8cf92b34b418b285a20478c2066f7eab0449d0c Mon Sep 17 00:00:00 2001 From: Lisandro Lorea Date: Wed, 15 Feb 2023 01:48:43 -0300 Subject: [PATCH] Move version_tag and user_agent to Globals singleton --- scenes/Globals.gd | 11 +++++++++++ scenes/NewsFeed.gd | 4 ++-- scenes/NewsItem.gd | 2 +- scenes/Version.gd | 19 +++++-------------- scenes/VersionSelect.gd | 4 ++-- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/scenes/Globals.gd b/scenes/Globals.gd index c6d5315..0dd6909 100644 --- a/scenes/Globals.gd +++ b/scenes/Globals.gd @@ -5,6 +5,17 @@ const DOWNLOAD_DB_FILE_PATH: String = "user://download_db.json" const DEFAULT_CONFIG : Dictionary = { "ui":{"alpha": false, "beta": false, "rc": false}, "versions" : [] } +# Update before commiting +# Use semver +# Add '-devel' for versions not intended for release +# Remove '-devel' when commiting a build to be tagged as release +# Remember to update version in export settings before exporting +const version_tag = "v1.11.1" +var user_agent : String + +func _ready(): + user_agent = "Godot Version Manager/%s (%s) Godot/%s" % [version_tag.lstrip("v"), OS.get_name(), Engine.get_version_info().string ] + # Read the config from file func read_config() -> Dictionary: var file = File.new() diff --git a/scenes/NewsFeed.gd b/scenes/NewsFeed.gd index 02dcbc0..e7a7431 100644 --- a/scenes/NewsFeed.gd +++ b/scenes/NewsFeed.gd @@ -26,7 +26,7 @@ func _refresh_news(): var up_to_date := false var new_etag := "" - $req.request(BASE_URL + "/blog/", ["User-Agent: %s" % $"%Version".user_agent], true, HTTPClient.METHOD_HEAD) + $req.request(BASE_URL + "/blog/", ["User-Agent: %s" % Globals.user_agent], true, HTTPClient.METHOD_HEAD) var head_response = yield($req,"request_completed") for header in head_response[2]: # No ETag being returned by the server as of 2023/02/15 @@ -37,7 +37,7 @@ func _refresh_news(): break if not up_to_date: - $req.request(BASE_URL + "/blog/", ["User-Agent: %s" % $"%Version".user_agent]) + $req.request(BASE_URL + "/blog/", ["User-Agent: %s" % Globals.user_agent]) var response = yield($req,"request_completed") var news = _get_news(response[3]) diff --git a/scenes/NewsItem.gd b/scenes/NewsItem.gd index 23590f0..3407b0b 100644 --- a/scenes/NewsItem.gd +++ b/scenes/NewsItem.gd @@ -47,7 +47,7 @@ func _load_image(_url : String, target : TextureRect): dir.make_dir(BASE_DIR) if not dir.file_exists(local_path): $req.download_file = local_path - $req.request(_url) + $req.request(_url, ["User-Agent: %s" % Globals.user_agent]) var response = yield($req,"request_completed") if not response[1] == 200: printerr("Could not find or download image") diff --git a/scenes/Version.gd b/scenes/Version.gd index 672f722..a713083 100644 --- a/scenes/Version.gd +++ b/scenes/Version.gd @@ -2,14 +2,6 @@ extends HBoxContainer const api_endpoint = "https://api.github.com/repos/noidexe/godot-version-manager/releases" -# Update before commiting -# Use semver -# Add '-devel' for versions not intended for release -# Remove '-devel' when commiting a build to be tagged as release -# Remember to update version in export settings before exporting -const version_tag = "v1.11.1" -var user_agent : String - const DOWNLOAD_SUFFIXES = { "OSX" : "osx.zip", "Windows": "win.zip", @@ -24,11 +16,10 @@ var download_url = RELEASES_URL func _ready(): - user_agent = "Godot Version Manager/%s (%s) Godot/%s" % [version_tag.lstrip("v"), OS.get_name(), Engine.get_version_info().string ] $update.hide() - $tag.text = "Version Tag: " + version_tag + $tag.text = "Version Tag: " + Globals.version_tag - $req.request(api_endpoint, ["Accept: application/vnd.github.v3+json"]) + $req.request(api_endpoint, ["Accept: application/vnd.github.v3+json", "User-Agent: %s" % Globals.user_agent]) func _on_request_completed(_result, response_code : int, _headers, body : PoolByteArray): @@ -44,12 +35,12 @@ func _on_request_completed(_result, response_code : int, _headers, body : PoolBy printerr("Invalid data received when requesting release list") var last_tag : Dictionary = json[0] - var last_version_tag : String = last_tag.get("tag_name", version_tag) + var last_version_tag : String = last_tag.get("tag_name", Globals.version_tag) # The update button SHOULD always appear if the local version tag doesn't # match tag for the latest official release, even if it is a lower version - if last_version_tag == version_tag: - $tag.text = "Version Tag: " + version_tag + " (up to date)" + if last_version_tag == Globals.version_tag: + $tag.text = "Version Tag: " + Globals.version_tag + " (up to date)" else: $update.hint_tooltip = last_tag.get("name", "") # Show title of new release as tooltip diff --git a/scenes/VersionSelect.gd b/scenes/VersionSelect.gd index f7c2a3a..174346f 100644 --- a/scenes/VersionSelect.gd +++ b/scenes/VersionSelect.gd @@ -271,7 +271,7 @@ func _find_links(url:String, db : Dictionary): var req = HTTPRequest.new() add_child(req) - req.request(url, ["User-Agent: %s" % $"%Version".user_agent] ) + req.request(url, ["User-Agent: %s" % Globals.user_agent] ) refresh_button.text = "Scraping%s %s" % [ [".", "..", "..."][randi() % 3] ,url.rsplit("/",true,2)[1] ] @@ -359,7 +359,7 @@ func _on_Download_pressed(): var req = HTTPRequest.new() add_child(req) req.download_file = filename - req.request(url, ["User-Agent: %s" % $"%Version".user_agent], false) + req.request(url, ["User-Agent: %s" % Globals.user_agent], false) var divisor : float = 1024 * 1024