-
Notifications
You must be signed in to change notification settings - Fork 13
/
Menus.cpp
388 lines (298 loc) · 9.4 KB
/
Menus.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include "ObjectRef.h"
#include "Menus.h"
#include "Camera.h"
#include "Utils.h"
#include "Settings.h"
#include "PatternScanner.h"
uintptr_t g_hudMenuNextFrameAddr = 0;
uintptr_t g_barterMenuDtorAddr = 0;
uintptr_t g_trainingMenuCtorAddr = 0;
uintptr_t g_trainingMenuDtorAddr = 0;
uintptr_t g_dialMenuVtblAddr = 0;
uintptr_t g_dialNextFrameAddr = 0;
namespace Tralala
{
bool g_isTrainingMenu = false;
void HUDMenuNextFrame_Hook(HUDMenu* hudMenu)
{
if (!Settings::bLetterBox)
return;
GFxValue messagesBlock;
if (hudMenu->hudBaseInstance.GetMember("MessagesBlock", &messagesBlock))
{
GFxValue::DisplayInfo dispInfo;
if (messagesBlock.GetDisplayInfo(&dispInfo))
{
static bool bInitDispInfo = false;
static double oriPosX = 0.0f;
static double oriPosY = 0.0f;
if (!bInitDispInfo)
{
oriPosX = dispInfo._x;
oriPosY = dispInfo._y;
bInitDispInfo = true;
}
MenuTopicManager* mtm = MenuTopicManager::GetSingleton();
if (!mtm)
return;
bool isDialogueMenuClose = *(bool*)g_isDialogueMenuCloseAddr;
UInt32 containerHandle = *(UInt32*)g_containerHandle;
UInt32 barterHandle = *(UInt32*)g_barterHandle;
UInt32 activeHandle = containerHandle;
if (barterHandle)
activeHandle = barterHandle;
if (mtm->isInDialogueState && !isDialogueMenuClose && !activeHandle && !g_isTrainingMenu && g_zoom)
{
if (Settings::fMessagePosX == -601.f)
dispInfo.SetPosition(oriPosX, Settings::fMessagePosY);
else
dispInfo.SetPosition(Settings::fMessagePosX, Settings::fMessagePosY);
messagesBlock.SetDisplayInfo(&dispInfo);
}
else
{
dispInfo.SetPosition(oriPosX, oriPosY);
messagesBlock.SetDisplayInfo(&dispInfo);
}
}
}
}
void BarterMenuDtor_Hook()
{
UInt32 invalidHandle = InvalidRefHandle();
*(UInt32*)g_barterHandle = invalidHandle;
}
void TrainingMenuCtor_Hook()
{
g_isTrainingMenu = true;
}
void TrainingMenuDtor_Hook()
{
g_isTrainingMenu = false;
}
void DialMenuNextFrame_Hook(IMenu* menu, float unk1, UInt32 unk2)
{
typedef void(*NextFrame_t)(IMenu*, float, UInt32);
NextFrame_t NextFrame = (NextFrame_t)g_dialNextFrameAddr;
GFxMovieView* view = (Tralala::GFxMovieView*)menu->view;
if (!view || !Settings::bHideDialogueMenu)
return NextFrame(menu, unk1, unk2);
GFxValue topicListHolder;
if (view->GetVariable(&topicListHolder, "_root.DialogueMenu_mc.TopicListHolder"))
{
GFxValue::DisplayInfo displayInfo;
if (topicListHolder.GetDisplayInfo(&displayInfo))
{
MenuTopicManager* mtm = MenuTopicManager::GetSingleton();
GFxValue greetingState(0);
GFxValue topicShownState(1);
GFxValue topicClickedState(2);
GFxValue curState;
if (!g_zoom)
{
if (curState.GetType() == 3 && curState.data.number == 1)
displayInfo.SetVisible(true);
topicListHolder.SetDisplayInfo(&displayInfo);
return NextFrame(menu, unk1, unk2);
}
view->GetVariable(&curState, "_root.DialogueMenu_mc.eMenuState");
PlayerCamera* camera = PlayerCamera::GetSingleton();
if (Settings::bSwitchTarget && (Settings::bForceThirdPerson || camera->IsCameraThirdPerson()))
{
if (camera->cameraRefHandle == PlayerRefHandle)
{
if (curState.GetType() == 3 && curState.data.number == 1)
view->SetVariable("_root.DialogueMenu_mc.eMenuState", &topicClickedState, 0);
displayInfo.SetVisible(false);
}
else
{
view->SetVariable("_root.DialogueMenu_mc.eMenuState", &topicShownState, 0);
displayInfo.SetVisible(true);
}
}
else
{
if ((curState.GetType() == 3 && curState.data.number == 0) || (mtm->unk70 && !mtm->unkB9))
{
view->SetVariable("_root.DialogueMenu_mc.eMenuState", &topicClickedState, 0);
displayInfo.SetVisible(false);
}
else
{
if (curState.GetType() == 3 && curState.data.number == 1)
displayInfo.SetVisible(true);
}
}
topicListHolder.SetDisplayInfo(&displayInfo);
}
}
return NextFrame(menu, unk1, unk2);
}
}
#include "skse64_common/Utilities.h"
#include "skse64_common/Relocation.h"
#include "skse64_common/BranchTrampoline.h"
#include "skse64_common/SafeWrite.h"
#include "xbyak/xbyak.h"
namespace Menus
{
void GetAddresses()
{
const std::array<BYTE, 10> hudpattern = { 0xFF, 0x90, 0x80, 0x00, 0x00, 0x00, 0x48, 0x8B, 0x4E, 0x10 };
g_hudMenuNextFrameAddr = (uintptr_t)scan_memory(hudpattern, 0x2A, false);
//const std::array<BYTE, 9> barterpattern = { 0x90, 0x48, 0x83, 0xBF, 0x80, 0x00, 0x00, 0x00, 0x00 };
//g_barterMenuDtorAddr = (uintptr_t)scan_memory(barterpattern, 0x68, true);
const std::array<BYTE, 11> barterpattern = { 0x48, 0x83, 0xC4, 0x30, 0x5F, 0xC3, 0xBA, 0xA8, 0x00, 0x00, 0x00 };
g_barterMenuDtorAddr = (uintptr_t)scan_memory(barterpattern, 0x64, false);
const std::array<BYTE, 8> traincpattern = { 0x83, 0x4F, 0x1C, 0x04, 0xC6, 0x47, 0x18, 0x03 };
g_trainingMenuCtorAddr = (uintptr_t)scan_memory(traincpattern, 0x17, false);
const std::array<BYTE, 8> traindpattern = { 0x48, 0x89, 0x6B, 0x48, 0x48, 0x8B, 0x4B, 0x38 };
g_trainingMenuDtorAddr = (uintptr_t)scan_memory(traindpattern, 0x39, true);
const std::array<BYTE, 9> dialpattern = { 0x48, 0x8D, 0x5E, 0x38, 0x48, 0x89, 0x5C, 0x24, 0x58 };
g_dialMenuVtblAddr = (uintptr_t)scan_memory(dialpattern, 0x15, false, 0x3, 0x7);
g_dialNextFrameAddr = (uintptr_t)((void**)g_dialMenuVtblAddr)[5];
}
bool InstallHook()
{
{
struct InstallHookHUDNextFrameCode : Xbyak::CodeGenerator {
InstallHookHUDNextFrameCode(void* buf, uintptr_t funcAddr) : Xbyak::CodeGenerator(4096, buf)
{
Xbyak::Label retnLabel;
Xbyak::Label funcLabel;
push(r11);
push(r11);
push(r9);
push(r8);
push(rdx);
push(rcx);
sub(rsp, 0x20);
mov(rcx, rsi);
call(ptr[rip + funcLabel]);
add(rsp, 0x20);
pop(rcx);
pop(rdx);
pop(r8);
pop(r9);
pop(r11);
pop(r11);
mov(qword[r11 - 0x50], rdi);
mov(dword[rsp + 0x30], 0x3);
//mov(qword[rsp + 0x28], 0); SE
jmp(ptr[rip + retnLabel]);
L(funcLabel);
dq(funcAddr);
L(retnLabel);
dq(g_hudMenuNextFrameAddr + 0x6);
}
};
void* codeBuf = g_localTrampoline.StartAlloc();
InstallHookHUDNextFrameCode code(codeBuf, GetFnAddr(Tralala::HUDMenuNextFrame_Hook));
g_localTrampoline.EndAlloc(code.getCurr());
if (!g_branchTrampoline.Write6Branch(g_hudMenuNextFrameAddr, uintptr_t(code.getCode())))
return false;
SafeWrite16(g_hudMenuNextFrameAddr + 0x6, NOP16);
}
{
struct InstallHookBarterDtorCode : Xbyak::CodeGenerator {
InstallHookBarterDtorCode(void* buf, uintptr_t funcAddr) : Xbyak::CodeGenerator(4096, buf)
{
Xbyak::Label retnLabel;
Xbyak::Label funcLabel;
push(r9);
push(r8);
push(rdx);
push(rcx);
sub(rsp, 0x20);
call(ptr[rip + funcLabel]);
add(rsp, 0x20);
pop(rcx);
pop(rdx);
pop(r8);
pop(r9);
//mov(rbx, qword[rsp + 0x40]);
lea(rcx, qword[rbx + 0x80]);
jmp(ptr[rip + retnLabel]);
L(funcLabel);
dq(funcAddr);
L(retnLabel);
dq(g_barterMenuDtorAddr + 0x7);
}
};
void* codeBuf = g_localTrampoline.StartAlloc();
InstallHookBarterDtorCode code(codeBuf, GetFnAddr(Tralala::BarterMenuDtor_Hook));
g_localTrampoline.EndAlloc(code.getCurr());
if (!g_branchTrampoline.Write5Branch(g_barterMenuDtorAddr, uintptr_t(code.getCode())))
return false;
SafeWrite16(g_barterMenuDtorAddr + 0x5, NOP16);
}
{
struct InstallHookTrainCtorCode : Xbyak::CodeGenerator {
InstallHookTrainCtorCode(void* buf, uintptr_t funcAddr) : Xbyak::CodeGenerator(4096, buf)
{
Xbyak::Label retnLabel;
Xbyak::Label funcLabel;
push(r9);
push(r8);
push(rdx);
push(rcx);
sub(rsp, 0x20);
call(ptr[rip + funcLabel]);
add(rsp, 0x20);
pop(rcx);
pop(rdx);
pop(r8);
pop(r9);
//mov(rax, rbx);
//mov(rbx, qword[rsp + 0x58]);
mov(dword[rdi + 0x1C], 0x409);
jmp(ptr[rip + retnLabel]);
L(funcLabel);
dq(funcAddr);
L(retnLabel);
dq(g_trainingMenuCtorAddr + 0x7);
}
};
void* codeBuf = g_localTrampoline.StartAlloc();
InstallHookTrainCtorCode code(codeBuf, GetFnAddr(Tralala::TrainingMenuCtor_Hook));
g_localTrampoline.EndAlloc(code.getCurr());
if (!g_branchTrampoline.Write5Branch(g_trainingMenuCtorAddr, uintptr_t(code.getCode())))
return false;
SafeWrite16(g_barterMenuDtorAddr + 0x5, NOP16);
}
{
struct InstallHookTrainDtorCode : Xbyak::CodeGenerator {
InstallHookTrainDtorCode(void* buf, uintptr_t funcAddr) : Xbyak::CodeGenerator(4096, buf)
{
Xbyak::Label retnLabel;
Xbyak::Label funcLabel;
push(r9);
push(r8);
push(rdx);
push(rcx);
sub(rsp, 0x20);
call(ptr[rip + funcLabel]);
add(rsp, 0x20);
pop(rcx);
pop(rdx);
pop(r8);
pop(r9);
mov(rbx, qword[rsp + 0x40]);
jmp(ptr[rip + retnLabel]);
L(funcLabel);
dq(funcAddr);
L(retnLabel);
dq(g_trainingMenuDtorAddr + 0x5);
}
};
void* codeBuf = g_localTrampoline.StartAlloc();
InstallHookTrainDtorCode code(codeBuf, GetFnAddr(Tralala::TrainingMenuDtor_Hook));
g_localTrampoline.EndAlloc(code.getCurr());
if (!g_branchTrampoline.Write5Branch(g_trainingMenuDtorAddr, uintptr_t(code.getCode())))
return false;
}
SafeWrite64(g_dialMenuVtblAddr + 0x5 * 8, GetFnAddr(Tralala::DialMenuNextFrame_Hook));
return true;
}
}