Skip to content

Commit 2a5ef3f

Browse files
committed
move json to dict util from ModData to utils
1 parent 046ebdc commit 2a5ef3f

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

addons/mod_loader/mod_data.gd

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ func load_manifest() -> void:
4343
ModLoaderUtils.log_info("Loading mod_manifest (manifest.json) for -> %s" % dir_name, LOG_NAME)
4444

4545
# Load meta data file
46-
var manifest_path = get_required_mod_file_path(required_mod_files.MANIFEST)
47-
var manifest_dict = _get_json_as_dict(manifest_path) # todo get from utils
46+
var manifest_path := get_required_mod_file_path(required_mod_files.MANIFEST)
47+
var manifest_dict := ModLoaderUtils.get_json_as_dict(manifest_path)
4848

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

@@ -59,8 +59,8 @@ func load_manifest() -> void:
5959

6060
## Validates if [member dir_name] matches [method ModManifest.get_mod_id]
6161
func is_mod_dir_name_same_as_id() -> bool:
62-
var manifest_id = manifest.get_mod_id()
63-
if dir_name != manifest_id:
62+
var manifest_id := manifest.get_mod_id()
63+
if not dir_name == manifest_id:
6464
ModLoaderUtils.log_fatal('Mod directory name "%s" does not match the data in manifest.json. Expected "%s"' % [ dir_name, manifest_id ], LOG_NAME)
6565
is_loadable = false
6666
return false
@@ -69,10 +69,10 @@ func is_mod_dir_name_same_as_id() -> bool:
6969

7070
## Confirms that all files from [member required_mod_files] exist
7171
func has_required_files() -> bool:
72-
var file_check = File.new()
72+
var file_check := File.new()
7373

7474
for required_file in required_mod_files:
75-
var file_path = get_required_mod_file_path(required_mod_files[required_file])
75+
var file_path := get_required_mod_file_path(required_mod_files[required_file])
7676

7777
if !file_check.file_exists(file_path):
7878
ModLoaderUtils.log_fatal("ERROR - %s is missing a required file: %s" % [dir_name, file_path], LOG_NAME)
@@ -95,25 +95,6 @@ func get_required_mod_file_path(required_file: int) -> String:
9595
return ""
9696

9797

98-
## Parses JSON from a given file path and returns a dictionary.
99-
## Returns an empty dictionary if no file exists (check with size() < 1)
100-
static func _get_json_as_dict(path:String) -> Dictionary: # todo move to utils
101-
var file = File.new()
102-
103-
if !file.file_exists(path):
104-
file.close()
105-
return {}
106-
107-
file.open(path, File.READ)
108-
var content = file.get_as_text()
109-
110-
var parsed := JSON.parse(content)
111-
if parsed.error:
112-
# log error
113-
return {}
114-
return parsed.result
115-
116-
11798
#func _to_string() -> String:
11899
# todo if we want it pretty printed
119100

addons/mod_loader/mod_loader_utils.gd

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
extends Node
22
class_name ModLoaderUtils
33

4+
const LOG_NAME = "ModLoader:ModLoaderUtils"
45
const MOD_LOG_PATH = "user://mods.log"
56

67
enum verbosity_level {
@@ -177,6 +178,28 @@ static func get_file_name_from_path(path: String, make_lower_case := true, remov
177178
return file_name
178179

179180

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)
183+
static func get_json_as_dict(path: String) -> Dictionary:
184+
var file := File.new()
185+
186+
if !file.file_exists(path):
187+
file.close()
188+
return {}
189+
190+
file.open(path, File.READ)
191+
var content := file.get_as_text()
192+
193+
var parsed := JSON.parse(content)
194+
if parsed.error:
195+
log_error("Error parsing JSON", LOG_NAME)
196+
return {}
197+
if not parsed.result is Dictionary:
198+
log_error("JSON is not a dictionary", LOG_NAME)
199+
return {}
200+
return parsed.result
201+
202+
180203
# Get a flat array of all files in the target directory. This was needed in the
181204
# original version of this script, before becoming deprecated. It may still be
182205
# used if DEBUG_ENABLE_STORING_FILEPATHS is true.

0 commit comments

Comments
 (0)