From 145f2d81d838a7bd3b374007d1acf53fae326e41 Mon Sep 17 00:00:00 2001 From: fed <58637860+fedddddd@users.noreply.github.com> Date: Sat, 1 Jul 2023 02:42:49 +0200 Subject: [PATCH] Increase pmem size --- src/client/component/gsc/script_loading.cpp | 27 +---- src/client/component/memory.cpp | 104 ++++++++++++++++++++ src/client/component/memory.hpp | 6 ++ src/client/game/structs.hpp | 44 +++++++-- src/client/game/symbols.hpp | 10 +- 5 files changed, 156 insertions(+), 35 deletions(-) create mode 100644 src/client/component/memory.cpp create mode 100644 src/client/component/memory.hpp diff --git a/src/client/component/gsc/script_loading.cpp b/src/client/component/gsc/script_loading.cpp index 52f9b139..a9b6d421 100644 --- a/src/client/component/gsc/script_loading.cpp +++ b/src/client/component/gsc/script_loading.cpp @@ -8,6 +8,7 @@ #include "component/console.hpp" #include "component/scripting.hpp" #include "component/fastfiles.hpp" +#include "component/memory.hpp" #include "script_loading.hpp" @@ -33,7 +34,7 @@ namespace gsc { char* buf = nullptr; char* pos = nullptr; - unsigned int size = 0x1000000; + unsigned int size = memory::custom_script_mem_size; } script_memory; char* allocate_buffer(size_t size) @@ -350,27 +351,6 @@ namespace gsc utils::hook::invoke(0x1405BFBF0); } - void pmem_init_stub() - { - utils::hook::invoke(0x14061EC80); - - const auto type_0 = &game::g_scriptmem[0]; - const auto type_1 = &game::g_scriptmem[1]; - - const auto size_0 = 0x200000; // default size - const auto size_1 = 0x200000 + script_memory.size; - - const auto block = reinterpret_cast(VirtualAlloc(NULL, size_0 + size_1, MEM_RESERVE, PAGE_READWRITE)); - - type_0->buf = block; - type_0->size = size_0; - - type_1->buf = block + size_0; - type_1->size = size_1; - - utils::hook::set(0x14061EC72, size_0 + size_1); - } - void add_function_name(const std::string& name, const std::uint16_t id) { const std::string_view name_ = utils::memory::get_allocator()->duplicate_string(name); @@ -427,9 +407,6 @@ namespace gsc utils::hook::call(0x1404C8F71, g_load_structs_stub); utils::hook::call(0x1404C8F80, scr_load_level_stub); - // increase script memory - utils::hook::call(0x1405A4798, pmem_init_stub); - add_function_name("isusinghdr", 0x242); add_function_name("tablegetrowcount", 0x2A6); diff --git a/src/client/component/memory.cpp b/src/client/component/memory.cpp new file mode 100644 index 00000000..eaacd465 --- /dev/null +++ b/src/client/component/memory.cpp @@ -0,0 +1,104 @@ +#include +#include "loader/component_loader.hpp" + +#include "memory.hpp" + +#include "game/game.hpp" + +#include +#include + +namespace memory +{ + namespace + { + constexpr auto mem_low_size = 0x80000000ull * 2; // default: 0x80000000 + constexpr auto mem_high_size = 0x80000000ull * 2; // default: 0x80000000 + + constexpr auto script_mem_low_size = 0x200000ull; // default: 0x200000 + constexpr auto script_mem_high_size = 0x200000ull + custom_script_mem_size; // default: 0x200000 + + constexpr auto phys_mem_low_size = 0x400000000ull * 2; // default: 0x400000000 + constexpr auto phys_mem_high_size = 0x400000000ull * 2; // default: 0x400000000 + + constexpr auto pmem_alloc_size = + mem_low_size + + mem_high_size + + script_mem_low_size + + script_mem_high_size + + phys_mem_low_size + + phys_mem_high_size; + + constexpr auto mem_low_buf = 0; + constexpr auto mem_high_buf = mem_low_buf + mem_low_size; + + constexpr auto script_mem_low_buf = mem_high_buf + mem_high_size; + constexpr auto script_mem_high_buf = script_mem_low_buf + script_mem_low_size; + + constexpr auto phys_mem_low_buf = script_mem_high_buf + script_mem_high_size; + constexpr auto phys_mem_high_buf = phys_mem_low_buf + phys_mem_low_size; + + constexpr auto stream_mem_size = 0x2000000ull; + + void pmem_init() + { + const auto size = pmem_alloc_size; + const auto allocated_buffer = VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_READWRITE); + auto buffer = reinterpret_cast(allocated_buffer); + *game::pmem_size = size; + *game::pmem_buffer = buffer; + + std::memset(game::g_mem, 0, sizeof(*game::g_mem)); + + game::g_mem->prim[game::PHYS_ALLOC_LOW].buf = buffer + mem_low_buf; + game::g_mem->prim[game::PHYS_ALLOC_LOW].pos = mem_low_size; + game::g_mem->prim[game::PHYS_ALLOC_HIGH].buf = buffer + mem_high_buf; + game::g_mem->prim[game::PHYS_ALLOC_HIGH].pos = mem_high_size; + + game::g_mem->prim[game::PHYS_ALLOC_LOW].unk1 = 0; + game::g_mem->prim[game::PHYS_ALLOC_HIGH].unk1 = 0; + + std::memset(game::g_scriptmem, 0, sizeof(*game::g_scriptmem)); + + game::g_scriptmem->prim[game::PHYS_ALLOC_LOW].buf = buffer + script_mem_low_buf; + game::g_scriptmem->prim[game::PHYS_ALLOC_LOW].pos = script_mem_low_size; + game::g_scriptmem->prim[game::PHYS_ALLOC_HIGH].buf = buffer + script_mem_high_buf; + game::g_scriptmem->prim[game::PHYS_ALLOC_HIGH].pos = script_mem_high_size; + + game::g_scriptmem->prim[game::PHYS_ALLOC_LOW].unk1 = 0; + game::g_scriptmem->prim[game::PHYS_ALLOC_HIGH].unk1 = 0; + + std::memset(game::g_physmem, 0, sizeof(*game::g_physmem)); + + game::g_physmem->prim[game::PHYS_ALLOC_LOW].buf = buffer + phys_mem_low_buf; + game::g_physmem->prim[game::PHYS_ALLOC_LOW].pos = phys_mem_low_size; + game::g_physmem->prim[game::PHYS_ALLOC_HIGH].buf = buffer + phys_mem_high_buf; + game::g_physmem->prim[game::PHYS_ALLOC_HIGH].pos = phys_mem_high_size; + + game::g_physmem->prim[game::PHYS_ALLOC_LOW].unk1 = 2; + game::g_physmem->prim[game::PHYS_ALLOC_HIGH].unk1 = 2; + + *game::stream_size = stream_mem_size; + *game::stream_buffer = reinterpret_cast(VirtualAlloc(NULL, *game::stream_size, MEM_COMMIT, PAGE_READWRITE)); + } + + void pmem_init_stub() + { + pmem_init(); + const auto script_mem_size = script_mem_low_size + script_mem_high_size; + utils::hook::set(0x14061EC72, static_cast(script_mem_size)); + } + } + + class component final : public component_interface + { + public: + void post_unpack() override + { + // patch PMem_Init, so we can use whatever memory size we want + utils::hook::call(0x1405A4798, pmem_init_stub); + } + }; +} + +REGISTER_COMPONENT(memory::component) diff --git a/src/client/component/memory.hpp b/src/client/component/memory.hpp new file mode 100644 index 00000000..c0f0ea63 --- /dev/null +++ b/src/client/component/memory.hpp @@ -0,0 +1,6 @@ +#pragma once + +namespace memory +{ + constexpr auto custom_script_mem_size = 0x1000000ull; +} diff --git a/src/client/game/structs.hpp b/src/client/game/structs.hpp index 7131ad9e..fab98422 100644 --- a/src/client/game/structs.hpp +++ b/src/client/game/structs.hpp @@ -2844,6 +2844,13 @@ namespace game int vertAlign; }; + enum PMem_Direction + { + PHYS_ALLOC_LOW = 0x0, + PHYS_ALLOC_HIGH = 0x1, + PHYS_ALLOC_COUNT = 0x2, + }; + enum PMem_Source { PMEM_SOURCE_EXTERNAL = 0x0, @@ -2852,19 +2859,38 @@ namespace game PMEM_SOURCE_DEFAULT_HIGH = 0x3, PMEM_SOURCE_MOVIE = 0x4, PMEM_SOURCE_SCRIPT = 0x5, + PMEM_SOURCE_UNK5 = 0x5, + PMEM_SOURCE_UNK6 = 0x6, + PMEM_SOURCE_UNK7 = 0x7, + PMEM_SOURCE_UNK8 = 0x8, + PMEM_SOURCE_CUSTOMIZATION = 0x9, }; - struct physical_memory + struct PhysicalMemoryAllocation { - char __pad0[0x10]; - char* buf; - char __pad1[0x8]; - int unk1; - size_t size; - char __pad2[0x500]; - }; + const char* name; + char __pad0[16]; + unsigned __int64 pos; + char __pad1[8]; + }; static_assert(sizeof(PhysicalMemoryAllocation) == 40); - static_assert(sizeof(physical_memory) == 0x530); + struct PhysicalMemoryPrim + { + const char* name; + unsigned int allocListCount; + char __pad0[4]; + unsigned char* buf; + char __pad1[8]; + int unk1; + char __pad2[4]; + unsigned __int64 pos; + PhysicalMemoryAllocation allocList[32]; + }; static_assert(sizeof(PhysicalMemoryPrim) == 1328); + + struct PhysicalMemory + { + PhysicalMemoryPrim prim[2]; + }; static_assert(sizeof(PhysicalMemory) == 0xA60); union GamerProfileDataUnion { diff --git a/src/client/game/symbols.hpp b/src/client/game/symbols.hpp index 047fecd5..efd254f3 100644 --- a/src/client/game/symbols.hpp +++ b/src/client/game/symbols.hpp @@ -271,7 +271,15 @@ namespace game WEAK symbol g_script_error{0x14BA9CD40}; WEAK symbol g_classMap{0x140BF95C0}; - WEAK symbol g_scriptmem{0x14CC9FEC0}; + WEAK game::symbol pmem_size{0x14CC9F458}; + WEAK game::symbol pmem_buffer{0x14CC9F450}; + + WEAK game::symbol g_mem{0x14CC9F460}; + WEAK game::symbol g_scriptmem{0x14CC9FEC0}; + WEAK game::symbol g_physmem{0x14CCA0920}; + + WEAK game::symbol stream_size{0x141865C90}; + WEAK game::symbol stream_buffer{0x141865C88}; WEAK symbol scr_VarGlob{0x14B617C00}; WEAK symbol scr_VmPub{0x14BA9EE40};