Skip to content

Commit 52104d5

Browse files
Config JSON Tweaks (#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 e984b57 commit 52104d5

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
@@ -536,64 +536,75 @@ func save_scene(modified_scene: Node, scene_path: String) -> void:
536536
_saved_objects.append(packed_scene)
537537

538538

539+
enum MLConfigStatus {
540+
OK, # 0 = No errors
541+
INVALID_MOD_ID, # 1 = Invalid mod ID
542+
NO_JSON_OK, # 2 = No custom JSON. File probably does not exist. Defaults will be used if available
543+
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`)
544+
INVALID_KEY # 4 = Invalid key, although config data does exists
545+
}
546+
539547
# Get the config data for a specific mod. Always returns a dictionary with two
540548
# keys: `error` and `data`.
541549
# Data (`data`) is either the full config, or data from a specific key if one was specified.
542550
# Error (`error`) is 0 if there were no errors, or > 0 if the setting could not be retrieved:
543-
# 0 = No errors
544-
# 1 = Invalid mod ID
545-
# 2 = No custom JSON. File probably does not exist. Defaults will be used if available
546-
# 3 = No custom JSON, and key was invalid when trying to get the default from your manifest defaults (`extra.godot.config_defaults`)
547-
# 4 = Invalid key, although config data does exists
548551
func get_mod_config(mod_dir_name: String = "", key: String = "") -> Dictionary:
549-
var error_num := 0
550-
var error_msg := ""
552+
var status_code = MLConfigStatus.OK
553+
var status_msg := ""
551554
var data = {} # can be anything
552555
var defaults := {}
553556

554557
# Invalid mod ID
555558
if not mod_data.has(mod_dir_name):
556-
error_num = 1
557-
error_msg = "ERROR - Mod ID was invalid: %s" % mod_dir_name
559+
status_code = MLConfigStatus.INVALID_MOD_ID
560+
status_msg = "Mod ID was invalid: %s" % mod_dir_name
558561

559562
# Mod ID is valid
560-
if error_num == 0:
563+
if status_code == MLConfigStatus.OK:
561564
var mod := mod_data[mod_dir_name] as ModData
562565
var config_data := mod.config
563566
defaults = mod.manifest.config_defaults
564567

565568
# No custom JSON file
566-
if config_data.size() == 0:
567-
error_num = 2
568-
error_msg = "WARNING - No config file for %s.json." % mod_dir_name
569+
if config_data.size() == MLConfigStatus.OK:
570+
status_code = MLConfigStatus.NO_JSON_OK
571+
var noconfig_msg = "No config file for %s.json. " % mod_dir_name
569572
if key == "":
570573
data = defaults
571-
error_msg += "Using defaults (extra.godot.config_defaults)"
574+
status_msg += str(noconfig_msg, "Using defaults (extra.godot.config_defaults)")
572575
else:
573576
if defaults.has(key):
574577
data = defaults[key]
575-
error_msg += "Using defaults for key '%s' (extra.godot.config_defaults.%s)" % [key, key]
578+
status_msg += str(noconfig_msg, "Using defaults for key '%s' (extra.godot.config_defaults.%s)" % [key, key])
576579
else:
577-
error_num = 3
578-
error_msg += "Requested key '%s' is not present in the defaults (extra.godot.config_defaults.%s)" % [key, key]
580+
status_code = MLConfigStatus.NO_JSON_INVALID_KEY
581+
status_msg += str(
582+
"Could not get the requested data for %s: " % mod_dir_name,
583+
"Requested key '%s' is not present in the 'config_defaults' of the mod's manifest.json file (extra.godot.config_defaults.%s). " % [key, key]
584+
)
579585

580586
# JSON file exists
581-
if error_num == 0:
587+
if status_code == MLConfigStatus.OK:
582588
if key == "":
583589
data = config_data
584590
else:
585591
if config_data.has(key):
586592
data = config_data[key]
587593
else:
588-
error_num = 4
589-
error_msg = "WARNING - Invalid key '%s' for mod ID: %s" % [key, mod_dir_name]
594+
status_code = MLConfigStatus.INVALID_KEY
595+
status_msg = "Invalid key '%s' for mod ID: %s" % [key, mod_dir_name]
590596

591597
# Log if any errors occured
592-
if not error_num == 0:
593-
ModLoaderUtils.log_debug("Config Error: %s" % error_msg, mod_dir_name)
598+
if not status_code == MLConfigStatus.OK:
599+
if status_code == MLConfigStatus.NO_JSON_OK:
600+
# No user config file exists. Low importance as very likely to trigger
601+
ModLoaderUtils.log_debug("Config JSON Notice: %s" % status_msg, mod_dir_name)
602+
else:
603+
# Code error (eg. invalid mod ID)
604+
ModLoaderUtils.log_fatal("Config JSON Error (%s): %s" % [status_code, status_msg], mod_dir_name)
594605

595606
return {
596-
"error": error_num,
597-
"error_msg": error_msg,
607+
"status_code": status_code,
608+
"status_msg": status_msg,
598609
"data": data,
599610
}

0 commit comments

Comments
 (0)