CORE/smgr.gd
JeremyStarTM 99703cf03e CORE rewrite (#1)
Reviewed-on: StarOpenSource/core#1

Rewrote CORE and improved the startup process and startup time significantly. The documentation has been beefed up too and is now much better. Existing projects may need major refactoring however.
Co-authored-by: JeremyStarTM <jeremystartm@staropensource.de>
Co-committed-by: JeremyStarTM <jeremystartm@staropensource.de>
2023-08-25 14:34:57 +02:00

153 lines
7.8 KiB
GDScript

######################################
# THE CORE FRAMEWORK #
# MADE BY THE STAROPENSOURCE PROJECT #
# AND CONTRIBUTERS (THANK YOU!) #
# #
# COPYRIGHT 2023 THE STAROPENSOURCE #
# PROJECT AND CONTRIBUTERS #
# #
# LICENSED UNDER THE GNU GENERAL #
# PUBLIC LICENSE VERSION 3 (ONLY) #
######################################
extends Node
# CORE modules
var core: Node = null
var logger: Node = null
var resourcemanager: Node = null
# Overlays
## The debug overlay
var debug_overlay: Node = Node.new()
var debug_overlay_scene: Dictionary = {}
## The cutscene overlay
var cutscene_overlay: Node = Node.new()
var cutscene_overlay_scene: String = ""
## The menu overlay
var menu_overlay: Node = Node.new()
var menu_overlay_scene: Dictionary = {}
## The action overlay
var action_overlay: Node = Node.new()
var action_overlay_scene: String = ""
# Initializes the scene manager
func initialize() -> void:
if core.protection_mode: return
logger.diag("CORE/smgr.gd","Constructing overlays")
# Set overlay names
debug_overlay.name = "DebugOverlay"
cutscene_overlay.name = "CutsceneOverlay"
menu_overlay.name = "MenuOverlay"
action_overlay.name = "ActionOverlay"
logger.diag("CORE/smgr.gd","Adding overlays")
# Adds overlays to /root/
get_tree().root.add_child(debug_overlay)
get_tree().root.add_child(cutscene_overlay)
get_tree().root.add_child(menu_overlay)
get_tree().root.add_child(action_overlay)
# Adds a debug scene
func add_debug(resource_name:String) -> int:
if core.protection_mode: return core.Errors.CORE_PROTECTIONMODE
logger.diag("CORE/smgr.gd","Adding debug scene \"" + resource_name + "\"")
# Load resource into memory
var resource: Object = resourcemanager.getres(resource_name)
if resource == null or resource.get_class() != "PackedScene":
# If resource is null (not loaded) or is not a PackedScene, throw an error
var err = core.error("SceneManager",core.Errors.SCENEMANAGER_NOT_PACKEDSCENE,{"scene":resource_name,"overlay":"debug"})
logger.error("CORE/smgr.gd",err["error"])
return err["code"]
if debug_overlay_scene.has(resource_name) and debug_overlay.get_children().has(resource):
# If scene already exists, throw an error
var err = core.error("SceneManager",core.Errors.SCENEMANAGER_ALREADY_LOADED,{"scene":resource_name,"overlay":"debug"})
logger.error("CORE/smgr.gd",err["error"])
return err["code"]
# In theory unecessary but if something for some reason did something very wrong it would be catched here
if !debug_overlay_scene.has(resource_name) and debug_overlay.get_children().has(resource):
core.exception("SceneManager","debug_overlay_scene does not contain \"" + resource_name + "\" but debug_overlay does")
return core.Errors.CORE_EXCEPTION
if debug_overlay_scene.has(resource_name) and !debug_overlay.get_children().has(resource):
core.exception("SceneManager","debug_overlay does not contain \"" + resource_name + "\" but debug_overlay_scene does")
return core.Errors.CORE_EXCEPTION
# Add scene to debug overlay
debug_overlay.add_child(resource)
debug_overlay_scene.merge({resource_name:resource},true)
return core.Errors.OK
# Adds a menu scene
func add_menu(resource_name:String) -> int:
if core.protection_mode: return core.Errors.CORE_PROTECTIONMODE
logger.diag("CORE/smgr.gd","Adding menu scene \"" + resource_name + "\"")
# Load resource into memory
var resource: Object = resourcemanager.getres(resource_name)
if resource == null or resource.get_class() != "PackedScene":
# If resource is null (not loaded) or is not a PackedScene, throw an error
var err = core.error("SceneManager",core.Errors.SCENEMANAGER_NOT_PACKEDSCENE,{"scene":resource_name,"overlay":"menu"})
logger.error("CORE/smgr.gd",err["error"])
return err["code"]
if menu_overlay_scene.has(resource_name) and menu_overlay.get_children().has(resource):
# If resource is null (not loaded) or is not a PackedScene, throw an error
var err = core.error("SceneManager",core.Errors.SCENEMANAGER_ALREADY_LOADED,{"scene":resource_name,"overlay":"menu"})
logger.error("CORE/smgr.gd",err["error"])
return err["code"]
if !menu_overlay_scene.has(resource_name) and menu_overlay.get_children().has(resource):
core.exception("SceneManager","menu_overlay_scene does not contain \"" + resource_name + "\" but menu_overlay does")
return core.Errors.CORE_EXCEPTION
if menu_overlay_scene.has(resource_name) and !menu_overlay.get_children().has(resource):
core.exception("SceneManager","menu_overlay does not contain \"" + resource_name + "\" but menu_overlay_scene does")
return core.Errors.CORE_EXCEPTION
menu_overlay.add_child(resource)
menu_overlay_scene.merge({resource_name:resource},true)
return core.Errors.OK
# Adds a cutscene scene
func add_cutscene(resource_name:String) -> int:
if core.protection_mode: return core.Errors.CORE_PROTECTIONMODE
logger.diag("CORE/smgr.gd","Adding cutscene scene \"" + resource_name + "\"")
# Load resource into memory
var resource: Object = resourcemanager.getres(resource_name)
if resource == null or resource.get_class() != "PackedScene":
# If resource is null (not loaded) or is not a PackedScene, throw an error
var err = core.error("SceneManager",core.Errors.SCENEMANAGER_NOT_PACKEDSCENE,{"scene":resource_name,"overlay":"cutscene"})
logger.error("CORE/smgr.gd",err["error"])
return err["code"]
if cutscene_overlay_scene == resource_name and cutscene_overlay.get_children().has(resource):
# If resource is null (not loaded) or is not a PackedScene, throw an error
var err = core.error("SceneManager",core.Errors.SCENEMANAGER_ALREADY_LOADED,{"scene":resource_name,"overlay":"cutscene"})
logger.error("CORE/smgr.gd",err["error"])
return err["code"]
if cutscene_overlay_scene != resource_name and cutscene_overlay.get_children().has(resource):
core.exception("SceneManager","cutscene_overlay_scene does not contain \"" + resource_name + "\" but cutscene_overlay does")
return core.Errors.CORE_EXCEPTION
if cutscene_overlay_scene == resource_name and !cutscene_overlay.get_children().has(resource):
core.exception("SceneManager","cutscene_overlay does not contain \"" + resource_name + "\" but cutscene_overlay_scene does")
return core.Errors.CORE_EXCEPTION
cutscene_overlay.add_child(resource)
cutscene_overlay_scene = resource_name
return core.Errors.OK
# Adds a action scene
func add_action(resource_name:String) -> int:
if core.protection_mode: return core.Errors.CORE_PROTECTIONMODE
logger.diag("CORE/smgr.gd","Adding action scene \"" + resource_name + "\"")
# Load resource into memory
var resource = resourcemanager.getres(resource_name)
if resource == null or typeof(resource) != TYPE_OBJECT or resource.get_class() != "PackedScene":
# If resource is null (not loaded) or is not a PackedScene, throw an error
var err = core.error("SceneManager",core.Errors.SCENEMANAGER_NOT_PACKEDSCENE,{"scene":resource_name,"overlay":"action"})
logger.error("CORE/smgr.gd",err["error"])
return err["code"]
if action_overlay_scene == resource_name and action_overlay.get_children().has(resource):
# If resource is null (not loaded) or is not a PackedScene, throw an error
var err = core.error("SceneManager",core.Errors.SCENEMANAGER_ALREADY_LOADED,{"scene":resource_name,"overlay":"action"})
logger.error("CORE/smgr.gd",err["error"])
return err["code"]
if action_overlay_scene != resource_name and action_overlay.get_children().has(resource):
core.exception("SceneManager","action_overlay_scene does not contain \"" + resource_name + "\" but action_overlay does")
return core.Errors.CORE_EXCEPTION
if action_overlay_scene == resource_name and !action_overlay.get_children().has(resource):
core.exception("SceneManager","action_overlay does not contain \"" + resource_name + "\" but action_overlay_scene does")
return core.Errors.CORE_EXCEPTION
action_overlay.add_child(resource)
action_overlay_scene = resource_name
return core.Errors.OK