parent
b179e4f2f5
commit
2b62f9fa4d
@ -1,111 +1,17 @@
|
||||
using System.Reflection;
|
||||
using EccsLogicWorldAPI.Shared.AccessHelper;
|
||||
using FancyPantsConsole;
|
||||
using HarmonyLib;
|
||||
using JimmysUnityUtilities;
|
||||
using LogicAPI.Client;
|
||||
using LogicLog;
|
||||
|
||||
namespace ConsoleImprovements.Client {
|
||||
public class ConsoleImprovementsMod : ClientMod {
|
||||
public new static ILogicLogger Logger = null!;
|
||||
private static bool _initSuccessful;
|
||||
private static MethodInfo? _removeRichTextTagsMethod;
|
||||
private static FieldInfo? _dataProperty;
|
||||
private static FieldInfo? _messageRichTextField;
|
||||
private static FieldInfo? _messageTypeField;
|
||||
|
||||
protected override void Initialize() {
|
||||
Logger = base.Logger;
|
||||
|
||||
Prepare();
|
||||
Inject();
|
||||
|
||||
Logger.Info("ConsoleImprovements mod loaded");
|
||||
}
|
||||
|
||||
private static void Prepare() {
|
||||
_initSuccessful = true;
|
||||
|
||||
// Get the private RemoveRichTextTags method of Message
|
||||
_removeRichTextTagsMethod = AccessTools.Method(
|
||||
typeof(Message),
|
||||
"RemoveRichTextTags",
|
||||
new[] {typeof(string)}
|
||||
);
|
||||
if (_removeRichTextTagsMethod == null) {
|
||||
Logger.Error("Cannot get method RemoveRichTextTags of message");
|
||||
_initSuccessful = false;
|
||||
}
|
||||
|
||||
// Get access to the Data property of Message, which is of internal type MessageData
|
||||
_dataProperty = AccessTools.Field(typeof(Message), "Data");
|
||||
if (_dataProperty == null) {
|
||||
Logger.Error("Cannot get field Data of message");
|
||||
_initSuccessful = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Get access to the rich text string inside the MessageData class
|
||||
_messageRichTextField = AccessTools.Field(_dataProperty.FieldType, "messageRichText");
|
||||
if (_messageRichTextField == null) {
|
||||
Logger.Error("Cannot get field messageRichText of message data");
|
||||
_initSuccessful = false;
|
||||
}
|
||||
|
||||
// Get access to the message type enum inside the MessageData class
|
||||
_messageTypeField = AccessTools.Field(_dataProperty.FieldType, "messageType");
|
||||
if (_messageTypeField == null) {
|
||||
Logger.Error("Cannot get field messageRichText of message data");
|
||||
_initSuccessful = false;
|
||||
}
|
||||
}
|
||||
|
||||
private static void Inject() {
|
||||
var methodTarget = Methods.getPrivate(typeof(Message), "CopyMessageToClipboard");
|
||||
var methodHook = Methods.getPrivateStatic(typeof(ConsoleImprovementsMod), nameof(Hook));
|
||||
var harmony = new Harmony(nameof(ConsoleImprovementsMod));
|
||||
harmony.Patch(methodTarget, prefix: new HarmonyMethod(methodHook));
|
||||
}
|
||||
|
||||
// Prefix the CopyMessageToClipboard method with this one
|
||||
// ReSharper disable once InconsistentNaming
|
||||
private static bool Hook(Message __instance) {
|
||||
if (!_initSuccessful) {
|
||||
return true; // Resume original functionality
|
||||
}
|
||||
|
||||
var dataInstance = _dataProperty!.GetValue(__instance);
|
||||
if (dataInstance == null) {
|
||||
Logger.Error("Cannot get instance of MessageData");
|
||||
return true; // Resume original functionality
|
||||
if (MessageCopyPatch.Prepare(Logger)) {
|
||||
MessageCopyPatch.Inject(harmony);
|
||||
}
|
||||
|
||||
MessageType messageType = (MessageType) _messageTypeField!.GetValue(dataInstance)!;
|
||||
|
||||
// Check if the message is user inputted command
|
||||
if (messageType != MessageType.UserInput) {
|
||||
return true; // Resume original functionality
|
||||
}
|
||||
|
||||
string? messageRichText = _messageRichTextField!.GetValue(dataInstance) as string;
|
||||
if (messageRichText == null) {
|
||||
Logger.Error("Cannot get messageRichText value");
|
||||
return true; // Resume original functionality
|
||||
}
|
||||
|
||||
string? removedTags = _removeRichTextTagsMethod!.Invoke(__instance, new object[] {messageRichText}) as string;
|
||||
if (removedTags == null) {
|
||||
Logger.Error("Failed to remove rich text tags");
|
||||
return true; // Resume original functionality
|
||||
}
|
||||
|
||||
if (removedTags.StartsWith("> ")) {
|
||||
removedTags = removedTags.Substring(2);
|
||||
}
|
||||
|
||||
ClipboardAccess.SetClipboardText(removedTags);
|
||||
return false; // Prevent original functionality.
|
||||
Logger.Info("ConsoleImprovements mod loaded");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
using System.Reflection;
|
||||
using EccsLogicWorldAPI.Shared.AccessHelper;
|
||||
using FancyPantsConsole;
|
||||
using HarmonyLib;
|
||||
using JimmysUnityUtilities;
|
||||
using LogicLog;
|
||||
|
||||
namespace ConsoleImprovements.Client {
|
||||
public class MessageCopyPatch {
|
||||
private static ILogicLogger _logger = null!;
|
||||
private static MethodInfo? _removeRichTextTagsMethod;
|
||||
private static FieldInfo? _dataProperty;
|
||||
private static FieldInfo? _messageRichTextField;
|
||||
private static FieldInfo? _messageTypeField;
|
||||
|
||||
public static bool Prepare(ILogicLogger logger) {
|
||||
_logger = logger;
|
||||
|
||||
// Get the private RemoveRichTextTags method of Message
|
||||
_removeRichTextTagsMethod = AccessTools.Method(
|
||||
typeof(Message),
|
||||
"RemoveRichTextTags",
|
||||
new[] {typeof(string)}
|
||||
);
|
||||
if (_removeRichTextTagsMethod == null) {
|
||||
_logger.Error("Cannot get method RemoveRichTextTags of message");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get access to the Data property of Message, which is of internal type MessageData
|
||||
_dataProperty = AccessTools.Field(typeof(Message), "Data");
|
||||
if (_dataProperty == null) {
|
||||
_logger.Error("Cannot get field Data of message");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get access to the rich text string inside the MessageData class
|
||||
_messageRichTextField = AccessTools.Field(_dataProperty.FieldType, "messageRichText");
|
||||
if (_messageRichTextField == null) {
|
||||
_logger.Error("Cannot get field messageRichText of message data");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get access to the message type enum inside the MessageData class
|
||||
_messageTypeField = AccessTools.Field(_dataProperty.FieldType, "messageType");
|
||||
if (_messageTypeField == null) {
|
||||
_logger.Error("Cannot get field messageRichText of message data");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void Inject(Harmony harmony) {
|
||||
var methodTarget = Methods.getPrivate(typeof(Message), "CopyMessageToClipboard");
|
||||
var methodHook = Methods.get(typeof(MessageCopyPatch), nameof(Hook));
|
||||
harmony.Patch(methodTarget, prefix: new HarmonyMethod(methodHook));
|
||||
}
|
||||
|
||||
// Prefix the CopyMessageToClipboard method with this one
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public static bool Hook(Message __instance) {
|
||||
var dataInstance = _dataProperty!.GetValue(__instance);
|
||||
if (dataInstance == null) {
|
||||
_logger.Error("Cannot get instance of MessageData");
|
||||
return true; // Resume original functionality
|
||||
}
|
||||
|
||||
MessageType messageType = (MessageType) _messageTypeField!.GetValue(dataInstance)!;
|
||||
|
||||
// Check if the message is user inputted command
|
||||
if (messageType != MessageType.UserInput) {
|
||||
return true; // Resume original functionality
|
||||
}
|
||||
|
||||
string? messageRichText = _messageRichTextField!.GetValue(dataInstance) as string;
|
||||
if (messageRichText == null) {
|
||||
_logger.Error("Cannot get messageRichText value");
|
||||
return true; // Resume original functionality
|
||||
}
|
||||
|
||||
string? removedTags =
|
||||
_removeRichTextTagsMethod!.Invoke(__instance, new object[] {messageRichText}) as string;
|
||||
if (removedTags == null) {
|
||||
_logger.Error("Failed to remove rich text tags");
|
||||
return true; // Resume original functionality
|
||||
}
|
||||
|
||||
if (removedTags.StartsWith("> ")) {
|
||||
removedTags = removedTags.Substring(2);
|
||||
}
|
||||
|
||||
ClipboardAccess.SetClipboardText(removedTags);
|
||||
return false; // Prevent original functionality.
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue