Handle exceptions in prepare methods

master
d4vid 3 months ago
parent 70684d140e
commit c5c261fc1b

@ -1,7 +1,7 @@
ID: D4VID_ConsoleImprovements ID: D4VID_ConsoleImprovements
Name: ConsoleImprovements Name: ConsoleImprovements
Author: D4VID Author: D4VID
Version: 1.2.0 Version: 1.2.1
Priority: -90 Priority: -90
ClientOnly: true ClientOnly: true
Dependencies: Dependencies:

@ -11,7 +11,7 @@ using TMPro;
using Console = FancyPantsConsole.Console; using Console = FancyPantsConsole.Console;
namespace ConsoleImprovements.Client { namespace ConsoleImprovements.Client {
public class CommandHistoryPatch { public class CommandInputPatch {
const int HistLength = 1000; const int HistLength = 1000;
private static ILogicLogger _logger = null!; private static ILogicLogger _logger = null!;
@ -24,22 +24,28 @@ namespace ConsoleImprovements.Client {
public static bool Prepare(ILogicLogger logger) { public static bool Prepare(ILogicLogger logger) {
_logger = logger; _logger = logger;
// Get access to the rich text string inside the MessageData class try {
_commandInputFieldField = Fields.getPrivate(typeof(Console), "CommandInputField"); // Get access to the rich text string inside the MessageData class
_commandInputFieldField = Fields.getPrivate(typeof(Console), "CommandInputField");
CommandConsole commandConsole = CommandConsole.Current;
// ICommandRegistryInternal CommandConsole commandConsole = CommandConsole.Current;
var registry = Fields.getPrivate(typeof(CommandConsole), "CommandRegistry").GetValue(commandConsole); // ICommandRegistryInternal
// CommandRegistry.CommandCollectionByName var registry = Fields.getPrivate(typeof(CommandConsole), "CommandRegistry").GetValue(commandConsole);
var commandCollection = Properties.get(registry, "AllRegisteredCommands").GetValue(registry); // CommandRegistry.CommandCollectionByName
// IEnumerable<Command> var commandCollection = Properties.get(registry, "AllRegisteredCommands").GetValue(registry);
var commands = (IEnumerable) Methods.getPublic(commandCollection, "EnumerateAllCommands").Invoke(commandCollection, null)!; // IEnumerable<Command>
var commands = (IEnumerable) Methods.getPublic(commandCollection, "EnumerateAllCommands")
foreach (var command in commands) { .Invoke(commandCollection, null)!;
var nameProperty = Properties.getPublic(command, "Name");
var hiddenProperty = Properties.getPublic(command, "Hidden"); foreach (var command in commands) {
if ((bool) hiddenProperty.GetValue(command)!) continue; var nameProperty = Properties.getPublic(command, "Name");
_commandRegistry.AddEntry((string) nameProperty.GetValue(command)!); 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; return true;
@ -52,13 +58,13 @@ namespace ConsoleImprovements.Client {
private static void PatchUpdate(Harmony harmony) { private static void PatchUpdate(Harmony harmony) {
var methodTarget = Methods.getPrivate(typeof(Console), "Update"); 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)); harmony.Patch(methodTarget, prefix: new HarmonyMethod(methodHook));
} }
private static void PatchCommandSubmit(Harmony harmony) { private static void PatchCommandSubmit(Harmony harmony) {
var methodTarget = Methods.getPrivate(typeof(Console), "OnCommandInputFieldSubmit"); 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)); harmony.Patch(methodTarget, prefix: new HarmonyMethod(methodHook));
} }

@ -11,8 +11,8 @@ namespace ConsoleImprovements.Client {
Logger.Info("Message Copy Patch Successful"); Logger.Info("Message Copy Patch Successful");
} }
if (CommandHistoryPatch.Prepare(Logger)) { if (CommandInputPatch.Prepare(Logger)) {
CommandHistoryPatch.Inject(harmony); CommandInputPatch.Inject(harmony);
Logger.Info("Command History Patch Successful"); Logger.Info("Command History Patch Successful");
} }

@ -16,17 +16,22 @@ namespace ConsoleImprovements.Client {
public static bool Prepare(ILogicLogger logger) { public static bool Prepare(ILogicLogger logger) {
_logger = logger; _logger = logger;
// Get the private RemoveRichTextTags method of Message try {
_removeRichTextTagsMethod = Methods.getPrivate(typeof(Message), "RemoveRichTextTags"); // 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 // Get access to the Data field of Message, which is of internal type MessageData
_dataField = Fields.getPrivate(typeof(Message), "Data"); _dataField = Fields.getPrivate(typeof(Message), "Data");
// Get access to the rich text string inside the MessageData class // Get access to the rich text string inside the MessageData class
_messageRichTextField = Fields.getPublic(_dataField.FieldType, "messageRichText"); _messageRichTextField = Fields.getPublic(_dataField.FieldType, "messageRichText");
// Get access to the message type enum inside the MessageData class // Get access to the message type enum inside the MessageData class
_messageTypeField = Fields.getPublic(_dataField.FieldType, "messageType"); _messageTypeField = Fields.getPublic(_dataField.FieldType, "messageType");
} catch (AccessHelperException e) {
_logger.Error($"Error occured while preparing message copy patch: {e}");
return false;
}
return true; return true;
} }

Loading…
Cancel
Save