Add a command history

master
d4vid 4 months ago
parent 2b62f9fa4d
commit 9e9a8b5d7d

@ -2,8 +2,9 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<RootNamespace/>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@ -31,5 +32,11 @@
<Reference Include="JimmysUnityUtilities"> <Reference Include="JimmysUnityUtilities">
<HintPath>$(LogicWorldGameLocation)\Logic_World_Data\Managed\JimmysUnityUtilities.dll</HintPath> <HintPath>$(LogicWorldGameLocation)\Logic_World_Data\Managed\JimmysUnityUtilities.dll</HintPath>
</Reference> </Reference>
<Reference Include="FancyInput">
<HintPath>$(LogicWorldGameLocation)\Logic_World_Data\Managed\FancyInput.dll</HintPath>
</Reference>
<Reference Include="Unity.TextMeshPro">
<HintPath>$(LogicWorldGameLocation)\Logic_World_Data\Managed\Unity.TextMeshPro.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -1,7 +1,7 @@
ID: D4VID_ConsoleImprovements ID: D4VID_ConsoleImprovements
Name: ConsoleImprovements Name: ConsoleImprovements
Author: D4VID Author: D4VID
Version: 0.1.0 Version: 0.2.0
Priority: 0 Priority: 0
ClientOnly: true ClientOnly: true
Dependencies: Dependencies:

@ -0,0 +1,98 @@
using System.Reflection;
using System.Collections.Generic;
using EccsLogicWorldAPI.Shared.AccessHelper;
using FancyInput;
using HarmonyLib;
using LogicLog;
using TMPro;
using Console = FancyPantsConsole.Console;
namespace ConsoleImprovements.Client {
public class CommandHistoryPatch {
const int HistLength = 1000;
private static ILogicLogger _logger = null!;
private static FieldInfo? _commandInputFieldField;
private static LinkedList<string> _history = new LinkedList<string>();
private static LinkedListNode<string>? _command;
public static bool Prepare(ILogicLogger logger) {
_logger = logger;
// Get access to the rich text string inside the MessageData class
_commandInputFieldField = AccessTools.Field(typeof(Console), "CommandInputField");
if (_commandInputFieldField == null) {
_logger.Error("Cannot get field CommandInputField of Console");
return false;
}
return true;
}
public static void Inject(Harmony harmony) {
PatchUpdate(harmony);
PatchCommandSubmit(harmony);
}
private static void PatchUpdate(Harmony harmony) {
var methodTarget = Methods.getPrivate(typeof(Console), "Update");
var methodHook = Methods.get(typeof(CommandHistoryPatch), 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));
harmony.Patch(methodTarget, prefix: new HarmonyMethod(methodHook));
}
// ReSharper disable once InconsistentNaming
public static bool HookUpdate(Console __instance) {
if (RawInput.UpArrow.DownThisFrame()) {
if (_command == null) {
if (_history.Count > 0) {
_command = _history.First;
UpdateCommandTextField(__instance, _command!.Value);
}
} else if (_command.Next != null) {
_command = _command.Next;
UpdateCommandTextField(__instance, _command!.Value);
}
}
if (RawInput.DownArrow.DownThisFrame()) {
if (_command != null) {
_command = _command.Previous;
UpdateCommandTextField(__instance, _command?.Value ?? "");
}
}
return true; // Resume original functionality
}
private static void UpdateCommandTextField(Console instance, string text) {
TMP_InputField? commandInputField = _commandInputFieldField!.GetValue(instance) as TMP_InputField;
if (commandInputField == null) {
_logger.Error("Cannot get CommandInputField value");
return;
}
commandInputField.text = text;
}
// ReSharper disable once InconsistentNaming
public static bool HookCommandSubmit(Console __instance, ref string line) {
// Keep the set max number of entries in the history
if (_history.Count >= HistLength) {
_history.RemoveLast();
}
_history.AddFirst(line);
_command = null; // Clear the command pointer
return true; // Resume original functionality
}
}
}

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

Loading…
Cancel
Save