Skip to content

Commit

Permalink
Re-added xbyak after CommonLibSSE-NG was updated with support for it.…
Browse files Browse the repository at this point in the history
… Fixed the bug i created in the last commit.
  • Loading branch information
Eloquence4 committed Jun 21, 2022
1 parent 0920bc6 commit 8463343
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.22)

project(
QuickLootRE
VERSION 2.9.3
VERSION 2.9.4
LANGUAGES CXX
)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ find_package(frozen REQUIRED CONFIG)
find_package(spdlog REQUIRED CONFIG)
find_package(srell MODULE REQUIRED)
find_package(CommonLibSSE CONFIG REQUIRED)
find_package(xbyak CONFIG REQUIRED)

add_commonlibsse_plugin(
"${PROJECT_NAME}"
Expand Down Expand Up @@ -129,6 +130,7 @@ target_link_libraries(
frozen::frozen
spdlog::spdlog
srell::srell
xbyak::xbyak
)

target_precompile_headers(
Expand Down
75 changes: 75 additions & 0 deletions src/Events/Events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
#define NODEFERWINDOWPOS
#define NOMCX

#include <xbyak/xbyak.h>

namespace Events
{
void CrosshairRefManager::Evaluate(RE::TESObjectREFRPtr a_ref)
Expand All @@ -61,5 +63,78 @@ namespace Events
auto& loot = Loot::GetSingleton();
loot.Close();
}

void LifeStateManager::Register()
{
if (REL::Module::IsAE()) {
struct Patch :
Xbyak::CodeGenerator
{
explicit Patch(std::uintptr_t a_target)
{
mov(rcx, rdi); // rdi == Actor* this

pop(r15);
pop(r14);
pop(rdi);
pop(rsi);
pop(rbp);

mov(rax, a_target);
jmp(rax);
}
};

constexpr std::size_t begin = 0x494;
constexpr std::size_t end = 0x49B;
constexpr std::size_t size = end - begin;
static_assert(size >= 6);

REL::Relocation<std::uintptr_t> target{ REL::ID(37612), begin }; // Actor::SetLifeState
REL::safe_fill(target.address(), REL::INT3, size);

auto& trampoline = SKSE::GetTrampoline();
Patch p{ reinterpret_cast<std::uintptr_t>(OnLifeStateChanged) };
trampoline.write_branch<6>(
target.address(),
trampoline.allocate(p));
} else {
struct Patch :
Xbyak::CodeGenerator
{
explicit Patch(std::uintptr_t a_target)
{
mov(rcx, rsi); // rsi == Actor* this

pop(r15);
pop(r14);
pop(r12);
pop(rdi);
pop(rsi);
pop(rbx);
pop(rbp);

mov(rax, a_target);
jmp(rax);
}
};

constexpr std::size_t begin = 0x503;
constexpr std::size_t end = 0x50D;
constexpr std::size_t size = end - begin;
static_assert(size >= 6);

REL::Relocation<std::uintptr_t> target{ REL::ID(36604), begin }; // Actor::SetLifeState
REL::safe_fill(target.address(), REL::INT3, size);

auto& trampoline = SKSE::GetTrampoline();
Patch p{ reinterpret_cast<std::uintptr_t>(OnLifeStateChanged) };
trampoline.write_branch<6>(
target.address(),
trampoline.allocate(p));
}

logger::info("Registered {}"sv, typeid(LifeStateManager).name());
}
}

16 changes: 16 additions & 0 deletions src/Events/Events.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ namespace Events
}

protected:
friend class LifeStateManager;

using EventResult = RE::BSEventNotifyControl;

EventResult ProcessEvent(const SKSE::CrosshairRefEvent* a_event, RE::BSTEventSource<SKSE::CrosshairRefEvent>*) override
Expand Down Expand Up @@ -161,9 +163,23 @@ namespace Events
void Close();
};

class LifeStateManager
{
public:
static void Register();

private:
static void OnLifeStateChanged(RE::Actor* a_actor)
{
const auto manager = CrosshairRefManager::GetSingleton();
manager->OnLifeStateChanged(*a_actor);
}
};

inline void Register()
{
CrosshairRefManager::Register();
LifeStateManager::Register();

if (*Settings::closeInCombat) {
CombatManager::Register();
Expand Down
10 changes: 5 additions & 5 deletions vcpkg-configuration.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

{
"registries": [
{
"kind": "git",
"repository": "https://gitlab.com/colorglass/vcpkg-colorglass",
"baseline": "bc4700d551971f31dd32db66916685760c88ba36",
{
"kind": "git",
"repository": "https://gitlab.com/colorglass/vcpkg-colorglass",
"baseline": "5dffebb79a2c9cdc48bca6bcee3dfcf78eeeb300",
"packages": [
"articuno",
"skse",
Expand All @@ -21,7 +21,7 @@
"commonlibsse-ng-vr",
"commonlibsse-ng-flatrim"
]
}
}
]
}

3 changes: 2 additions & 1 deletion vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"srell",
"tomlplusplus",
"articuno",
"commonlibsse-ng-flatrim"
"commonlibsse-ng-flatrim",
"xbyak"
],
"builtin-baseline": "b49b2bdf22559c89eddbf29a29621a1dc6929320"
}

0 comments on commit 8463343

Please sign in to comment.