Skip to content

Commit b4cf559

Browse files
authored
Merge pull request #31 from shad0wshayd3-FO4/dev-write-func
feat: Relocation::write_func
2 parents 93f6e4f + 327cd15 commit b4cf559

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

CommonLibF4/include/REL/Relocation.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,16 @@ namespace REL
329329
safe_fill(address(), a_value, a_count);
330330
}
331331

332+
#ifdef F4SE_SUPPORT_XBYAK
333+
void write_func(const std::size_t a_count, const std::uintptr_t a_dst) requires(std::same_as<value_type, std::uintptr_t>);
334+
335+
template <class F>
336+
void write_func(const std::size_t a_count, const F a_dst) requires(std::same_as<value_type, std::uintptr_t>)
337+
{
338+
write_func(a_count, stl::unrestricted_cast<std::uintptr_t>(a_dst));
339+
}
340+
#endif
341+
332342
template <class U = value_type>
333343
std::uintptr_t write_vfunc(std::size_t a_idx, std::uintptr_t a_newFunc) requires(std::same_as<U, std::uintptr_t>)
334344
{

CommonLibF4/src/REL/Relocation.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,33 @@ namespace REL
3232
assert(success);
3333
}
3434
}
35+
36+
#ifdef F4SE_SUPPORT_XBYAK
37+
# include <xbyak/xbyak.h>
38+
39+
namespace REL
40+
{
41+
struct write_func_impl : Xbyak::CodeGenerator
42+
{
43+
write_func_impl(std::uintptr_t a_dst)
44+
{
45+
Xbyak::Label dst;
46+
jmp(ptr[rip + dst]);
47+
L(dst);
48+
dq(a_dst);
49+
}
50+
};
51+
52+
template <class T>
53+
void Relocation<T>::write_func(const std::size_t a_count, const std::uintptr_t a_dst) requires(std::same_as<value_type, std::uintptr_t>)
54+
{
55+
safe_fill(address(), INT3, a_count);
56+
auto patch = write_func_impl{ a_dst };
57+
patch.ready();
58+
assert(patch.getSize() <= a_count);
59+
safe_write(address(), std::span{ patch.getCode<const std::byte*>(), patch.getSize() });
60+
}
61+
62+
template void Relocation<std::uintptr_t>::write_func(const std::size_t, const std::uintptr_t);
63+
}
64+
#endif

0 commit comments

Comments
 (0)