Godot-RTS-Template/assetloader/ModManifest.gd

70 lines
2.2 KiB
GDScript3
Raw Normal View History

2025-08-01 16:02:47 +00:00
extends RefCounted
class_name ModManifest
## Object for easier json manifest operations.
#region declaration
var id: StringName ## Mod unique id. Internal usage only.
var name: String ## Displayed mod name
var version: String ## Displayed mod version
var desc: String ## Displayed mod description
var author:String ## Mod Author
var dependencies := [] ## Mod dependecies, optional.
var tags:= [] ## ["units", "vehicles", "buildings", "textures", "maps", "quests"]
var packs := [] ## Optional. [br] By default [param mod_id.pck] is loaded.
var _validated = false ## true if the manifest as a correct syntax
#endregion
# === Init === # Initialization logic, if needed
func _init(_id:String,_name:=_id,_desc:="",_dependencies:=[],_packs:=[],_author := "",_version:="1.0",_tags:=[]) -> void:
if id == "":
push_warning("Mod id cannot be null, fallback to "+_name)
self.id = _name.strip_edges()
else:
self.id = _id
self.name= _name
self.version = _version
self.desc=_desc
self.author = _author
self.dependencies = _dependencies
self.tags= _tags
self.packs =_packs
# === PUBLIC FUNCTIONS ===
func is_valid() -> bool:
return _validated
## Return [param null] if the json is not parsed correctly
static func new_from_file(_manifest_file:FileAccess) -> ModManifest:
var man_dic:Dictionary = JSON.parse_string(_manifest_file.get_as_text())
if !man_dic:
push_warning("Invalid manifest format: %s" % _manifest_file.get_path())
return null
var _id: String
if man_dic["id"]:
_id = man_dic["id"]
else:
_id = str(_manifest_file).strip_edges() # ugly. But should fallback on a generated one.
return ModManifest.new(_id, man_dic["name"],man_dic["desc"],man_dic["dependencies"],man_dic["packs"],man_dic["author"],man_dic["version"],man_dic["tags"])
## Check if the array is empty, if not, return the packs and the main one if it's not on the list.
func get_mod_packs() -> Array[String]:
if packs.size() > 0:
# check if the main pack is in the list
if packs.has(self.id):
return self.packs
else:
var pck = self.packs
pck.append(id)
return pck
return [id]
# === PRIVATE FUNCTIONS === # use _underscore() to make the difference between private and public functions
# ====================