From 5b70fab2f2c7e43905841982856a15944e8af2f9 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sat, 11 May 2024 19:17:33 +0200 Subject: [PATCH] Add entrypoint validation and class --- example/src/entrypoint.gd | 1 + src/classes/entrypoint.gd | 25 +++++++++++++++++++++++ src/presenloader.gd | 42 +++++++++++++++++++++++++++++++-------- 3 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 example/src/entrypoint.gd create mode 100644 src/classes/entrypoint.gd diff --git a/example/src/entrypoint.gd b/example/src/entrypoint.gd new file mode 100644 index 0000000..3436bba --- /dev/null +++ b/example/src/entrypoint.gd @@ -0,0 +1 @@ +extends PresencodeEntrypoint diff --git a/src/classes/entrypoint.gd b/src/classes/entrypoint.gd new file mode 100644 index 0000000..292cfde --- /dev/null +++ b/src/classes/entrypoint.gd @@ -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 . +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") diff --git a/src/presenloader.gd b/src/presenloader.gd index f1ce5cb..e20ffc3 100644 --- a/src/presenloader.gd +++ b/src/presenloader.gd @@ -81,10 +81,15 @@ func load_presentation(load_path: String) -> String: var output_array: Array[String] = check_manifest() 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() if output != "": return output + # Inject entrypoint inject_entrypoint() return "" @@ -174,13 +179,37 @@ func check_manifest() -> Array[String]: return schema.evaluate() -func load_entrypoint() -> String: - logger.verb("Loading entrypoint") +func validate_entrypoint() -> String: + logger.verb("Validating entrypoint") + var content: String = "" + + # Get entrypoint content if is_file: if !reader.file_exists("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) # -> Remove temporary file (if exists) if FileAccess.file_exists("user://entrypoint.gd"): @@ -194,9 +223,6 @@ func load_entrypoint() -> String: # -> Load temporary file entrypoint = ResourceLoader.load("user://entrypoint.gd") else: - if !FileAccess.file_exists(path + "/src/" + manifest["entrypoint"]): - return "Specified entrypoint file could not be located at src/" + manifest["entrypoint"] - # Load entrypoint entrypoint = ResourceLoader.load(path + "/src/" + manifest["entrypoint"]) @@ -205,8 +231,8 @@ func load_entrypoint() -> String: func inject_entrypoint() -> void: logger.verb("Injecting entrypoint") - # Instantiate new Node - var entrypoint_node: Node = Node.new() + # Instantiate new PresencodeEntrypoint + var entrypoint_node: PresencodeEntrypoint = PresencodeEntrypoint.new() # Give the new node a name and the entrypoint script entrypoint_node.name = "Entrypoint" entrypoint_node.set_script(entrypoint)