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:
parent
751adc312e
commit
a3ba0bf759
1 changed files with 53 additions and 28 deletions
|
@ -56,7 +56,7 @@ func open_presentation(path: String, zip: bool) -> Error:
|
|||
# Close the currently open presentation
|
||||
func close_presentation() -> Error:
|
||||
if !is_open:
|
||||
logger.error("No presentation is currently opened")
|
||||
logger.warn("No presentation is currently opened")
|
||||
return Error.FAILED
|
||||
# Disable operations
|
||||
is_open = false
|
||||
|
@ -78,34 +78,74 @@ func close_presentation() -> Error:
|
|||
# Read a file from the presentation archive/directory
|
||||
func read_file(path: String) -> PackedByteArray:
|
||||
if !is_open:
|
||||
logger.error("No presentation is currently opened")
|
||||
logger.warn("No presentation is currently opened")
|
||||
return PackedByteArray([])
|
||||
if directorypath == "":
|
||||
# Check if file exists, if not return empty PackedByteArray
|
||||
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([])
|
||||
# Read and return file
|
||||
return ziphandler.read_file(path, true)
|
||||
else:
|
||||
# Check if file exists, if not return empty PackedByteArray
|
||||
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([])
|
||||
# Open file as ro
|
||||
var file = FileAccess.open(directorypath + "/" + path, FileAccess.READ)
|
||||
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 and close file
|
||||
var content = file.get_buffer(file.get_length())
|
||||
file.close()
|
||||
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
|
||||
func read_manifest() -> misc.Error:
|
||||
if !is_open:
|
||||
logger.error("No presentation is currently opened")
|
||||
logger.warn("No presentation is currently opened")
|
||||
return misc.Error.PREADER_NO_PRESENTATION_OPEN
|
||||
# Read and parse manifest.json
|
||||
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
|
||||
func read_entrypoint() -> misc.Error:
|
||||
if !is_open:
|
||||
logger.error("No presentation is currently opened")
|
||||
logger.warn("No presentation is currently opened")
|
||||
return misc.Error.PREADER_NO_PRESENTATION_OPEN
|
||||
# Check if manifest is loaded in memory
|
||||
if manifest == {}:
|
||||
logger.error("Manifest not loaded in memory, please call read_manifest() first")
|
||||
return misc.Error.PREADER_NO_MANIFEST
|
||||
# Create empty script variable
|
||||
var script: Script
|
||||
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"])
|
||||
var script: Script = read_resource(manifest["entrypoint"])
|
||||
# script == null == script did not load
|
||||
if script == null:
|
||||
logger.error("Entrypoint file is invalid (does it contain errors?)")
|
||||
|
@ -161,7 +186,7 @@ func read_entrypoint() -> misc.Error:
|
|||
# Return entrypoint node
|
||||
func get_entrypoint() -> Node:
|
||||
if !is_open:
|
||||
logger.error("No presentation is currently opened")
|
||||
logger.warn("No presentation is currently opened")
|
||||
return null
|
||||
if typeof(entrypoint) != TYPE_OBJECT:
|
||||
logger.error("Entrypoint not loaded in memory, please call read_entrypoint() first")
|
||||
|
@ -171,7 +196,7 @@ func get_entrypoint() -> Node:
|
|||
# Return presentation topic
|
||||
func get_topic() -> String:
|
||||
if !is_open:
|
||||
logger.error("No presentation is currently opened")
|
||||
logger.warn("No presentation is currently opened")
|
||||
return ""
|
||||
# Check if manifest is loaded in memory
|
||||
if manifest == {}:
|
||||
|
@ -182,7 +207,7 @@ func get_topic() -> String:
|
|||
# Return presentation authors
|
||||
func get_authors() -> String:
|
||||
if !is_open:
|
||||
logger.error("No presentation is currently opened")
|
||||
logger.warn("No presentation is currently opened")
|
||||
return ""
|
||||
# Check if manifest is loaded in memory
|
||||
if manifest == {}:
|
||||
|
@ -197,7 +222,7 @@ func get_authors() -> String:
|
|||
# Return display ratio
|
||||
func get_ratio() -> String:
|
||||
if !is_open:
|
||||
logger.error("No presentation is currently opened")
|
||||
logger.warn("No presentation is currently opened")
|
||||
return ""
|
||||
# Check if manifest is loaded in memory
|
||||
if manifest == {}:
|
||||
|
@ -208,7 +233,7 @@ func get_ratio() -> String:
|
|||
# Return display resolution from display ratio
|
||||
func get_ratio_resolution() -> Vector2i:
|
||||
if !is_open:
|
||||
logger.error("No presentation is currently opened")
|
||||
logger.warn("No presentation is currently opened")
|
||||
return Vector2i(0, 0)
|
||||
# Check if manifest is loaded in memory
|
||||
if manifest == {}:
|
||||
|
|
Loading…
Reference in a new issue