From e45da37766e7f029a444e337af2e87d5b2205f3c Mon Sep 17 00:00:00 2001 From: FutureRave Date: Wed, 1 Feb 2023 15:09:42 +0000 Subject: [PATCH] add gsc dumping in scriptfile form --- src/module/fastfiles.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/module/fastfiles.cpp b/src/module/fastfiles.cpp index 4aae09e..e5bd40e 100644 --- a/src/module/fastfiles.cpp +++ b/src/module/fastfiles.cpp @@ -10,6 +10,8 @@ namespace { utils::hook::detour db_find_x_asset_header_hook; + const game::native::dvar_t* g_dump_scripts; + __declspec(naked) void db_load_stub_client(game::native::XZoneInfo*, unsigned int, int) { __asm @@ -24,12 +26,38 @@ namespace } } + void dump_gsc_script(const std::string& name, game::native::XAssetHeader header) + { + if (g_dump_scripts->current.enabled) + { + return; + } + + std::string buffer; + buffer.append(header.scriptfile->name, std::strlen(header.scriptfile->name) + 1); + buffer.append(reinterpret_cast(&header.scriptfile->compressedLen), sizeof(int)); + buffer.append(reinterpret_cast(&header.scriptfile->len), sizeof(int)); + buffer.append(reinterpret_cast(&header.scriptfile->bytecodeLen), sizeof(int)); + buffer.append(header.scriptfile->buffer, header.scriptfile->compressedLen); + buffer.append(reinterpret_cast(header.scriptfile->bytecode), header.scriptfile->bytecodeLen); + + const auto out_name = std::format("gsc_dump/{}.gscbin", name); + utils::io::write_file(out_name, buffer); + + console::info("Dumped %s\n", out_name.data()); + } + game::native::XAssetHeader db_find_x_asset_header_stub(game::native::XAssetType type, const char* name, int allow_create_default) { const auto start = game::native::Sys_Milliseconds(); const auto result = db_find_x_asset_header_hook.invoke(type, name, allow_create_default); const auto diff = game::native::Sys_Milliseconds() - start; + if (type == game::native::ASSET_TYPE_SCRIPTFILE) + { + dump_gsc_script(name, result); + } + if (diff > 100) { console::print( @@ -55,6 +83,8 @@ public: utils::hook(game::native::DB_LoadXAssets, db_load_stub, HOOK_JUMP).install()->quick(); db_find_x_asset_header_hook.create(game::native::DB_FindXAssetHeader, &db_find_x_asset_header_stub); + + g_dump_scripts = game::Dvar_RegisterBool("g_dumpScripts", false, game::native::DVAR_NONE, "Dump GSC scripts to binary format"); } private: