-
Notifications
You must be signed in to change notification settings - Fork 13
/
Events.cpp
564 lines (452 loc) · 15.4 KB
/
Events.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
#include "Game/Events.h"
#include "Serialization/Events.h"
namespace Events
{
namespace Script
{
EventResult EventHandler::ProcessEvent(const RE::TESCellFullyLoadedEvent* a_event, RE::BSTEventSource<RE::TESCellFullyLoadedEvent>*)
{
if (!a_event || !a_event->cell) {
return EventResult::kContinue;
}
OnCellFullyLoadedRegSet::GetSingleton()->QueueEvent(a_event->cell);
return EventResult::kContinue;
}
EventResult EventHandler::ProcessEvent(const RE::TESQuestStartStopEvent* a_event, RE::BSTEventSource<RE::TESQuestStartStopEvent>*)
{
if (!a_event) {
return EventResult::kContinue;
}
if (const auto quest = RE::TESForm::LookupByID<RE::TESQuest>(a_event->formID); quest) {
a_event->started ?
OnQuestStartRegMap::GetSingleton()->QueueEvent(a_event->formID, quest) :
OnQuestStopRegMap::GetSingleton()->QueueEvent(a_event->formID, quest);
}
return EventResult::kContinue;
}
EventResult EventHandler::ProcessEvent(const RE::TESQuestStageEvent* a_event, RE::BSTEventSource<RE::TESQuestStageEvent>*)
{
if (!a_event) {
return EventResult::kContinue;
}
if (const auto quest = RE::TESForm::LookupByID<RE::TESQuest>(a_event->formID); quest) {
OnQuestStageRegMap::GetSingleton()->QueueEvent(a_event->formID, quest, a_event->stage);
}
return EventResult::kContinue;
}
EventResult EventHandler::ProcessEvent(const RE::TESObjectLoadedEvent* a_event, RE::BSTEventSource<RE::TESObjectLoadedEvent>*)
{
if (!a_event) {
return EventResult::kContinue;
}
const auto object = RE::TESForm::LookupByID<RE::TESObjectREFR>(a_event->formID);
const auto base = object ? object->GetBaseObject() : nullptr;
if (base) {
auto baseType = base->GetFormType();
a_event->loaded ?
OnObjectLoadedRegMap::GetSingleton()->QueueEvent(baseType, object, baseType) :
OnObjectUnloadedRegMap::GetSingleton()->QueueEvent(baseType, object, baseType);
}
return EventResult::kContinue;
}
EventResult EventHandler::ProcessEvent(const RE::TESGrabReleaseEvent* a_event, RE::BSTEventSource<RE::TESGrabReleaseEvent>*)
{
if (!a_event) {
return EventResult::kContinue;
}
if (const auto object = a_event->ref.get(); object) {
a_event->grabbed ?
OnGrabRegSet::GetSingleton()->QueueEvent(object) :
OnReleaseRegSet::GetSingleton()->QueueEvent(object);
}
return EventResult::kContinue;
}
}
namespace Story
{
EventResult EventHandler::ProcessEvent(const RE::ActorKill::Event* a_event, RE::BSTEventSource<RE::ActorKill::Event>*)
{
if (!a_event) {
return EventResult::kContinue;
}
const auto victim = a_event->victim;
const auto killer = a_event->killer;
if (victim) {
OnActorKillRegSet::GetSingleton()->QueueEvent(victim, killer);
}
return EventResult::kContinue;
}
EventResult EventHandler::ProcessEvent(const RE::BooksRead::Event* a_event, RE::BSTEventSource<RE::BooksRead::Event>*)
{
if (!a_event) {
return EventResult::kContinue;
}
if (const auto book = a_event->book; book) {
OnBooksReadRegSet::GetSingleton()->QueueEvent(book);
}
return EventResult::kContinue;
}
EventResult EventHandler::ProcessEvent(const RE::CriticalHit::Event* a_event, RE::BSTEventSource<RE::CriticalHit::Event>*)
{
if (!a_event) {
return EventResult::kContinue;
}
const auto agressor = a_event->aggressor;
const auto agressorActor = agressor ? agressor->As<RE::Actor>() : nullptr;
const auto weapon = a_event->weapon;
if (agressorActor && weapon) {
OnCriticalHitRegSet::GetSingleton()->QueueEvent(agressorActor, weapon, a_event->sneakHit);
}
return EventResult::kContinue;
}
EventResult EventHandler::ProcessEvent(const RE::DisarmedEvent::Event* a_event, RE::BSTEventSource<RE::DisarmedEvent::Event>*)
{
if (!a_event) {
return EventResult::kContinue;
}
const auto source = a_event->source;
const auto target = a_event->target;
if (source && target) {
OnDisarmedRegSet::GetSingleton()->QueueEvent(source, target);
}
return EventResult::kContinue;
}
EventResult EventHandler::ProcessEvent(const RE::DragonSoulsGained::Event* a_event, RE::BSTEventSource<RE::DragonSoulsGained::Event>*)
{
if (!a_event) {
return EventResult::kContinue;
}
OnDragonSoulsGainedRegSet::GetSingleton()->QueueEvent(a_event->souls);
return EventResult::kContinue;
}
EventResult EventHandler::ProcessEvent(const RE::TESHarvestedEvent::ItemHarvested* a_event, RE::BSTEventSource<RE::TESHarvestedEvent::ItemHarvested>*)
{
if (!a_event) {
return EventResult::kContinue;
}
if (const auto produce = a_event->produceItem; produce) {
OnItemHarvestedRegSet::GetSingleton()->QueueEvent(produce);
}
return EventResult::kContinue;
}
EventResult EventHandler::ProcessEvent(const RE::LevelIncrease::Event* a_event, RE::BSTEventSource<RE::LevelIncrease::Event>*)
{
if (!a_event) {
return EventResult::kContinue;
}
OnLevelIncreaseRegSet::GetSingleton()->QueueEvent(a_event->newLevel);
return EventResult::kContinue;
}
EventResult EventHandler::ProcessEvent(const RE::LocationDiscovery::Event* a_event, RE::BSTEventSource<RE::LocationDiscovery::Event>*)
{
if (!a_event) {
return EventResult::kContinue;
}
if (const auto data = a_event->mapMarkerData; data) {
OnLocationDiscoveryRegSet::GetSingleton()->QueueEvent(data->locationName.fullName, a_event->worldspaceID);
}
return EventResult::kContinue;
}
EventResult EventHandler::ProcessEvent(const RE::ShoutAttack::Event* a_event, RE::BSTEventSource<RE::ShoutAttack::Event>*)
{
if (!a_event) {
return EventResult::kContinue;
}
if (const auto shout = a_event->shout; shout) {
OnShoutAttackRegSet::GetSingleton()->QueueEvent(shout);
}
return EventResult::kContinue;
}
EventResult EventHandler::ProcessEvent(const RE::SkillIncrease::Event* a_event, RE::BSTEventSource<RE::SkillIncrease::Event>*)
{
if (!a_event) {
return EventResult::kContinue;
}
const auto avList = RE::ActorValueList::GetSingleton();
const auto avInfo = avList ?
avList->GetActorValue(a_event->actorValue) :
nullptr;
if (avInfo) {
OnSkillIncreaseRegSet::GetSingleton()->QueueEvent(avInfo->enumName);
}
return EventResult::kContinue;
}
EventResult EventHandler::ProcessEvent(const RE::SoulsTrapped::Event* a_event, RE::BSTEventSource<RE::SoulsTrapped::Event>*)
{
if (!a_event) {
return EventResult::kContinue;
}
const auto trapper = a_event->trapper;
const auto target = a_event->target;
if (target) {
OnSoulsTrappedRegSet::GetSingleton()->QueueEvent(target, trapper);
}
return EventResult::kContinue;
}
EventResult EventHandler::ProcessEvent(const RE::SpellsLearned::Event* a_event, RE::BSTEventSource<RE::SpellsLearned::Event>*)
{
if (!a_event) {
return EventResult::kContinue;
}
if (const auto spell = a_event->spell; spell) {
OnSpellsLearnedRegSet::GetSingleton()->QueueEvent(spell);
}
return EventResult::kContinue;
}
}
namespace Game
{
namespace Resurrect
{
struct Resurrect
{
static void thunk(RE::Character* a_this, bool a_resetInventory, bool a_attach3D)
{
func(a_this, a_resetInventory, a_attach3D);
OnActorResurrectRegSet::GetSingleton()->QueueEvent(a_this, a_this, a_resetInventory);
}
static inline REL::Relocation<decltype(&thunk)> func;
};
void Install()
{
stl::write_vfunc<RE::Character, 0x0AB, Resurrect>();
}
}
namespace ReanimateStart
{
struct ReanimateStart
{
static void thunk(RE::ReanimateEffect* a_this)
{
func(a_this);
if (a_this->flags.none(RE::ActiveEffect::Flag::kDispelled)) { //effect can get dispelled in original func
const auto zombiePtr = a_this->commandedActor.get();
const auto zombie = zombiePtr.get();
if (!zombie) {
return;
}
if (zombie->boolBits.all(RE::Actor::BOOL_BITS::kParalyzed)) {
const auto root = zombie->Get3D(false);
const auto charController = zombie->GetCharController();
if (root && charController) {
zombie->boolBits.reset(RE::Actor::BOOL_BITS::kParalyzed);
root->SetRigidConstraints(false);
std::uint32_t filterInfo = 0;
charController->GetCollisionFilterInfo(filterInfo);
root->UpdateRigidBodySettings(32, filterInfo >> 16);
}
}
const auto casterPtr = a_this->caster.get();
const auto caster = casterPtr.get();
if (caster) {
OnActorReanimateStartRegSet::GetSingleton()->QueueEvent(zombie, zombie, caster);
}
}
}
static inline REL::Relocation<decltype(&thunk)> func;
};
void Install()
{
stl::write_vfunc<RE::ReanimateEffect, 0x14, ReanimateStart>();
}
};
namespace ReanimateStop
{
struct ReanimateStop
{
static void thunk(RE::ReanimateEffect* a_this)
{
const auto zombiePtr = a_this->commandedActor.get();
const auto zombie = zombiePtr.get();
if (zombie) {
const auto casterPtr = a_this->caster.get();
const auto caster = casterPtr.get();
if (caster) {
OnActorReanimateStopRegSet::GetSingleton()->QueueEvent(zombie, zombie, caster);
}
}
func(a_this);
}
static inline REL::Relocation<decltype(&thunk)> func;
};
void Install()
{
stl::write_vfunc<RE::ReanimateEffect, 0x15, ReanimateStop>();
}
}
namespace MagicEffectApply
{
struct MagicTargetApply
{
static bool thunk(RE::MagicTarget* a_this, RE::MagicTarget::CreationData* a_data)
{
const auto result = func(a_this, a_data);
const auto target = a_this->GetTargetStatsObject();
const auto effect = a_data ? a_data->effect : nullptr;
const auto baseEffect = effect ? effect->baseEffect : nullptr;
if (target && baseEffect) {
OnMagicEffectApplyRegMap::GetSingleton()->QueueEvent(target, baseEffect, a_data->caster, baseEffect, a_data->magicItem, result);
}
return result;
}
static inline REL::Relocation<decltype(thunk)> func;
};
static void Install()
{
REL::Relocation<std::uintptr_t> target{ REL::ID(33742), 0x1E8 };
stl::write_thunk_call<MagicTargetApply>(target.address());
}
}
namespace MagicHit
{
struct SendHitEvent
{
static void thunk(RE::BSTEventSource<RE::TESHitEvent>& a_source, RE::TESHitEvent& a_event)
{
if (const auto aggressor = a_event.cause.get(); aggressor) {
const auto target = a_event.target.get();
const auto source = RE::TESForm::LookupByID(a_event.source);
const auto projectile = RE::TESForm::LookupByID<RE::BGSProjectile>(a_event.projectile);
OnMagicHitRegSet::GetSingleton()->QueueEvent(aggressor, target, source, projectile);
}
func(a_source, a_event);
}
static inline REL::Relocation<decltype(&thunk)> func;
};
void Install()
{
REL::Relocation<std::uintptr_t> target{ REL::ID(37832), 0x1C3 };
stl::write_thunk_call<SendHitEvent>(target.address());
}
}
namespace WeaponHit
{
namespace Actor
{
REL::Relocation<std::uintptr_t> target{ REL::ID(37633), 0x16A };
struct SendHitEvent
{
static void thunk(RE::ScriptEventSourceHolder* a_holder,
RE::NiPointer<RE::TESObjectREFR>& a_target,
RE::NiPointer<RE::TESObjectREFR>& a_aggressor,
RE::FormID a_source,
RE::FormID a_projectile,
RE::HitData& a_data)
{
if (const auto aggressor = a_aggressor.get(); aggressor) {
const auto hitTarget = a_target.get();
const auto source = RE::TESForm::LookupByID(a_source);
const auto flags = stl::to_underlying(a_data.flags);
OnWeaponHitRegSet::GetSingleton()->QueueEvent(aggressor, hitTarget, source, nullptr, flags);
}
func(a_holder, a_target, a_aggressor, a_source, a_projectile, a_data);
}
static inline REL::Relocation<decltype(thunk)> func;
};
}
namespace Static
{
REL::Relocation<std::uintptr_t> target{ REL::ID(37674), 0x6C7 };
struct SendHitEvent
{
static void thunk(RE::BSTEventSource<RE::TESHitEvent>& a_source, RE::TESHitEvent& a_event)
{
if (const auto aggressor = a_event.cause.get(); aggressor) {
const auto hitTarget = a_event.target.get();
const auto source = RE::TESForm::LookupByID(a_event.source);
OnWeaponHitRegSet::GetSingleton()->QueueEvent(aggressor, hitTarget, source, nullptr, 0);
}
func(a_source, a_event);
}
static inline REL::Relocation<decltype(thunk)> func;
};
}
namespace Projectile
{
REL::Relocation<std::uintptr_t> target{ REL::ID(43022), 0x38D };
struct SendHitEvent
{
static void thunk(RE::BSTEventSource<RE::TESHitEvent>& a_source, RE::TESHitEvent& a_event)
{
if (const auto aggressor = a_event.cause.get(); aggressor) {
const auto hitTarget = a_event.target.get();
const auto source = RE::TESForm::LookupByID(a_event.source);
const auto projectile = RE::TESForm::LookupByID<RE::BGSProjectile>(a_event.projectile);
if (projectile && projectile->data.types.all(RE::BGSProjectileData::Type::kArrow)) {
OnWeaponHitRegSet::GetSingleton()->QueueEvent(aggressor, hitTarget, source, projectile, 0);
}
OnProjectileHitRegSet::GetSingleton()->QueueEvent(aggressor, hitTarget, source, projectile);
}
func(a_source, a_event);
}
static inline REL::Relocation<decltype(thunk)> func;
};
}
inline void Install()
{
stl::write_thunk_call<Actor::SendHitEvent>(Actor::target.address());
stl::write_thunk_call<Static::SendHitEvent>(Static::target.address());
stl::write_thunk_call<Projectile::SendHitEvent>(Projectile::target.address());
}
}
class WeatherEvent
{
public:
static void Install()
{
REL::Relocation<std::uintptr_t> target{ REL::ID(25684), 0x416 };
struct Patch : Xbyak::CodeGenerator
{
Patch(std::uintptr_t a_func, std::uintptr_t a_target)
{
Xbyak::Label funcLbl;
Xbyak::Label returnLbl;
mov(dword[rbx + 0x1DC], eax); // original code
call(ptr[rip + funcLbl]); // new function
jmp(ptr[rip + returnLbl]);
L(funcLbl);
dq(a_func);
L(returnLbl);
dq(a_target + 0x6); // next line
}
};
Patch patch(reinterpret_cast<std::uintptr_t>(SendWeatherEvent), target.address());
patch.ready();
auto& trampoline = SKSE::GetTrampoline();
trampoline.write_branch<6>(
target.address(),
trampoline.allocate(patch));
}
private:
static void SendWeatherEvent()
{
const auto sky = RE::Sky::GetSingleton();
const auto currentWeather = sky ? sky->currentWeather : nullptr;
const auto lastWeather = sky ? sky->lastWeather : nullptr;
OnWeatherChangeRegSet::GetSingleton()->QueueEvent(lastWeather, currentWeather);
}
};
}
void Register()
{
Script::EventHandler::Register();
Story::EventHandler::Register();
}
void Hook()
{
logger::info("{:*^30}", "HOOKED EVENTS"sv);
Game::Resurrect::Install();
logger::info("Hooked Actor Resurrect"sv);
Game::ReanimateStart::Install();
Game::ReanimateStop::Install();
logger::info("Hooked Actor Reanimate"sv);
Game::MagicEffectApply::Install();
logger::info("Hooked Magic Effect Apply"sv);
Game::WeaponHit::Install();
logger::info("Hooked Weapon Hit"sv);
Game::MagicHit::Install();
logger::info("Hooked Magic Hit"sv);
Game::WeatherEvent::Install();
logger::info("Hooked Weather Change"sv);
}
}