CORE/resmgr.gd

95 lines
3.4 KiB
GDScript3
Raw Normal View History

######################################
# 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]