Skip to content

Commit 34420bd

Browse files
committed
move logging to new utils class
1 parent 64efef4 commit 34420bd

File tree

2 files changed

+188
-92
lines changed

2 files changed

+188
-92
lines changed

addons/mod_loader/mod_loader.gd

Lines changed: 39 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ extends Node
2626
# Most of these settings should never need to change, aside from the DEBUG_*
2727
# options (which should be `false` when distributing compiled PCKs)
2828

29-
# Enables logging messages made with dev_log. Usually these are enabled with the
30-
# command line arg `--log-dev`, but you can also enable them this way if you're
31-
# debugging in the editor
32-
const DEBUG_ENABLE_DEV_LOG = false
33-
3429
# If true, a complete array of filepaths is stored for each mod. This is
3530
# disabled by default because the operation can be very expensive, but may
3631
# be useful for debugging
@@ -90,24 +85,24 @@ func _init():
9085
return
9186

9287
# Log game install dir
93-
mod_log(str("game_install_directory: ", _get_local_folder_dir()), LOG_NAME)
88+
ModLoaderUtils.log_info(str("game_install_directory: ", _get_local_folder_dir()), LOG_NAME)
9489

9590
# check if we want to use a different mods path that is provided as a command line argument
9691
var cmd_line_mod_path = _get_cmd_line_arg("--mods-path")
9792
if cmd_line_mod_path != "":
9893
os_mods_path_override = cmd_line_mod_path
99-
mod_log("The path mods are loaded from has been changed via the CLI arg `--mods-path`, to: " + cmd_line_mod_path, LOG_NAME)
94+
ModLoaderUtils.log_info("The path mods are loaded from has been changed via the CLI arg `--mods-path`, to: " + cmd_line_mod_path, LOG_NAME)
10095

10196
# Check for the CLI arg that overrides the configs path
10297
var cmd_line_configs_path = _get_cmd_line_arg("--configs-path")
10398
if cmd_line_configs_path != "":
10499
os_configs_path_override = cmd_line_configs_path
105-
mod_log("The path configs are loaded from has been changed via the CLI arg `--configs-path`, to: " + cmd_line_configs_path, LOG_NAME)
100+
ModLoaderUtils.log_info("The path configs are loaded from has been changed via the CLI arg `--configs-path`, to: " + cmd_line_configs_path, LOG_NAME)
106101

107102
# Loop over "res://mods" and add any mod zips to the unpacked virtual
108103
# directory (UNPACKED_DIR)
109104
_load_mod_zips()
110-
mod_log("DONE: Loaded all mod files into the virtual filesystem", LOG_NAME)
105+
ModLoaderUtils.log_success("DONE: Loaded all mod files into the virtual filesystem", LOG_NAME)
111106

112107
# Loop over UNPACKED_DIR. This triggers _init_mod_data for each mod
113108
# directory, which adds their data to mod_data.
@@ -125,7 +120,7 @@ func _init():
125120
var mod: ModData = mod_data[dir_name]
126121
mod.load_manifest()
127122

128-
mod_log("DONE: Loaded all meta data", LOG_NAME)
123+
ModLoaderUtils.log_success("DONE: Loaded all meta data", LOG_NAME)
129124

130125
# Run dependency checks after loading mod_manifest. If a mod depends on another
131126
# mod that hasn't been loaded, that dependent mod won't be loaded.
@@ -142,61 +137,17 @@ func _init():
142137
var mod_i = 1
143138
for mod in mod_load_order: # mod === mod_data
144139
mod = mod as ModData
145-
dev_log("mod_load_order -> %s) %s" % [mod_i, mod.dir_name], LOG_NAME)
140+
ModLoaderUtils.log_debug("mod_load_order -> %s) %s" % [mod_i, mod.dir_name], LOG_NAME)
146141
mod_i += 1
147142

