Skip to content

Commit

Permalink
Merge Kernel TU Support (mr-525)
Browse files Browse the repository at this point in the history
3edd558 - tweak(five): Kernel TU Support
  • Loading branch information
prikolium-cfx committed Sep 17, 2024
2 parents 27f153a + 3edd558 commit 2ee45e6
Show file tree
Hide file tree
Showing 20 changed files with 157,362 additions and 271 deletions.
5 changes: 5 additions & 0 deletions code/client/launcher/ExecutableLoader.Snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ inline uintptr_t GetTriggerEP()
{
return ep;
}

if (xbr::IsGameBuild<3323>())
{
return 0x1418492F0;
}

if (xbr::IsGameBuild<3258>())
{
Expand Down
361 changes: 231 additions & 130 deletions code/client/launcher/GameCache.cpp

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions code/client/shared/CrossBuildRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ inline bool IsGameBuild()
return GetGameBuild() == Build;
}

inline bool IsSupportedGameBuild(uint32_t targetBuild)
{
switch (targetBuild)
{
#define EXPAND(_, __, x) case x: return true;
BOOST_PP_SEQ_FOR_EACH(EXPAND, , GAME_BUILDS)
#undef EXPAND
}
return false;
}

#ifdef _WIN32
inline const wchar_t* GetGameWndClass()
{
Expand Down
11 changes: 11 additions & 0 deletions code/client/shared/Hooking.FlexStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,16 @@ namespace hook
{
return *(T*)((uintptr_t)this + offset);
}

template<typename Ret, typename... Args>
Ret CallVirtual(int32_t offset, Args... args)
{
// Calculate the address of the virtual method
auto vtable = *(uintptr_t**)this; // Dereference to get the vtable
auto method = (Ret(*)(void*, Args...))(*(uintptr_t*)((uintptr_t)vtable + offset));

// Call the virtual method
return method(this, std::forward<Args>(args)...);
}
};
}
9 changes: 9 additions & 0 deletions code/client/shared/Hooking.h
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,15 @@ inline T get_call(T address)
return (T)target;
}

template<typename T, typename OffsetType>
inline T* get_by_offset(uint8_t* address, OffsetType offset)
{
intptr_t base_address = get_adjusted(address);
intptr_t target = *(OffsetType*)(base_address + offset);
target += (base_address + offset + sizeof(OffsetType));
return reinterpret_cast<T*>(target);
}

template<typename TTarget, typename T>
inline void set_call(TTarget* target, T address)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ inline bool Is3258()

return value;
}

