Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: misc #28

Merged
merged 5 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CommonLibF4/cmake/sourcelist.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ set(SOURCES
include/RE/Bethesda/Settings.h
include/RE/Bethesda/Sky.h
include/RE/Bethesda/SplineUtils.h
include/RE/Bethesda/TaskQueueInterface.h
include/RE/Bethesda/TESBoundAnimObjects.h
include/RE/Bethesda/TESBoundObjects.h
include/RE/Bethesda/TESCamera.h
Expand All @@ -230,7 +231,7 @@ set(SOURCES
include/RE/Bethesda/TESRace.h
include/RE/Bethesda/TESWaterForm.h
include/RE/Bethesda/TESWorldSpace.h
include/RE/Bethesda/TaskQueueInterface.h
include/RE/Bethesda/TLS.h
include/RE/Bethesda/UI.h
include/RE/Bethesda/UIMessage.h
include/RE/Bethesda/UIMessageQueue.h
Expand Down Expand Up @@ -390,6 +391,7 @@ set(SOURCES
include/REX/W32/DXGI_5.h
include/REX/W32/DXGI_6.h
include/REX/W32/KERNEL32.h
include/REX/W32/NT.h
include/REX/W32/OLE32.h
include/REX/W32/SHELL32.h
include/REX/W32/USER32.h
Expand Down
9 changes: 8 additions & 1 deletion CommonLibF4/include/RE/Bethesda/TESCondition.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,18 @@ namespace RE

[[nodiscard]] bool IsTrue(TESObjectREFR* a_actionRef, TESObjectREFR* a_targetRef)
{
using func_t = decltype(&TESConditionItem::IsTrue);
using func_t = bool(*)(TESConditionItem*, TESObjectREFR*, TESObjectREFR*);
static REL::Relocation<func_t> func{ REL::ID(2212008) };
return func(this, a_actionRef, a_targetRef);
}

[[nodiscard]] bool IsTrue(ConditionCheckParams& a_params)
{
using func_t = bool(*)(TESConditionItem*, ConditionCheckParams&);
static REL::Relocation<func_t> func{ REL::ID(2212009) };
return func(this, a_params);
}

// members
TESConditionItem* next; // 00
CONDITION_ITEM_DATA data; // 08
Expand Down
18 changes: 18 additions & 0 deletions CommonLibF4/include/RE/Bethesda/TLS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "REX/W32/NT.h"

namespace RE
{
struct TLS
{
[[nodiscard]] static TLS* GetSingleton()
{
return *static_cast<TLS**>(REX::W32::NtCurrentTeb()->threadLocalStoragePointer);
}

// members
std::byte pad000[0x830]; // 000
bool consoleMode; // 830
};
}
3 changes: 2 additions & 1 deletion CommonLibF4/include/RE/Fallout.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
#include "RE/Bethesda/Settings.h"
#include "RE/Bethesda/Sky.h"
#include "RE/Bethesda/SplineUtils.h"
#include "RE/Bethesda/TaskQueueInterface.h"
#include "RE/Bethesda/TESBoundAnimObjects.h"
#include "RE/Bethesda/TESBoundObjects.h"
#include "RE/Bethesda/TESCamera.h"
Expand All @@ -225,7 +226,7 @@
#include "RE/Bethesda/TESRace.h"
#include "RE/Bethesda/TESWaterForm.h"
#include "RE/Bethesda/TESWorldSpace.h"
#include "RE/Bethesda/TaskQueueInterface.h"
#include "RE/Bethesda/TLS.h"
#include "RE/Bethesda/UI.h"
#include "RE/Bethesda/UIMessage.h"
#include "RE/Bethesda/UIMessageQueue.h"
Expand Down
28 changes: 16 additions & 12 deletions CommonLibF4/include/REL/Relocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,28 +300,32 @@ namespace REL
safe_write(address(), a_data.data(), a_data.size_bytes());
}

template <std::size_t N>
std::uintptr_t write_branch(const std::uintptr_t a_dst) requires(std::same_as<value_type, std::uintptr_t>)
template <std::size_t N, std::ptrdiff_t O = 0>
std::uintptr_t write_branch(const std::uintptr_t a_dst)
requires(std::same_as<value_type, std::uintptr_t>)
{
return F4SE::GetTrampoline().write_branch<N>(address(), a_dst);
return F4SE::GetTrampoline().write_branch<N>(address() + O, a_dst);
}

template <std::size_t N, class F>
std::uintptr_t write_branch(const F a_dst) requires(std::same_as<value_type, std::uintptr_t>)
template <std::size_t N, std::ptrdiff_t O = 0, class F>
std::uintptr_t write_branch(const F a_dst)
requires(std::same_as<value_type, std::uintptr_t>)
{
return F4SE::GetTrampoline().write_branch<N>(address(), stl::unrestricted_cast<std::uintptr_t>(a_dst));
return F4SE::GetTrampoline().write_branch<N>(address() + O, stl::unrestricted_cast<std::uintptr_t>(a_dst));
}

template <std::size_t N>
std::uintptr_t write_call(const std::uintptr_t a_dst) requires(std::same_as<value_type, std::uintptr_t>)
template <std::size_t N, std::ptrdiff_t O = 0>
std::uintptr_t write_call(const std::uintptr_t a_dst)
requires(std::same_as<value_type, std::uintptr_t>)
{
return F4SE::GetTrampoline().write_call<N>(address(), a_dst);
return F4SE::GetTrampoline().write_call<N>(address() + O , a_dst);
}

template <std::size_t N, class F>
std::uintptr_t write_call(const F a_dst) requires(std::same_as<value_type, std::uintptr_t>)
template <std::size_t N, std::ptrdiff_t O = 0, class F>
std::uintptr_t write_call(const F a_dst)
requires(std::same_as<value_type, std::uintptr_t>)
{
return F4SE::GetTrampoline().write_call<N>(address(), stl::unrestricted_cast<std::uintptr_t>(a_dst));
return F4SE::GetTrampoline().write_call<N>(address() + O, stl::unrestricted_cast<std::uintptr_t>(a_dst));
}

void write_fill(const std::uint8_t a_value, const std::size_t a_count) requires(std::same_as<value_type, std::uintptr_t>)
Expand Down
65 changes: 65 additions & 0 deletions CommonLibF4/include/REX/REX.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,70 @@
#pragma once

namespace REX
{
template <
class E,
class U = std::underlying_type_t<E>>
class Enum
{
public:
using enum_type = E;
using underlying_type = U;

static_assert(std::is_enum_v<E>, "Enum<E, ...> must be an enum");
static_assert(std::is_integral_v<U>, "Enum<..., U> must be an integral");

constexpr Enum() noexcept = default;
constexpr Enum(const Enum&) noexcept = default;
constexpr Enum(Enum&&) noexcept = default;

template <class U2> // NOLINTNEXTLINE(google-explicit-constructor)
constexpr Enum(Enum<E, U2> a_rhs) noexcept :
_impl(static_cast<U>(a_rhs.get()))
{}

constexpr Enum(E a_value) noexcept :
_impl(static_cast<U>(a_value))
{}

~Enum() noexcept = default;

constexpr Enum& operator=(const Enum&) noexcept = default;
constexpr Enum& operator=(Enum&&) noexcept = default;

template <class U2>
constexpr Enum& operator=(Enum<E, U2> a_rhs) noexcept
{
_impl = static_cast<U>(a_rhs.get());
}

constexpr Enum& operator=(E a_value) noexcept
{
_impl = static_cast<U>(a_value);
return *this;
}

public:
[[nodiscard]] explicit constexpr operator bool() const noexcept { return _impl != static_cast<U>(0); }
[[nodiscard]] constexpr E operator*() const noexcept { return get(); }
[[nodiscard]] constexpr E get() const noexcept { return static_cast<E>(_impl); }
[[nodiscard]] constexpr U underlying() const noexcept { return _impl; }

public:
friend constexpr bool operator==(Enum a_lhs, Enum a_rhs) noexcept { return a_lhs.underlying() == a_rhs.underlying(); }
friend constexpr bool operator==(Enum a_lhs, E a_rhs) noexcept { return a_lhs.underlying() == static_cast<U>(a_rhs); }
friend constexpr bool operator==(E a_lhs, Enum a_rhs) noexcept { return static_cast<U>(a_lhs) == a_rhs.underlying(); }

private:
U _impl{ 0 };
};
template <class... Args>
Enum(Args...) -> Enum<
std::common_type_t<Args...>,
std::underlying_type_t<
std::common_type_t<Args...>>>;
}

namespace REX
{
template <
Expand Down
1 change: 1 addition & 0 deletions CommonLibF4/include/REX/W32.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "REX/W32/DXGI_5.h"
#include "REX/W32/DXGI_6.h"
#include "REX/W32/KERNEL32.h"
#include "REX/W32/NT.h"
#include "REX/W32/OLE32.h"
#include "REX/W32/USER32.h"
#include "REX/W32/VERSION.h"
Expand Down
10 changes: 10 additions & 0 deletions CommonLibF4/include/REX/W32/BASE.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ namespace REX::W32
};
std::int64_t value;
};
static_assert(sizeof(LARGE_INTEGER) == 0x8);

union ULARGE_INTEGER
{
Expand All @@ -172,6 +173,15 @@ namespace REX::W32
};
std::uint64_t value;
};
static_assert(sizeof(ULARGE_INTEGER) == 0x8);

