/
main.cpp
341 lines (278 loc) · 8.55 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "obse_common\SafeWrite.h"
enum
{
kSpclChar_Backspace = 0x80000000,
kSpclChar_LeftArrow = 0x80000001,
kSpclChar_RightArrow = 0x80000002,
kSpclChar_Delete = 0x80000007,
kSpclChar_Enter = 0x80000008,
};
void CheckCTRLHotkeys();
bool versionCheck(const OBSEInterface* obse);
void GetClipboardText(char **buffer);
const UInt32 ConsoleManager_GetSingleton = 0x585C10;
const UInt32 kConsoleSendInput = 0x586000;
const UInt32 hookLocation = 0x583550;
const int MAX_BUFFER_SIZE = 512;
static char *clipboardText = (char*)malloc(MAX_BUFFER_SIZE);
#define ClearLines(consoleManager) ThisStdCall(0x573880, consoleManager+1);
#define RefreshConsoleOutput(consoleManager) ThisStdCall(0x585620, consoleManager);
extern "C" {
bool OBSEPlugin_Query(const OBSEInterface* obse, PluginInfo* info)
{
info->infoVersion = PluginInfo::kInfoVersion;
info->name = "No Scroll Switch POV";
info->version = 1;
return true;
}
bool OBSEPlugin_Load(const OBSEInterface* obse)
{
// SafeWrite8(0x671A8D, 0xEB); // 3rd -> 1st
// SafeWrite8(0x671AD0, 0xEB); // 1st -> 3rd
// SafeWrite8(0x672A01, 0xEB); // jump while aiming
WriteRelCall(hookLocation, UInt32(CheckCTRLHotkeys));
return true;
}
};
bool __fastcall PrintClipBoardToConsoleInput(UInt32 *consoleMgr, UInt32 dummyEDX, UInt32 inKey) {
GetClipboardText(&clipboardText);
for (int i = 0, c = clipboardText[0]; c != '\0' && i < MAX_BUFFER_SIZE; i++) {
c = clipboardText[i];
switch (c) {
// replace newlines with spaces
case '\n':
ThisStdCall(kConsoleSendInput, consoleMgr, ' ');
break;
// remove pipe and \r characters
case '\r':
case '|':
break;
default:
ThisStdCall(kConsoleSendInput, consoleMgr, clipboardText[i]);
}
}
return true;
}
void GetClipboardText(char **buffer) {
// Try opening the clipboard
if (!OpenClipboard(NULL)) {
// clipboard empty so return empty string
*buffer[0] = '\0';
return;
}
// Get handle of clipboard object for ANSI text
HANDLE hData = GetClipboardData(CF_TEXT);
if (hData == NULL) {
CloseClipboard();
// clipboard empty so return empty string
*buffer[0] = '\0';
return;
}
// Lock the handle to get the actual text pointer
char *pszText = static_cast<char *>(GlobalLock(hData));
if (pszText == NULL) {
GlobalUnlock(hData);
CloseClipboard();
// clipboard empty so return empty string
*buffer[0] = '\0';
return;
}
// Save text in a string class instance
std::string text(pszText);
// Release the lock and clipboard
GlobalUnlock(hData);
CloseClipboard();
// copy clipboard text into buffer
strncpy(*buffer, text.c_str(), MAX_BUFFER_SIZE);
}
bool __fastcall ClearConsoleOutput(UInt32 *consoleMgr, UInt32 dummyEDX, UInt32 inKey) {
ClearLines(consoleMgr); // clears the TextList printedLines
*(consoleMgr + 11) = 0;
RefreshConsoleOutput(consoleMgr); // refreshes the output so changes show
return true;
}
bool isCtrlHeld() {
SHORT ctrlKeyState = GetAsyncKeyState(VK_CONTROL);
// Test high bit - if set, key was down when GetAsyncKeyState was called.
return ((1 << 15) & ctrlKeyState);
}
int indexOfChar(char *text, char c) {
int index = 0;
for (; text[index] != '\0'; index++)
if (text[index] == c)
return index;
return -1;
}
/* check 0x14 for | character then check each row starting at the bottom
* this is a hacky solution until I work out how to get the address properly
* - it fails if the console output has a line with one | in it.
*/
char *getConsoleInputString() {
static const int CONSOLE_TEXT_BASE_ADDR = 0xB3A6A4;
static const int LINE_STRUCT_SIZE = 0x1C;
static const int firstTextOffset = 0x10;
static const int lastOffset = 0x240;
int consoleLineAddress = *((int*)CONSOLE_TEXT_BASE_ADDR) + firstTextOffset;
if (!consoleLineAddress) return NULL;
char* consoleLine = *(char**)consoleLineAddress;
if (consoleLine && indexOfChar(consoleLine, '|') > -1) return consoleLine;
for (int i = lastOffset; i > firstTextOffset; i -= LINE_STRUCT_SIZE) {
consoleLineAddress = *((int*)CONSOLE_TEXT_BASE_ADDR) + i;
if (consoleLineAddress != NULL) {
consoleLine = *((char**)consoleLineAddress);
if (consoleLine && indexOfChar(consoleLine, '|') > -1) return consoleLine;
}
}
return NULL;
}
int getCharsSinceSpace(char *text) {
char *barPos = strchr(text, '|');
int numChars = 0;
while (barPos != text && !isalnum(*--barPos)) numChars++;
while (barPos >= text && isalnum(*barPos--)) numChars++;
return numChars;
}
int getCharsTillSpace(char *text) {
char *barPos = strchr(text, '|');
if (!barPos) return 0;
int numChars = 0;
while (isalnum(*++barPos)) numChars++;
while (*barPos && !isalnum(*barPos++)) numChars++;
return numChars;
}
bool __fastcall DeletePreviousWord(UInt32 *consoleMgr, UInt32 dummyEDX, UInt32 inKey) {
char *consoleInput = getConsoleInputString();
if (!consoleInput || !(*consoleInput)) return true;
int charsToDelete = getCharsSinceSpace(consoleInput);
for (; charsToDelete > 0; charsToDelete--) {
ThisStdCall(kConsoleSendInput, consoleMgr, kSpclChar_Backspace);
}
return true;
}
bool __fastcall DeleteNextWord(UInt32 *consoleMgr, UInt32 dummyEDX, UInt32 inKey) {
char *consoleInput = getConsoleInputString();
if (!consoleInput || !(*consoleInput)) return true;
int charsToDelete = getCharsTillSpace(consoleInput);
for (; charsToDelete > 0; charsToDelete--) {
ThisStdCall(kConsoleSendInput, consoleMgr, kSpclChar_Delete);
}
return true;
}
bool __fastcall MoveToStartOfWord(UInt32 *consoleMgr, UInt32 dummyEDX, UInt32 inKey) {
char *consoleInput = getConsoleInputString();
if (!consoleInput || !(*consoleInput)) return true;
int charsToMove = getCharsSinceSpace(consoleInput);
for (; charsToMove > 0; charsToMove--) {
ThisStdCall(kConsoleSendInput, consoleMgr, kSpclChar_LeftArrow);
}
return true;
}
bool __fastcall MoveToEndOfWord(UInt32 *consoleMgr, UInt32 dummyEDX, UInt32 inKey) {
char *consoleInput = getConsoleInputString();
if (!consoleInput || !(*consoleInput)) return true;
int charsToMove = getCharsTillSpace(consoleInput);
for (; charsToMove > 0; charsToMove--) {
ThisStdCall(kConsoleSendInput, consoleMgr, kSpclChar_RightArrow);
}
return true;
}
bool __fastcall ClearInputString(UInt32 *consoleMgr, UInt32 dummyEDX, UInt32 inKey) {
char *buffer = getConsoleInputString();
buffer[0] = '\0';
ThisStdCall(kConsoleSendInput, consoleMgr, kSpclChar_RightArrow);
return true;
}
void removeChar(char *str, const char garbage) {
char *src, *dst;
for (src = dst = str; *src != '\0'; src++) {
*dst = *src;
if (*dst != garbage) dst++;
}
*dst = '\0';
}
bool __fastcall CopyInputToClipboard(UInt32 *consoleMgr, UInt32 dummyEDX, UInt32 inKey) {
char *inputString = getConsoleInputString();
char* sanitisedInput = (char*)malloc(strlen(inputString) + 1);
strcpy(sanitisedInput, inputString);
removeChar(sanitisedInput, '|');
OpenClipboard(NULL);
EmptyClipboard();
HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, strlen(sanitisedInput) + 1);
if (!hg) {
CloseClipboard();
return true;
}
memcpy(GlobalLock(hg), sanitisedInput, strlen(inputString) + 1);
GlobalUnlock(hg);
SetClipboardData(CF_TEXT, hg);
CloseClipboard();
GlobalFree(hg);
return true;
}
__declspec(naked) void CheckCTRLHotkeys()
{
__asm
{
movsx edi, [ecx + 0x31] //ConsoleManager::isConsoleOpen
test edi, edi
jle consoleMenuNotOpen
push ecx
call isCtrlHeld
pop ecx
jz handleNormalInput
checkHotkeys :
mov edi, [esp + 4]
cmp edi, kSpclChar_Backspace
jz handleBack
cmp edi, kSpclChar_LeftArrow
jz handleLeft
cmp edi, kSpclChar_RightArrow
jz handleRight
cmp edi, kSpclChar_Delete
jz handleDelete
or bl, 0x20 // Change to lower-case
cmp edi, 'v'
jz handleV
cmp edi, 'l'
jz handleL
cmp edi, 'c'
jz handleC
cmp edi, 'x'
jz handleX
mov al, 1
retn 4
handleBack:
jmp DeletePreviousWord
handleLeft :
jmp MoveToStartOfWord
handleRight :
jmp MoveToEndOfWord
handleDelete :
jmp DeleteNextWord
handleV :
jmp PrintClipBoardToConsoleInput
handleL :
jmp ClearConsoleOutput
handleC :
jmp CopyInputToClipboard
handleX :
jmp ClearInputString
consoleMenuNotOpen :
mov edi, [esp + 4]
handleNormalInput :
jmp kConsoleSendInput
}
}
bool versionCheck(const OBSEInterface* obse) {
if (obse->isEditor) return false;
if (obse->obseVersion < OBSE_VERSION_INTEGER) {
_ERROR("OBSE version too old (got %08X expected at least %08X)", obse->obseVersion, OBSE_VERSION_INTEGER);
return false;
}
if (obse->oblivionVersion!= OBLIVION_VERSION)
{
_ERROR("incorrect runtime version (got %08X need %08X)", obse->oblivionVersion, OBLIVION_VERSION);
return false;
}
return true;
}