Skip to content

Commit

Permalink
Config JSON Tweaks (GodotModding#108)
Browse files Browse the repository at this point in the history
* `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
  • Loading branch information
ithinkandicode authored and KANAjetzt committed Feb 23, 2023
1 parent d420d3c commit 01a2fbe
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions addons/mod_loader/mod_loader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -561,64 +561,75 @@ func save_scene(modified_scene: Node, scene_path: String) -> void:
_saved_objects.append(packed_scene)


enum MLConfigStatus {
OK, # 0 = No errors
INVALID_MOD_ID, # 1 = Invalid mod ID
NO_JSON_OK, # 2 = No custom JSON. File probably does not exist. Defaults will be used if available
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`)
INVALID_KEY # 4 = Invalid key, although config data does exists
}

# Get the config data for a specific mod. Always returns a dictionary with two
# keys: `error` and `data`.
# Data (`data`) is either the full config, or data from a specific key if one was specified.
# Error (`error`) is 0 if there were no errors, or > 0 if the setting could not be retrieved:
# 0 = No errors
# 1 = Invalid mod ID
# 2 = No custom JSON. File probably does not exist. Defaults will be used if available
# 3 = No custom JSON, and key was invalid when trying to get the default from your manifest defaults (`extra.godot.config_defaults`)
# 4 = Invalid key, although config data does exists
func get_mod_config(mod_dir_name: String = "", key: String = "") -> Dictionary:
var error_num := 0
var error_msg := ""
var status_code = MLConfigStatus.OK
var status_msg := ""
var data = {} # can be anything
var defaults := {}

# Invalid mod ID
if not mod_data.has(mod_dir_name):
error_num = 1
error_msg = "ERROR - Mod ID was invalid: %s" % mod_dir_name
status_code = MLConfigStatus.INVALID_MOD_ID
status_msg = "Mod ID was invalid: %s" % mod_dir_name

# Mod ID is valid
if error_num == 0:
if status_code == MLConfigStatus.OK:
var mod := mod_data[mod_dir_name] as ModData
var config_data := mod.config
defaults = mod.manifest.config_defaults

# No custom JSON file
if config_data.size() == 0:
error_num = 2
error_msg = "WARNING - No config file for %s.json." % mod_dir_name
if config_data.size() == MLConfigStatus.OK:
status_code = MLConfigStatus.NO_JSON_OK
var noconfig_msg = "No config file for %s.json. " % mod_dir_name
if key == "":
data = defaults
error_msg += "Using defaults (extra.godot.config_defaults)"
status_msg += str(noconfig_msg, "Using defaults (extra.godot.config_defaults)")
else:
if defaults.has(key):
data = defaults[key]
error_msg += "Using defaults for key '%s' (extra.godot.config_defaults.%s)" % [key, key]
status_msg += str(noconfig_msg, "Using defaults for key '%s' (extra.godot.config_defaults.%s)" % [key, key])
else:
error_num = 3
error_msg += "Requested key '%s' is not present in the defaults (extra.godot.config_defaults.%s)" % [key, key]
status_code = MLConfigStatus.NO_JSON_INVALID_KEY
status_msg += str(
"Could not get the requested data for %s: " % mod_dir_name,
"Requested key '%s' is not present in the 'config_defaults' of the mod's manifest.json file (extra.godot.config_defaults.%s). " % [key, key]
)

# JSON file exists
if error_num == 0:
if status_code == MLConfigStatus.OK:
if key == "":
data = config_data
else:
if config_data.has(key):
data = config_data[key]
else:
error_num = 4
error_msg = "WARNING - Invalid key '%s' for mod ID: %s" % [key, mod_dir_name]
status_code = MLConfigStatus.INVALID_KEY
status_msg = "Invalid key '%s' for mod ID: %s" % [key, mod_dir_name]

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

return {
"error": error_num,
"error_msg": error_msg,
"status_code": status_code,
"status_msg": status_msg,
"data": data,
}

0 comments on commit 01a2fbe

Please sign in to comment.