Merge branch 'develop' into rich-presence
This commit is contained in:
@ -8,6 +8,7 @@
|
||||
|
||||
#include <utils/hook.hpp>
|
||||
#include <utils/string.hpp>
|
||||
#include <utils/io.hpp>
|
||||
|
||||
namespace filesystem
|
||||
{
|
||||
@ -70,6 +71,40 @@ namespace filesystem
|
||||
return this->name_;
|
||||
}
|
||||
|
||||
std::unordered_set<std::string>& get_search_paths()
|
||||
{
|
||||
static std::unordered_set<std::string> search_paths{};
|
||||
return search_paths;
|
||||
}
|
||||
|
||||
std::string read_file(const std::string& path)
|
||||
{
|
||||
for (const auto& search_path : get_search_paths())
|
||||
{
|
||||
const auto path_ = search_path + "/" + path;
|
||||
if (utils::io::file_exists(path_))
|
||||
{
|
||||
return utils::io::read_file(path_);
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
bool read_file(const std::string& path, std::string* data)
|
||||
{
|
||||
for (const auto& search_path : get_search_paths())
|
||||
{
|
||||
const auto path_ = search_path + "/" + path;
|
||||
if (utils::io::read_file(path_, data))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
@ -87,6 +122,10 @@ namespace filesystem
|
||||
utils::hook::call(SELECT_VALUE(0x1403B8D31, 0x1404EE3D0), register_custom_path_stub);
|
||||
utils::hook::call(SELECT_VALUE(0x1403B8D51, 0x1404EE3F0), register_custom_path_stub);
|
||||
utils::hook::call(SELECT_VALUE(0x1403B8D90, 0x1404EE42F), register_custom_path_stub);
|
||||
|
||||
get_search_paths().insert(".");
|
||||
get_search_paths().insert("h1-mod");
|
||||
get_search_paths().insert("data");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -16,4 +16,8 @@ namespace filesystem
|
||||
std::string name_;
|
||||
std::string buffer_;
|
||||
};
|
||||
|
||||
std::unordered_set<std::string>& get_search_paths();
|
||||
std::string read_file(const std::string& path);
|
||||
bool read_file(const std::string& path, std::string* data);
|
||||
}
|
136
src/client/component/fonts.cpp
Normal file
136
src/client/component/fonts.cpp
Normal file
@ -0,0 +1,136 @@
|
||||
#include <std_include.hpp>
|
||||
#include "loader/component_loader.hpp"
|
||||
|
||||
#include "fonts.hpp"
|
||||
#include "console.hpp"
|
||||
#include "filesystem.hpp"
|
||||
|
||||
#include "game/game.hpp"
|
||||
#include "game/dvars.hpp"
|
||||
|
||||
#include <utils/hook.hpp>
|
||||
#include <utils/memory.hpp>
|
||||
#include <utils/io.hpp>
|
||||
#include <utils/string.hpp>
|
||||
#include <utils/image.hpp>
|
||||
#include <utils/concurrency.hpp>
|
||||
|
||||
namespace fonts
|
||||
{
|
||||
namespace
|
||||
{
|
||||
struct font_data_t
|
||||
{
|
||||
std::unordered_map<std::string, game::TTF*> fonts;
|
||||
std::unordered_map<std::string, std::string> raw_fonts;
|
||||
};
|
||||
|
||||
utils::concurrency::container<font_data_t> font_data;
|
||||
|
||||
game::TTF* create_font(const std::string& name, const std::string& data)
|
||||
{
|
||||
const auto font = utils::memory::get_allocator()->allocate<game::TTF>();
|
||||
font->name = utils::memory::get_allocator()->duplicate_string(name);
|
||||
font->buffer = utils::memory::get_allocator()->duplicate_string(data);
|
||||
font->len = static_cast<int>(data.size());
|
||||
font->fontFace = 0;
|
||||
return font;
|
||||
}
|
||||
|
||||
void free_font(game::TTF* font)
|
||||
{
|
||||
utils::memory::get_allocator()->free(font->buffer);
|
||||
utils::memory::get_allocator()->free(font->name);
|
||||
utils::memory::get_allocator()->free(font);
|
||||
}
|
||||
|
||||
game::TTF* load_font(const std::string& name)
|
||||
{
|
||||
return font_data.access<game::TTF*>([&](font_data_t& data_) -> game::TTF*
|
||||
{
|
||||
if (const auto i = data_.fonts.find(name); i != data_.fonts.end())
|
||||
{
|
||||
return i->second;
|
||||
}
|
||||
|
||||
std::string data{};
|
||||
if (const auto i = data_.raw_fonts.find(name); i != data_.raw_fonts.end())
|
||||
{
|
||||
data = i->second;
|
||||
}
|
||||
|
||||
if (data.empty() && !filesystem::read_file(name, &data))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const auto material = create_font(name, data);
|
||||
data_.fonts[name] = material;
|
||||
|
||||
return material;
|
||||
});
|
||||
}
|
||||
|
||||
game::TTF* try_load_font(const std::string& name)
|
||||
{
|
||||
try
|
||||
{
|
||||
return load_font(name);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
console::error("Failed to load font %s: %s\n", name.data(), e.what());
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
game::TTF* db_find_xasset_header_stub(game::XAssetType type, const char* name, int create_default)
|
||||
{
|
||||
auto result = try_load_font(name);
|
||||
if (result == nullptr)
|
||||
{
|
||||
result = game::DB_FindXAssetHeader(type, name, create_default).ttf;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
void add(const std::string& name, const std::string& data)
|
||||
{
|
||||
font_data.access([&](font_data_t& data_)
|
||||
{
|
||||
data_.raw_fonts[name] = data;
|
||||
});
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
font_data.access([&](font_data_t& data_)
|
||||
{
|
||||
for (auto& font : data_.fonts)
|
||||
{
|
||||
free_font(font.second);
|
||||
}
|
||||
|
||||
data_.fonts.clear();
|
||||
utils::hook::set<int>(SELECT_VALUE(0x14F09DBB8, 0x14FD61EE8), 0); // reset registered font count
|
||||
});
|
||||
}
|
||||
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
{
|
||||
if (game::environment::is_dedi())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
utils::hook::call(SELECT_VALUE(0x1404D41B6, 0x1405D9296), db_find_xasset_header_stub);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
REGISTER_COMPONENT(fonts::component)
|
7
src/client/component/fonts.hpp
Normal file
7
src/client/component/fonts.hpp
Normal file
@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
namespace fonts
|
||||
{
|
||||
void add(const std::string& name, const std::string& data);
|
||||
void clear();
|
||||
}
|
@ -571,7 +571,7 @@ namespace game_console
|
||||
{
|
||||
if (key == game::keyNum_t::K_F10)
|
||||
{
|
||||
if (!game::Com_InFrontEnd())
|
||||
if (!game::Com_InFrontend())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include "game/game.hpp"
|
||||
|
||||
#include "game_console.hpp"
|
||||
#include "game/ui_scripting/execution.hpp"
|
||||
|
||||
#include <utils/hook.hpp>
|
||||
|
||||
@ -14,8 +15,22 @@ namespace input
|
||||
utils::hook::detour cl_char_event_hook;
|
||||
utils::hook::detour cl_key_event_hook;
|
||||
|
||||
bool lui_running()
|
||||
{
|
||||
return *game::hks::lua_state != nullptr;
|
||||
}
|
||||
|
||||
void cl_char_event_stub(const int local_client_num, const int key)
|
||||
{
|
||||
if (lui_running())
|
||||
{
|
||||
ui_scripting::notify("keypress",
|
||||
{
|
||||
{"keynum", key},
|
||||
{"key", game::Key_KeynumToString(key, 0, 1)},
|
||||
});
|
||||
}
|
||||
|
||||
if (!game_console::console_char_event(local_client_num, key))
|
||||
{
|
||||
return;
|
||||
@ -26,6 +41,15 @@ namespace input
|
||||
|
||||
void cl_key_event_stub(const int local_client_num, const int key, const int down)
|
||||
{
|
||||
if (lui_running())
|
||||
{
|
||||
ui_scripting::notify(down ? "keydown" : "keyup",
|
||||
{
|
||||
{"keynum", key},
|
||||
{"key", game::Key_KeynumToString(key, 0, 1)},
|
||||
});
|
||||
}
|
||||
|
||||
if (!game_console::console_key_event(local_client_num, key, down))
|
||||
{
|
||||
return;
|
||||
|
@ -212,7 +212,7 @@ namespace logfile
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto hook = vm_execute_hooks[pos];
|
||||
const auto& hook = vm_execute_hooks[pos];
|
||||
const auto state = hook.lua_state();
|
||||
|
||||
const scripting::entity self = local_id_to_entity(game::scr_VmPub->function_frame->fs.localId);
|
||||
@ -296,6 +296,8 @@ namespace logfile
|
||||
public:
|
||||
void post_unpack() override
|
||||
{
|
||||
utils::hook::jump(SELECT_VALUE(0x140376655, 0x140444645), utils::hook::assemble(vm_execute_stub), true);
|
||||
|
||||
if (game::environment::is_sp())
|
||||
{
|
||||
return;
|
||||
@ -308,8 +310,6 @@ namespace logfile
|
||||
|
||||
utils::hook::call(0x140484EC0, g_shutdown_game_stub);
|
||||
utils::hook::call(0x1404853C1, g_shutdown_game_stub);
|
||||
|
||||
utils::hook::jump(SELECT_VALUE(0x140376655, 0x140444645), utils::hook::assemble(vm_execute_stub), true);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include "materials.hpp"
|
||||
#include "console.hpp"
|
||||
#include "filesystem.hpp"
|
||||
|
||||
#include "game/game.hpp"
|
||||
#include "game/dvars.hpp"
|
||||
@ -20,6 +21,7 @@ namespace materials
|
||||
{
|
||||
utils::hook::detour db_material_streaming_fail_hook;
|
||||
utils::hook::detour material_register_handle_hook;
|
||||
utils::hook::detour db_get_material_index_hook;
|
||||
|
||||
struct material_data_t
|
||||
{
|
||||
@ -27,6 +29,8 @@ namespace materials
|
||||
std::unordered_map<std::string, std::string> images;
|
||||
};
|
||||
|
||||
char constant_table[0x20] = {};
|
||||
|
||||
utils::concurrency::container<material_data_t> material_data;
|
||||
|
||||
game::GfxImage* setup_image(game::GfxImage* image, const utils::image& raw_image)
|
||||
@ -47,8 +51,7 @@ namespace materials
|
||||
|
||||
game::Material* create_material(const std::string& name, const std::string& data)
|
||||
{
|
||||
const auto white = *reinterpret_cast<game::Material**>(SELECT_VALUE(0x141F3D860, 0x14282C330));
|
||||
|
||||
const auto white = material_register_handle_hook.invoke<game::Material*>("white");
|
||||
const auto material = utils::memory::get_allocator()->allocate<game::Material>();
|
||||
const auto texture_table = utils::memory::get_allocator()->allocate<game::MaterialTextureDef>();
|
||||
const auto image = utils::memory::get_allocator()->allocate<game::GfxImage>();
|
||||
@ -57,6 +60,7 @@ namespace materials
|
||||
std::memcpy(texture_table, white->textureTable, sizeof(game::MaterialTextureDef));
|
||||
std::memcpy(image, white->textureTable->u.image, sizeof(game::GfxImage));
|
||||
|
||||
material->constantTable = &constant_table;
|
||||
material->name = utils::memory::get_allocator()->duplicate_string(name);
|
||||
image->name = material->name;
|
||||
|
||||
@ -66,6 +70,16 @@ namespace materials
|
||||
return material;
|
||||
}
|
||||
|
||||
void free_material(game::Material* material)
|
||||
{
|
||||
material->textureTable->u.image->textures.___u0.map->Release();
|
||||
material->textureTable->u.image->textures.shaderView->Release();
|
||||
utils::memory::get_allocator()->free(material->textureTable->u.image);
|
||||
utils::memory::get_allocator()->free(material->textureTable);
|
||||
utils::memory::get_allocator()->free(material->name);
|
||||
utils::memory::get_allocator()->free(material);
|
||||
}
|
||||
|
||||
game::Material* load_material(const std::string& name)
|
||||
{
|
||||
return material_data.access<game::Material*>([&](material_data_t& data_) -> game::Material*
|
||||
@ -81,10 +95,9 @@ namespace materials
|
||||
data = i->second;
|
||||
}
|
||||
|
||||
if (data.empty()
|
||||
&& !utils::io::read_file(utils::string::va("h1-mod/materials/%s.png", name.data()), &data)
|
||||
&& !utils::io::read_file(utils::string::va("data/materials/%s.png", name.data()), &data))
|
||||
if (data.empty() && !filesystem::read_file(utils::string::va("materials/%s.png", name.data()), &data))
|
||||
{
|
||||
data_.materials[name] = nullptr;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@ -124,24 +137,24 @@ namespace materials
|
||||
return result;
|
||||
}
|
||||
|
||||
bool db_material_streaming_fail_stub(game::Material* material)
|
||||
int db_material_streaming_fail_stub(game::Material* material)
|
||||
{
|
||||
const auto found = material_data.access<bool>([material](material_data_t& data_)
|
||||
if (material->constantTable == &constant_table)
|
||||
{
|
||||
if (data_.materials.find(material->name) != data_.materials.end())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
if (found)
|
||||
{
|
||||
return false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return db_material_streaming_fail_hook.invoke<bool>(material);
|
||||
return db_material_streaming_fail_hook.invoke<int>(material);
|
||||
}
|
||||
|
||||
unsigned int db_get_material_index_stub(game::Material* material)
|
||||
{
|
||||
if (material->constantTable == &constant_table)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return db_get_material_index_hook.invoke<unsigned int>(material);
|
||||
}
|
||||
}
|
||||
|
||||
@ -153,6 +166,24 @@ namespace materials
|
||||
});
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
material_data.access([&](material_data_t& data_)
|
||||
{
|
||||
for (auto& material : data_.materials)
|
||||
{
|
||||
if (material.second == nullptr)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
free_material(material.second);
|
||||
}
|
||||
|
||||
data_.materials.clear();
|
||||
});
|
||||
}
|
||||
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
@ -165,6 +196,7 @@ namespace materials
|
||||
|
||||
material_register_handle_hook.create(game::Material_RegisterHandle, material_register_handle_stub);
|
||||
db_material_streaming_fail_hook.create(SELECT_VALUE(0x1401D3180, 0x1402C6260), db_material_streaming_fail_stub);
|
||||
db_get_material_index_hook.create(SELECT_VALUE(0x1401CAD00, 0x1402BBB20), db_get_material_index_stub);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -3,4 +3,5 @@
|
||||
namespace materials
|
||||
{
|
||||
void add(const std::string& name, const std::string& data);
|
||||
void clear();
|
||||
}
|
||||
|
119
src/client/component/mods.cpp
Normal file
119
src/client/component/mods.cpp
Normal file
@ -0,0 +1,119 @@
|
||||
#include <std_include.hpp>
|
||||
#include "loader/component_loader.hpp"
|
||||
|
||||
#include "game/game.hpp"
|
||||
#include "game/dvars.hpp"
|
||||
|
||||
#include "command.hpp"
|
||||
#include "console.hpp"
|
||||
#include "scheduler.hpp"
|
||||
#include "filesystem.hpp"
|
||||
#include "materials.hpp"
|
||||
#include "fonts.hpp"
|
||||
#include "mods.hpp"
|
||||
|
||||
#include <utils/hook.hpp>
|
||||
#include <utils/io.hpp>
|
||||
|
||||
namespace mods
|
||||
{
|
||||
std::string mod_path{};
|
||||
|
||||
namespace
|
||||
{
|
||||
utils::hook::detour db_release_xassets_hook;
|
||||
bool release_assets = false;
|
||||
|
||||
void db_release_xassets_stub()
|
||||
{
|
||||
if (release_assets)
|
||||
{
|
||||
materials::clear();
|
||||
fonts::clear();
|
||||
}
|
||||
|
||||
db_release_xassets_hook.invoke<void>();
|
||||
}
|
||||
|
||||
void restart()
|
||||
{
|
||||
scheduler::once([]()
|
||||
{
|
||||
release_assets = true;
|
||||
game::Com_Shutdown("");
|
||||
release_assets = false;
|
||||
}, scheduler::pipeline::main);
|
||||
}
|
||||
}
|
||||
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
{
|
||||
if (!game::environment::is_sp())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!utils::io::directory_exists("mods"))
|
||||
{
|
||||
utils::io::create_directory("mods");
|
||||
}
|
||||
|
||||
db_release_xassets_hook.create(SELECT_VALUE(0x1401CD560, 0x1402BF160), db_release_xassets_stub);
|
||||
|
||||
command::add("loadmod", [](const command::params& params)
|
||||
{
|
||||
if (params.size() < 2)
|
||||
{
|
||||
console::info("Usage: loadmod mods/<modname>");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!game::Com_InFrontend())
|
||||
{
|
||||
console::info("Cannot load mod while in-game!\n");
|
||||
game::CG_GameMessage(0, "^1Cannot unload mod while in-game!");
|
||||
return;
|
||||
}
|
||||
|
||||
const auto path = params.get(1);
|
||||
if (!utils::io::directory_exists(path))
|
||||
{
|
||||
console::info("Mod %s not found!\n", path);
|
||||
return;
|
||||
}
|
||||
|
||||
console::info("Loading mod %s\n", path);
|
||||
filesystem::get_search_paths().erase(mod_path);
|
||||
filesystem::get_search_paths().insert(path);
|
||||
mod_path = path;
|
||||
restart();
|
||||
});
|
||||
|
||||
command::add("unloadmod", [](const command::params& params)
|
||||
{
|
||||
if (mod_path.empty())
|
||||
{
|
||||
console::info("No mod loaded\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!game::Com_InFrontend())
|
||||
{
|
||||
console::info("Cannot unload mod while in-game!\n");
|
||||
game::CG_GameMessage(0, "^1Cannot unload mod while in-game!");
|
||||
return;
|
||||
}
|
||||
|
||||
console::info("Unloading mod %s\n", mod_path.data());
|
||||
filesystem::get_search_paths().erase(mod_path);
|
||||
mod_path.clear();
|
||||
restart();
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
REGISTER_COMPONENT(mods::component)
|
6
src/client/component/mods.hpp
Normal file
6
src/client/component/mods.hpp
Normal file
@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
namespace mods
|
||||
{
|
||||
extern std::string mod_path;
|
||||
}
|
@ -241,10 +241,10 @@ namespace patches
|
||||
// unlock safeArea_*
|
||||
utils::hook::jump(0x1402624F5, 0x140262503);
|
||||
utils::hook::jump(0x14026251C, 0x140262547);
|
||||
dvars::override::register_int("safeArea_adjusted_horizontal", 1, 0, 1, game::DVAR_FLAG_SAVED);
|
||||
dvars::override::register_int("safeArea_adjusted_vertical", 1, 0, 1, game::DVAR_FLAG_SAVED);
|
||||
dvars::override::register_int("safeArea_horizontal", 1, 0, 1, game::DVAR_FLAG_SAVED);
|
||||
dvars::override::register_int("safeArea_vertical", 1, 0, 1, game::DVAR_FLAG_SAVED);
|
||||
dvars::override::register_float("safeArea_adjusted_horizontal", 1, 0, 1, game::DVAR_FLAG_SAVED);
|
||||
dvars::override::register_float("safeArea_adjusted_vertical", 1, 0, 1, game::DVAR_FLAG_SAVED);
|
||||
dvars::override::register_float("safeArea_horizontal", 1, 0, 1, game::DVAR_FLAG_SAVED);
|
||||
dvars::override::register_float("safeArea_vertical", 1, 0, 1, game::DVAR_FLAG_SAVED);
|
||||
|
||||
// allow servers to check for new packages more often
|
||||
dvars::override::register_int("sv_network_fps", 1000, 20, 1000, game::DVAR_FLAG_SAVED);
|
||||
@ -257,7 +257,7 @@ namespace patches
|
||||
|
||||
dvars::register_int("scr_game_spectatetype", 1, 0, 99, game::DVAR_FLAG_REPLICATED, "");
|
||||
|
||||
dvars::override::register_bool("ui_drawcrosshair", true, game::DVAR_FLAG_WRITE);
|
||||
dvars::override::register_bool("ui_drawCrosshair", true, game::DVAR_FLAG_WRITE);
|
||||
|
||||
dvars::override::register_int("com_maxfps", 0, 0, 1000, game::DVAR_FLAG_SAVED);
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
#include "loader/component_loader.hpp"
|
||||
|
||||
#include "game/game.hpp"
|
||||
#include <utils/hook.hpp>
|
||||
#include "game/dvars.hpp"
|
||||
|
||||
#include "game/scripting/entity.hpp"
|
||||
#include "game/scripting/functions.hpp"
|
||||
@ -13,14 +13,20 @@
|
||||
#include "scheduler.hpp"
|
||||
#include "scripting.hpp"
|
||||
|
||||
#include <utils/hook.hpp>
|
||||
#include <utils/io.hpp>
|
||||
#include <utils/string.hpp>
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
std::unordered_map<int, std::unordered_map<std::string, int>> fields_table;
|
||||
std::unordered_map<std::string, std::unordered_map<std::string, const char*>> script_function_table;
|
||||
utils::concurrency::container<shared_table_t> shared_table;
|
||||
|
||||
namespace
|
||||
{
|
||||
utils::hook::detour vm_notify_hook;
|
||||
utils::hook::detour vm_execute_hook;
|
||||
utils::hook::detour scr_load_level_hook;
|
||||
utils::hook::detour g_shutdown_game_hook;
|
||||
|
||||
@ -29,7 +35,14 @@ namespace scripting
|
||||
utils::hook::detour scr_set_thread_position_hook;
|
||||
utils::hook::detour process_script_hook;
|
||||
|
||||
utils::hook::detour sl_get_canonical_string_hook;
|
||||
|
||||
utils::hook::detour db_find_xasset_header_hook;
|
||||
|
||||
std::string current_file;
|
||||
unsigned int current_file_id{};
|
||||
|
||||
game::dvar_t* g_dump_scripts;
|
||||
|
||||
void vm_notify_stub(const unsigned int notify_list_owner_id, const game::scr_string_t string_value,
|
||||
game::VariableValue* top)
|
||||
@ -48,11 +61,6 @@ namespace scripting
|
||||
e.arguments.emplace_back(*value);
|
||||
}
|
||||
|
||||
if (e.name == "entitydeleted")
|
||||
{
|
||||
scripting::clear_entity_fields(e.entity);
|
||||
}
|
||||
|
||||
lua::engine::notify(e);
|
||||
}
|
||||
}
|
||||
@ -60,6 +68,16 @@ namespace scripting
|
||||
vm_notify_hook.invoke<void>(notify_list_owner_id, string_value, top);
|
||||
}
|
||||
|
||||
unsigned int vm_execute_stub()
|
||||
{
|
||||
if (!lua::engine::is_running())
|
||||
{
|
||||
lua::engine::start();
|
||||
}
|
||||
|
||||
return vm_execute_hook.invoke<unsigned int>();
|
||||
}
|
||||
|
||||
void scr_load_level_stub()
|
||||
{
|
||||
scr_load_level_hook.invoke<void>();
|
||||
@ -71,20 +89,25 @@ namespace scripting
|
||||
|
||||
void g_shutdown_game_stub(const int free_scripts)
|
||||
{
|
||||
if (free_scripts)
|
||||
{
|
||||
script_function_table.clear();
|
||||
}
|
||||
|
||||
lua::engine::stop();
|
||||
return g_shutdown_game_hook.invoke<void>(free_scripts);
|
||||
}
|
||||
|
||||
void scr_add_class_field_stub(unsigned int classnum, game::scr_string_t _name, unsigned int canonicalString, unsigned int offset)
|
||||
void scr_add_class_field_stub(unsigned int classnum, game::scr_string_t name, unsigned int canonical_string, unsigned int offset)
|
||||
{
|
||||
const auto name = game::SL_ConvertToString(_name);
|
||||
const auto name_str = game::SL_ConvertToString(name);
|
||||
|
||||
if (fields_table[classnum].find(name) == fields_table[classnum].end())
|
||||
if (fields_table[classnum].find(name_str) == fields_table[classnum].end())
|
||||
{
|
||||
fields_table[classnum][name] = offset;
|
||||
fields_table[classnum][name_str] = offset;
|
||||
}
|
||||
|
||||
scr_add_class_field_hook.invoke<void>(classnum, _name, canonicalString, offset);
|
||||
scr_add_class_field_hook.invoke<void>(classnum, name, canonical_string, offset);
|
||||
}
|
||||
|
||||
void process_script_stub(const char* filename)
|
||||
@ -92,21 +115,69 @@ namespace scripting
|
||||
const auto file_id = atoi(filename);
|
||||
if (file_id)
|
||||
{
|
||||
current_file = scripting::find_token(file_id);
|
||||
current_file_id = file_id;
|
||||
}
|
||||
else
|
||||
{
|
||||
current_file_id = 0;
|
||||
current_file = filename;
|
||||
}
|
||||
|
||||
process_script_hook.invoke<void>(filename);
|
||||
}
|
||||
|
||||
void scr_set_thread_position_stub(unsigned int threadName, const char* codePos)
|
||||
void add_function(const std::string& file, unsigned int id, const char* pos)
|
||||
{
|
||||
const auto function_name = scripting::find_token(threadName);
|
||||
script_function_table[current_file][function_name] = codePos;
|
||||
scr_set_thread_position_hook.invoke<void>(threadName, codePos);
|
||||
const auto function_names = scripting::find_token(id);
|
||||
for (const auto& name : function_names)
|
||||
{
|
||||
script_function_table[file][name] = pos;
|
||||
}
|
||||
}
|
||||
|
||||
void scr_set_thread_position_stub(unsigned int thread_name, const char* code_pos)
|
||||
{
|
||||
if (current_file_id)
|
||||
{
|
||||
const auto names = scripting::find_token(current_file_id);
|
||||
for (const auto& name : names)
|
||||
{
|
||||
add_function(name, thread_name, code_pos);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
add_function(current_file, thread_name, code_pos);
|
||||
}
|
||||
|
||||
scr_set_thread_position_hook.invoke<void>(thread_name, code_pos);
|
||||
}
|
||||
|
||||
unsigned int sl_get_canonical_string_stub(const char* str)
|
||||
{
|
||||
const auto result = sl_get_canonical_string_hook.invoke<unsigned int>(str);
|
||||
scripting::token_map[str] = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
game::XAssetHeader db_find_xasset_header_stub(game::XAssetType type, const char* name, int allow_create_default)
|
||||
{
|
||||
const auto result = db_find_xasset_header_hook.invoke<game::XAssetHeader>(type, name, allow_create_default);
|
||||
if (!g_dump_scripts->current.enabled || type != game::XAssetType::ASSET_TYPE_SCRIPTFILE)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string buffer;
|
||||
buffer.append(result.scriptfile->name, strlen(result.scriptfile->name) + 1);
|
||||
buffer.append(reinterpret_cast<char*>(&result.scriptfile->compressedLen), 4);
|
||||
buffer.append(reinterpret_cast<char*>(&result.scriptfile->len), 4);
|
||||
buffer.append(reinterpret_cast<char*>(&result.scriptfile->bytecodeLen), 4);
|
||||
buffer.append(result.scriptfile->buffer, result.scriptfile->compressedLen);
|
||||
buffer.append(result.scriptfile->bytecode, result.scriptfile->bytecodeLen);
|
||||
utils::io::write_file(utils::string::va("gsc_dump/%s.gscbin", name), buffer);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@ -115,21 +186,28 @@ namespace scripting
|
||||
public:
|
||||
void post_unpack() override
|
||||
{
|
||||
if (game::environment::is_sp())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
vm_notify_hook.create(SELECT_VALUE(0x140379A00, 0x1404479F0), vm_notify_stub);
|
||||
|
||||
scr_add_class_field_hook.create(SELECT_VALUE(0x140370370, 0x14043E2C0), scr_add_class_field_stub);
|
||||
|
||||
scr_set_thread_position_hook.create(SELECT_VALUE(0x14036A180, 0x140437D10), scr_set_thread_position_stub);
|
||||
process_script_hook.create(SELECT_VALUE(0x1403737E0, 0x1404417E0), process_script_stub);
|
||||
sl_get_canonical_string_hook.create(game::SL_GetCanonicalString, sl_get_canonical_string_stub);
|
||||
|
||||
if (!game::environment::is_sp())
|
||||
{
|
||||
scr_load_level_hook.create(SELECT_VALUE(0x1402A5BE0, 0x1403727C0), scr_load_level_stub);
|
||||
}
|
||||
else
|
||||
{
|
||||
vm_execute_hook.create(SELECT_VALUE(0x140376590, 0x140444580), vm_execute_stub);
|
||||
}
|
||||
|
||||
scr_load_level_hook.create(SELECT_VALUE(0x1402A5BE0, 0x1403727C0), scr_load_level_stub);
|
||||
g_shutdown_game_hook.create(SELECT_VALUE(0x140277D40, 0x140345A60), g_shutdown_game_stub);
|
||||
|
||||
db_find_xasset_header_hook.create(game::DB_FindXAssetHeader, db_find_xasset_header_stub);
|
||||
g_dump_scripts = dvars::register_bool("g_dumpScripts", false, game::DVAR_FLAG_NONE, "Dump GSC scripts");
|
||||
|
||||
scheduler::loop([]()
|
||||
{
|
||||
lua::engine::run_frame();
|
||||
@ -138,4 +216,4 @@ namespace scripting
|
||||
};
|
||||
}
|
||||
|
||||
REGISTER_COMPONENT(scripting::component)
|
||||
REGISTER_COMPONENT(scripting::component)
|
||||
|
@ -3,6 +3,9 @@
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
using shared_table_t = std::unordered_map<std::string, std::string>;
|
||||
|
||||
extern std::unordered_map<int, std::unordered_map<std::string, int>> fields_table;
|
||||
extern std::unordered_map<std::string, std::unordered_map<std::string, const char*>> script_function_table;
|
||||
extern utils::concurrency::container<shared_table_t> shared_table;
|
||||
}
|
@ -21,7 +21,7 @@ namespace stats
|
||||
utils::hook::detour is_item_unlocked_hook2;
|
||||
utils::hook::detour is_item_unlocked_hook3;
|
||||
|
||||
int is_item_unlocked_stub(void* a1, void* a2, void* a3)
|
||||
int is_item_unlocked_stub(int a1, void* a2, int a3)
|
||||
{
|
||||
if (cg_unlock_all_items->current.enabled)
|
||||
{
|
||||
@ -31,7 +31,7 @@ namespace stats
|
||||
return is_item_unlocked_hook.invoke<int>(a1, a2, a3);
|
||||
}
|
||||
|
||||
int is_item_unlocked_stub2(void* a1, void* a2, void* a3, void* a4, void* a5, void* a6)
|
||||
int is_item_unlocked_stub2(int a1, void* a2, void* a3, void* a4, int a5, void* a6)
|
||||
{
|
||||
if (cg_unlock_all_items->current.enabled)
|
||||
{
|
||||
@ -41,7 +41,7 @@ namespace stats
|
||||
return is_item_unlocked_hook2.invoke<int>(a1, a2, a3, a4, a5, a6);
|
||||
}
|
||||
|
||||
int is_item_unlocked_stub3(void* a1)
|
||||
int is_item_unlocked_stub3(int a1)
|
||||
{
|
||||
if (cg_unlock_all_items->current.enabled)
|
||||
{
|
||||
@ -50,6 +50,11 @@ namespace stats
|
||||
|
||||
return is_item_unlocked_hook3.invoke<int>(a1);
|
||||
}
|
||||
|
||||
int is_item_unlocked()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
class component final : public component_interface
|
||||
@ -57,19 +62,28 @@ namespace stats
|
||||
public:
|
||||
void post_unpack() override
|
||||
{
|
||||
if (!game::environment::is_mp())
|
||||
if (game::environment::is_sp())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
cg_unlock_all_items = dvars::register_bool("cg_unlockall_items", false, game::DVAR_FLAG_SAVED,
|
||||
"Whether items should be locked based on the player's stats or always unlocked.");
|
||||
dvars::register_bool("cg_unlockall_classes", false, game::DVAR_FLAG_SAVED,
|
||||
"Whether classes should be locked based on the player's stats or always unlocked.");
|
||||
if (game::environment::is_dedi())
|
||||
{
|
||||
utils::hook::jump(0x140413E60, is_item_unlocked);
|
||||
utils::hook::jump(0x140413860, is_item_unlocked);
|
||||
utils::hook::jump(0x140412B70, is_item_unlocked);
|
||||
}
|
||||
else
|
||||
{
|
||||
cg_unlock_all_items = dvars::register_bool("cg_unlockall_items", false, game::DVAR_FLAG_SAVED,
|
||||
"Whether items should be locked based on the player's stats or always unlocked.");
|
||||
dvars::register_bool("cg_unlockall_classes", false, game::DVAR_FLAG_SAVED,
|
||||
"Whether classes should be locked based on the player's stats or always unlocked.");
|
||||
|
||||
is_item_unlocked_hook.create(0x140413E60, is_item_unlocked_stub);
|
||||
is_item_unlocked_hook2.create(0x140413860, is_item_unlocked_stub2);
|
||||
is_item_unlocked_hook3.create(0x140412B70, is_item_unlocked_stub3);
|
||||
is_item_unlocked_hook.create(0x140413E60, is_item_unlocked_stub);
|
||||
is_item_unlocked_hook2.create(0x140413860, is_item_unlocked_stub2);
|
||||
is_item_unlocked_hook3.create(0x140412B70, is_item_unlocked_stub3);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user