Skip to content

Commit

Permalink
refactor: ♻️ split off dep and inc validation function (#170)
Browse files Browse the repository at this point in the history
* refactor: ♻️ split off dep and inc validation function

splited `validate_dependencies_and_incompatibilities()` in to `validate_dependencies()`, `validate_incompatibilities()` and `is_mod_id_array_valid()`. Will be used in the ModTool to validate the fields separately.

* style: 🎨 added missing space after `,`

* fix: 💬 Added array description to self assignment error

Added *mod_id_array_description* to log_fatal() in `is_mod_id_array_valid()`

* refactor: 🔥 Remove break in `is_mod_id_array_valid()` loop

Removed the `break` in the mod id check, so all invalid values get logged at once and the user doesn't have to fix one error to get the next one.
  • Loading branch information
KANAjetzt authored Mar 5, 2023
1 parent 6f9068b commit 547c437
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions addons/mod_loader/classes/mod_manifest.gd
Original file line number Diff line number Diff line change
Expand Up @@ -207,27 +207,42 @@ static func is_semver_valid(check_version_number: String, is_silent := false) ->


static func validate_dependencies_and_incompatibilities(mod_id: String, dependencies: PoolStringArray, incompatibilities: PoolStringArray, is_silent := false) -> bool:
var valid_dep = true
var valid_inc = true

if dependencies.size() > 0:
for dep in dependencies:
if dep == mod_id:
ModLoaderUtils.log_fatal("The mod \"%s\" lists itself as a dependency in its own manifest.json file" % mod_id, LOG_NAME)
valid_dep = is_mod_id_valid(mod_id, dep, "dependency", is_silent)

if incompatibilities.size() > 0:
for inc in incompatibilities:
if inc == mod_id:
ModLoaderUtils.log_fatal("The mod \"%s\" lists itself as an incompatible mod in its own manifest.json file" % mod_id, LOG_NAME)
valid_inc = is_mod_id_valid(mod_id, inc, "incompatibility", is_silent)

if not valid_dep or not valid_inc:
var valid_dependencies := validate_dependencies(mod_id, dependencies, is_silent)
var valid_incompatibilities := validate_incompatibilities(mod_id, incompatibilities, is_silent)

if not valid_dependencies or not valid_incompatibilities:
return false

return true


static func validate_dependencies(mod_id: String, dependencies: PoolStringArray, is_silent := false) -> bool:
return is_mod_id_array_valid(mod_id, dependencies, "dependency", is_silent)


static func validate_incompatibilities(mod_id: String, incompatibilities: PoolStringArray, is_silent := false) -> bool:
return is_mod_id_array_valid(mod_id, incompatibilities, "incompatibility", is_silent)


static func is_mod_id_array_valid(own_mod_id: String, mod_id_array: PoolStringArray, mod_id_array_description: String, is_silent := false) -> bool:
var is_valid := true

# If there are mod ids
if mod_id_array.size() > 0:
for mod_id in mod_id_array:
# Check if mod id is the same as the mods mod id.
if mod_id == own_mod_id:
is_valid = false
if not is_silent:
ModLoaderUtils.log_fatal("The mod \"%s\" lists itself as \"%s\" in its own manifest.json file" % [mod_id, mod_id_array_description], LOG_NAME)

# Check if the mod id is a valid mod id.
if not is_mod_id_valid(own_mod_id, mod_id, mod_id_array_description, is_silent):
is_valid = false

return is_valid


static func is_mod_id_valid(original_mod_id: String, check_mod_id: String, type := "", is_silent := false) -> bool:
var intro_text = "A %s for the mod '%s' is invalid: " % [type, original_mod_id] if not type == "" else ""

Expand Down

0 comments on commit 547c437

Please sign in to comment.