From a0b38728dbb69e60eaf43465265bb1537fe8230b Mon Sep 17 00:00:00 2001 From: steen Date: Mon, 3 Feb 2025 16:31:19 +0100 Subject: [PATCH] feat: new editor-only log type for dev hints (#538) * feat: new editor only log type for dev hints * fix log string * hint color options * use hint color --- addons/mod_loader/api/log.gd | 23 +++++++++++++++++-- addons/mod_loader/api/mod.gd | 2 +- addons/mod_loader/mod_loader.gd | 13 ++++++----- addons/mod_loader/mod_loader_store.gd | 18 +++++++++++---- .../mod_loader/resources/options_profile.gd | 1 + 5 files changed, 43 insertions(+), 14 deletions(-) diff --git a/addons/mod_loader/api/log.gd b/addons/mod_loader/api/log.gd index db6ff080..c260e9d0 100644 --- a/addons/mod_loader/api/log.gd +++ b/addons/mod_loader/api/log.gd @@ -31,6 +31,7 @@ static var logged_messages := { "info": {}, "success": {}, "debug": {}, + "hint": {}, } } @@ -42,6 +43,8 @@ static var verbosity: VERBOSITY_LEVEL = VERBOSITY_LEVEL.DEBUG ## Array of mods that should be ignored when logging messages (contains mod IDs as strings) static var ignored_mods: Array[String] = [] +## Highlighting color for hint type log messages +static var hint_color := Color("#70bafa") ## This Sub-Class represents a log entry in ModLoader. class ModLoaderLogEntry: @@ -205,6 +208,21 @@ static func debug(message: String, mod_name: String, only_once := false) -> void _log(message, mod_name, "debug", only_once) +## Logs the message. Prefixed HINT and highligted.[br] +## [br] +## [i]Note: Logged with verbosity level at or above debug (-vvv) and in the editor only. Not written to mod loader log.[/i][br] +## Use this to help other developers debug issues by giving them error-specific hints.[br] +## [br] +## [b]Parameters:[/b][br] +## [param message] ([String]): The message to be logged as a debug.[br] +## [param mod_name] ([String]): The name of the mod or ModLoader class associated with this log entry.[br] +## [param only_once] ([bool]): (Optional) If true, the log entry will only be logged once, even if called multiple times. Default is false.[br] +## [br] +## [b]Returns:[/b] [code]void[/code] +static func hint(message: String, mod_name: String, only_once := false) -> void: + _log(message, mod_name, "hint", only_once) + + ## Logs the message formatted with [method JSON.print]. Prefixed DEBUG.[br] ## [br] ## [i]Note: Logged with verbosity level at or above debug (-vvv).[/i] [br] @@ -361,8 +379,6 @@ static func get_all_entries_as_string(log_entries: Array) -> Array: return log_entry_strings - - # Internal log functions # ============================================================================= @@ -413,6 +429,9 @@ static func _log(message: String, mod_name: String, log_type: String = "info", o if verbosity >= VERBOSITY_LEVEL.DEBUG: print(log_entry.get_prefix() + message) _write_to_log_file(log_entry.get_entry()) + "hint": + if OS.has_feature("editor") and verbosity >= VERBOSITY_LEVEL.DEBUG: + print_rich("[color=%s]%s[/color]" % [hint_color.to_html(false), log_entry.get_prefix() + message]) static func _is_mod_name_ignored(mod_name: String) -> bool: diff --git a/addons/mod_loader/api/mod.gd b/addons/mod_loader/api/mod.gd index 9181283e..2159985a 100644 --- a/addons/mod_loader/api/mod.gd +++ b/addons/mod_loader/api/mod.gd @@ -100,7 +100,7 @@ static func install_script_hooks(vanilla_script_path: String, hook_script_path: var closest_vanilla: String = vanilla_methods.front() if closest_vanilla.similarity(hook.name) > 0.8: - ModLoaderLog.debug( + ModLoaderLog.hint( 'Did you mean "%s" instead of "%s"?' % [closest_vanilla, hook.name], LOG_NAME ) diff --git a/addons/mod_loader/mod_loader.gd b/addons/mod_loader/mod_loader.gd index 384cbcbf..6c30c5c1 100644 --- a/addons/mod_loader/mod_loader.gd +++ b/addons/mod_loader/mod_loader.gd @@ -120,9 +120,9 @@ func _init() -> void: # https://github.com/godotengine/godot/issues/19815 # https://github.com/godotengine/godot/issues/16798 if is_in_editor: - ModLoaderLog.warning( - "Loading any resource packs (.zip/.pck) with `load_resource_pack` will WIPE the entire virtual res:// directory. - If you have any unpacked mods in %s, they will not be loaded.Please unpack your mod ZIPs instead, and add them to %s" % + ModLoaderLog.hint( + "Loading any resource packs (.zip/.pck) with `load_resource_pack` will WIPE the entire virtual res:// directory. " + + "If you have any unpacked mods in %s, they will not be loaded.Please unpack your mod ZIPs instead, and add them to %s" % [_ModLoaderPath.get_unpacked_mods_dir_path(), _ModLoaderPath.get_unpacked_mods_dir_path()], LOG_NAME, true ) @@ -216,9 +216,10 @@ func _ready(): # Variables initialized with an autoload property cause errors otherwise. if ModLoaderStore.any_mod_hooked: if OS.has_feature("editor"): - ModLoaderLog.warning("No mod hooks .zip will be created when running from the editor.", LOG_NAME) - ModLoaderLog.info("You can test mod hooks by running the preprocessor on the vanilla scripts once.", LOG_NAME) - ModLoaderLog.info("We recommend using the Mod Loader Dev Tool to process scripts in the editor. You can find it here: %s" % ModLoaderStore.MOD_LOADER_DEV_TOOL_URL, LOG_NAME) + _ModLoaderModHookPacker.start() + ModLoaderLog.hint("No mod hooks .zip will be created when running from the editor.", LOG_NAME) + ModLoaderLog.hint("You can test mod hooks by running the preprocessor on the vanilla scripts once.", LOG_NAME) + ModLoaderLog.hint("We recommend using the Mod Loader Dev Tool to process scripts in the editor. You can find it here: %s" % ModLoaderStore.MOD_LOADER_DEV_TOOL_URL, LOG_NAME) else: # Generate mod hooks _ModLoaderModHookPacker.start() diff --git a/addons/mod_loader/mod_loader_store.gd b/addons/mod_loader/mod_loader_store.gd index f9fc89fe..b0fb72d5 100644 --- a/addons/mod_loader/mod_loader_store.gd +++ b/addons/mod_loader/mod_loader_store.gd @@ -118,10 +118,16 @@ var ml_options: ModLoaderOptionsProfile func _init(): _update_ml_options_from_options_resource() _update_ml_options_from_cli_args() + _configure_logger() # ModLoaderStore is passed as argument so the cache data can be loaded on _init() _ModLoaderCache.init_cache(self) +func _exit_tree() -> void: + # Save the cache to the cache file. + _ModLoaderCache.save_to_file() + + # Update ModLoader's options, via the custom options resource func _update_ml_options_from_options_resource() -> void: # Path to the options resource @@ -178,11 +184,6 @@ func _update_ml_options_from_options_resource() -> void: ml_options = override_options -func _exit_tree() -> void: - # Save the cache to the cache file. - _ModLoaderCache.save_to_file() - - # Update ModLoader's options, via CLI args func _update_ml_options_from_cli_args() -> void: # Disable mods @@ -217,3 +218,10 @@ func _update_ml_options_from_cli_args() -> void: var ignore_mod_names := _ModLoaderCLI.get_cmd_line_arg_value("--log-ignore") if not ignore_mod_names == "": ml_options.ignored_mod_names_in_log = ignore_mod_names.split(",") + + +# Update static variables from the options +func _configure_logger() -> void: + ModLoaderLog.verbosity = ml_options.log_level + ModLoaderLog.ignored_mods = ml_options.ignored_mod_names_in_log + ModLoaderLog.hint_color = ml_options.hint_color diff --git a/addons/mod_loader/resources/options_profile.gd b/addons/mod_loader/resources/options_profile.gd index b87d84d7..fc5f1e32 100644 --- a/addons/mod_loader/resources/options_profile.gd +++ b/addons/mod_loader/resources/options_profile.gd @@ -21,6 +21,7 @@ extends Resource ## [code]ModLoader:Dependency[/code] - ignore the exact name [br] ## [code]ModLoader:*[/code] - ignore all beginning with this name [br] @export var ignored_mod_names_in_log: Array[String] = [] +@export var hint_color := Color("#70bafa") @export_group("Game Data") ## Steam app id, can be found in the steam page url