11using HarmonyLib ;
22using UnityEngine ;
3+ using static AUnlocker . ChatHistory_ChatController_SendChat_Prefix ;
34
45namespace AUnlocker ;
56
67[ HarmonyPatch ( typeof ( ChatController ) , nameof ( ChatController . Update ) ) ]
78public 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 ) ) ]
102172public static class AllowAllCharacters_TextBoxTMP_IsCharAllowed_Prefix
103173{
0 commit comments