2023-08-25 14:34:57 +02:00
|
|
|
######################################
|
|
|
|
# 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) #
|
|
|
|
######################################
|
2023-05-29 20:46:47 +02:00
|
|
|
extends Node
|
|
|
|
|
2023-08-25 14:34:57 +02:00
|
|
|
# CORE modules
|
|
|
|
var core: Node = null
|
|
|
|
var logger: Node = null
|
2023-05-29 20:46:47 +02:00
|
|
|
|
2023-08-25 14:34:57 +02:00
|
|
|
# Loaded resources list
|
|
|
|
var resources: Dictionary = {}
|
2023-08-07 14:49:12 +02:00
|
|
|
|
2023-08-25 14:34:57 +02:00
|
|
|
# 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):
|
2023-08-07 14:49:12 +02:00
|
|
|
if replace:
|
2023-08-25 14:34:57 +02:00
|
|
|
# If resource is already loaded and replace is true, remove it first (and then add it)
|
|
|
|
resources.erase(resource_name)
|
2023-08-07 14:49:12 +02:00
|
|
|
else:
|
2023-08-25 14:34:57 +02:00
|
|
|
# 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
|
2023-05-29 20:46:47 +02:00
|
|
|
if batch == {}:
|
2023-08-25 14:34:57 +02:00
|
|
|
# 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"]
|
2023-05-29 20:46:47 +02:00
|
|
|
for i in batch:
|
2023-08-25 14:34:57 +02:00
|
|
|
# Call loadres() method for every item in batch
|
2023-07-07 22:30:19 +02:00
|
|
|
loadres(i,batch[i],replace)
|
2023-08-25 14:34:57 +02:00
|
|
|
return core.Errors.OK
|
2023-05-29 20:46:47 +02:00
|
|
|
|
2023-08-25 14:34:57 +02:00
|
|
|
# Unloads a batch of resources
|
|
|
|
func unloadbatch(batch:Array) -> int:
|
|
|
|
if core.protection_mode: return 9223372036854775807
|
2023-05-29 20:46:47 +02:00
|
|
|
if batch == []:
|
2023-08-25 14:34:57 +02:00
|
|
|
# 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"]
|
2023-05-29 20:46:47 +02:00
|
|
|
for i in batch:
|
2023-08-25 14:34:57 +02:00
|
|
|
# Call unloadres() method for every item in batch
|
2023-05-29 20:46:47 +02:00
|
|
|
unloadres(i)
|
2023-08-25 14:34:57 +02:00
|
|
|
return core.Errors.OK
|
2023-05-29 20:46:47 +02:00
|
|
|
|
2023-08-25 14:34:57 +02:00
|
|
|
# Returns a loaded resource
|
|
|
|
# No return type is set because it can return literally everything.
|
|
|
|
func getres(resource_name:String):
|
2023-08-07 14:49:12 +02:00
|
|
|
if core.protection_mode: return null
|
2023-08-25 14:34:57 +02:00
|
|
|
if !resources.has(resource_name):
|
|
|
|
# Return null if resource is not loaded
|
2023-05-29 20:46:47 +02:00
|
|
|
return null
|
2023-08-25 14:34:57 +02:00
|
|
|
# Return the content
|
|
|
|
return resources[resource_name]
|