-
Notifications
You must be signed in to change notification settings - Fork 7
/
Hooks.cpp
612 lines (469 loc) · 14.8 KB
/
Hooks.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#include <skse64/NiNodes.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <algorithm>
#include <vector>
#include "Utils.h"
#include "Hooks.h"
#include "Camera.h"
#include "Menus.h"
#include "ScaleformUtil.h"
#include "Scaleform.h"
#include "Settings.h"
namespace Tralala
{
/* Camera Hooking */
/* ============== */
const float cameraSpeed = 50.0;
ThumbstickInfo leftThumbstick = { 0, 0 };
ThumbstickInfo rightThumbstick = { 0, 0 };
static void CalcAngle(NiPoint3* targetPos, AngleZX* angle)
{
PlayerCharacter* player = PlayerCharacter::GetSingleton();
PlayerCamera* camera = PlayerCamera::GetSingleton();
AngleZX baseAngle;
NiPoint3 cameraPos;
camera->GetCameraPos(&cameraPos);
GetAngle(cameraPos, *targetPos, &baseAngle);
double angleDiffZ = baseAngle.z - (double)player->rot.z;
double angleDiffX = baseAngle.x - (double)player->rot.x;
InputEventDispatcher * g_inputEventDispatcher = InputEventDispatcher::GetSingleton();
if (g_inputEventDispatcher && g_inputEventDispatcher->IsGamepadEnabled())
{
float angle = atan2(128.0, baseAngle.distance);
angleDiffZ += rightThumbstick.x * angle;
if (*(bool*)g_bInvertYValuesAddr){
angleDiffX += rightThumbstick.y * angle;
}
else{
angleDiffX -= rightThumbstick.y * angle;
}
}
angle->z = NormalRelativeAngle(angleDiffZ);
angle->x = NormalRelativeAngle(angleDiffX);
angle->distance = baseAngle.distance;
}
static void RotateCamera(AngleZX* angle)
{
if (TESCameraController::GetSingleton()->unk1C == 0)
return;
PlayerCamera* camera = PlayerCamera::GetSingleton();
PlayerCharacter* player = PlayerCharacter::GetSingleton();
double angleZ = NormalAbsoluteAngle((double)player->rot.z + angle->z / (cameraSpeed * 60 / 2000));
double angleX = NormalRelativeAngle((double)player->rot.x + angle->x / (cameraSpeed * 60 / 2000));
if (player->IsNotInFurniture())
{
if (camera->IsCameraThirdPerson())
{
ThirdPersonState* tps = camera->GetThirdPersonCamera();
tps->diffRotZ = 0.0;
tps->diffRotX = 0.0;
}
player->SetAngleZ(static_cast<float>(angleZ));
player->SetAngleX(static_cast<float>(angleX));
}
else
{ // Just ignore these, should be deleted
if (camera->IsCameraFirstPerson())
{
FirstPersonState* fps = camera->GetFirstPersonCamera();
angleZ -= player->GetActorRotationZ(0);
fps->sittingRot = angleZ;
player->SetAngleX(static_cast<float>(angleX));
}
else if (camera->IsCameraThirdPerson())
{
ThirdPersonState* tps = camera->GetThirdPersonCamera();
angleZ -= camera->camRotZ;
tps->diffRotZ = angleZ;
tps->diffRotX = 0;
}
else
{
player->SetAngleZ(static_cast<float>(angleZ));
player->SetAngleX(static_cast<float>(angleX));
}
//
}
}
static void OnCameraMove()
{
BSFixedString dialogueMenu("Dialogue Menu");
MenuManager* mm = MenuManager::GetSingleton();
if (!mm || mm->IsMenuOpen(&dialogueMenu))
{
dialogueMenu.Release();
return;
}
dialogueMenu.Release();
LockOnMenu* lockOnMenu = (LockOnMenu*)LockOnMenu::GetSingleton();
TESObjectREFR* refTarget = lockOnMenu->GetTarget();
if (!refTarget)
return;
if (!refTarget->GetNiNode())
{
lockOnMenu->Close();
return;
}
if (refTarget->IsDead(1))
{
lockOnMenu->Close();
return;
}
if (PlayerCharacter::GetSingleton()->IsInKillMove())
{
lockOnMenu->Close();
return;
}
NiPoint3 cameraPos, cameraAngle, targetPos;
Actor* actor = (Actor*)refTarget;
if (actor->GetTargetPos(&targetPos))
{
GFxMovieView* view = lockOnMenu->view;
AngleZX targetAngle;
PlayerCamera * camera = PlayerCamera::GetSingleton();
double fov = 1 / tan(camera->worldFOV * M_PI / 360.0);
camera->GetCameraPos(&cameraPos);
camera->GetCameraAngle(&cameraAngle);
GetAngle(cameraPos, targetPos, &targetAngle);
targetAngle.z = NormalRelativeAngle(targetAngle.z - cameraAngle.z);
targetAngle.x = NormalRelativeAngle(targetAngle.x - cameraAngle.x);
double distance = targetAngle.distance * cos(targetAngle.z) * cos(targetAngle.x);
double x = targetAngle.distance * sin(targetAngle.z);
double y = targetAngle.distance * sin(targetAngle.x);
if (abs(cameraAngle.y) > 0)
{
double x2 = x * cos(cameraAngle.y) - y * sin(cameraAngle.y);
double y2 = y * cos(cameraAngle.y) + x * sin(cameraAngle.y);
x = x2;
y = y2;
}
static UInt32 screenWidth = 0;
static UInt32 screenHeight = 0;
if (screenHeight == 0)
{
screenWidth = *(UInt32*)g_iSizeWDisplayAddr;
screenHeight = *(UInt32*)g_iSizeHDisplayAddr;
}
double g_targetPosX = x / distance * fov * 800.0 / screenWidth * screenHeight;
double g_targetPosY = y / distance * fov * 480.0;
double g_targetDist = targetAngle.distance;
if (view)
{
GFxValue args[3];
args[0].SetNumber(g_targetPosX);
args[1].SetNumber(g_targetPosY);
args[2].SetNumber(g_targetDist);
lockOnMenu->SetTargetPosition(args);
if (!view->GetVisible())
view->SetVisible(true);
}
CalcAngle(&targetPos, &targetAngle);
RotateCamera(&targetAngle);
}
}
/* Events Hooking */
/* ============== */
EventResult LockOnInputEventHandler::ReceiveEvent(InputEvent** evns, InputEventDispatcher* dispatcher)
{
if (!*evns)
return kEvent_Continue;
for (InputEvent* e = *evns; e; e = e->next)
{
if (e->eventType == InputEvent::kEventType_Button)
{
ButtonEvent* t = (ButtonEvent*)e;
UInt32 keyCode;
UInt32 deviceType = t->deviceType;
UInt32 keyMask = t->keyMask;
// Mouse
if (deviceType == kDeviceType_Mouse)
keyCode = InputMap::kMacro_MouseButtonOffset + keyMask;
// Gamepad
else if (deviceType == kDeviceType_Gamepad)
keyCode = InputMap::GamepadMaskToKeycode(keyMask);
// Keyboard
else
keyCode = keyMask;
// Valid scancode?
if (keyCode >= InputMap::kMaxMacros)
continue;
float timer = t->timer;
bool isDown = t->flags != 0 && timer == 0.0;
if ((keyCode == Settings::uHotkey) && isDown)
{
BSFixedString dialog("Dialogue Menu");
MenuManager* mm = MenuManager::GetSingleton();
InputManager* im = InputManager::GetSingleton();
PlayerCamera* playerCamera = PlayerCamera::GetSingleton();
if (playerCamera->IsCameraFree())
continue;
if (im->IsTextInputAllowed() || mm->IsInMenuMode() || mm->IsMenuOpen(&dialog))
continue;
dialog.Release();
LockOnMenu* lockOnMenu = (LockOnMenu*)LockOnMenu::GetSingleton();
if (!lockOnMenu->IsOpen())
{
Actor* akRef = nullptr;
PlayerCharacter* player = PlayerCharacter::GetSingleton();
if (player->IsOnMount() || player->IsInKillMove() || player->actorState.IsSittingOrSleeping())
continue;
float fovThreshold = playerCamera->worldFOV / 180.0 * M_PI / 2;
UnkCellInfo** unkCellInfo = UnkCellInfo::GetSingleton();
tArray<UInt32>* actorHandles = &(*unkCellInfo)->actorHandles;
if (actorHandles->count == 0)
continue;
std::vector<std::pair<double, Actor*>> vec;
vec.reserve(actorHandles->count);
NiPoint3 camPos;
playerCamera->GetCameraPos(&camPos);
UInt32 handle;
size_t i = 0;
while (actorHandles->GetNthItem(i++, handle))
{
TESObjectREFR* ref = nullptr;
if (handle != InvalidRefHandle())
LookupRefByHandle(&handle, &ref);
if (ref && ref->formType == kFormType_Character && ref->GetNiNode() && !ref->IsDead(1) && !ref->IsChild())
{
UInt8 result = 0;
if (!(player->HasLOS(ref, &result)))
{
if (ref)
ref->handleRefObject.DecRef();
continue;
}
Actor* actor = (Actor*)ref;
if (Settings::bEssentialActors && actor->IsEssential())
{
if (ref)
ref->handleRefObject.DecRef();
continue;
}
if (Settings::bHostileActors && !(player->IsHostileToActor(actor)))
{
if (ref)
ref->handleRefObject.DecRef();
continue;
}
NiPoint3 pos;
actor->GetTargetPos(&pos);
float dx = pos.x - camPos.x;
float dy = pos.y - camPos.y;
float dz = pos.z - camPos.z;
float dd = sqrt(dx * dx + dy * dy + dz * dz);
if (Settings::uMaxDistance < 2048)
Settings::uMaxDistance = 2048;
if (dd <= Settings::uMaxDistance)
{
float point;
NiPoint3 cameraAngle;
playerCamera->GetCameraAngle(&cameraAngle);
float angleZ = NormalRelativeAngle(atan2(dx, dy) - cameraAngle.z);
float angleX = NormalRelativeAngle(atan2(-dz, sqrt(dx * dx + dy * dy)) - cameraAngle.x);
if (abs(angleZ) < fovThreshold)
{
point = sqrt(angleZ * angleZ + angleX * angleX); //crosshair
if (point >= 0)
{
vec.push_back(std::make_pair(point, actor));
}
}
}
}
if (ref)
ref->handleRefObject.DecRef();
}
if (vec.size() == 0)
continue;
std::sort(vec.begin(), vec.end());
akRef = vec.front().second;
if (akRef)
lockOnMenu->Open(akRef);
else
lockOnMenu->Close();
}
else
{
lockOnMenu->Close();
}
}
}
}
return kEvent_Continue;
};
EventResult LockOnDeathEventHandler::ReceiveEvent(TESDeathEvent* evn, EventDispatcher<TESDeathEvent>* dispatcher)
{
if (evn && evn->source == PlayerCharacter::GetSingleton())
((LockOnMenu*)LockOnMenu::GetSingleton())->Close();
return kEvent_Continue;
};
EventResult LockOnMenuEventHandler::ReceiveEvent(MenuOpenCloseEvent* evn, EventDispatcher<MenuOpenCloseEvent>* dispatcher)
{
LockOnMenu* lockOnMenu = (LockOnMenu*)LockOnMenu::GetSingleton();
if (lockOnMenu->IsOpen())
{
UIStringHolder* holder = UIStringHolder::GetSingleton();
MenuManager* mm = MenuManager::GetSingleton();
IMenu* menu = mm->GetMenu(&evn->menuName);
GFxMovieView* view = lockOnMenu->view;
if (evn->opening)
{
if (menu)
{
if ((menu->flags & 0x4001) != 0)
{
view->SetVisible(false);
}
}
}
else if (!evn->opening)
{
MenuTopicManager* mtm = MenuTopicManager::GetSingleton();
if (!mtm->isInDialogueState && mm->GetNumPauseGame() == 0 && !view->GetVisible())
{
view->SetVisible(true);
}
if (evn->menuName == holder->loadingMenu && !mm->IsMenuOpen(&holder->mainMenu))
{
lockOnMenu->Close();
}
}
}
return kEvent_Continue;
};
LockOnInputEventHandler g_inputEventHandler;
LockOnDeathEventHandler g_deathEventHandler;
LockOnMenuEventHandler g_menuEventHandler;
static void InstallEventsHooks(PlayerControls* playerControls)
{
*(PlayerControls * *)g_playerControlsAddr = playerControls;
InputEventDispatcher* inputEventDispatcher = InputEventDispatcher::GetSingleton();
if (inputEventDispatcher)
inputEventDispatcher->AddEventSinkEx(&g_inputEventHandler);
EventDispatcherList::GetEventDispatcherList()->deathDispatcher.AddEventSinkEx(&g_deathEventHandler);
MenuManager* mm = MenuManager::GetSingleton();
if (mm)
{
mm->Register("LockOnMenu", LockOnMenu::GetSingleton);
mm->GetMenuOpenCloseEventDispatcher()->AddEventSinkEx(&g_menuEventHandler);
}
}
static void RemoveEventsHooks()
{
InputEventDispatcher* inputEventDispatcher = InputEventDispatcher::GetSingleton();
if (inputEventDispatcher)
inputEventDispatcher->RemoveEventSinkEx(&g_inputEventHandler);
EventDispatcherList::GetEventDispatcherList()->deathDispatcher.RemoveEventSinkEx(&g_deathEventHandler);
MenuManager* mm = MenuManager::GetSingleton();
if (mm)
mm->GetMenuOpenCloseEventDispatcher()->RemoveEventSinkEx(&g_menuEventHandler);
}
}
#include <skse64_common/BranchTrampoline.h>
#include <skse64_common/SafeWrite.h>
#include <xbyak/xbyak.h>
namespace MainHooks
{
uintptr_t g_cameraHookAddr = 0;
uintptr_t g_eventHookAddr = 0;
uintptr_t g_removeEventAddr = 0;
void GetAddresses()
{
const std::array<BYTE, 11> campattern = { 0x4C, 0x8B, 0xC3, 0x48, 0x8B, 0xD7, 0xB9, 0x14, 0x00, 0x00, 0x00 };
g_cameraHookAddr = (uintptr_t)scan_memory(campattern, 0x64, false);
const std::array<BYTE, 7> eventpattern = { 0x43, 0x8B, 0x1C, 0x34, 0x89, 0x5D, 0xCF };
g_eventHookAddr = (uintptr_t)scan_memory(eventpattern, 0xC6, true);
const std::array<BYTE, 8> removepattern = { 0x44, 0x89, 0x75, 0x10, 0x4C, 0x39, 0x76, 0x70 };
g_removeEventAddr = (uintptr_t)scan_memory(removepattern, 0x52, false);
}
void Install()
{
{
struct InstallHookOnCameraMove_Code : Xbyak::CodeGenerator
{
InstallHookOnCameraMove_Code(void* buf, UInt64 funcAddr) : Xbyak::CodeGenerator(4096, buf)
{
Xbyak::Label retn1Label;
Xbyak::Label retn2Label;
Xbyak::Label funcLabel;
Xbyak::Label jumpLabel;
push(rax);
sub(rsp, 0x28);
call(ptr[rip + funcLabel]);
add(rsp, 0x28);
pop(rax);
mov(rbx, rax);
test(rax, rax);
je(jumpLabel);
jmp(ptr[rip + retn1Label]);
L(funcLabel);
dq(funcAddr);
L(jumpLabel);
jmp(ptr[rip + retn2Label]);
L(retn1Label);
dq(g_cameraHookAddr + 0x6);
L(retn2Label);
dq(g_cameraHookAddr + 0x91);
}
};
void* codeBuf = g_localTrampoline.StartAlloc();
InstallHookOnCameraMove_Code code(codeBuf, GetFnAddr(Tralala::OnCameraMove));
g_localTrampoline.EndAlloc(code.getCurr());
g_branchTrampoline.Write6Branch(g_cameraHookAddr, uintptr_t(code.getCode()));
}
{
struct InstallEventsHook_Code : Xbyak::CodeGenerator
{
InstallEventsHook_Code(void* buf, UInt64 funcAddr) : Xbyak::CodeGenerator(4096, buf)
{
Xbyak::Label retnLabel;
Xbyak::Label funcLabel;
push(rcx);
push(rax);
sub(rsp, 0x20);
mov(rcx, rax);
call(ptr[rip + funcLabel]);
add(rsp, 0x20);
pop(rax);
pop(rcx);
jmp(ptr[rip + retnLabel]);
L(funcLabel);
dq(funcAddr);
L(retnLabel);
dq(g_eventHookAddr + 0x6);
}
};
void* codeBuf = g_localTrampoline.StartAlloc();
InstallEventsHook_Code code(codeBuf, GetFnAddr(Tralala::InstallEventsHooks));
g_localTrampoline.EndAlloc(code.getCurr());
g_branchTrampoline.Write6Branch(g_eventHookAddr, uintptr_t(code.getCode()));
SafeWrite8(g_eventHookAddr + 0x6, 0x90);
}
{
struct UninstallEventsHook_Code : Xbyak::CodeGenerator
{
UninstallEventsHook_Code(void* buf, UInt64 funcAddr) : Xbyak::CodeGenerator(4096, buf)
{
Xbyak::Label retnLabel;
Xbyak::Label funcLabel;
push(rcx);
sub(rsp, 0x28);
call(ptr[rip + funcLabel]);
add(rsp, 0x28);
pop(rcx);
mov(qword[rsp + 0x70], rsi);
jmp(ptr[rip + retnLabel]);
L(funcLabel);
dq(funcAddr);
L(retnLabel);
dq(g_removeEventAddr + 0x5);
}
};
void* codeBuf = g_localTrampoline.StartAlloc();
UninstallEventsHook_Code code(codeBuf, GetFnAddr(Tralala::RemoveEventsHooks));
g_localTrampoline.EndAlloc(code.getCurr());
g_branchTrampoline.Write5Branch(g_removeEventAddr, uintptr_t(code.getCode()));
}
}
}