From c5c261fc1b11af968071862ea6581b51a8a3e933 Mon Sep 17 00:00:00 2001 From: d4vid Date: Sun, 15 Jun 2025 16:17:48 +0200 Subject: [PATCH] Handle exceptions in prepare methods --- .../ConsoleImprovements/manifest.jecs | 2 +- ...ndHistoryPatch.cs => CommandInputPatch.cs} | 44 +++++++++++-------- .../src/client/ConsoleImprovementsMod.cs | 4 +- .../src/client/MessageCopyPatch.cs | 21 +++++---- 4 files changed, 41 insertions(+), 30 deletions(-) rename ConsoleImprovements/ConsoleImprovements/src/client/{CommandHistoryPatch.cs => CommandInputPatch.cs} (73%) diff --git a/ConsoleImprovements/ConsoleImprovements/manifest.jecs b/ConsoleImprovements/ConsoleImprovements/manifest.jecs index 1f43c4e..6c6f0c0 100644 --- a/ConsoleImprovements/ConsoleImprovements/manifest.jecs +++ b/ConsoleImprovements/ConsoleImprovements/manifest.jecs @@ -1,7 +1,7 @@ ID: D4VID_ConsoleImprovements Name: ConsoleImprovements Author: D4VID -Version: 1.2.0 +Version: 1.2.1 Priority: -90 ClientOnly: true Dependencies: diff --git a/ConsoleImprovements/ConsoleImprovements/src/client/CommandHistoryPatch.cs b/ConsoleImprovements/ConsoleImprovements/src/client/CommandInputPatch.cs similarity index 73% rename from ConsoleImprovements/ConsoleImprovements/src/client/CommandHistoryPatch.cs rename to ConsoleImprovements/ConsoleImprovements/src/client/CommandInputPatch.cs index 62803aa..215832f 100644 --- a/ConsoleImprovements/ConsoleImprovements/src/client/CommandHistoryPatch.cs +++ b/ConsoleImprovements/ConsoleImprovements/src/client/CommandInputPatch.cs @@ -11,7 +11,7 @@ using TMPro; using Console = FancyPantsConsole.Console; namespace ConsoleImprovements.Client { - public class CommandHistoryPatch { + public class CommandInputPatch { const int HistLength = 1000; private static ILogicLogger _logger = null!; @@ -24,22 +24,28 @@ namespace ConsoleImprovements.Client { public static bool Prepare(ILogicLogger logger) { _logger = logger; - // Get access to the rich text string inside the MessageData class - _commandInputFieldField = Fields.getPrivate(typeof(Console), "CommandInputField"); - - CommandConsole commandConsole = CommandConsole.Current; - // ICommandRegistryInternal - var registry = Fields.getPrivate(typeof(CommandConsole), "CommandRegistry").GetValue(commandConsole); - // CommandRegistry.CommandCollectionByName - var commandCollection = Properties.get(registry, "AllRegisteredCommands").GetValue(registry); - // IEnumerable - var commands = (IEnumerable) Methods.getPublic(commandCollection, "EnumerateAllCommands").Invoke(commandCollection, null)!; - - foreach (var command in commands) { - var nameProperty = Properties.getPublic(command, "Name"); - var hiddenProperty = Properties.getPublic(command, "Hidden"); - if ((bool) hiddenProperty.GetValue(command)!) continue; - _commandRegistry.AddEntry((string) nameProperty.GetValue(command)!); + try { + // Get access to the rich text string inside the MessageData class + _commandInputFieldField = Fields.getPrivate(typeof(Console), "CommandInputField"); + + CommandConsole commandConsole = CommandConsole.Current; + // ICommandRegistryInternal + var registry = Fields.getPrivate(typeof(CommandConsole), "CommandRegistry").GetValue(commandConsole); + // CommandRegistry.CommandCollectionByName + var commandCollection = Properties.get(registry, "AllRegisteredCommands").GetValue(registry); + // IEnumerable + var commands = (IEnumerable) Methods.getPublic(commandCollection, "EnumerateAllCommands") + .Invoke(commandCollection, null)!; + + foreach (var command in commands) { + var nameProperty = Properties.getPublic(command, "Name"); + var hiddenProperty = Properties.getPublic(command, "Hidden"); + if ((bool) hiddenProperty.GetValue(command)!) continue; + _commandRegistry.AddEntry((string) nameProperty.GetValue(command)!); + } + } catch (AccessHelperException e) { + _logger.Error($"Error occured while preparing command history patch: {e}"); + return false; } return true; @@ -52,13 +58,13 @@ namespace ConsoleImprovements.Client { private static void PatchUpdate(Harmony harmony) { var methodTarget = Methods.getPrivate(typeof(Console), "Update"); - var methodHook = Methods.get(typeof(CommandHistoryPatch), nameof(HookUpdate)); + var methodHook = Methods.get(typeof(CommandInputPatch), nameof(HookUpdate)); harmony.Patch(methodTarget, prefix: new HarmonyMethod(methodHook)); } private static void PatchCommandSubmit(Harmony harmony) { var methodTarget = Methods.getPrivate(typeof(Console), "OnCommandInputFieldSubmit"); - var methodHook = Methods.get(typeof(CommandHistoryPatch), nameof(HookCommandSubmit)); + var methodHook = Methods.get(typeof(CommandInputPatch), nameof(HookCommandSubmit)); harmony.Patch(methodTarget, prefix: new HarmonyMethod(methodHook)); } diff --git a/ConsoleImprovements/ConsoleImprovements/src/client/ConsoleImprovementsMod.cs b/ConsoleImprovements/ConsoleImprovements/src/client/ConsoleImprovementsMod.cs index 0d6bf56..a4cecec 100644 --- a/ConsoleImprovements/ConsoleImprovements/src/client/ConsoleImprovementsMod.cs +++ b/ConsoleImprovements/ConsoleImprovements/src/client/ConsoleImprovementsMod.cs @@ -11,8 +11,8 @@ namespace ConsoleImprovements.Client { Logger.Info("Message Copy Patch Successful"); } - if (CommandHistoryPatch.Prepare(Logger)) { - CommandHistoryPatch.Inject(harmony); + if (CommandInputPatch.Prepare(Logger)) { + CommandInputPatch.Inject(harmony); Logger.Info("Command History Patch Successful"); } diff --git a/ConsoleImprovements/ConsoleImprovements/src/client/MessageCopyPatch.cs b/ConsoleImprovements/ConsoleImprovements/src/client/MessageCopyPatch.cs index f2d0197..82b27bf 100644 --- a/ConsoleImprovements/ConsoleImprovements/src/client/MessageCopyPatch.cs +++ b/ConsoleImprovements/ConsoleImprovements/src/client/MessageCopyPatch.cs @@ -16,17 +16,22 @@ namespace ConsoleImprovements.Client { public static bool Prepare(ILogicLogger logger) { _logger = logger; - // Get the private RemoveRichTextTags method of Message - _removeRichTextTagsMethod = Methods.getPrivate(typeof(Message), "RemoveRichTextTags"); + try { + // Get the private RemoveRichTextTags method of Message + _removeRichTextTagsMethod = Methods.getPrivate(typeof(Message), "RemoveRichTextTags"); - // Get access to the Data field of Message, which is of internal type MessageData - _dataField = Fields.getPrivate(typeof(Message), "Data"); + // Get access to the Data field of Message, which is of internal type MessageData + _dataField = Fields.getPrivate(typeof(Message), "Data"); - // Get access to the rich text string inside the MessageData class - _messageRichTextField = Fields.getPublic(_dataField.FieldType, "messageRichText"); + // Get access to the rich text string inside the MessageData class + _messageRichTextField = Fields.getPublic(_dataField.FieldType, "messageRichText"); - // Get access to the message type enum inside the MessageData class - _messageTypeField = Fields.getPublic(_dataField.FieldType, "messageType"); + // Get access to the message type enum inside the MessageData class + _messageTypeField = Fields.getPublic(_dataField.FieldType, "messageType"); + } catch (AccessHelperException e) { + _logger.Error($"Error occured while preparing message copy patch: {e}"); + return false; + } return true; }