Added events and resmgr API + improved startup

This commit is contained in:
JeremyStar™ 2023-05-29 20:46:47 +02:00
parent fc04436787
commit 2e23c39bcd
6 changed files with 139 additions and 10 deletions

View file

@ -6,6 +6,9 @@
# Licensed under GNU GPLv3 # Licensed under GNU GPLv3
extends Node extends Node
@onready
var core = get_node(NodePath("/root/core"))
func singleton(singleton_name:String,singleton_path:String) -> void: func singleton(singleton_name:String,singleton_path:String) -> void:
var node = Control.new() var node = Control.new()
node.name = singleton_name node.name = singleton_name
@ -23,3 +26,6 @@ func preset(plugin_id:String) -> bool:
return false return false
get_tree().root.add_child(node) get_tree().root.add_child(node)
return true return true
func _ready() -> void:
core.setready()

15
core.gd
View file

@ -8,10 +8,14 @@ extends Node
const version = "git-develop" const version = "git-develop"
var locked = false var locked = false
var readycount = 0
var readylized = false # Fun Fact: "ready" is a signal from "Node" and I (JeremyStarTM) just added "lized" from "initialized" to it to avoid the error thrown by Godot
var config = null var config = null
var wmgr = null var wmgr = null
var smgr = null var smgr = null
var resmgr = null
var autoload = null var autoload = null
var events = null
func attach(type:String,component:Script) -> void: func attach(type:String,component:Script) -> void:
if locked: if locked:
@ -30,15 +34,26 @@ func attach(type:String,component:Script) -> void:
"smgr": "smgr":
add_child(comp) add_child(comp)
smgr = comp smgr = comp
"resmgr":
add_child(comp)
resmgr = comp
"autoload": "autoload":
add_child(comp) add_child(comp)
autoload = comp autoload = comp
"events":
add_child(comp)
events = comp
_: _:
Logger.error("core","Failed attaching " + type + " to CORE: Invalid component") Logger.error("core","Failed attaching " + type + " to CORE: Invalid component")
comp.free() comp.free()
return return
Logger.diag("core","Attached " + type + " successfully") Logger.diag("core","Attached " + type + " successfully")
func setready() -> void:
readycount = readycount+1
if readycount == 4:
readylized = true
func lock() -> void: func lock() -> void:
locked = true locked = true
Logger.diag("core","CORE is now locked. No new attachments can be made.") Logger.diag("core","CORE is now locked. No new attachments can be made.")

View file

@ -15,7 +15,9 @@ func _ready() -> void:
var scr_core = ResourceLoader.load("res://CORE/core.gd") var scr_core = ResourceLoader.load("res://CORE/core.gd")
var scr_wmgr = ResourceLoader.load("res://CORE/wmgr.gd") var scr_wmgr = ResourceLoader.load("res://CORE/wmgr.gd")
var scr_smgr = ResourceLoader.load("res://CORE/smgr.gd") var scr_smgr = ResourceLoader.load("res://CORE/smgr.gd")
var scr_resmgr = ResourceLoader.load("res://CORE/resmgr.gd")
var scr_autoload = ResourceLoader.load("res://CORE/autoload.gd") var scr_autoload = ResourceLoader.load("res://CORE/autoload.gd")
var scr_events = ResourceLoader.load("res://CORE/events.gd")
Logger.info("coreloader","Constructing CORE") Logger.info("coreloader","Constructing CORE")
var core = Control.new() var core = Control.new()
core.name = "core" core.name = "core"
@ -30,19 +32,17 @@ func _ready() -> void:
core.attach("wmgr",scr_wmgr) core.attach("wmgr",scr_wmgr)
Logger.info("coreloader","Attaching smgr to CORE") Logger.info("coreloader","Attaching smgr to CORE")
core.attach("smgr",scr_smgr) core.attach("smgr",scr_smgr)
Logger.info("coreloader","Attaching resmgr to CORE")
core.attach("resmgr",scr_resmgr)
Logger.info("coreloader","Attaching autoload to CORE") Logger.info("coreloader","Attaching autoload to CORE")
core.attach("autoload",scr_autoload) core.attach("autoload",scr_autoload)
Logger.info("coreloader","Attaching events to CORE")
core.attach("events",scr_events)
Logger.info("coreloader","Locking CORE") Logger.info("coreloader","Locking CORE")
core.lock() core.lock()
Logger.info("coreloader","Waiting for CORE to fully initialize") Logger.info("coreloader","Waiting for CORE to fully initialize")
while !core.readylized:
await get_tree().create_timer(1).timeout await get_tree().create_timer(1).timeout
Logger.info("coreloader","Checking CORE availability")
if !core.wmgr.available:
Logger.error("coreloader","CORE wmgr is not available.<nl>Please check the log for any errors during initialization.")
return
if !core.smgr.available:
Logger.error("coreloader","CORE smgr is not available.<nl>Please check the log for any errors during initialization.")
Logger.info("coreloader","CORE is available.")
Logger.info("coreloader","CORE has been initialized and is ready.") Logger.info("coreloader","CORE has been initialized and is ready.")
Logger.info("coreloader","Preloading project init script") Logger.info("coreloader","Preloading project init script")
var initscr = ResourceLoader.load(core.config.core_startscript) var initscr = ResourceLoader.load(core.config.core_startscript)

