Skip to content

Commit

Permalink
feat: new editor-only log type for dev hints (#538)
Browse files Browse the repository at this point in the history
* feat: new editor only log type for dev hints

* fix log string

* hint color options

* use hint color
  • Loading branch information
Qubus0 authored Feb 3, 2025
1 parent 5c61730 commit a0b3872
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
23 changes: 21 additions & 2 deletions addons/mod_loader/api/log.gd
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static var logged_messages := {
"info": {},
"success": {},
"debug": {},
"hint": {},
}
}

Expand All @@ -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:
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -361,8 +379,6 @@ static func get_all_entries_as_string(log_entries: Array) -> Array:
return log_entry_strings




# Internal log functions
# =============================================================================

Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion addons/mod_loader/api/mod.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
13 changes: 7 additions & 6 deletions addons/mod_loader/mod_loader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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()
Expand Down
18 changes: 13 additions & 5 deletions addons/mod_loader/mod_loader_store.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions addons/mod_loader/resources/options_profile.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a0b3872

Please sign in to comment.