Skip to content

Commit 5ca12fe

Browse files
authored
Merge pull request #66 from GodotModding/json_as_dict_fixes
smaller fixes for utils json as dict
2 parents 768b847 + dde57e7 commit 5ca12fe

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

addons/mod_loader/mod_data.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func load_manifest() -> void:
4646
var manifest_path := get_required_mod_file_path(required_mod_files.MANIFEST)
4747
var manifest_dict := ModLoaderUtils.get_json_as_dict(manifest_path)
4848

49-
ModLoaderUtils.log_info("%s loaded manifest data -> %s" % [dir_name, manifest_dict], LOG_NAME)
49+
ModLoaderUtils.log_debug_json_print("%s loaded manifest data -> " % dir_name, manifest_dict, LOG_NAME)
5050

5151
var mod_manifest := ModManifest.new(manifest_dict)
5252

addons/mod_loader/mod_loader.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ func _load_mod_configs():
298298
var new_path = mod_config.load_from
299299
if new_path != "" && new_path != str(dir_name, ".json"):
300300
ModLoaderUtils.log_info(str("Config JSON: Following load_from path: ", new_path), LOG_NAME)
301-
var new_config = ModData._get_json_as_dict(configs_path + new_path)
301+
var new_config = ModLoaderUtils.get_json_as_dict(configs_path + new_path)
302302
if new_config.size() > 0 != null:
303303
mod_config = new_config
304304
ModLoaderUtils.log_info(str("Config JSON: Loaded from custom json: ", new_path), LOG_NAME)

addons/mod_loader/mod_loader_utils.gd

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,19 +178,29 @@ static func get_file_name_from_path(path: String, make_lower_case := true, remov
178178
return file_name
179179

180180

181-
# Parses JSON from a given file path and returns a dictionary.
182-
# Returns an empty dictionary if no file exists (check with size() < 1)
181+
# Parses JSON from a given file path and returns a [Dictionary].
182+
# Returns an empty [Dictionary] if no file exists (check with size() < 1)
183183
static func get_json_as_dict(path: String) -> Dictionary:
184184
var file := File.new()
185185

186186
if !file.file_exists(path):
187187
file.close()
188188
return {}
189189

190-
file.open(path, File.READ)
190+
var error = file.open(path, File.READ)
191+
if not error == OK:
192+
log_error("Error opening file. Code: %s" % error, LOG_NAME)
193+
191194
var content := file.get_as_text()
195+
return get_json_string_as_dict(content)
196+
192197

193-
var parsed := JSON.parse(content)
198+
# Parses JSON from a given [String] and returns a [Dictionary].
199+
# Returns an empty [Dictionary] on error (check with size() < 1)
200+
static func get_json_string_as_dict(string: String) -> Dictionary:
201+
if string == "":
202+
return {}
203+
var parsed := JSON.parse(string)
194204
if parsed.error:
195205
log_error("Error parsing JSON", LOG_NAME)
196206
return {}

0 commit comments

Comments
 (0)