struct UNICODE_STRING
{
std::uint16_t length;
std::uint16_t maxLength;
wchar_t* buffer;
};
static_assert(sizeof(UNICODE_STRING) == 0x10);
}

namespace REX::W32
Expand Down
92 changes: 92 additions & 0 deletions CommonLibF4/include/REX/W32/NT.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#pragma once

#include "REX/W32/BASE.h"

namespace REX::W32
{
struct EXCEPTION_REGISTRATION_RECORD;
struct PEB_LDR_DATA;
struct RTL_USER_PROCESS_PARAMETERS;
struct UNICODE_STRING;

using PS_POST_PROCESS_INIT_ROUTINE = void (*)();

struct LIST_ENTRY
{
struct LIST_ENTRY* fLink;
struct LIST_ENTRY* bLink;
};

struct NT_TIB
{
EXCEPTION_REGISTRATION_RECORD* exceptionList;
void* stackBase;
void* stackLimit;
void* subSystemTib;
union
{
void* fiberData;
std::uint32_t version;
};
void* arbitraryUserPointer;
struct NT_TIB* self;
};

struct PEB
{
std::byte reserved1[2];
std::byte beingDebugged;
std::byte reserved2[1];
void* reserved3[2];
PEB_LDR_DATA* ldr;
RTL_USER_PROCESS_PARAMETERS* processParameters;
void* reserved4[3];
void* atlThunkSListPtr;
void* reserved5;
std::uint32_t reserved6;
void* reserved7;
std::uint32_t reserved8;
std::uint32_t atlThunkSListPtr32;
void* reserved9[45];
std::byte reserved10[96];
PS_POST_PROCESS_INIT_ROUTINE postProcessInitRoutine;
std::byte reserved11[128];
void* reserved12[1];
std::uint32_t sessionID;
};

struct PEB_LDR_DATA
{
std::byte reserved1[8];
void* reserved2[3];
LIST_ENTRY inMemoryOrderModuleList;
};

struct RTL_USER_PROCESS_PARAMETERS
{
std::byte reserved1[16];
void* reserved2[10];
UNICODE_STRING imagePathName;
UNICODE_STRING commandLine;
};

struct TEB
{
void* reserved1[11];
void* threadLocalStoragePointer;
PEB* processEnvironmentBlock;
void* reserved2[399];
std::byte reserved3[1952];
void* tlsSlots[64];
std::byte reserved4[8];
void* reserved5[26];
void* reservedForOle;
void* reserved6[4];
void* tlsExpansionSlots;
};
}

namespace REX::W32
{
TEB* NtCurrentTeb() noexcept;
}
11 changes: 11 additions & 0 deletions CommonLibF4/src/REX/W32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "REX/W32/DBGHELP.h"
#include "REX/W32/DXGI.h"
#include "REX/W32/KERNEL32.h"
#include "REX/W32/NT.h"
#include "REX/W32/OLE32.h"
#include "REX/W32/SHELL32.h"
#include "REX/W32/USER32.h"
Expand Down Expand Up @@ -796,6 +797,16 @@ namespace REX::W32
}
}

// NT

namespace REX::W32
{
TEB* NtCurrentTeb() noexcept
{
return reinterpret_cast<TEB*>(__readgsqword(offsetof(NT_TIB, self)));
}
}

// OLE32

REX_W32_IMPORT(void, CoTaskMemFree, void*);
Expand Down
Loading