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,6 +24,7 @@ namespace ConsoleImprovements.Client {
public static bool Prepare(ILogicLogger logger) {
_logger = logger;
try {
// Get access to the rich text string inside the MessageData class
_commandInputFieldField = Fields.getPrivate(typeof(Console), "CommandInputField");
@ -33,7 +34,8 @@ namespace ConsoleImprovements.Client {
// CommandRegistry.CommandCollectionByName
var commandCollection = Properties.get(registry, "AllRegisteredCommands").GetValue(registry);
// IEnumerable<Command>
var commands = (IEnumerable) Methods.getPublic(commandCollection, "EnumerateAllCommands").Invoke(commandCollection, null)!;
var commands = (IEnumerable) Methods.getPublic(commandCollection, "EnumerateAllCommands")
.Invoke(commandCollection, null)!;
foreach (var command in commands) {
var nameProperty = Properties.getPublic(command, "Name");
@ -41,6 +43,10 @@ namespace ConsoleImprovements.Client {
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,6 +16,7 @@ namespace ConsoleImprovements.Client {
public static bool Prepare(ILogicLogger logger) {
_logger = logger;
try {
// Get the private RemoveRichTextTags method of Message
_removeRichTextTagsMethod = Methods.getPrivate(typeof(Message), "RemoveRichTextTags");
@ -27,6 +28,10 @@ namespace ConsoleImprovements.Client {
// 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