Launcher/Main_menu.gd

81 lines
2.4 KiB
GDScript

extends Control
# Define variables
var newest_version # The newest version available online
var newest_version_data # The data about the newest version online
var local_version # The version installed on this computer
var os_name # The simplified name of the OS (linux, or windows)
var suffix #
func _process(delta):
if newest_version != null:
local_version = Versions.get_version()
if local_version:
if newest_version > local_version :
$Panel/Play_button.text = "Update"
else:
$Panel/Play_button.text = "Play"
else:
$Panel/Play_button.text = "Update"
func _ready():
if OS.get_name() == "X11":
os_name = "linux"
elif OS.get_name() == "Windows":
os_name = "windows"
if os_name == "windows":
suffix = ".exe"
else:
suffix = ""
var versions_request = HTTPRequest.new()
add_child(versions_request)
versions_request.connect("request_completed", self, "_versions_request_completed")
var error = versions_request.request("https://code.retroedge.tech/api/v1/repos/UltraFlare/UltraFlare/releases?draft=false&pre-release=true&limit=1")
func _versions_request_completed(result, response_code, headers, body):
var data = JSON.parse(body.get_string_from_ascii()).result
newest_version = data[0].tag_name
newest_version_data = data[0]
print("Got versions!")
func _download_request_completed(result, response_code, headers, body):
var dl_file = File.new()
dl_file.open(OS.get_config_dir()+"/UltraFlare/current/UltraFlare-"+os_name+"-x86_64", 7)
dl_file.store_buffer(body)
dl_file.close()
local_version = newest_version_data.tag_name
Store.write_store("current_version", local_version)
if os_name == "linux":
OS.execute("chmod", ["+x", OS.get_config_dir()+"/UltraFlare/current/UltraFlare-"+os_name+"-x86_64"])
func _on_Play_button_pressed():
if not local_version or newest_version > local_version:
var download = HTTPRequest.new()
var url
add_child(download)
download.connect("request_completed", self, "_download_request_completed")
for asset in newest_version_data.assets:
if asset.name == "UltraFlare-"+os_name+"-x86_64"+suffix:
url = asset.browser_download_url
break
var error = download.request(url)
else:
var exit_code = OS.execute(OS.get_config_dir()+"/UltraFlare/current/UltraFlare-"+os_name+"-x86_64"+suffix, [], false)
if exit_code != -1:
get_tree().quit()
else:
print("Running UltraFlare failed with code: "+exit_code)