Launcher/Main_menu.gd

218 lines
7.5 KiB
GDScript3
Raw Normal View History

2022-10-10 18:11:59 -05:00
extends Control
# Define variables
var newest_version # The newest version available online
2022-11-03 14:53:44 -05:00
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 #
2022-10-10 20:59:43 -05:00
var random
2022-10-28 15:17:54 -05:00
# Load in the Notice_msg scene (Will be instanced)
2022-10-28 15:16:00 -05:00
var Notice_msg = load("res://Notice_msg.tscn")
2022-10-28 15:17:54 -05:00
# Define notice()
# Takes a message and a type, then instances a new Notice_msg instance
2022-10-28 15:16:00 -05:00
func notice(msg, type):
2022-10-28 15:17:54 -05:00
# Get a new Notice_msg instance,
2022-10-28 15:16:00 -05:00
var notice = Notice_msg.instance()
2022-10-28 15:17:54 -05:00
# Pass arguments to notice node
2022-10-28 15:16:00 -05:00
notice.message = msg
notice.type = type
2022-10-28 15:17:54 -05:00
# Set the position
2022-10-28 15:16:00 -05:00
notice.set_position(Vector2(20, $Notices.get_child_count()*30))
2022-10-28 15:17:54 -05:00
2022-10-28 15:16:00 -05:00
$Notices.add_child(notice)
2022-11-03 11:56:04 -05:00
# 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]
2022-11-03 14:53:44 -05:00
update_changelog(newest_version_data)
2022-11-03 11:56:04 -05:00
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
2022-11-03 14:53:44 -05:00
# SETGETS
func update_changelog(new_value):
2022-11-09 16:49:49 -06:00
$Changelog_panel/Label.text = new_value.name
2022-11-03 14:53:44 -05:00
$Changelog_panel/ScrollContainer/RichTextLabel.text = new_value.body
2022-11-03 11:56:04 -05:00
2022-10-28 13:31:46 -05:00
# Every _process tick
2022-10-28 14:06:07 -05:00
func _process(_delta):
2022-10-28 13:31:46 -05:00
# If the newest_version variable exists,
if newest_version != null:
2022-10-28 13:31:46 -05:00
# Get the local_version (By looking at the store.json file)
local_version = Versions.get_version()
2022-10-28 13:31:46 -05:00
# If the local version is not null,
if local_version:
2022-10-28 13:31:46 -05:00
# If the newest version available (online) is greater than the currently installed version,
if newest_version > local_version :
2022-10-28 13:31:46 -05:00
# Set the Play_button's text to "Update"
$Panel/Play_button.text = "Update"
2022-10-28 13:31:46 -05:00
# If not,
else:
2022-10-28 13:31:46 -05:00
# Set the Play_button text to "Play"
$Panel/Play_button.text = "Play"
2022-10-28 13:31:46 -05:00
# If the local version *is* null,
else:
2022-10-28 13:31:46 -05:00
# Set the Play_button to "Update"
$Panel/Play_button.text = "Update"
2022-10-28 21:32:55 -05:00
# 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
2022-10-10 20:59:43 -05:00
2022-10-28 13:31:46 -05:00
# When everything is loaded,
func _ready():
# Get the installed version
local_version = Versions.get_version()
2022-10-28 20:59:00 -05:00
# Randomize
random = RandomNumberGenerator.new()
2022-10-28 20:59:00 -05:00
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")
2022-10-28 13:31:46 -05:00
# Determine the os_name
if OS.get_name() == "X11": # X11 is linuxy-based
os_name = "linux"
elif OS.get_name() == "Windows":
os_name = "windows"
2022-10-28 12:58:19 -05:00
2022-10-28 13:31:46 -05:00
# Determine what to put after the executable name (the suffix)
2022-10-28 12:58:19 -05:00
if os_name == "windows":
suffix = ".exe"
else:
suffix = ""
2022-11-03 11:56:04 -05:00
check_for_update()
2022-10-10 20:59:43 -05:00
2022-10-28 13:31:46 -05:00
# When the play button is pressed,
2022-10-10 20:59:43 -05:00
func _on_Play_button_pressed():
2022-10-28 13:31:46 -05:00
# 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:
2022-10-28 13:31:46 -05:00
# Get a new HTTPRequest node
var download = HTTPRequest.new()
2022-10-28 13:31:46 -05:00
var url # Define a place to hold the url
2022-10-28 12:58:19 -05:00
2022-10-28 13:31:46 -05:00
# Add the HTTPRequest node as a child of the current node (Main_menu)
add_child(download)
2022-10-28 13:31:46 -05:00
# Connect the request_completed signal to self (This script)'s _download_request_completed function
download.connect("request_completed", self, "_download_request_completed")
2022-10-28 13:31:46 -05:00
# For each asset in the newest release's assets,
for asset in newest_version_data.assets:
2022-10-28 13:31:46 -05:00
# If the asset name matches the current operating system,
if asset.name == "UltraFlare-"+os_name+"-x86_64"+suffix:
2022-10-28 13:31:46 -05:00
# Set the url to the download url for that asset
url = asset.browser_download_url
2022-10-28 13:31:46 -05:00
break # End the for loop
2022-10-28 13:31:46 -05:00
# Send an HTTP request to the url
var error = download.request(url)
$Panel/ProgressBar.show()
$Panel/ProgressBar.request = download
2022-10-28 13:44:56 -05:00
# 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, "")
2022-10-28 13:44:56 -05:00
2022-10-28 13:31:46 -05:00
# 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")
2022-11-03 11:56:04 -05:00
# When the refresh button is pressed,
func _on_Refresh_button_pressed():
check_for_update()
2022-11-03 14:53:44 -05:00
func _on_Changelog_button_pressed():
if $Changelog_panel.visible == false:
$Changelog_panel.show()
else:
$Changelog_panel.hide()