CORE/resmgr.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

94 lines
3.4 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
# Loaded resources list
var resources: Dictionary = {}
# Config
var config_load_invalid_file_as_null: bool = false
# Loads a resource
func loadres(resource_name:String,resource_path:String,replace:bool = false) -> int:
if core.protection_mode: return 9223372036854775807
logger.diag("CORE/resmgr.gd","Loading resource \"" + resource_name + "\" from path \"" + resource_path + "\" (replace=" + str(replace) + ")")
if resources.has(resource_name):
if replace:
# If resource is already loaded and replace is true, remove it first (and then add it)
resources.erase(resource_name)
else:
# If resource is already loaded and replace is false, throw an error
var err = core.error("ResourceManager",core.Errors.RESOURCEMANAGER_ALREADY_EXISTS,{"name":resource_name})
logger.error("CORE/resmgr.gd",err["error"])
return err["code"]
# Check if resource exists
if !FileAccess.file_exists(resource_path):
if config_load_invalid_file_as_null:
resources.merge({resource_name:null})
var err = core.error("ResourceManager",core.Errors.RESOURCEMANAGER_INVALID_FILEPATH,{"filepath":resource_path})
logger.error("CORE/resmgr.gd",err["error"])
return err["code"]
# Load the resource
resources.merge({resource_name:ResourceLoader.load(resource_path)})
return core.Errors.OK
# Unloads a resource
func unloadres(resource_name:String) -> int:
if core.protection_mode: return 9223372036854775807
if !resources.has(resource_name):
# If resource is not loaded, throw an error
var err = core.error("ResourceManager",core.Errors.RESOURCEMANAGER_RESOURCE_MISSING,{"name":resource_name})
logger.error("CORE/resmgr.gd",err["error"])
return err["code"]
# Unload the resource
resources.erase(resource_name)
return core.Errors.OK
# Loads a batch of resources
func loadbatch(batch:Dictionary,replace:bool = false) -> int:
if core.protection_mode: return 9223372036854775807
if batch == {}:
# If batch is empty, throw an error
var err = core.error("ResourceManager",core.Errors.RESOURCEMANAGER_BATCH_EMPTY)
logger.error("CORE/resmgr.gd",err["error"])
return err["code"]
for i in batch:
# Call loadres() method for every item in batch
loadres(i,batch[i],replace)
return core.Errors.OK
# Unloads a batch of resources
func unloadbatch(batch:Array) -> int:
if core.protection_mode: return 9223372036854775807
if batch == []:
# If batch is empty, throw an error
var err = core.error("ResourceManager",core.Errors.RESOURCEMANAGER_BATCH_EMPTY)
logger.error("CORE/resmgr.gd",err["error"])
return err["code"]
for i in batch:
# Call unloadres() method for every item in batch
unloadres(i)
return core.Errors.OK
# Returns a loaded resource
# No return type is set because it can return literally everything.
func getres(resource_name:String):
if core.protection_mode: return null
if !resources.has(resource_name):
# Return null if resource is not loaded
return null
# Return the content
return resources[resource_name]