Handle exceptions in prepare methods

master
d4vid 3 months ago
parent 70684d140e
commit c5c261fc1b

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

@ -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<Command>
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<Command>
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));
}

@ -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");
}

@ -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;
}

Loading…
Cancel
Save