inline bool Is3323()
{
static bool value = ([]()
{
return fx::GetEnforcedGameBuildNumber() >= 3323;
})();

return value;
}
#elif defined(STATE_RDR3)
inline bool Is1311()
{
Expand Down
19 changes: 19 additions & 0 deletions code/components/glue/include/UpdateRpfHeaderHashes.h
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,25 @@ Sha256Result updateSafeHashesInit[] = {
ShaUnpack("c2bfefac4448c3271c15c1eb39c81c405505a966f698bda3408032ac010cb526"), // update2.rpf/x64/data/lang/russian_rel.rpf
ShaUnpack("828962418a93ecf93044d0a9a79f27b9ef528f040432eb3f97a6f47e2d99ec19"), // update2.rpf/x64/data/lang/spanish_rel.rpf
ShaUnpack("d64a9acc5b84d3db6da92c62429e995e19e5aa621343a2759e11e12b9d4947ca"), // update2.rpf/x64/levels/gta5/script/script_rel.rpf
// 3323/update.rpf
ShaUnpack("e3101eae56495a4d0eafe450c04073e3a41e15b743888561ee44d644a4a11400"), // update.rpf
ShaUnpack("824c778c183a32e05afe9f9181d46f9f60e070cb00857a2c32fa7fd8132b6347"), // update.rpf/x64/data/cdimages/scaleform_frontend.rpf
// 3323/update2.rpf
ShaUnpack("7d266cc3ccbf844193f695e1111ba028d392796ba1439eef9c0873cb0b49ed6b"), // update2.rpf
ShaUnpack("b0da021d0df99b5b89e3697d236566f59edd818461037b32e917c8925fa22468"), // update2.rpf/x64/data/lang/american_rel.rpf
ShaUnpack("8390727556bfc5a165c5cf2dbfe9e39ffbca62d5e80e12c5e8cfa98b2b009a24"), // update2.rpf/x64/data/lang/chinese_rel.rpf
ShaUnpack("3ffcdddb8c20c2d076928550f629844f7e7bbee67cd75b41c9a545037ae80237"), // update2.rpf/x64/data/lang/chinesesimp_rel.rpf
ShaUnpack("1fd69d06075fa33c528e651669fc75c5d50f6eaffd784c488058beb615a57f32"), // update2.rpf/x64/data/lang/french_rel.rpf
ShaUnpack("1da1c61739793b39d2ec4eb86bcdb356d8479d9bddeff561a0ebafc208c36ea2"), // update2.rpf/x64/data/lang/german_rel.rpf
ShaUnpack("629bb2c20cea9ab31f7e7c270dd4e459939d2cf6a4bd24298cc0f302e4a0e85d"), // update2.rpf/x64/data/lang/italian_rel.rpf
ShaUnpack("9a355be1c150c8bb46a251adc3c8695d3eedc24c5d466bb6389ea8a769e764d4"), // update2.rpf/x64/data/lang/japanese_rel.rpf
ShaUnpack("fcdd3f694222b58447fda6ff31394cf146fa4cd0825012de1a699d608edd51a2"), // update2.rpf/x64/data/lang/korean_rel.rpf
ShaUnpack("b484024762292b06fe83108c71d89944a4c928333089aa6a679ff191d4ed38f6"), // update2.rpf/x64/data/lang/mexican_rel.rpf
ShaUnpack("7ca239eadaa75c57bfe2a38c8478ca5fb4ff1da2e8aec69393ea776adf7b7fed"), // update2.rpf/x64/data/lang/polish_rel.rpf
ShaUnpack("cf1418b6526e5dc05b699743c9caada1e3cd9e717c6997fdb441a27b400df0e6"), // update2.rpf/x64/data/lang/portuguese_rel.rpf
ShaUnpack("c9a7b8187442edf2e624501ecd5bcc19cf9ed31343e7388a28ecaff7a1a06c3e"), // update2.rpf/x64/data/lang/russian_rel.rpf
ShaUnpack("e7282580378be4407de22388000e617787af93c6920779e66f444f3469ae3f64"), // update2.rpf/x64/data/lang/spanish_rel.rpf
ShaUnpack("657fd2cd6040159bb51011b8f4f64b60c02419a93bb537ec4c24dd25a3ed0f63"), // update2.rpf/x64/levels/gta5/script/script_rel.rpf
};

}}
52 changes: 52 additions & 0 deletions code/components/gta-core-five/src/AllocatorHacks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <StdInc.h>

#include <CoreConsole.h>

#include <jitasm.h>
#include <Hooking.h>
#include <Hooking.Stubs.h>

static int32_t getHeapSize_offset = 0;
static int32_t getMemoryUsed_offset = 0;
static int32_t getMemoryAvailable_offset = 0;
static int32_t memAllocIdOffset = 0;
static int32_t heapIdBits = 0;

static size_t lastAllocationSize = 0;
static void*(*g_Allocate)(void* self, size_t size, size_t align);
static void* Allocate_Hook(void* self, size_t size, size_t align)
{
lastAllocationSize = size;
return g_Allocate(self, size, align);
}

static void OnAllocationFailed(hook::FlexStruct* self)
{
size_t heapSize = self->CallVirtual<size_t>(getHeapSize_offset);
size_t memoryUsed = self->CallVirtual<size_t, int32_t>(getMemoryUsed_offset, -1);
size_t memoryAvailable = self->CallVirtual<size_t>(getMemoryAvailable_offset);

const int heapIdMask = (1 << heapIdBits) - 1;
const int heapId = self->Get<int>(memAllocIdOffset) & ~heapIdMask;

trace("Error allocating %d bytes in heap with id %d (Total: %d, Used: %d, Free: %d)\n", lastAllocationSize, heapId, heapSize, memoryUsed, memoryAvailable);
}

