Update preader

- [src/preader.gd] Replace "logger.error()" calls with "logger.warn()" if function is returning directly after
- [src/preader.gd] Add function read_resource()
This commit is contained in:
JeremyStar™ 2024-01-16 21:28:45 +01:00
parent 751adc312e
commit a3ba0bf759

View file

@ -56,7 +56,7 @@ func open_presentation(path: String, zip: bool) -> Error:
# Close the currently open presentation # Close the currently open presentation
func close_presentation() -> Error: func close_presentation() -> Error:
if !is_open: if !is_open:
logger.error("No presentation is currently opened") logger.warn("No presentation is currently opened")
return Error.FAILED return Error.FAILED
# Disable operations # Disable operations
is_open = false is_open = false
@ -78,34 +78,74 @@ func close_presentation() -> Error:
# Read a file from the presentation archive/directory # Read a file from the presentation archive/directory
func read_file(path: String) -> PackedByteArray: func read_file(path: String) -> PackedByteArray:
if !is_open: if !is_open:
logger.error("No presentation is currently opened") logger.warn("No presentation is currently opened")
return PackedByteArray([]) return PackedByteArray([])
if directorypath == "": if directorypath == "":
# Check if file exists, if not return empty PackedByteArray # Check if file exists, if not return empty PackedByteArray
if !ziphandler.file_exists(path, true): if !ziphandler.file_exists(path, true):
logger.error("Requested file is missing in presentation archive") logger.warn("Requested file is missing in presentation archive")
return PackedByteArray([]) return PackedByteArray([])
# Read and return file # Read and return file
return ziphandler.read_file(path, true) return ziphandler.read_file(path, true)
else: else:
# Check if file exists, if not return empty PackedByteArray # Check if file exists, if not return empty PackedByteArray
if !FileAccess.file_exists(directorypath + "/" + path): if !FileAccess.file_exists(directorypath + "/" + path):
logger.error("Requested file is missing in presentation directory") logger.warn("Requested file is missing in presentation directory")
return PackedByteArray([]) return PackedByteArray([])
# Open file as ro # Open file as ro
var file = FileAccess.open(directorypath + "/" + path, FileAccess.READ) var file = FileAccess.open(directorypath + "/" + path, FileAccess.READ)
if file == null: if file == null:
logger.error("Requested file could not be opened, error=" + str(FileAccess.get_open_error())) logger.warn("Requested file could not be opened, error=" + str(FileAccess.get_open_error()))
return PackedByteArray([]) return PackedByteArray([])
# Return PackedByteArray and close file # Return PackedByteArray and close file
var content = file.get_buffer(file.get_length()) var content = file.get_buffer(file.get_length())
file.close() file.close()
return content return content
# Read a resource from the presentation archive/directory
func read_resource(path: String) -> Resource:
if !is_open:
logger.warn("No presentation is currently opened")
return null
if directorypath == "":
var resource_bytes: PackedByteArray = read_file(path)
if resource_bytes.size() == 0:
logger.warn("Resource could not be read")
return null
var split_path: Array = path.split("/")
var file = FileAccess.open(misc.get_temporary_dir() + "/" + str(split_path[split_path.size()-1]), FileAccess.WRITE)
if file == null:
logger.warn("Resource could not be read, FileAccess[1] failed: " + str(FileAccess.get_open_error()))
return null
file.store_buffer(resource_bytes)
file.close()
var resource: Resource = ResourceLoader.load(misc.get_temporary_dir() + "/" + str(split_path[split_path.size()-1]))
if resource == null:
logger.warn("Resource could not be read, resource is null")
return null
return resource
else:
if !FileAccess.file_exists(directorypath + "/" + path):
logger.error("Requested resource is missing in presentation directory")
return null
var resource: Resource = ResourceLoader.load(directorypath + "/" + path)
if resource == null:
logger.warn("Resource could not be read, resource is null")
return null
return resource
# Check if a file exists in presentation archive/direcoty
func file_exists(path: String) -> bool:
if !is_open:
logger.warn("No presentation is currently opened")
return false
if directorypath == "": return ziphandler.file_exists(path, true)
else: return FileAccess.file_exists(directorypath + "/" + path)
# Reads the manifest.json file # Reads the manifest.json file
func read_manifest() -> misc.Error: func read_manifest() -> misc.Error:
if !is_open: if !is_open:
logger.error("No presentation is currently opened") logger.warn("No presentation is currently opened")
return misc.Error.PREADER_NO_PRESENTATION_OPEN return misc.Error.PREADER_NO_PRESENTATION_OPEN
# Read and parse manifest.json # Read and parse manifest.json
manifest = JSON.parse_string(read_file("manifest.json").get_string_from_utf8()) manifest = JSON.parse_string(read_file("manifest.json").get_string_from_utf8())
@ -126,29 +166,14 @@ func read_manifest() -> misc.Error:
# Read the entrypoint file defined in manifest.json # Read the entrypoint file defined in manifest.json
func read_entrypoint() -> misc.Error: func read_entrypoint() -> misc.Error:
if !is_open: if !is_open:
logger.error("No presentation is currently opened") logger.warn("No presentation is currently opened")
return misc.Error.PREADER_NO_PRESENTATION_OPEN return misc.Error.PREADER_NO_PRESENTATION_OPEN
# Check if manifest is loaded in memory # Check if manifest is loaded in memory
if manifest == {}: if manifest == {}:
logger.error("Manifest not loaded in memory, please call read_manifest() first") logger.error("Manifest not loaded in memory, please call read_manifest() first")
return misc.Error.PREADER_NO_MANIFEST return misc.Error.PREADER_NO_MANIFEST
# Create empty script variable # Create empty script variable
var script: Script var script: Script = read_resource(manifest["entrypoint"])
if directorypath == "":
# Read entrypoint script from .zip file
if !ziphandler.file_exists(manifest["entrypoint"]):
logger.error("Entrypoint file does not exist")
# Store entrypoint script to temporary directory as we can't load a "Script" from memory :/
var file = FileAccess.open(misc.get_temporary_dir() + "/entrypoint.gd", FileAccess.WRITE)
file.store_string(ziphandler.read_file(manifest["entrypoint"]).get_string_from_utf8())
file.close()
# Load entrypoint from tempdir
script = ResourceLoader.load(misc.get_temporary_dir() + "/entrypoint.gd")
else:
# Load entrypoint script from directory
if !FileAccess.file_exists(directorypath + "/" + manifest["entrypoint"]):
logger.error("Entrypoint file does not exist")
script = ResourceLoader.load(directorypath + "/" + manifest["entrypoint"])
# script == null == script did not load # script == null == script did not load
if script == null: if script == null:
logger.error("Entrypoint file is invalid (does it contain errors?)") logger.error("Entrypoint file is invalid (does it contain errors?)")
@ -161,7 +186,7 @@ func read_entrypoint() -> misc.Error:
# Return entrypoint node # Return entrypoint node
func get_entrypoint() -> Node: func get_entrypoint() -> Node:
if !is_open: if !is_open:
logger.error("No presentation is currently opened") logger.warn("No presentation is currently opened")
return null return null
if typeof(entrypoint) != TYPE_OBJECT: if typeof(entrypoint) != TYPE_OBJECT:
logger.error("Entrypoint not loaded in memory, please call read_entrypoint() first") logger.error("Entrypoint not loaded in memory, please call read_entrypoint() first")
@ -171,7 +196,7 @@ func get_entrypoint() -> Node:
# Return presentation topic # Return presentation topic
func get_topic() -> String: func get_topic() -> String:
if !is_open: if !is_open:
logger.error("No presentation is currently opened") logger.warn("No presentation is currently opened")
return "" return ""
# Check if manifest is loaded in memory # Check if manifest is loaded in memory
if manifest == {}: if manifest == {}:
@ -182,7 +207,7 @@ func get_topic() -> String:
# Return presentation authors # Return presentation authors
func get_authors() -> String: func get_authors() -> String:
if !is_open: if !is_open:
logger.error("No presentation is currently opened") logger.warn("No presentation is currently opened")
return "" return ""
# Check if manifest is loaded in memory # Check if manifest is loaded in memory
if manifest == {}: if manifest == {}:
@ -197,7 +222,7 @@ func get_authors() -> String:
# Return display ratio # Return display ratio
func get_ratio() -> String: func get_ratio() -> String:
if !is_open: if !is_open:
logger.error("No presentation is currently opened") logger.warn("No presentation is currently opened")
return "" return ""
# Check if manifest is loaded in memory # Check if manifest is loaded in memory
if manifest == {}: if manifest == {}:
@ -208,7 +233,7 @@ func get_ratio() -> String:
# Return display resolution from display ratio # Return display resolution from display ratio
func get_ratio_resolution() -> Vector2i: func get_ratio_resolution() -> Vector2i:
if !is_open: if !is_open:
logger.error("No presentation is currently opened") logger.warn("No presentation is currently opened")
return Vector2i(0, 0) return Vector2i(0, 0)
# Check if manifest is loaded in memory # Check if manifest is loaded in memory
if manifest == {}: if manifest == {}: