Skip to content

Commit 8f92df8

Browse files
committed
refactor: ♻️ simplified get_mod_config()
Only returning the full config data without the key parameter, as it only allowed retrieving top-level properties. | Removing the distinction between user config and default config. The default config is now generated based on the default key of the config_schema and saved to the mod config file. This ensures that there is always a user config. - To revert to the default config, simply delete the config JSON file.
1 parent 8ee8bf6 commit 8f92df8

File tree

1 file changed

+37
-47
lines changed

1 file changed

+37
-47
lines changed

addons/mod_loader/api/config.gd

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extends Object
55

66
const LOG_NAME := "ModLoader:Config"
77

8-
enum MLConfigStatus {
8+
enum ML_CONFIG_STATUS {
99
OK, # 0 = No errors
1010
NO_JSON_OK, # 1 = No custom JSON (file probably does not exist). Uses defaults from manifest, if available
1111
INVALID_MOD_ID, # 2 = Invalid mod ID
@@ -14,65 +14,55 @@ enum MLConfigStatus {
1414
}
1515

1616

17-
# Get the config data for a specific mod. Always returns a dictionary with two
18-
# keys: `error` and `data`.
19-
# Data (`data`) is either the full config, or data from a specific key if one was specified.
20-
# Error (`error`) is 0 if there were no errors, or > 0 if the setting could not be retrieved:
21-
static func get_mod_config(mod_dir_name: String = "", key: String = "") -> Dictionary:
22-
var status_code = MLConfigStatus.OK
17+
# Retrieves the configuration data for a specific mod.
18+
# Returns a dictionary with three keys:
19+
# - "status_code": an integer indicating the status of the request
20+
# - "status_msg": a string containing a message explaining the status code
21+
# - "data": the configuration data
22+
#
23+
# Parameters:
24+
# - mod_dir_name: the name of the mod's directory / id
25+
#
26+
# Status Codes:
27+
# - ML_CONFIG_STATUS.OK: the request was successful and the configuration data is included in the "data" key
28+
# - ML_CONFIG_STATUS.INVALID_MOD_ID: the mod ID is not valid, and the "status_msg" key contains an error message
29+
# - ML_CONFIG_STATUS.NO_JSON_OK: there is no configuration file for the mod, the "data" key contains an empty dictionary
30+
#
31+
# Returns:
32+
# A dictionary with three keys: "status_code", "status_msg", and "data"
33+
static func get_mod_config(mod_dir_name: String) -> Dictionary:
34+
var status_code = ML_CONFIG_STATUS.OK
2335
var status_msg := ""
2436
var data = {} # can be anything
25-
var defaults := {}
2637

27-
# Invalid mod ID
38+
# Check if the mod ID is invalid
2839
if not ModLoaderStore.mod_data.has(mod_dir_name):
29-
status_code = MLConfigStatus.INVALID_MOD_ID
40+
status_code = ML_CONFIG_STATUS.INVALID_MOD_ID
3041
status_msg = "Mod ID was invalid: %s" % mod_dir_name
3142

3243
# Mod ID is valid
33-
if status_code == MLConfigStatus.OK:
44+
if status_code == ML_CONFIG_STATUS.OK:
3445
var mod := ModLoaderStore.mod_data[mod_dir_name] as ModData
3546
var config_data := mod.config
36-
defaults = mod.manifest.config_defaults
37-
38-
# No custom JSON file
39-
if config_data.size() == MLConfigStatus.OK:
40-
status_code = MLConfigStatus.NO_JSON_OK
41-
var noconfig_msg = "No config file for %s.json. " % mod_dir_name
42-
if key == "":
43-
data = defaults
44-
status_msg += str(noconfig_msg, "Using defaults (extra.godot.config_defaults)")
45-
else:
46-
if defaults.has(key):
47-
data = defaults[key]
48-
status_msg += str(noconfig_msg, "Using defaults for key '%s' (extra.godot.config_defaults.%s)" % [key, key])
49-
else:
50-
status_code = MLConfigStatus.NO_JSON_INVALID_KEY
51-
status_msg += str(
52-
"Could not get the requested data for %s: " % mod_dir_name,
53-
"Requested key '%s' is not present in the 'config_defaults' of the mod's manifest.json file (extra.godot.config_defaults.%s). " % [key, key]
54-
)
55-
56-
# JSON file exists
57-
if status_code == MLConfigStatus.OK:
58-
if key == "":
47+
48+
# Check if there is no config file for the mod
49+
if config_data.size() == 0:
50+
status_code = ML_CONFIG_STATUS.NO_JSON_OK
51+
status_msg = "No config file for %s.json. " % mod_dir_name
52+
53+
# Config file exists
54+
if status_code == ML_CONFIG_STATUS.OK:
5955
data = config_data
60-
else:
61-
if config_data.has(key):
62-
data = config_data[key]
63-
else:
64-
status_code = MLConfigStatus.INVALID_KEY
65-
status_msg = "Invalid key '%s' for mod ID: %s" % [key, mod_dir_name]
66-
67-
# Log if any errors occured
68-
if not status_code == MLConfigStatus.OK:
69-
if status_code == MLConfigStatus.NO_JSON_OK:
70-
# No user config file exists. Low importance as very likely to trigger
56+
57+
# Log any errors that occurred
58+
if not status_code == ML_CONFIG_STATUS.OK:
59+
if status_code == ML_CONFIG_STATUS.NO_JSON_OK:
60+
# The mod has no user config file, which is not a critical error
7161
var full_msg = "Config JSON Notice: %s" % status_msg
7262
# Only log this once, to avoid flooding the log
7363
ModLoaderLog.debug(full_msg, mod_dir_name, true)
7464
else:
75-
# Code error (eg. invalid mod ID)
65+
# The error is critical (e.g. invalid mod ID)
7666
ModLoaderLog.fatal("Config JSON Error (%s): %s" % [status_code, status_msg], mod_dir_name)
7767

7868
return {
@@ -86,7 +76,7 @@ static func get_mod_config(mod_dir_name: String = "", key: String = "") -> Dicti
8676
# Requires the full config object (ie. the dictionary that's returned by
8777
# `get_mod_config`)
8878
static func is_mod_config_data_valid(config_obj: Dictionary) -> bool:
89-
return config_obj.status_code <= MLConfigStatus.NO_JSON_OK
79+
return config_obj.status_code <= ML_CONFIG_STATUS.NO_JSON_OK
9080

9181

9282
# Saves a full dictionary object to a mod's custom config file, as JSON.

0 commit comments

Comments
 (0)