2022-10-10 18:11:59 -05:00
|
|
|
extends Control
|
|
|
|
|
2022-10-28 13:05:40 -05:00
|
|
|
# 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 #
|
2022-10-10 20:59:43 -05:00
|
|
|
|
2022-10-28 21:02:13 -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-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,
|
2022-10-13 09:51:32 -05:00
|
|
|
if newest_version != null:
|
2022-10-28 13:31:46 -05:00
|
|
|
# Get the local_version (By looking at the store.json file)
|
2022-10-28 13:05:40 -05:00
|
|
|
local_version = Versions.get_version()
|
2022-10-28 13:31:46 -05:00
|
|
|
|
|
|
|
# If the local version is not null,
|
2022-10-28 13:05:40 -05:00
|
|
|
if local_version:
|
2022-10-28 13:31:46 -05:00
|
|
|
# If the newest version available (online) is greater than the currently installed version,
|
2022-10-28 13:05:40 -05:00
|
|
|
if newest_version > local_version :
|
2022-10-28 13:31:46 -05:00
|
|
|
# Set the Play_button's text to "Update"
|
2022-10-13 09:51:32 -05:00
|
|
|
$Panel/Play_button.text = "Update"
|
2022-10-28 13:31:46 -05:00
|
|
|
# If not,
|
2022-10-13 09:51:32 -05:00
|
|
|
else:
|
2022-10-28 13:31:46 -05:00
|
|
|
# Set the Play_button text to "Play"
|
2022-10-13 09:51:32 -05:00
|
|
|
$Panel/Play_button.text = "Play"
|
2022-10-28 13:31:46 -05:00
|
|
|
# If the local version *is* null,
|
2022-10-27 11:31:30 -05:00
|
|
|
else:
|
2022-10-28 13:31:46 -05:00
|
|
|
# Set the Play_button to "Update"
|
2022-10-27 11:31:30 -05:00
|
|
|
$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,
|
2022-10-27 11:31:30 -05:00
|
|
|
func _ready():
|
2022-10-28 21:58:00 -05:00
|
|
|
# Get the installed version
|
|
|
|
local_version = Versions.get_version()
|
|
|
|
|
2022-10-28 20:59:00 -05:00
|
|
|
# Randomize
|
2022-10-28 21:02:13 -05:00
|
|
|
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
|
2022-10-13 09:51:32 -05:00
|
|
|
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-10-28 13:31:46 -05:00
|
|
|
# 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)
|
2022-10-13 09:51:32 -05:00
|
|
|
|
2022-10-28 13:31:46 -05:00
|
|
|
# Connect the request_completed signal to self (this script)'s _versions_request_completed function
|
2022-10-13 09:51:32 -05:00
|
|
|
versions_request.connect("request_completed", self, "_versions_request_completed")
|
2022-10-28 13:31:46 -05:00
|
|
|
# Send an HTTP request to the releases page of UltraFlare on code.retroedge.tech
|
2022-10-13 09:51:32 -05:00
|
|
|
var error = versions_request.request("https://code.retroedge.tech/api/v1/repos/UltraFlare/UltraFlare/releases?draft=false&pre-release=true&limit=1")
|
|
|
|
|
2022-10-28 13:31:46 -05:00
|
|
|
# Define _versions_request_completed
|
|
|
|
# Handles the signal sent by the HTTPRequest when the download completes.
|
2022-10-28 14:06:07 -05:00
|
|
|
func _versions_request_completed(_result, _response_code, _headers, body):
|
2022-10-28 13:31:46 -05:00
|
|
|
# Get the JSON data
|
2022-10-13 09:51:32 -05:00
|
|
|
var data = JSON.parse(body.get_string_from_ascii()).result
|
2022-10-28 13:31:46 -05:00
|
|
|
# Get the newest version
|
2022-10-13 09:51:32 -05:00
|
|
|
newest_version = data[0].tag_name
|
2022-10-28 13:31:46 -05:00
|
|
|
# Set the newest_version_data to the data received.
|
2022-10-13 09:51:32 -05:00
|
|
|
newest_version_data = data[0]
|
2022-10-27 11:31:30 -05:00
|
|
|
|
|
|
|
print("Got versions!")
|
2022-10-13 09:51:32 -05:00
|
|
|
|
2022-10-28 13:31:46 -05:00
|
|
|
# Define _download_request_completed
|
|
|
|
# Handles the signal sent by the HTTPRequest when the download completes.
|
2022-10-28 14:06:07 -05:00
|
|
|
func _download_request_completed(_result, _response_code, _headers, body):
|
2022-10-28 13:31:46 -05:00
|
|
|
# Get a new File object for the executable
|
2022-10-13 09:51:32 -05:00
|
|
|
var dl_file = File.new()
|
2022-10-28 13:31:46 -05:00
|
|
|
# Open the dl_file using the following path,
|
|
|
|
dl_file.open(OS.get_config_dir()+"/UltraFlare/current/UltraFlare-"+os_name+"-x86_64"+suffix, 7)
|
|
|
|
# Store the downloaded body in the dl_file
|
2022-10-13 09:51:32 -05:00
|
|
|
dl_file.store_buffer(body)
|
2022-10-28 13:31:46 -05:00
|
|
|
# Close the file
|
2022-10-13 09:51:32 -05:00
|
|
|
dl_file.close()
|
2022-10-27 11:31:30 -05:00
|
|
|
|
2022-10-28 13:31:46 -05:00
|
|
|
# Set the local_version (The one installed on this computer) to the newest_version_data's tag_name
|
2022-10-28 13:05:40 -05:00
|
|
|
local_version = newest_version_data.tag_name
|
2022-10-28 13:31:46 -05:00
|
|
|
# Write the current_version to the store file
|
2022-10-28 13:05:40 -05:00
|
|
|
Store.write_store("current_version", local_version)
|
2022-10-27 11:31:30 -05:00
|
|
|
|
2022-10-28 13:31:46 -05:00
|
|
|
# IF the os is "linux" based, use chmod to change the file to executable
|
2022-10-27 11:31:30 -05:00
|
|
|
if os_name == "linux":
|
2022-10-28 14:06:07 -05:00
|
|
|
var error = OS.execute("chmod", ["+x", OS.get_config_dir()+"/UltraFlare/current/UltraFlare-"+os_name+"-x86_64"])
|
2022-10-28 13:44:56 -05:00
|
|
|
|
|
|
|
# Enable (Un-disable?) the Play_button
|
|
|
|
$Panel/Play_button.disabled = false
|
2022-10-28 16:00:37 -05:00
|
|
|
$Panel/ProgressBar.hide()
|
|
|
|
$Panel/ProgressBar.value = 0
|
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
|
2022-10-28 13:05:40 -05:00
|
|
|
if not local_version or newest_version > local_version:
|
2022-10-28 13:31:46 -05:00
|
|
|
# Get a new HTTPRequest node
|
2022-10-27 11:31:30 -05:00
|
|
|
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)
|
2022-10-27 11:31:30 -05:00
|
|
|
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
|
2022-10-27 11:31:30 -05:00
|
|
|
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,
|
2022-10-27 11:31:30 -05:00
|
|
|
for asset in newest_version_data.assets:
|
2022-10-28 13:31:46 -05:00
|
|
|
# If the asset name matches the current operating system,
|
2022-10-27 11:31:30 -05:00
|
|
|
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
|
2022-10-27 11:31:30 -05:00
|
|
|
url = asset.browser_download_url
|
2022-10-28 13:31:46 -05:00
|
|
|
break # End the for loop
|
2022-10-27 11:31:30 -05:00
|
|
|
|
2022-10-28 13:31:46 -05:00
|
|
|
# Send an HTTP request to the url
|
2022-10-27 11:31:30 -05:00
|
|
|
var error = download.request(url)
|
2022-10-28 16:00:37 -05:00
|
|
|
$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!
|
2022-10-28 16:00:37 -05:00
|
|
|
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
|
2022-10-27 11:31:30 -05:00
|
|
|
else:
|
2022-10-28 15:41:44 -05:00
|
|
|
# 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,
|
2022-10-13 09:51:32 -05:00
|
|
|
else:
|
2022-10-28 15:41:44 -05:00
|
|
|
notice("Executable not found! Click 'UPDATE' to install it again", "alert")
|
|
|
|
Store.write_store("current_version", "")
|
2022-10-28 21:02:13 -05:00
|
|
|
|
|
|
|
# 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")
|