1
1
using Celeste . Mod . Core ;
2
2
using Celeste . Mod . TASHelper . Utils ;
3
+ using Microsoft . Xna . Framework . Input ;
3
4
using Mono . Cecil . Cil ;
4
5
using Monocle ;
5
6
using MonoMod . Cil ;
@@ -14,7 +15,11 @@ public static class ConsoleEnhancement {
14
15
15
16
private static bool lastOpen = false ;
16
17
18
+ private static int historyLineShift = 0 ;
17
19
20
+ private static int origValue ;
21
+
22
+ private static bool historyScrollEnabled => TasHelperSettings . EnableScrollableHistoryLog ;
18
23
public static void SetOpenConsole ( ) {
19
24
if ( Manager . Running && ! lastOpen ) {
20
25
openConsole = true ;
@@ -24,16 +29,24 @@ public static bool GetOpenConsole() { // openConsole.getter may not be called (e
24
29
return openConsole ;
25
30
}
26
31
32
+ public static void GoBackToBottom ( ) {
33
+ historyLineShift = 0 ;
34
+ }
35
+
27
36
[ Load ]
28
37
public static void Load ( ) {
29
38
IL . Monocle . Commands . UpdateClosed += ILCommandUpdateClosed ;
30
39
On . Celeste . Level . BeforeRender += OnLevelBeforeRender ;
40
+ IL . Monocle . Commands . Render += ILCommandsRender ;
41
+ On . Monocle . Commands . UpdateOpen += OnCommandUpdateOpen ;
31
42
}
32
43
33
44
[ Unload ]
34
45
public static void Unload ( ) {
35
46
IL . Monocle . Commands . UpdateClosed -= ILCommandUpdateClosed ;
36
47
On . Celeste . Level . BeforeRender -= OnLevelBeforeRender ;
48
+ IL . Monocle . Commands . Render -= ILCommandsRender ;
49
+ On . Monocle . Commands . UpdateOpen -= OnCommandUpdateOpen ;
37
50
}
38
51
39
52
[ Initialize ]
@@ -42,6 +55,74 @@ public static void Initialize() {
42
55
typeof ( Manager ) . GetMethod ( "DisableRun" ) . HookAfter ( MinorBugFixer ) ;
43
56
}
44
57
58
+ private static void ILCommandsRender ( ILContext context ) {
59
+ ILCursor cursor = new ILCursor ( context ) ;
60
+ if ( cursor . TryGotoNext (
61
+ ins => ins . MatchLdarg ( 0 ) ,
62
+ ins => ins . MatchLdfld < Monocle . Commands > ( "drawCommands" ) ,
63
+ ins => ins . MatchCallOrCallvirt < List < Monocle . Commands . Line > > ( "get_Count" ) ,
64
+ ins => ins . MatchLdcI4 ( 0 ) ,
65
+ ins => ins . OpCode == OpCodes . Ble ,
66
+ ins => ins . MatchLdloc ( 1 ) ) ) {
67
+ cursor . Index += 5 ;
68
+ ILLabel end = ( ILLabel ) cursor . Prev . Operand ;
69
+ cursor . Emit ( OpCodes . Ldarg_0 ) ;
70
+ cursor . EmitDelegate ( BeforeAction ) ;
71
+ cursor . GotoLabel ( end ) ;
72
+ cursor . Emit ( OpCodes . Ldarg_0 ) ;
73
+ cursor . EmitDelegate ( AfterAction ) ;
74
+ }
75
+ }
76
+
77
+ private static void BeforeAction ( Monocle . Commands commands ) {
78
+ origValue = commands . firstLineIndexToDraw ;
79
+ commands . firstLineIndexToDraw = Calc . Clamp ( commands . firstLineIndexToDraw + historyLineShift , 0 , Math . Max ( commands . drawCommands . Count - 1 , 0 ) ) ;
80
+ }
81
+
82
+ private static void AfterAction ( Monocle . Commands commands ) {
83
+ if ( commands . drawCommands . Count > 0 ) {
84
+ historyLineShift = commands . firstLineIndexToDraw - origValue ; // this automatically bounds our shift
85
+ commands . firstLineIndexToDraw = origValue ;
86
+ }
87
+ }
88
+
89
+ private static int repeatCounter = 0 ;
90
+
91
+ private static void OnCommandUpdateOpen ( On . Monocle . Commands . orig_UpdateOpen orig , Monocle . Commands commands ) {
92
+ orig ( commands ) ;
93
+ if ( historyScrollEnabled ) {
94
+ bool controlPressed = commands . currentState [ Keys . LeftControl ] == KeyState . Down || commands . currentState [ Keys . RightControl ] == KeyState . Down ;
95
+ if ( commands . currentState [ Keys . PageUp ] == KeyState . Down && commands . oldState [ Keys . PageUp ] == KeyState . Up ) {
96
+ historyLineShift += ( Engine . ViewHeight - 100 ) / 30 ;
97
+ }
98
+ else if ( commands . currentState [ Keys . PageDown ] == KeyState . Down && commands . oldState [ Keys . PageDown ] == KeyState . Up ) {
99
+ if ( controlPressed ) {
100
+ historyLineShift = 0 ;
101
+ }
102
+ else {
103
+ historyLineShift -= ( Engine . ViewHeight - 100 ) / 30 ;
104
+ }
105
+ }
106
+ else if ( commands . currentState [ Keys . Up ] == KeyState . Down && controlPressed ) {
107
+ repeatCounter += 1 ;
108
+ while ( repeatCounter >= 6 ) {
109
+ repeatCounter -= 2 ;
110
+ historyLineShift += 1 ;
111
+ }
112
+ }
113
+ else if ( commands . currentState [ Keys . Down ] == KeyState . Down && controlPressed ) {
114
+ repeatCounter += 1 ;
115
+ while ( repeatCounter >= 6 ) {
116
+ repeatCounter -= 2 ;
117
+ historyLineShift -= 1 ;
118
+ }
119
+ }
120
+ else {
121
+ repeatCounter = 0 ;
122
+ }
123
+ }
124
+ }
125
+
45
126
private static void MinorBugFixer ( ) {
46
127
// if open debugconsole and close it when in tas, then exit tas (without running any frame), debugconsole will show up
47
128
0 commit comments