Add write_temporary_file() method

This commit is contained in:
JeremyStar™ 2024-05-14 01:00:42 +02:00
parent 06b83a0589
commit aa3c1ecd30
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D

View file

@ -18,6 +18,7 @@ extends Node
const version: int = 1 const version: int = 1
const version_float: float = float(version) const version_float: float = float(version)
const temporary_file: String = "user://temporary_file"
@onready var core: Core = get_node("/root/CORE") @onready var core: Core = get_node("/root/CORE")
@onready var logger: CoreLoggerInstance = core.logger.get_instance("src/presenloader.gd", self) @onready var logger: CoreLoggerInstance = core.logger.get_instance("src/presenloader.gd", self)
@ -209,16 +210,9 @@ func load_entrypoint() -> String:
if is_file: if is_file:
# Workaround (we can't load resources from buffer. why? idk) # Workaround (we can't load resources from buffer. why? idk)
# -> Remove temporary file (if exists) # -> Remove temporary file (if exists)
if FileAccess.file_exists("user://entrypoint.gd"): write_temporary_file(reader.read_file("src/" + manifest["entrypoint"]))
DirAccess.remove_absolute("user://entrypoint.gd")
# -> Write entrypoint file (buffer from ZIPReader) into temporary file
var file: FileAccess = FileAccess.open("user://entrypoint.gd", FileAccess.WRITE)
if file == null:
return core.misc.stringify_variables("Can't write temporary entrypoint: %error_string% (%error%)", { "error": FileAccess.get_open_error(), "error_string": error_string(FileAccess.get_open_error()) })
file.store_buffer(reader.read_file("src/" + manifest["entrypoint"]))
file.close()
# -> Load temporary file # -> Load temporary file
entrypoint = ResourceLoader.load("user://entrypoint.gd") entrypoint = ResourceLoader.load(temporary_file)
else: else:
# Load entrypoint # Load entrypoint
entrypoint = ResourceLoader.load(path + "/src/" + manifest["entrypoint"]) entrypoint = ResourceLoader.load(path + "/src/" + manifest["entrypoint"])
@ -243,3 +237,20 @@ func inject_entrypoint() -> void:
# Execute initialize() # Execute initialize()
await entrypoint_node.initialize() await entrypoint_node.initialize()
# +++ etc +++
func write_temporary_file(bytes: PackedByteArray) -> void:
# Check if exists, if so delete the file
if FileAccess.file_exists(temporary_file):
logger.diag("Removing temporary file")
# Open
var file: FileAccess = FileAccess.open(temporary_file, FileAccess.WRITE)
if file == null:
await logger.crash(core.misc.stringify_variables("Can't write temporary file: %error_string% (%error%)", { "error": FileAccess.get_open_error(), "error_string": error_string(FileAccess.get_open_error()) }))
# Write
file.store_buffer(bytes)
# Close
file.close()