Add entrypoint validation and class
This commit is contained in:
parent
fe2917f77e
commit
5b70fab2f2
3 changed files with 60 additions and 8 deletions
1
example/src/entrypoint.gd
Normal file
1
example/src/entrypoint.gd
Normal file
|
@ -0,0 +1 @@
|
||||||
|
extends PresencodeEntrypoint
|
25
src/classes/entrypoint.gd
Normal file
25
src/classes/entrypoint.gd
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
# PRESENCODE SOURCE FILE
|
||||||
|
# Copyright (c) 2024 JeremyStarTM & Contributors
|
||||||
|
# Licensed under the GNU Affero General Public License v3
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as
|
||||||
|
# published by the Free Software Foundation, either version 3 of the
|
||||||
|
# License, or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
extends Node
|
||||||
|
class_name PresencodeEntrypoint
|
||||||
|
|
||||||
|
var core: Core
|
||||||
|
var logger: CoreLoggerInstance
|
||||||
|
|
||||||
|
# Required methods
|
||||||
|
func load_resources() -> void: core.logger.crash("PresencodeEntrypoint", "Method load_resources was not overriden")
|
||||||
|
func switch_to_slide(_slide: int) -> void: core.logger.crash("PresencodeEntrypoint", "Method switch_to_slide was not overriden")
|
|
@ -81,10 +81,15 @@ func load_presentation(load_path: String) -> String:
|
||||||
var output_array: Array[String] = check_manifest()
|
var output_array: Array[String] = check_manifest()
|
||||||
if output_array != []: return core.misc.format_stringarray(output_array, "- ", "", "\n", "\n")
|
if output_array != []: return core.misc.format_stringarray(output_array, "- ", "", "\n", "\n")
|
||||||
|
|
||||||
# Load and inject entrypoint
|
# Validate entrypoint
|
||||||
|
output = validate_entrypoint()
|
||||||
|
if output != "": return output
|
||||||
|
|
||||||
|
# Load entrypoint
|
||||||
output = load_entrypoint()
|
output = load_entrypoint()
|
||||||
if output != "": return output
|
if output != "": return output
|
||||||
|
|
||||||
|
# Inject entrypoint
|
||||||
inject_entrypoint()
|
inject_entrypoint()
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
|
@ -174,13 +179,37 @@ func check_manifest() -> Array[String]:
|
||||||
|
|
||||||
return schema.evaluate()
|
return schema.evaluate()
|
||||||
|
|
||||||
func load_entrypoint() -> String:
|
func validate_entrypoint() -> String:
|
||||||
logger.verb("Loading entrypoint")
|
logger.verb("Validating entrypoint")
|
||||||
|
|
||||||
|
var content: String = ""
|
||||||
|
|
||||||
|
# Get entrypoint content
|
||||||
if is_file:
|
if is_file:
|
||||||
if !reader.file_exists("src/" + manifest["entrypoint"]):
|
if !reader.file_exists("src/" + manifest["entrypoint"]):
|
||||||
return "Specified entrypoint file could not be located at src/" + manifest["entrypoint"]
|
return "Specified entrypoint file could not be located at src/" + manifest["entrypoint"]
|
||||||
|
|
||||||
|
content = reader.read_file("src/" + manifest["entrypoint"]).get_string_from_utf8()
|
||||||
|
else:
|
||||||
|
if !FileAccess.file_exists(path + "/src/" + manifest["entrypoint"]):
|
||||||
|
return "Specified entrypoint file could not be located at src/" + manifest["entrypoint"]
|
||||||
|
|
||||||
|
var file: FileAccess = FileAccess.open(path + "/src/" + manifest["entrypoint"], FileAccess.READ)
|
||||||
|
if file == null:
|
||||||
|
return core.misc.stringify_variables("Can't read entrypoint: %error_string% (%error%)", { "error": FileAccess.get_open_error(), "error_string": error_string(FileAccess.get_open_error()) })
|
||||||
|
content = file.get_as_text()
|
||||||
|
file.close()
|
||||||
|
|
||||||
|
# Validate entrypoint is extending PresencodeEntrypoint
|
||||||
|
if !content.contains("\nextends PresencodeEntrypoint\n") and !content.begins_with("extends PresencodeEntrypoint\n"):
|
||||||
|
return "The entrypoint script does not extend 'PresencodeEntrypoint'"
|
||||||
|
|
||||||
|
return ""
|
||||||
|
|
||||||
|
func load_entrypoint() -> String:
|
||||||
|
logger.verb("Loading entrypoint")
|
||||||
|
|
||||||
|
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"):
|
if FileAccess.file_exists("user://entrypoint.gd"):
|
||||||
|
@ -194,9 +223,6 @@ func load_entrypoint() -> String:
|
||||||
# -> Load temporary file
|
# -> Load temporary file
|
||||||
entrypoint = ResourceLoader.load("user://entrypoint.gd")
|
entrypoint = ResourceLoader.load("user://entrypoint.gd")
|
||||||
else:
|
else:
|
||||||
if !FileAccess.file_exists(path + "/src/" + manifest["entrypoint"]):
|
|
||||||
return "Specified entrypoint file could not be located at src/" + manifest["entrypoint"]
|
|
||||||
|
|
||||||
# Load entrypoint
|
# Load entrypoint
|
||||||
entrypoint = ResourceLoader.load(path + "/src/" + manifest["entrypoint"])
|
entrypoint = ResourceLoader.load(path + "/src/" + manifest["entrypoint"])
|
||||||
|
|
||||||
|
@ -205,8 +231,8 @@ func load_entrypoint() -> String:
|
||||||
func inject_entrypoint() -> void:
|
func inject_entrypoint() -> void:
|
||||||
logger.verb("Injecting entrypoint")
|
logger.verb("Injecting entrypoint")
|
||||||
|
|
||||||
# Instantiate new Node
|
# Instantiate new PresencodeEntrypoint
|
||||||
var entrypoint_node: Node = Node.new()
|
var entrypoint_node: PresencodeEntrypoint = PresencodeEntrypoint.new()
|
||||||
# Give the new node a name and the entrypoint script
|
# Give the new node a name and the entrypoint script
|
||||||
entrypoint_node.name = "Entrypoint"
|
entrypoint_node.name = "Entrypoint"
|
||||||
entrypoint_node.set_script(entrypoint)
|
entrypoint_node.set_script(entrypoint)
|
||||||
|
|
Loading…
Reference in a new issue