Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smaller fixes for utils json as dict #66

Merged
merged 1 commit into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion addons/mod_loader/mod_data.gd
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func load_manifest() -> void:
var manifest_path := get_required_mod_file_path(required_mod_files.MANIFEST)
var manifest_dict := ModLoaderUtils.get_json_as_dict(manifest_path)

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

var mod_manifest := ModManifest.new(manifest_dict)

Expand Down
2 changes: 1 addition & 1 deletion addons/mod_loader/mod_loader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func _load_mod_configs():
var new_path = mod_config.load_from
if new_path != "" && new_path != str(dir_name, ".json"):
ModLoaderUtils.log_info(str("Config JSON: Following load_from path: ", new_path), LOG_NAME)
var new_config = ModData._get_json_as_dict(configs_path + new_path)
var new_config = ModLoaderUtils.get_json_as_dict(configs_path + new_path)
if new_config.size() > 0 != null:
mod_config = new_config
ModLoaderUtils.log_info(str("Config JSON: Loaded from custom json: ", new_path), LOG_NAME)
Expand Down
18 changes: 14 additions & 4 deletions addons/mod_loader/mod_loader_utils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -178,19 +178,29 @@ static func get_file_name_from_path(path: String, make_lower_case := true, remov
return file_name


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

if !file.file_exists(path):
file.close()
return {}

file.open(path, File.READ)
var error = file.open(path, File.READ)
if not error == OK:
log_error("Error opening file. Code: %s" % error, LOG_NAME)

var content := file.get_as_text()
return get_json_string_as_dict(content)


var parsed := JSON.parse(content)
# Parses JSON from a given [String] and returns a [Dictionary].
# Returns an empty [Dictionary] on error (check with size() < 1)
static func get_json_string_as_dict(string: String) -> Dictionary:
if string == "":
return {}
var parsed := JSON.parse(string)
if parsed.error:
log_error("Error parsing JSON", LOG_NAME)
return {}
Expand Down