Godot-RTS-Template/assetloader/ModManifest.gd

78 lines
2.4 KiB
GDScript3
Raw Permalink 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
2025-08-01 22:38:02 +00:00
var path:StringName ## set if the mode is loaded
2025-08-01 16:02:47 +00:00
#endregion
# === Init === # Initialization logic, if needed
func _init(_id:String,_name:=_id,_desc:="",_dependencies:=[],_packs:=[],_author := "",_version:="1.0",_tags:=[]) -> void:
2025-08-01 22:38:02 +00:00
if _id == "":
2025-08-01 16:02:47 +00:00
push_warning("Mod id cannot be null, fallback to "+_name)
2025-08-01 22:38:02 +00:00
self.id = StringName(_name.strip_edges())
2025-08-01 16:02:47 +00:00
else:
2025-08-01 22:38:02 +00:00
self.id = StringName(_id)
2025-08-01 16:02:47 +00:00
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
2025-08-01 22:38:02 +00:00
_id = man_dic.get("id",_manifest_file.get_path().get_file().get_basename().strip_edges()) # ugly. But should fallback on a generated one.
return ModManifest.new(
_id,
man_dic.get("name", _id),
man_dic.get("desc", ""),
man_dic.get("dependencies", []),
man_dic.get("packs", []),
man_dic.get("author", ""),
str(man_dic.get("version", "1.0")),
man_dic.get("tags", [])
)
2025-08-01 16:02:47 +00:00
## 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
# ====================