63
events.gd Normal file
View file

@ -0,0 +1,63 @@
extends Node
@onready
var core = get_node(NodePath("/root/core"))
var window_title_prev = ""
var window_size_x_prev = 0
var window_size_y_prev = 0
var window_pos_x_prev = 0
var window_pos_y_prev = 0
signal window_title
signal window_size
signal window_pos
signal logger_all
signal logger_diag
signal logger_info
signal logger_warn
signal logger_error
func _ready() -> void:
Logger.connect("logevent",Callable(self,"logger_update"))
core.setready()
func logger_update(type:String,script:String,message:String,_logmessage:String) -> void:
emit_signal("logger_all",type,script,message)
if type == "DIAG":
emit_signal("logger_diag",script,message)
elif type == "INFO":
emit_signal("logger_info",script,message)
elif type == "WARN":
emit_signal("logger_warn",script,message)
elif type == "ERROR":
emit_signal("logger_error",script,message)
func _process(_delta:float) -> void:
var window_title_var = core.wmgr.get_title()
var window_size_x = core.wmgr.get_size().x
var window_size_y = core.wmgr.get_size().y
var window_pos_x = core.wmgr.get_position().x
var window_pos_y = core.wmgr.get_position().y
if window_title_prev != window_title_var:
emit_signal("window_title",window_title_var,window_title_prev)
window_title_prev = window_title_var
if window_size_x != window_size_x_prev and window_size_y != window_size_y_prev:
emit_signal("window_size","BOTH",Vector2i(window_size_x,window_size_y),Vector2i(window_size_x_prev,window_size_y_prev))
window_size_x_prev = window_size_x
window_size_y_prev = window_size_y
elif window_size_x != window_size_x_prev:
emit_signal("window_size","X",Vector2i(window_size_x,window_size_y),Vector2i(window_size_x_prev,window_size_y_prev))
window_size_x_prev = window_size_x
elif window_size_y != window_size_y_prev:
emit_signal("window_size","Y",Vector2i(window_size_x,window_size_y),Vector2i(window_size_x_prev,window_size_y_prev))
window_size_y_prev = window_size_y
if window_pos_x != window_pos_x_prev and window_pos_y != window_pos_y_prev:
emit_signal("window_pos","BOTH",Vector2i(window_pos_x,window_pos_y),Vector2i(window_pos_x_prev,window_pos_y_prev))
window_pos_x_prev = window_pos_x
window_pos_y_prev = window_pos_y
elif window_pos_x != window_pos_x_prev:
emit_signal("window_pos","X",Vector2i(window_pos_x,window_pos_y),Vector2i(window_pos_x_prev,window_pos_y_prev))
window_pos_x_prev = window_pos_x
elif window_pos_y != window_pos_y_prev:
emit_signal("window_pos","Y",Vector2i(window_pos_x,window_pos_y),Vector2i(window_pos_x_prev,window_pos_y_prev))
window_pos_y_prev = window_pos_y

46
resmgr.gd Normal file
View file

@ -0,0 +1,46 @@
# resmgr.gd
# Resource Manager
#
# This file is part of StarOpenSource CORE (SOSCORE)
# Made by the StarOpenSource Project and Contributers
# Licensed under GNU GPLv3
extends Node
@onready
var core = get_node(NodePath("/root/core"))
var resources = {}
func loadres(resname:String,respath:String,replace:bool = false) -> void:
if resources.has(resname):
if replace:
resources.erase(resname)
else:
Logger.error("resmgr","Loading \"" + resname + "\" failed: Resource is loaded already")
return
resources.merge({resname:ResourceLoader.load(respath)})
func unloadres(resname:String) -> void:
if !resources.has(resname):
Logger.error("resmgr","Unloading \"" + resname + "\" failed: Resource is not present")
return
resources.erase(resname)
func loadbatch(batch:Dictionary) -> void:
if batch == {}:
Logger.error("resmgr","Loading a batch failed: Batch is empty")
for i in batch:
loadres(i,batch[i])
func unloadbatch(batch:Array) -> void:
if batch == []:
Logger.error("resmgr","Unloading a batch failed: Batch is empty")
for i in batch:
unloadres(i)
func getres(resname:String):
if !resources.has(resname):
return null
return resources[resname]
func _ready() -> void:
core.setready()

View file

@ -6,7 +6,6 @@
# Licensed under GNU GPLv3 # Licensed under GNU GPLv3
extends Node extends Node
var available = false
@onready @onready
var core = get_node(NodePath("/root/core")) var core = get_node(NodePath("/root/core"))
@onready @onready
@ -70,4 +69,4 @@ func _ready() -> void:
DisplayServer.window_set_min_size(Vector2i(960,540)) DisplayServer.window_set_min_size(Vector2i(960,540))
DisplayServer.window_set_title(window_title) DisplayServer.window_set_title(window_title)
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED) DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
available = true core.setready()