static HookFunction hookFunction([]
{
auto allocateLoc = hook::get_pattern<uint8_t>("48 89 5C 24 ? 48 89 6C 24 ? 48 89 74 24 ? 57 48 83 EC ? 48 8D B1 ? ? ? ? 49 8B E8");
g_Allocate = hook::trampoline(allocateLoc, Allocate_Hook);

auto onAllocationFailed = hook::get_pattern<uint8_t>("FF 90 ? ? ? ? 48 8B 03 83 CA");

getHeapSize_offset = *(uint32_t*)(onAllocationFailed + 2);
getMemoryUsed_offset = *(uint32_t*)(onAllocationFailed + 17);
getMemoryAvailable_offset = *(uint32_t*)(onAllocationFailed + 29);

auto loc = hook::get_pattern<int8_t>("C1 E7 ? FF C7 33 D2");
heapIdBits = *(int8_t*)(loc + 2);
memAllocIdOffset = *(int32_t*)(loc + 16);

hook::nop(onAllocationFailed, 33);
hook::call(onAllocationFailed, OnAllocationFailed);
});
18 changes: 15 additions & 3 deletions code/components/gta-core-five/src/BlockLoadSetters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ static inline int MapInitState(int initState)
}
}

if (initState >= 2 && xbr::IsGameBuildOrGreater<3323>())
{
initState += 1;
}
return initState;
}