148143
# Instance every mod and add it as a node to the Mod Loader
149144
for mod in mod_load_order:
150-
# mod_log(str("Initializing -> ", mod.mod_manifest.extra.godot.id), LOG_NAME)
151-
mod_log("Initializing -> %s" % mod.manifest.get_mod_id(), LOG_NAME)
145+
ModLoaderUtils.log_info("Initializing -> %s" % mod.manifest.get_mod_id(), LOG_NAME)
152146
_init_mod(mod)
153147

154-
dev_log(str("mod_data: ", JSON.print(mod_data, ' ')), LOG_NAME)
155-
156-
mod_log("DONE: Completely finished loading mods", LOG_NAME)
157-
158-
159-
# Log developer info. Has to be enabled, either with the command line arg
160-
# `--log-dev`, or by temporarily enabling DEBUG_ENABLE_DEV_LOG
161-
func dev_log(text:String, mod_name:String = "Unknown-Mod", pretty:bool = false):
162-
if DEBUG_ENABLE_DEV_LOG || (_check_cmd_line_arg("--log-dev")):
163-
mod_log(text, mod_name, pretty)
164-
165-
166-
# Log info for a mod. Accepts the mod name as the 2nd arg, which prefixes
167-
# the logged string with "{mod_name}: "
168-
func mod_log(text:String, mod_name:String = "Unknown-Mod", pretty:bool = false)->void:
169-
# Prefix with "{mod_name}: "
170-
var prefix = mod_name + ": "
171-
172-
var date_time = Time.get_datetime_dict_from_system()
148+
ModLoaderUtils.log_debug_json_print("mod data", mod_data, LOG_NAME)
173149

174-
# Add leading zeroes if needed
175-
var hour := (date_time.hour as String).pad_zeros(2)
176-
var mins := (date_time.minute as String).pad_zeros(2)
177-
var secs := (date_time.second as String).pad_zeros(2)
178-
179-
var date_time_string := "%s.%s.%s - %s:%s:%s" % [date_time.day, date_time.month, date_time.year, hour, mins, secs]
180-
181-
print(str(date_time_string,' ', prefix, text))
182-
183-
var log_file = File.new()
184-
185-
if(!log_file.file_exists(MOD_LOG_PATH)):
186-
log_file.open(MOD_LOG_PATH, File.WRITE)
187-
log_file.store_string('%s Created mod.log!' % date_time_string)
188-
log_file.close()
189-
190-
var _error = log_file.open(MOD_LOG_PATH, File.READ_WRITE)
191-
if _error:
192-
print(_error)
193-
return
194-
log_file.seek_end()
195-
if pretty:
196-
log_file.store_string("\n" + str(date_time_string,' ', prefix, JSON.print(text, " ")))
197-
else:
198-
log_file.store_string("\n" + str(date_time_string,' ', prefix, text))
199-
log_file.close()
150+
ModLoaderUtils.log_success("DONE: Completely finished loading mods", LOG_NAME)
200151

201152

202153
# Loop over "res://mods" and add any mod zips to the unpacked virtual directory
@@ -207,10 +158,10 @@ func _load_mod_zips():
207158

208159
var dir = Directory.new()
209160
if dir.open(game_mod_folder_path) != OK:
210-
mod_log("Can't open mod folder %s." % game_mod_folder_path, LOG_NAME)
161+
ModLoaderUtils.log_error("Can't open mod folder %s." % game_mod_folder_path, LOG_NAME)
211162
return
212163
if dir.list_dir_begin() != OK:
213-
mod_log("Can't read mod folder %s." % game_mod_folder_path, LOG_NAME)
164+
ModLoaderUtils.log_error("Can't read mod folder %s." % game_mod_folder_path, LOG_NAME)
214165
return
215166

216167
var has_shown_editor_warning = false
@@ -246,22 +197,22 @@ func _load_mod_zips():
246197
# https://github.com/godotengine/godot/issues/19815
247198
# https://github.com/godotengine/godot/issues/16798
248199
if OS.has_feature("editor") && !has_shown_editor_warning:
249-
mod_log(str(
250-
"WARNING: Loading any resource packs (.zip/.pck) with `load_resource_pack` will WIPE the entire virtual res:// directory. ",
200+
ModLoaderUtils.log_warning(str(
201+
"Loading any resource packs (.zip/.pck) with `load_resource_pack` will WIPE the entire virtual res:// directory. ",
251202
"If you have any unpacked mods in ", UNPACKED_DIR, ", they will not be loaded. ",
252203
"Please unpack your mod ZIPs instead, and add them to ", UNPACKED_DIR), LOG_NAME)
253204
has_shown_editor_warning = true
254205

255-
dev_log(str("Found mod ZIP: ", mod_folder_global_path), LOG_NAME)
206+
ModLoaderUtils.log_debug(str("Found mod ZIP: ", mod_folder_global_path), LOG_NAME)
256207

257208
# If there was an error loading the mod zip file
258209
if !is_mod_loaded_success:
259210
# Log the error and continue with the next file
260-
mod_log(str(mod_zip_file_name, " failed to load."), LOG_NAME)
211+
ModLoaderUtils.log_error(str(mod_zip_file_name, " failed to load."), LOG_NAME)
261212
continue
262213

263214
# Mod successfully loaded!
264-
mod_log(str(mod_zip_file_name, " loaded."), LOG_NAME)
215+
ModLoaderUtils.log_success(str(mod_zip_file_name, " loaded."), LOG_NAME)
265216

266217
dir.list_dir_end()
267218

@@ -274,10 +225,10 @@ func _setup_mods():
274225

275226
var dir = Directory.new()
276227
if dir.open(unpacked_mods_path) != OK:
277-
mod_log("Can't open unpacked mods folder %s." % unpacked_mods_path, LOG_NAME)
228+
ModLoaderUtils.log_error("Can't open unpacked mods folder %s." % unpacked_mods_path, LOG_NAME)
278229
return
279230
if dir.list_dir_begin() != OK:
280-
mod_log("Can't read unpacked mods folder %s." % unpacked_mods_path, LOG_NAME)
231+
ModLoaderUtils.log_error("Can't read unpacked mods folder %s." % unpacked_mods_path, LOG_NAME)
281232
return
282233

283234
# Get all unpacked mod dirs
@@ -317,13 +268,13 @@ func _load_mod_configs():
317268
var json_path = configs_path.plus_file(dir_name + ".json")
318269
var mod_config = ModData._get_json_as_dict(json_path)
319270

320-
dev_log(str("Config JSON: Looking for config at path: ", json_path), LOG_NAME)
271+
ModLoaderUtils.log_debug(str("Config JSON: Looking for config at path: ", json_path), LOG_NAME)
321272

322273
if mod_config.size() > 0:
323274
found_configs_count += 1
324275

325-
mod_log(str("Config JSON: Found a config file: '", json_path, "'"), LOG_NAME)
326-
dev_log(str("Config JSON: File data: ", JSON.print(mod_config)), LOG_NAME)
276+
ModLoaderUtils.log_info(str("Config JSON: Found a config file: '", json_path, "'"), LOG_NAME)
277+
ModLoaderUtils.log_debug(str("Config JSON: File data: ", JSON.print(mod_config)), LOG_NAME)
327278

328279
# Check `load_from` option. This lets you specify the name of a
329280
# different JSON file to load your config from. Must be in the same
@@ -333,21 +284,21 @@ func _load_mod_configs():
333284
if mod_config.has("load_from"):
334285
var new_path = mod_config.load_from
335286
if new_path != "" && new_path != str(dir_name, ".json"):
336-
mod_log(str("Config JSON: Following load_from path: ", new_path), LOG_NAME)
287+
ModLoaderUtils.log_info(str("Config JSON: Following load_from path: ", new_path), LOG_NAME)
337288
var new_config = ModData._get_json_as_dict(configs_path + new_path)
338289
if new_config.size() > 0 != null:
339290
mod_config = new_config
340-
mod_log(str("Config JSON: Loaded from custom json: ", new_path), LOG_NAME)
341-
dev_log(str("Config JSON: File data: ", JSON.print(mod_config)), LOG_NAME)
291+
ModLoaderUtils.log_info(str("Config JSON: Loaded from custom json: ", new_path), LOG_NAME)
292+
ModLoaderUtils.log_debug(str("Config JSON: File data: ", JSON.print(mod_config)), LOG_NAME)
342293
else:
343-
mod_log(str("Config JSON: ERROR - Could not load data via `load_from` for ", dir_name, ", at path: ", new_path), LOG_NAME)
294+
ModLoaderUtils.log_error(str("Config JSON: ERROR - Could not load data via `load_from` for ", dir_name, ", at path: ", new_path), LOG_NAME)
344295

345296
mod_data[dir_name].config = mod_config
346297

347298
if found_configs_count > 0:
348-
mod_log(str("Config JSON: Loaded ", str(found_configs_count), " config(s)"), LOG_NAME)
299+
ModLoaderUtils.log_success(str("Config JSON: Loaded ", str(found_configs_count), " config(s)"), LOG_NAME)
349300
else:
350-
mod_log(str("Config JSON: No mod configs were found"), LOG_NAME)
301+
ModLoaderUtils.log_info(str("Config JSON: No mod configs were found"), LOG_NAME)
351302

352303

353304
# Add a mod's data to mod_data.
@@ -376,7 +327,7 @@ func _init_mod_data(mod_folder_path):
376327
# mod_manifest (ie. its manifest.json file). If a mod depends on another mod that
377328
# hasn't been loaded, the dependent mod won't be loaded.
378329
func _check_dependencies(mod_id:String, deps:Array):
379-
dev_log(str("Checking dependencies - mod_id: ", mod_id, " dependencies: ", deps), LOG_NAME)
330+
ModLoaderUtils.log_debug(str("Checking dependencies - mod_id: ", mod_id, " dependencies: ", deps), LOG_NAME)
380331

381332
# loop through each dependency
382333
for dependency_id in deps:
@@ -392,7 +343,7 @@ func _check_dependencies(mod_id:String, deps:Array):
392343

393344
# increase importance score by 1
394345
dependency.importance = dependency.importance + 1
395-
dev_log(str("Dependency -> ", dependency_id, " importance -> ", dependency.importance), LOG_NAME)
346+
ModLoaderUtils.log_debug(str("Dependency -> ", dependency_id, " importance -> ", dependency.importance), LOG_NAME)
396347

397348
# check if dependency has dependencies
398349
if(dependency_mod_manifest.dependencies.size() > 0):
@@ -401,7 +352,7 @@ func _check_dependencies(mod_id:String, deps:Array):
401352

402353
# Handle missing dependencies: Sets `is_loadable` to false and logs an error
403354
func _handle_missing_dependency(mod_id, dependency_id):
404-
mod_log(str("ERROR - missing dependency - mod_id -> ", mod_id, " dependency_id -> ", dependency_id), LOG_NAME)
355+
ModLoaderUtils.log_error(str("Missing dependency - mod_id -> ", mod_id, " dependency_id -> ", dependency_id), LOG_NAME)
405356
# if mod is not present in the missing dependencies array
406357
if(!mod_missing_dependencies.has(mod_id)):
407358
# add it
@@ -440,15 +391,15 @@ func _compare_importance(a, b):
440391
func _init_mod(mod: ModData):
441392
var mod_main_path = mod.get_required_mod_file_path(ModData.required_mod_files.MOD_MAIN)
442393

443-
dev_log("Loading script from -> %s" % mod_main_path, LOG_NAME)
394+
ModLoaderUtils.log_debug("Loading script from -> %s" % mod_main_path, LOG_NAME)
444395
var mod_main_script = ResourceLoader.load(mod_main_path)
445-
dev_log("Loaded script -> %s" % mod_main_script, LOG_NAME)
396+
ModLoaderUtils.log_debug("Loaded script -> %s" % mod_main_script, LOG_NAME)
446397

447398
var mod_main_instance = mod_main_script.new(self)
448399
# mod_main_instance.name = mod.mod_manifest.extra.godot.id
449400
mod_main_instance.name = mod.manifest.get_mod_id()
450401

451-
dev_log("Adding child -> %s" % mod_main_instance, LOG_NAME)
402+
ModLoaderUtils.log_debug("Adding child -> %s" % mod_main_instance, LOG_NAME)
452403
add_child(mod_main_instance, true)
453404

454405

@@ -495,19 +446,15 @@ func _get_local_folder_dir(subfolder:String = ""):
495446

496447

497448
func _get_file_name(path, is_lower_case = true, is_no_extension = false):
498-
# mod_log(str("Get file name from path -> ", path), LOG_NAME)
499449
var file_name = path.get_file()
500450

501451
if(is_lower_case):
502-
# mod_log(str("Get file name in lower case"), LOG_NAME)
503452
file_name = file_name.to_lower()
504453

505454
if(is_no_extension):
506-
# mod_log(str("Get file name without extension"), LOG_NAME)
507455
var file_extension = file_name.get_extension()
508456
file_name = file_name.replace(str(".",file_extension), '')
509457

510-
# mod_log(str("return file name -> ", file_name), LOG_NAME)
511458
return file_name
512459

513460

@@ -577,7 +524,7 @@ func _get_flat_view_dict(p_dir = "res://", p_match = "", p_match_is_regex = fals
577524
func install_script_extension(child_script_path:String):
578525
# Check path to file exists
579526
if !File.new().file_exists(child_script_path):
580-
mod_log("ERROR - The child script path '%s' does not exist" % [child_script_path], LOG_NAME)
527+
ModLoaderUtils.log_error("The child script path '%s' does not exist" % [child_script_path], LOG_NAME)
581528
return
582529

583530
var child_script = ResourceLoader.load(child_script_path)
@@ -593,7 +540,7 @@ func install_script_extension(child_script_path:String):
593540

594541
var parent_script = child_script.get_base_script()
595542
var parent_script_path = parent_script.resource_path
596-
mod_log("Installing script extension: %s <- %s" % [parent_script_path, child_script_path], LOG_NAME)
543+
ModLoaderUtils.log_info("Installing script extension: %s <- %s" % [parent_script_path, child_script_path], LOG_NAME)
597544
child_script.take_over_path(parent_script_path)
598545

599546

@@ -603,7 +550,7 @@ func install_script_extension(child_script_path:String):
603550
func add_translation_from_resource(resource_path: String):
604551
var translation_object = load(resource_path)
605552
TranslationServer.add_translation(translation_object)
606-
mod_log(str("Added Translation from Resource -> ", resource_path), LOG_NAME)
553+
ModLoaderUtils.log_info(str("Added Translation from Resource -> ", resource_path), LOG_NAME)
607554

608555

609556
func append_node_in_scene(modified_scene, node_name:String = "", node_parent = null, instance_path:String = "", is_visible:bool = true):
@@ -628,9 +575,9 @@ func append_node_in_scene(modified_scene, node_name:String = "", node_parent = n
628575
func save_scene(modified_scene, scene_path:String):
629576
var packed_scene = PackedScene.new()
630577
packed_scene.pack(modified_scene)
631-
dev_log(str("packing scene -> ", packed_scene), LOG_NAME)
578+
ModLoaderUtils.log_debug(str("packing scene -> ", packed_scene), LOG_NAME)
632579
packed_scene.take_over_path(scene_path)
633-
dev_log(str("save_scene - taking over path - new path -> ", packed_scene.resource_path), LOG_NAME)
580+
ModLoaderUtils.log_debug(str("save_scene - taking over path - new path -> ", packed_scene.resource_path), LOG_NAME)
634581
_saved_objects.append(packed_scene)
635582

636583

@@ -688,7 +635,7 @@ func get_mod_config(mod_id:String = "", key:String = "")->Dictionary:
688635

689636
# Log if any errors occured
690637
if error_num != 0:
691-
dev_log(str("Config: ", error_msg), mod_id)
638+
ModLoaderUtils.log_debug(str("Config: ", error_msg), mod_id)
692639

693640
return {
694641
"error": error_num,

0 commit comments

Comments
 (0)