@@ -5,119 +5,34 @@ extends Object
5
5
6
6
const LOG_NAME := "ModLoader:Config"
7
7
8
- enum ML_CONFIG_STATUS {
9
- OK , # 0 = No errors
10
- NO_JSON_OK , # 1 = No custom JSON (file probably does not exist). Uses defaults from manifest, if available
11
- INVALID_MOD_ID , # 2 = Invalid mod ID
12
- NO_JSON_INVALID_KEY , # 3 = Invalid key, and no custom JSON was specified in the manifest defaults (`extra.godot.config_defaults`)
13
- INVALID_KEY # 4 = Invalid key, although config data does exists
14
- }
15
-
16
-
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
35
- var status_msg := ""
36
- var data = {} # can be anything
37
8
9
+ # Retrieves the configuration data for a specific mod
10
+ static func get_mod_config (mod_id : String ) -> Dictionary :
38
11
# Check if the mod ID is invalid
39
- if not ModLoaderStore .mod_data .has (mod_dir_name ):
40
- status_code = ML_CONFIG_STATUS .INVALID_MOD_ID
41
- status_msg = "Mod ID was invalid: %s " % mod_dir_name
42
-
43
- # Mod ID is valid
44
- if status_code == ML_CONFIG_STATUS .OK :
45
- var mod := ModLoaderStore .mod_data [mod_dir_name ] as ModData
46
- var config_data := mod .config
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 :
55
- data = config_data
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
61
- var full_msg = "Config JSON Notice: %s " % status_msg
62
- # Only log this once, to avoid flooding the log
63
- ModLoaderLog .debug (full_msg , mod_dir_name , true )
64
- else :
65
- # The error is critical (e.g. invalid mod ID)
66
- ModLoaderLog .fatal ("Config JSON Error (%s ): %s " % [status_code , status_msg ], mod_dir_name )
67
-
68
- return {
69
- "status_code" : status_code ,
70
- "status_msg" : status_msg ,
71
- "data" : data ,
72
- }
73
-
74
-
75
- # Returns a bool indicating if a retrieved mod config is valid.
76
- # Requires the full config object (ie. the dictionary that's returned by
77
- # `get_mod_config`)
78
- static func is_mod_config_data_valid (config_obj : Dictionary ) -> bool :
79
- return config_obj .status_code <= ML_CONFIG_STATUS .NO_JSON_OK
80
-
12
+ if not ModLoaderStore .mod_data .has (mod_id ):
13
+ ModLoaderLog .fatal ("Mod ID \" %s \" not found" % [mod_id ], LOG_NAME )
14
+ return {}
81
15
82
- # Saves a full dictionary object to a mod's custom config file, as JSON.
83
- # Overwrites any existing data in the file.
84
- # Optionally updates the config object that's stored in memory (true by default).
85
- # Returns a bool indicating success or failure.
86
- # WARNING: Provides no validation
87
- static func save_mod_config_dictionary (mod_id : String , data : Dictionary , update_config : bool = true ) -> bool :
88
- # Use `get_mod_config` to check if a custom JSON file already exists.
89
- # This has the added benefit of logging a fatal error if mod_name is
90
- # invalid (as it already happens in `get_mod_config`)
91
- var config_obj := get_mod_config (mod_id )
16
+ var config_data = ModLoaderStore .mod_data [mod_id ].config
92
17
93
- if not is_mod_config_data_valid (config_obj ):
94
- ModLoaderLog .warning ("Could not save the config JSON file because the config data was invalid" , mod_id )
95
- return false
18
+ # Check if there is no config file for the mod
19
+ if not config_data :
20
+ ModLoaderLog .debug ("No config for mod id \" %s \" " % mod_id , LOG_NAME , true )
21
+ return {}
96
22
97
- var data_original : Dictionary = config_obj .data
98
- var data_new := {}
23
+ return config_data
99
24
100
- # Merge
101
- if update_config :
102
- # Update the config held in memory
103
- data_original .merge (data , true )
104
- data_new = data_original
105
- else :
106
- # Don't update the config in memory
107
- data_new = data_original .duplicate (true )
108
- data_new .merge (data , true )
109
25
110
- var configs_path := _ModLoaderPath .get_path_to_configs ()
111
- var json_path := configs_path .plus_file (mod_id + ".json" )
26
+ static func is_mod_config_data_valid (config_data : ModConfig ):
27
+ var json_schema := JSONSchema .new ()
28
+ var error := json_schema .validate (config_data .get_data_as_string (), config_data .get_schema_as_string ())
112
29
113
- return _ModLoaderFile .save_dictionary_to_json_file (data_new , json_path )
114
30
31
+ static func update_mod_config (mod_id : String , data : Dictionary ) -> void :
32
+ # Update the config held in memory
33
+ ModLoaderStore .mod_data [mod_id ].config .merge (data , true )
115
34
116
- # Saves a single settings to a mod's custom config file.
117
- # Returns a bool indicating success or failure.
118
- static func save_mod_config_setting (mod_id : String , key :String , value , update_config : bool = true ) -> bool :
119
- var new_data = {
120
- key : value
121
- }
122
35
123
- return save_mod_config_dictionary (mod_id , new_data , update_config )
36
+ # Saves a full dictionary object to a mod's config file, as JSON.
37
+ static func save_mod_config (config_data : ModConfig ) -> bool :
38
+ return _ModLoaderFile .save_dictionary_to_json_file (config_data .data , config_data .save_path )
0 commit comments