Commented Main_menu code
This commit is contained in:
		
							
								
								
									
										61
									
								
								Main_menu.gd
									
									
									
									
									
								
							
							
						
						
									
										61
									
								
								Main_menu.gd
									
									
									
									
									
								
							| @ -7,74 +7,119 @@ var local_version # The version installed on this computer | |||||||
| var os_name # The simplified name of the OS (linux, or windows) | var os_name # The simplified name of the OS (linux, or windows) | ||||||
| var suffix #  | var suffix #  | ||||||
|  |  | ||||||
|  | # Every _process tick | ||||||
| func _process(delta): | func _process(delta): | ||||||
|  | 	# If the newest_version variable exists, | ||||||
| 	if newest_version != null: | 	if newest_version != null: | ||||||
|  | 		# Get the local_version (By looking at the store.json file) | ||||||
| 		local_version = Versions.get_version() | 		local_version = Versions.get_version() | ||||||
|  | 		 | ||||||
|  | 		# If the local version is not null, | ||||||
| 		if local_version: | 		if local_version: | ||||||
|  | 			# If the newest version available (online) is greater than the currently installed version, | ||||||
| 			if newest_version > local_version : | 			if newest_version > local_version : | ||||||
|  | 				# Set the Play_button's text to "Update" | ||||||
| 				$Panel/Play_button.text = "Update" | 				$Panel/Play_button.text = "Update" | ||||||
|  | 			# If not, | ||||||
| 			else: | 			else: | ||||||
|  | 				# Set the Play_button text to "Play" | ||||||
| 				$Panel/Play_button.text = "Play" | 				$Panel/Play_button.text = "Play" | ||||||
|  | 		# If the local version *is* null, | ||||||
| 		else: | 		else: | ||||||
|  | 			# Set the Play_button to "Update" | ||||||
| 			$Panel/Play_button.text = "Update" | 			$Panel/Play_button.text = "Update" | ||||||
|  |  | ||||||
|  | # When everything is loaded, | ||||||
| func _ready(): | func _ready(): | ||||||
| 	if OS.get_name() == "X11": | 	# Determine the os_name | ||||||
|  | 	if OS.get_name() == "X11": # X11 is linuxy-based | ||||||
| 		os_name = "linux" | 		os_name = "linux" | ||||||
| 	elif OS.get_name() == "Windows": | 	elif OS.get_name() == "Windows": | ||||||
| 		os_name = "windows" | 		os_name = "windows" | ||||||
| 	 | 	 | ||||||
|  | 	# Determine what to put after the executable name (the suffix) | ||||||
| 	if os_name == "windows": | 	if os_name == "windows": | ||||||
| 		suffix = ".exe" | 		suffix = ".exe" | ||||||
| 	else: | 	else: | ||||||
| 		suffix = "" | 		suffix = "" | ||||||
|  |  | ||||||
|  | 	# 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) | ||||||
| 	 | 	 | ||||||
| 	var versions_request = HTTPRequest.new() | 	# Connect the request_completed signal to self (this script)'s _versions_request_completed function | ||||||
| 	add_child(versions_request) |  | ||||||
| 	 |  | ||||||
| 	versions_request.connect("request_completed", self, "_versions_request_completed") | 	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") | 	var error = versions_request.request("https://code.retroedge.tech/api/v1/repos/UltraFlare/UltraFlare/releases?draft=false&pre-release=true&limit=1") | ||||||
|  |  | ||||||
|  | # Define _versions_request_completed | ||||||
|  | #  Handles the signal sent by the HTTPRequest when the download completes. | ||||||
| func _versions_request_completed(result, response_code, headers, body): | func _versions_request_completed(result, response_code, headers, body): | ||||||
|  | 	# Get the JSON data | ||||||
| 	var data = JSON.parse(body.get_string_from_ascii()).result | 	var data = JSON.parse(body.get_string_from_ascii()).result | ||||||
|  | 	# Get the newest version | ||||||
| 	newest_version = data[0].tag_name | 	newest_version = data[0].tag_name | ||||||
|  | 	# Set the newest_version_data to the data received. | ||||||
| 	newest_version_data = data[0] | 	newest_version_data = data[0] | ||||||
| 	 | 	 | ||||||
| 	print("Got versions!") | 	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): | func _download_request_completed(result, response_code, headers, body): | ||||||
|  | 	# Get a new File object for the executable | ||||||
| 	var dl_file = File.new() | 	var dl_file = File.new() | ||||||
| 	dl_file.open(OS.get_config_dir()+"/UltraFlare/current/UltraFlare-"+os_name+"-x86_64", 7) | 	# 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 | ||||||
| 	dl_file.store_buffer(body) | 	dl_file.store_buffer(body) | ||||||
|  | 	# Close the file | ||||||
| 	dl_file.close() | 	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 | 	local_version = newest_version_data.tag_name | ||||||
|  | 	# Write the current_version to the store file | ||||||
| 	Store.write_store("current_version", local_version) | 	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": | 	if os_name == "linux": | ||||||
| 		OS.execute("chmod", ["+x", OS.get_config_dir()+"/UltraFlare/current/UltraFlare-"+os_name+"-x86_64"]) | 		OS.execute("chmod", ["+x", OS.get_config_dir()+"/UltraFlare/current/UltraFlare-"+os_name+"-x86_64"]) | ||||||
|  |  | ||||||
|  | # When the play button is pressed, | ||||||
| func _on_Play_button_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: | 	if not local_version or newest_version > local_version: | ||||||
|  | 		# Get a new HTTPRequest node | ||||||
| 		var download = HTTPRequest.new() | 		var download = HTTPRequest.new() | ||||||
| 		var url | 		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) | 		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") | 		download.connect("request_completed", self, "_download_request_completed") | ||||||
| 		 | 		 | ||||||
|  | 		# For each asset in the newest release's assets, | ||||||
| 		for asset in newest_version_data.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: | 			if asset.name == "UltraFlare-"+os_name+"-x86_64"+suffix: | ||||||
|  | 				# Set the url to the download url for that asset | ||||||
| 				url = asset.browser_download_url | 				url = asset.browser_download_url | ||||||
| 				break | 				break # End the for loop | ||||||
| 		 | 		 | ||||||
|  | 		# Send an HTTP request to the url | ||||||
| 		var error = download.request(url) | 		var error = download.request(url) | ||||||
| 		 | 	# If not, | ||||||
|  | 	#  Play the game | ||||||
| 	else: | 	else: | ||||||
|  | 		# Attempt to execute the UltraFlare executable | ||||||
| 		var exit_code = OS.execute(OS.get_config_dir()+"/UltraFlare/current/UltraFlare-"+os_name+"-x86_64"+suffix, [], false) | 		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: | 		if exit_code != -1: | ||||||
|  | 			# Close the launcher, the game should have started | ||||||
| 			get_tree().quit() | 			get_tree().quit() | ||||||
|  | 		# If not, print out a debug message with the exit code | ||||||
| 		else: | 		else: | ||||||
| 			print("Running UltraFlare failed with code: "+exit_code) | 			print("Running UltraFlare failed with code: "+exit_code) | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user