Skip to content

Commit 01a2fbe

Browse files
ithinkandicodeKANAjetzt
authored andcommitted
Config JSON Tweaks (GodotModding#108)
* `get_mod_config` - fix missing space in log string * `get_mod_config` - log improvements - Generally improve log messages, giving them better user notices that are more accurate to the current issue. - Use assert for cases where there are definite code errors that need addressing (eg. an invalid mod ID) * `get_mod_config` - use enums for the error codes * `get_mod_config` - rename "error" (eg. "error code") to "status" * `get_mod_config` - remove a redundant bit of text
1 parent d420d3c commit 01a2fbe

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

addons/mod_loader/mod_loader.gd

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -561,64 +561,75 @@ func save_scene(modified_scene: Node, scene_path: String) -> void:
561561
_saved_objects.append(packed_scene)
562562

563563

564+
enum MLConfigStatus {
565+
OK, # 0 = No errors
566+
INVALID_MOD_ID, # 1 = Invalid mod ID
567+
NO_JSON_OK, # 2 = No custom JSON. File probably does not exist. Defaults will be used if available
568+
NO_JSON_INVALID_KEY, # 3 = No custom JSON, and key was invalid when trying to get the default from your manifest defaults (`extra.godot.config_defaults`)
569+
INVALID_KEY # 4 = Invalid key, although config data does exists
570+
}
571+
564572
# Get the config data for a specific mod. Always returns a dictionary with two
565573
# keys: `error` and `data`.
566574
# Data (`data`) is either the full config, or data from a specific key if one was specified.
567575
# Error (`error`) is 0 if there were no errors, or > 0 if the setting could not be retrieved:
568-
# 0 = No errors
569-
# 1 = Invalid mod ID
570-
# 2 = No custom JSON. File probably does not exist. Defaults will be used if available
571-
# 3 = No custom JSON, and key was invalid when trying to get the default from your manifest defaults (`extra.godot.config_defaults`)
572-
# 4 = Invalid key, although config data does exists
573576
func get_mod_config(mod_dir_name: String = "", key: String = "") -> Dictionary:
574-
var error_num := 0
575-
var error_msg := ""
577+
var status_code = MLConfigStatus.OK
578+
var status_msg := ""
576579
var data = {} # can be anything
577580
var defaults := {}
578581

579582
# Invalid mod ID
580583
if not mod_data.has(mod_dir_name):
581-
error_num = 1
582-
error_msg = "ERROR - Mod ID was invalid: %s" % mod_dir_name
584+
status_code = MLConfigStatus.INVALID_MOD_ID
585+
status_msg = "Mod ID was invalid: %s" % mod_dir_name
583586

584587
# Mod ID is valid
585-
if error_num == 0:
588+
if status_code == MLConfigStatus.OK:
586589
var mod := mod_data[mod_dir_name] as ModData
587590
var config_data := mod.config
588591
defaults = mod.manifest.config_defaults
589592

590593
# No custom JSON file
591-
if config_data.size() == 0:
592-
error_num = 2
593-
error_msg = "WARNING - No config file for %s.json." % mod_dir_name
594+
if config_data.size() == MLConfigStatus.OK:
595+
status_code = MLConfigStatus.NO_JSON_OK
596+
var noconfig_msg = "No config file for %s.json. " % mod_dir_name
594597
if key == "":
595598
data = defaults
596-
error_msg += "Using defaults (extra.godot.config_defaults)"
599+
status_msg += str(noconfig_msg, "Using defaults (extra.godot.config_defaults)")
597600
else:
598601
if defaults.has(key):
599602
data = defaults[key]
600-
error_msg += "Using defaults for key '%s' (extra.godot.config_defaults.%s)" % [key, key]
603+
status_msg += str(noconfig_msg, "Using defaults for key '%s' (extra.godot.config_defaults.%s)" % [key, key])
601604
else:
602-
error_num = 3
603-
error_msg += "Requested key '%s' is not present in the defaults (extra.godot.config_defaults.%s)" % [key, key]
605+
status_code = MLConfigStatus.NO_JSON_INVALID_KEY
606+
status_msg += str(
607+
"Could not get the requested data for %s: " % mod_dir_name,
608+
"Requested key '%s' is not present in the 'config_defaults' of the mod's manifest.json file (extra.godot.config_defaults.%s). " % [key, key]
609+
)
604610

605611
# JSON file exists
606-
if error_num == 0:
612+
if status_code == MLConfigStatus.OK:
607613
if key == "":
608614
data = config_data
609615
else:
610616
if config_data.has(key):
611617
data = config_data[key]
612618
else:
613-
error_num = 4
614-
error_msg = "WARNING - Invalid key '%s' for mod ID: %s" % [key, mod_dir_name]
619+
status_code = MLConfigStatus.INVALID_KEY
620+
status_msg = "Invalid key '%s' for mod ID: %s" % [key, mod_dir_name]
615621

616622
# Log if any errors occured
617-
if not error_num == 0:
618-
ModLoaderUtils.log_debug("Config Error: %s" % error_msg, mod_dir_name)
623+
if not status_code == MLConfigStatus.OK:
624+
if status_code == MLConfigStatus.NO_JSON_OK:
625+
# No user config file exists. Low importance as very likely to trigger
626+
ModLoaderUtils.log_debug("Config JSON Notice: %s" % status_msg, mod_dir_name)
627+
else:
628+
# Code error (eg. invalid mod ID)
629+
ModLoaderUtils.log_fatal("Config JSON Error (%s): %s" % [status_code, status_msg], mod_dir_name)
619630

620631
return {
621-
"error": error_num,
622-
"error_msg": error_msg,
632+
"status_code": status_code,
633+
"status_msg": status_msg,
623634
"data": data,
624635
}

0 commit comments

Comments
 (0)