Skip to content

Commit 0df400e

Browse files
committed
Add chat recall (scroll through previous messages)
1 parent 06b59af commit 0df400e

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- Removes the time penalty after disconnecting from too many lobbies
3535
- 💬 Chat Features
3636
- Use `Ctrl + C` and `Ctrl + V` to copy-paste chat messages
37+
- Scroll through your previously sent chat messages using the Up and Down arrow keys
3738
- Be able to send URLs and Email addresses
3839
- Increase the character limit from 100 to 120
3940
- Modify the maximum amount of chat messages to keep in the chat history

src/AUnlocker.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public override void Load()
9292

9393
AddComponent<KeybindListener>().Plugin = this;
9494
HudManager_Start_Patch.Plugin = this;
95+
ChatJailbreak_ChatController_Update_Postfix.Plugin = this;
9596
}
9697
}
9798

src/ChatPatches.cs

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
using HarmonyLib;
22
using UnityEngine;
3+
using static AUnlocker.ChatHistory_ChatController_SendChat_Prefix;
34

45
namespace AUnlocker;
56

67
[HarmonyPatch(typeof(ChatController), nameof(ChatController.Update))]
78
public static class ChatJailbreak_ChatController_Update_Postfix
89
{
10+
public static AUnlocker Plugin { get; internal set; }
11+
12+
// CurrentHistorySelection: -1 = no selection, 0 = first message, Count - 1 = last message
13+
public static int CurrentHistorySelection = -1;
14+
private static string inProgressMessage = "";
15+
private static bool isNavigatingHistory = false;
16+
917
/// <summary>
10-
/// Remove the chat cooldown and the character limit.
18+
/// Remove the chat cooldown and the character limit. Add the ability to scroll through previous chat messages using the up and down arrow keys.
1119
/// </summary>
1220
/// <param name="__instance">The <c>ChatController</c> instance.</param>
1321
public static void Postfix(ChatController __instance)
@@ -33,6 +41,45 @@ public static void Postfix(ChatController __instance)
3341
__instance.freeChatField.textArea.allowAllCharacters = true;
3442
__instance.freeChatField.textArea.characterLimit = 120; // above 120 characters anti-cheat will kick you
3543
}
44+
45+
// User is trying to navigate up the chat history
46+
if (Input.GetKeyDown(KeyCode.UpArrow) && ChatHistory.Count > 0)
47+
{
48+
if (!isNavigatingHistory)
49+
{
50+
// Store the in-progress text so we can restore it later
51+
inProgressMessage = __instance.freeChatField.textArea.text;
52+
isNavigatingHistory = true;
53+
}
54+
55+
if (CurrentHistorySelection == 0)
56+
{
57+
SoundManager.Instance.PlaySound(__instance.warningSound, false);
58+
Plugin.Log.LogInfo("You have reached the end of your chat history.");
59+
}
60+
else
61+
{
62+
// Ensure the index (current selection) is within bounds of the ChatHistory list (0 to Count - 1)
63+
CurrentHistorySelection = Mathf.Clamp(--CurrentHistorySelection, 0, ChatHistory.Count - 1);
64+
__instance.freeChatField.textArea.SetText(ChatHistory[CurrentHistorySelection]);
65+
}
66+
}
67+
68+
// User is trying to navigate down the chat history
69+
if (Input.GetKeyDown(KeyCode.DownArrow) && ChatHistory.Count > 0)
70+
{
71+
CurrentHistorySelection++;
72+
if (CurrentHistorySelection < ChatHistory.Count)
73+
{
74+
__instance.freeChatField.textArea.SetText(ChatHistory[CurrentHistorySelection]);
75+
}
76+
// User has navigated past the most recent message, restore the in-progress text
77+
else
78+
{
79+
__instance.freeChatField.textArea.SetText(inProgressMessage);
80+
isNavigatingHistory = false;
81+
}
82+
}
3683
}
3784
}
3885

@@ -98,6 +145,29 @@ public static bool Prefix(ChatController __instance)
98145
}
99146
}
100147

148+
[HarmonyPatch(typeof(ChatController), nameof(ChatController.SendChat))]
149+
public static class ChatHistory_ChatController_SendChat_Prefix
150+
{
151+
// ChatHistory: index 0 = earliest message, highest index = latest message
152+
public static readonly List<string> ChatHistory = [];
153+
154+
/// <summary>
155+
/// When sending a chat message (either via pressing enter or clicking the send button), add it to the chat history.
156+
/// </summary>
157+
/// <param name="__instance">The <c>ChatController</c> instance.</param>
158+
/// <returns><c>false</c> to skip the original method, <c>true</c> to allow the original method to run.</returns>
159+
public static bool Prefix(ChatController __instance)
160+
{
161+
var text = __instance.freeChatField.textArea.text;
162+
// Add to chat history if empty or not the same as the previous message
163+
// Also this intentionally allows empty / whitespace-only messages to be added to history
164+
if (ChatHistory.LastOrDefault() != text)
165+
ChatHistory.Add(text);
166+
ChatJailbreak_ChatController_Update_Postfix.CurrentHistorySelection = ChatHistory.Count;
167+
return true;
168+
}
169+
}
170+
101171
[HarmonyPatch(typeof(TextBoxTMP), nameof(TextBoxTMP.IsCharAllowed))]
102172
public static class AllowAllCharacters_TextBoxTMP_IsCharAllowed_Prefix
103173
{

0 commit comments

Comments
 (0)