Launcher/Main_menu.gd

218 lines
7.5 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 #
var random
# Load in the Notice_msg scene (Will be instanced)
var Notice_msg = load("res://Notice_msg.tscn")
# Define notice()
# Takes a message and a type, then instances a new Notice_msg instance
func notice(msg, type):
# Get a new Notice_msg instance,
var notice = Notice_msg.instance()
# Pass arguments to notice node
notice.message = msg
notice.type = type
# Set the position
notice.set_position(Vector2(20, $Notices.get_child_count()*30))
$Notices.add_child(notice)
# Define check_for_update()
# Checks for new available versions online, and updates the newest_version if needed
func check_for_update():
# Attempt to get the newest available online version
var versions_request = HTTPRequest.new() # Get a new HTTPRequest node
add_child(versions_request) # Add the node as a child of this one (Main_menu)
# Connect the request_completed signal to self (this script)'s _versions_request_completed function
versions_request.connect("request_completed", self, "_versions_request_completed")
# Send an HTTP request to the releases page of UltraFlare on code.retroedge.tech
var _error = versions_request.request("https://code.retroedge.tech/api/v1/repos/UltraFlare/UltraFlare/releases?draft=false&pre-release=true&limit=1")
# SIGNALS
# Define _versions_request_completed
# Handles the signal sent by the HTTPRequest when the download completes.
func _versions_request_completed(_result, _response_code, _headers, body):
# Get the JSON data
var data = JSON.parse(body.get_string_from_ascii()).result
# Get the newest version
newest_version = data[0].tag_name
# Set the newest_version_data to the data received.
newest_version_data = data[0]
update_changelog(newest_version_data)
print("Got versions!")
# Define _download_request_completed
# Handles the signal sent by the HTTPRequest when the download completes.
func _download_request_completed(_result, _response_code, _headers, body):
# Get a new File object for the executable
var dl_file = File.new()
# Open the dl_file using the following path,
dl_file.open(OS.get_config_dir()+"/UltraFlare/current/UltraFlare-"+os_name+"-x86_64"+suffix, File.WRITE)
# Store the downloaded body in the dl_file
dl_file.store_buffer(body)
# Close the file
dl_file.close()
# Set the local_version (The one installed on this computer) to the newest_version_data's tag_name
local_version = newest_version_data.tag_name
# Write the current_version to the store file
Store.write_store("current_version", local_version)
# IF the os is "linux" based, use chmod to change the file to executable
if os_name == "linux":
var _error = OS.execute("chmod", ["+x", OS.get_config_dir()+"/UltraFlare/current/UltraFlare-"+os_name+"-x86_64"])
# Enable (Un-disable?) the Play_button
$Panel/Play_button.disabled = false
$Panel/ProgressBar.hide()
$Panel/ProgressBar.value = 0
# SETGETS
func update_changelog(new_value):
$Changelog_panel/Label.text = new_value.name
$Changelog_panel/ScrollContainer/RichTextLabel.text = new_value.body
# Every _process tick
func _process(_delta):
# If the newest_version variable exists,
if newest_version != null:
# Get the local_version (By looking at the store.json file)
local_version = Versions.get_version()
# If the local version is not null,
if local_version:
# If the newest version available (online) is greater than the currently installed version,
if newest_version > local_version :
# Set the Play_button's text to "Update"
$Panel/Play_button.text = "Update"
# If not,
else:
# Set the Play_button text to "Play"
$Panel/Play_button.text = "Play"
# If the local version *is* null,
else:
# Set the Play_button to "Update"
$Panel/Play_button.text = "Update"
# Set the Local_version label
if local_version:
$Panel/Panel/Local_version.text = "Local Version: "+local_version
if newest_version:
$Panel/Panel/Online_version.text = "Online Version: "+newest_version
# When everything is loaded,
func _ready():
# Get the installed version
local_version = Versions.get_version()
# Randomize
random = RandomNumberGenerator.new()
random.randomize()
# Choose a background
var background_num = random.randi_range(1, 2)
# Set the background
$Background.texture = load("res://textures/backgrounds/background"+str(background_num)+".png")
# Determine the os_name
if OS.get_name() == "X11": # X11 is linuxy-based
os_name = "linux"
elif OS.get_name() == "Windows":
os_name = "windows"
# Determine what to put after the executable name (the suffix)
if os_name == "windows":
suffix = ".exe"
else:
suffix = ""
check_for_update()
# When the play button is pressed,
func _on_Play_button_pressed():
# If local_version is null, or the newest version available online is greater than the version currently installed,
# Update the game
if not local_version or newest_version > local_version:
# Get a new HTTPRequest node
var download = HTTPRequest.new()
var url # Define a place to hold the url
# Add the HTTPRequest node as a child of the current node (Main_menu)
add_child(download)
# Connect the request_completed signal to self (This script)'s _download_request_completed function
download.connect("request_completed", self, "_download_request_completed")
# For each asset in the newest release's assets,
for asset in newest_version_data.assets:
# If the asset name matches the current operating system,
if asset.name == "UltraFlare-"+os_name+"-x86_64"+suffix:
# Set the url to the download url for that asset
url = asset.browser_download_url
break # End the for loop
# Send an HTTP request to the url
var error = download.request(url)
$Panel/ProgressBar.show()
$Panel/ProgressBar.request = download
# If the request was *created* successfully (Not neccessarily saying the server responded positively),
if error == OK:
# Disable the Play_button (So it can't be clicked multiple times)
$Panel/Play_button.disabled = true
# If not,
else:
# Debug message!
notice("Failed to send HTTP request to server! Error: "+error, "")
# If not,
# Play the game
else:
# Check if there's an executable installed
if File.new().file_exists(OS.get_config_dir()+"/UltraFlare/current/UltraFlare-"+os_name+"-x86_64"+suffix):
# Attempt to execute the UltraFlare executable
var exit_code = OS.execute(OS.get_config_dir()+"/UltraFlare/current/UltraFlare-"+os_name+"-x86_64"+suffix, [], false)
# If the exit_code is anything other than -1,
if exit_code != -1:
# Close the launcher, the game should have started
get_tree().quit()
# If not, print out a debug message with the exit code
else:
print("Running UltraFlare failed with code: "+exit_code)
# If not,
else:
notice("Executable not found! Click 'UPDATE' to install it again", "alert")
Store.write_store("current_version", null)
# When the background change timer time's out,
func _on_Change_timer_timeout():
# Choose a background
var background_num = random.randi_range(1, 2)
# Set the background
$Background.texture = load("res://textures/backgrounds/background"+str(background_num)+".png")
# When the refresh button is pressed,
func _on_Refresh_button_pressed():
check_for_update()
func _on_Changelog_button_pressed():
if $Changelog_panel.visible == false:
$Changelog_panel.show()
else:
$Changelog_panel.hide()