Expand Down Expand Up @@ -586,7 +590,7 @@ static HookFunction hookFunction([] ()

// NOP out any code that sets the 'entering state 2' (2, 0) FSM internal state to '7' (which is 'load game'), UNLESS it's digital distribution with standalone auth...
// Since game build 2699.16 executables now shared.
char* p = (xbr::IsGameBuild<2060>() || xbr::IsGameBuildOrGreater<2802>()) ? hook::pattern("BA 08 00 00 00 8D 41 FC 83 F8 01").count(1).get(0).get<char>(14) : hook::pattern("BA 07 00 00 00 8D 41 FC 83 F8 01").count(1).get(0).get<char>(14);
char* p = (xbr::IsGameBuild<2060>() || xbr::IsGameBuildOrGreater<2802>()) ? hook::pattern("48 83 EC ? E8 ? ? ? ? E8 ? ? ? ? 48 8B 0D ? ? ? ? E8").count(1).get(0).get<char>(55) : hook::pattern("BA 07 00 00 00 8D 41 FC 83 F8 01").count(1).get(0).get<char>(14);

char* varPtr = p + 2;
g_initState = (int*)(varPtr + *(int32_t*)varPtr + 4);
Expand All @@ -598,7 +602,7 @@ static HookFunction hookFunction([] ()
// and if this *is* digital distribution, we want to find a completely different place that sets the value to 8 (i.e. BA 08 ...)
if (g_isDigitalDistrib)
{
p = hook::pattern("BA 08 00 00 00 8D 41 FC 83 F8 01").count(1).get(0).get<char>(14);
p = hook::pattern("48 83 EC ? E8 ? ? ? ? E8 ? ? ? ? 48 8B 0D ? ? ? ? E8").count(1).get(0).get<char>(55);
}

{
Expand Down Expand Up @@ -693,7 +697,15 @@ static HookFunction hookFunction([] ()
hook::call(hook::get_pattern("C8 89 05 ? ? ? ? E8 ? ? ? ? 89 05", 7), BlipAsIndex);

// don't load commandline.txt
hook::return_function(hook::get_pattern("45 33 E4 83 39 02 4C 8B FA 45 8D 6C", -0x1C));
if (xbr::IsGameBuildOrGreater<3323>())
{
// force return 0 here, with xor eax, eax
hook::put<uint64_t>(hook::get_pattern("48 89 5C 24 ? 48 89 6C 24 ? 48 89 74 24 ? 57 41 54 41 55 41 56 41 57 48 83 EC ? 45 33 ED 83 39"), 0x90C3C031);
}
else
{
hook::return_function(hook::get_pattern("45 33 E4 83 39 02 4C 8B FA 45 8D 6C", -0x1C));
}

// sometimes this crashes
SafeRun([]()
Expand Down
91 changes: 13 additions & 78 deletions code/components/gta-core-five/src/GameInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,84 +153,19 @@ static bool (*g_isScWaitingForInit)();

void RunRlInitServicing()
{
// E8 ? ? ? ? C6 05 ? ? ? ? ? EB 41
if (xbr::IsGameBuildOrGreater<3258>())
{
((void (*)())hook::get_adjusted(0x140006B2C))();
((void (*)())hook::get_adjusted(0x14080D4E4))();
((void (*)())hook::get_adjusted(0x140028D24))();
((void (*)(void*))hook::get_adjusted(0x14166CC54))((void*)hook::get_adjusted(0x142FE3410));
}
else if (xbr::IsGameBuildOrGreater<3095>())
{
((void (*)())hook::get_adjusted(0x140006D04))();
((void (*)())hook::get_adjusted(0x140809E54))();
((void (*)())hook::get_adjusted(0x140028578))();
((void (*)(void*))hook::get_adjusted(0x141657DF0))((void*)hook::get_adjusted(0x142FB57D0));
}
else if (xbr::IsGameBuildOrGreater<2944>())
{
((void (*)())hook::get_adjusted(0x140006B28))();
((void (*)())hook::get_adjusted(0x140804254))();
((void (*)())hook::get_adjusted(0x140028200))();
((void (*)(void*))hook::get_adjusted(0x141643218))((void*)hook::get_adjusted(0x142F60330));
}
else if (xbr::IsGameBuildOrGreater<2802>())
{
((void (*)())hook::get_adjusted(0x1400069DC))();
((void (*)())hook::get_adjusted(0x140802754))();
((void (*)())hook::get_adjusted(0x14002802C))();
((void (*)(void*))hook::get_adjusted(0x14161D450))((void*)hook::get_adjusted(0x142ED8B20));
}
else if (xbr::IsGameBuildOrGreater<2699>())
{
((void (*)())hook::get_adjusted(0x1400069F4))();
((void (*)())hook::get_adjusted(0x1407FE28C))();
((void (*)())hook::get_adjusted(0x140027C20))();
((void (*)(void*))hook::get_adjusted(0x14160A9AC))((void*)hook::get_adjusted(0x142FF1F70));
}
else if (xbr::IsGameBuildOrGreater<2612>())
{
((void (*)())hook::get_adjusted(0x140006C38))();
((void (*)())hook::get_adjusted(0x1407FB420))();
((void (*)())hook::get_adjusted(0x14002778C))();
((void (*)(void*))hook::get_adjusted(0x1416135F8))((void*)hook::get_adjusted(0x142E710F0));
}
else if (xbr::IsGameBuildOrGreater<2545>())
{
((void (*)())hook::get_adjusted(0x140006A28))();
((void (*)())hook::get_adjusted(0x1407FB28C))();
((void (*)())hook::get_adjusted(0x1400275C8))();
((void (*)(void*))hook::get_adjusted(0x141612950))((void*)hook::get_adjusted(0x142E6F960));
}
else if (xbr::IsGameBuildOrGreater<2372>())
{
((void (*)())hook::get_adjusted(0x140006718))();
((void (*)())hook::get_adjusted(0x1407F6050))();
((void (*)())hook::get_adjusted(0x1400263CC))();
((void (*)(void*))hook::get_adjusted(0x14160104C))((void*)hook::get_adjusted(0x142E34900));
}
else if (xbr::IsGameBuildOrGreater<2189>())
{
((void (*)())hook::get_adjusted(0x140006748))();
((void (*)())hook::get_adjusted(0x1407F4150))();
((void (*)())hook::get_adjusted(0x140026120))();
((void (*)(void*))hook::get_adjusted(0x1415E4AC8))((void*)hook::get_adjusted(0x142E5C2D0));
}
else if (xbr::IsGameBuildOrGreater<2060>())
{
((void (*)())hook::get_adjusted(0x140006A80))();
((void (*)())hook::get_adjusted(0x1407EB39C))();
((void (*)())hook::get_adjusted(0x1400263A4))();
((void (*)(void*))hook::get_adjusted(0x1415CF268))((void*)hook::get_adjusted(0x142D3DCC0));
}
else
{
((void (*)())hook::get_adjusted(0x1400067E8))();
((void (*)())hook::get_adjusted(0x1407D1960))();
((void (*)())hook::get_adjusted(0x140025F7C))();
((void (*)(void*))hook::get_adjusted(0x141595FD4))((void*)hook::get_adjusted(0x142DC9BA0));
}
using dummyVoidFunc = void(*)();
using dummyVoidFunc2 = void(*)(void*);

dummyVoidFunc rlInitFunc1 = (dummyVoidFunc)hook::get_pattern<void*>("48 89 5C 24 ? 48 89 74 24 ? 55 57 41 54 41 56 41 57 48 8D 6C 24 ? 48 81 EC ? ? ? ? E8");
dummyVoidFunc rlInitFunc2 = (dummyVoidFunc)hook::get_call(hook::get_pattern<void*>("E8 ? ? ? ? E8 ? ? ? ? E8 ? ? ? ? 80 3D ? ? ? ? ? 74 ? 33 C9"));
dummyVoidFunc rlInitFunc3 = (dummyVoidFunc)hook::get_pattern<void*>("48 83 EC ? E8 ? ? ? ? E8 ? ? ? ? E8 ? ? ? ? E8 ? ? ? ? 48 8D 0D");
dummyVoidFunc2 rlInitFunc4 = (dummyVoidFunc2)hook::get_pattern<void*>("48 83 EC ? 48 8D 0D ? ? ? ? 33 D2 E8 ? ? ? ? E8");
void* argToRlInitFunc4 = hook::get_by_offset<void, int32_t>(hook::get_pattern<uint8_t>("48 8D 0D ? ? ? ? C6 05 ? ? ? ? ? E8 ? ? ? ? 48 8D 0D ? ? ? ? B2"), 3);

rlInitFunc1();
rlInitFunc2();
rlInitFunc3();
rlInitFunc4(argToRlInitFunc4);
}

void WaitForRlInit()
Expand Down
64 changes: 11 additions & 53 deletions code/components/gta-net-five/src/NetHook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,59 +345,7 @@ static hook::cdecl_stub<void(int, int, int)> hostGame([] () -> void*

// b2190+: hook::get_call(hook::get_pattern("8B D7 8B CE 41 0F 95 C0 41 0F BA E8 08 E8", 13))

if (xbr::IsGameBuild<2189>())
{
return (void*)hook::get_adjusted(0x14105DFE8);
}

if (xbr::IsGameBuild<2372>())
{
return (void*)hook::get_adjusted(0x1410646BC);
}

if (xbr::IsGameBuild<2545>())
{
return (void*)hook::get_adjusted(0x14106FF30);
}

if (xbr::IsGameBuild<2612>())
{
return (void*)hook::get_adjusted(0x141071468);
}

if (xbr::IsGameBuild<2699>())
{
return (void*)hook::get_adjusted(0x14107AE54);
}

if (xbr::IsGameBuild<2802>())
{
return (void*)hook::get_adjusted(0x14107A4DC);
}

if (xbr::IsGameBuild<2944>())
{
return (void*)hook::get_adjusted(0x141088EB0);
}

if (xbr::IsGameBuild<3095>())
{
return (void*)hook::get_adjusted(0x141094DF8);
}

if (xbr::IsGameBuild<3258>())
{
return (void*)hook::get_adjusted(0x1410A1444);
}

// 1737
//return (void*)0x141029A20;

// 1868
//return (void*)0x141037BCC;

// 2060
return (void*)hook::get_adjusted(0x1410494F8);
return hook::get_call(hook::get_pattern("48 89 5C 24 ? 48 89 74 24 ? 57 48 83 EC ? 41 8A D8 8B FA 8B F1 E8 ? ? ? ? 45 33 C0", 0x31));
});

static void** g_networkMgrPtr = nullptr;
Expand Down Expand Up @@ -1516,6 +1464,16 @@ static HookFunction hookFunction([] ()
ExitProcess(-1);
});

// Increase network heap size with 1mb for our needs
if (xbr::IsGameBuildOrGreater<3323>())
{
uint32_t* size1 = hook::get_pattern<uint32_t>("41 B8 00 00 E0 00 48 8B C8", 2);
uint32_t* size2 = hook::get_pattern<uint32_t>("BA 00 00 E0 00 48 8B 01", 1);

*size1 += 1024 * 1024;
*size2 += 1024 * 1024;
}

// exit game on game exit from alt-f4
hook::call(hook::get_pattern("48 83 F8 04 75 ? 40 88", 6), ExitCleanly);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ static void ReloadMapStore()

#ifdef GTA_FIVE
// needs verification for newer builds
if (!xbr::IsGameBuildOrGreater<3258 + 1>())
if (!xbr::IsGameBuildOrGreater<3323 + 1>())
{
ReloadMapStoreNative();
}
Expand Down
Loading

0 comments on commit 2ee45e6

Please sign in to comment.