@@ -5,7 +5,7 @@ extends Object
5
5
6
6
const LOG_NAME := "ModLoader:Config"
7
7
8
- enum MLConfigStatus {
8
+ enum ML_CONFIG_STATUS {
9
9
OK , # 0 = No errors
10
10
NO_JSON_OK , # 1 = No custom JSON (file probably does not exist). Uses defaults from manifest, if available
11
11
INVALID_MOD_ID , # 2 = Invalid mod ID
@@ -14,65 +14,55 @@ enum MLConfigStatus {
14
14
}
15
15
16
16
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
23
35
var status_msg := ""
24
36
var data = {} # can be anything
25
- var defaults := {}
26
37
27
- # Invalid mod ID
38
+ # Check if the mod ID is invalid
28
39
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
30
41
status_msg = "Mod ID was invalid: %s " % mod_dir_name
31
42
32
43
# Mod ID is valid
33
- if status_code == MLConfigStatus .OK :
44
+ if status_code == ML_CONFIG_STATUS .OK :
34
45
var mod := ModLoaderStore .mod_data [mod_dir_name ] as ModData
35
46
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 :
59
55
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
71
61
var full_msg = "Config JSON Notice: %s " % status_msg
72
62
# Only log this once, to avoid flooding the log
73
63
ModLoaderLog .debug (full_msg , mod_dir_name , true )
74
64
else :
75
- # Code error (eg . invalid mod ID)
65
+ # The error is critical (e.g . invalid mod ID)
76
66
ModLoaderLog .fatal ("Config JSON Error (%s ): %s " % [status_code , status_msg ], mod_dir_name )
77
67
78
68
return {
@@ -86,7 +76,7 @@ static func get_mod_config(mod_dir_name: String = "", key: String = "") -> Dicti
86
76
# Requires the full config object (ie. the dictionary that's returned by
87
77
# `get_mod_config`)
88
78
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
90
80
91
81
92
82
# Saves a full dictionary object to a mod's custom config file, as JSON.
0 commit comments