Use signature utils

This commit is contained in:
Federico Cecchetto 2022-03-26 19:51:51 +01:00
parent 7b995b3348
commit 4ffdb331b4
15 changed files with 246 additions and 7516 deletions

View File

@ -469,8 +469,12 @@ namespace gsc
return {(game::g_entities[client].client->flags & 4) != 0};
});
utils::hook::jump(0x56C8EB, call_builtin_stub);
utils::hook::jump(0x56CBDC, call_builtin_method_stub);
// let other plugins read the pointers
post_load_callbacks.push_back([]()
{
utils::hook::jump(0x56C8EB, call_builtin_stub);
utils::hook::jump(0x56CBDC, call_builtin_method_stub);
});
}
};
}

View File

@ -95,7 +95,7 @@ namespace scripting
const auto file_id = atoi(filename);
if (file_id)
{
current_file = scripting::file_list[file_id];
current_file = scripting::find_file(file_id);
}
process_script_hook.invoke<void>(filename);

View File

@ -0,0 +1,108 @@
#include <stdinc.hpp>
#include "signatures.hpp"
#include <utils/hook.hpp>
#define PAYLOAD_SIZE 0x20000000
namespace signatures
{
size_t load_image_size()
{
MODULEINFO info{};
GetModuleInformation(GetCurrentProcess(),
GetModuleHandle("plutonium-bootstrapper-win32.exe"), &info, sizeof(MODULEINFO));
return info.SizeOfImage;
}
size_t get_image_size()
{
static const auto image_size = load_image_size();
return image_size;
}
void load_function_tables()
{
static const auto ptr = *reinterpret_cast<size_t*>(0x56CBDC + 0x1) + 0x56CBDC + 0x5;
static const auto function_table = *reinterpret_cast<size_t*>(0x56C8EB + 0x3);
static const auto method_table = *reinterpret_cast<size_t*>(ptr + 0xA);
game::plutonium::function_table.set(function_table);
game::plutonium::method_table.set(method_table);
}
size_t find_string_ptr(const std::string& string)
{
const char* string_ptr = nullptr;
std::string mask(string.size(), 'x');
utils::hook::signature signature(PAYLOAD_SIZE, get_image_size() - PAYLOAD_SIZE);
signature.add({
string,
mask,
[&](char* address)
{
string_ptr = address;
}
});
signature.process();
return reinterpret_cast<size_t>(string_ptr);
}
size_t find_string_ref(const std::string& string)
{
char bytes[4] = {0};
const auto string_ptr = find_string_ptr(string);
memcpy(bytes, &string_ptr, sizeof(size_t));
return find_string_ptr(bytes);
}
bool process_maps()
{
const auto string_ref = find_string_ref("Couldn't resolve builtin function id for name '%s'!");
if (!string_ref)
{
return false;
}
const auto map_ptr = *reinterpret_cast<size_t*>(string_ref - 0x3A);
game::plutonium::function_map_rev.set(map_ptr);
game::plutonium::method_map_rev.set(map_ptr + 0x20);
game::plutonium::file_map_rev.set(map_ptr + 0x40);
game::plutonium::token_map_rev.set(map_ptr + 0x60);
for (const auto& function : *game::plutonium::function_map_rev)
{
utils::io::write_file("functions.txt", function.first, true);
utils::io::write_file("functions.txt", "\n", true);
}
for (const auto& function : *game::plutonium::method_map_rev)
{
utils::io::write_file("methods.txt", function.first, true);
utils::io::write_file("methods.txt", "\n", true);
}
return true;
}
bool process_printf()
{
const auto string_ref = find_string_ref("A critical exception occured!\n");
if (!string_ref)
{
return false;
}
const auto offset = *reinterpret_cast<size_t*>(string_ref + 5);
game::plutonium::printf.set(string_ref + 4 + 5 + offset);
return true;
}
bool process()
{
load_function_tables();
process_printf();
return process_maps();
}
}

View File

@ -0,0 +1,6 @@
#pragma once
namespace signatures
{
bool process();
}

View File

@ -43,4 +43,4 @@ namespace string
};
}
REGISTER_COMPONENT(string::component)
REGISTER_COMPONENT(string::component)

View File

@ -1,22 +1,27 @@
#include <stdinc.hpp>
#include "loader/component_loader.hpp"
#include "component/signatures.hpp"
BOOL APIENTRY DllMain(HMODULE /*hModule*/, DWORD ul_reason_for_call, LPVOID /*lpReserved*/)
{
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
{
const auto value = *reinterpret_cast<DWORD*>(0x21B00000);
if (value != 0x64AA1902)
if (!signatures::process())
{
MessageBoxA(NULL,
"This version of iw5-gsc-utils is outdated.\n" \
"Download the latest dll from here: https://github.com/fedddddd/iw5-gsc-utils/releases",
"Download the latest dll from here: https://github.com/fedddddd/iw5-gsc-utils/releases",
"ERROR", MB_ICONERROR);
return FALSE;
}
utils::hook::jump(reinterpret_cast<uintptr_t>(&printf), game::plutonium::printf);
if (game::plutonium::printf.get() != nullptr)
{
utils::hook::jump(reinterpret_cast<uintptr_t>(&printf), game::plutonium::printf);
}
component_loader::post_unpack();
}

View File

@ -16,6 +16,11 @@ namespace game
return dedi_;
}
void set(const size_t dedi)
{
this->dedi_ = reinterpret_cast<T*>(dedi);
}
operator T* () const
{
return this->get();

View File

@ -263,11 +263,9 @@ namespace scripting
}
}
void array::set(const std::string& key, const script_value& _value) const
void array::set(const std::string& key, const script_value& value_) const
{
const auto value = _value.get_raw();
const auto string_value = game::SL_GetString(key.data(), 0);
const auto value = value_.get_raw();
const auto variable_id = this->get_value_id(key);
if (!variable_id)
@ -284,9 +282,9 @@ namespace scripting
variable->u.u = value.u;
}
void array::set(const unsigned int index, const script_value& _value) const
void array::set(const unsigned int index, const script_value& value_) const
{
const auto value = _value.get_raw();
const auto value = value_.get_raw();
const auto variable_id = this->get_value_id(index);
if (!variable_id)

File diff suppressed because it is too large Load Diff

View File

@ -7,10 +7,10 @@ namespace scripting
{
namespace
{
std::unordered_map<std::string, unsigned> lowercase_map(
const std::unordered_map<std::string, unsigned>& old_map)
std::unordered_map<std::string, uint16_t> lowercase_map(
const std::unordered_map<std::string, uint16_t>& old_map)
{
std::unordered_map<std::string, unsigned> new_map{};
std::unordered_map<std::string, uint16_t> new_map{};
for (auto& entry : old_map)
{
new_map[utils::string::to_lower(entry.first)] = entry.second;
@ -19,15 +19,15 @@ namespace scripting
return new_map;
}
const std::unordered_map<std::string, unsigned>& get_methods()
const std::unordered_map<std::string, uint16_t>& get_methods()
{
static auto methods = lowercase_map(method_map);
static auto methods = lowercase_map(*game::plutonium::method_map_rev);
return methods;
}
const std::unordered_map<std::string, unsigned>& get_functions()
const std::unordered_map<std::string, uint16_t>& get_functions()
{
static auto function = lowercase_map(function_map);
static auto function = lowercase_map(*game::plutonium::function_map_rev);
return function;
}
@ -71,8 +71,23 @@ namespace scripting
}
}
std::string find_file(unsigned int id)
{
const auto& file_map = *game::plutonium::file_map_rev;
for (const auto& file : file_map)
{
if (file.second == id)
{
return file.first;
}
}
return {};
}
std::string find_token(unsigned int id)
{
const auto& token_map = *game::plutonium::token_map_rev;
for (const auto& token : token_map)
{
if (token.second == id)
@ -86,6 +101,7 @@ namespace scripting
int find_token_id(const std::string& name)
{
const auto& token_map = *game::plutonium::token_map_rev;
const auto result = token_map.find(name);
if (result != token_map.end())

View File

@ -3,14 +3,10 @@
namespace scripting
{
extern std::unordered_map<std::string, unsigned> method_map;
extern std::unordered_map<std::string, unsigned> function_map;
extern std::unordered_map<std::string, unsigned> token_map;
extern std::unordered_map<unsigned, std::string> file_list;
using script_function = void(*)(game::scr_entref_t);
script_function find_function(const std::string& name, const bool prefer_global);
int find_token_id(const std::string& name);
std::string find_token(unsigned int id);
std::string find_file(unsigned int id);
}

View File

@ -86,11 +86,12 @@ namespace game
namespace plutonium
{
WEAK symbol<std::unordered_map<std::string, std::uint16_t>> function_map_rev{0x20802D34};
WEAK symbol<std::unordered_map<std::string, std::uint16_t>> method_map_rev{0x20802D54};
WEAK symbol<std::unordered_map<std::string, std::uint16_t>> token_map_rev{0x20802D94};
WEAK symbol<int(const char* fmt, ...)> printf{0x209F30F0};
WEAK symbol<void*> function_table{0x20762008};
WEAK symbol<void*> method_table{0x207627D8};
WEAK symbol<std::unordered_map<std::string, std::uint16_t>> function_map_rev{0};
WEAK symbol<std::unordered_map<std::string, std::uint16_t>> method_map_rev{0};
WEAK symbol<std::unordered_map<std::string, std::uint16_t>> file_map_rev{0};
WEAK symbol<std::unordered_map<std::string, std::uint16_t>> token_map_rev{0};
WEAK symbol<int(const char* fmt, ...)> printf{0};
WEAK symbol<void*> function_table{0};
WEAK symbol<void*> method_table{0};
}
}
}

View File

@ -28,6 +28,7 @@
#include <map>
#include <csetjmp>
#include <atlcomcli.h>
#include <Psapi.h>
#pragma comment(lib, "urlmon.lib")

View File

@ -4,6 +4,48 @@
namespace utils::hook
{
// open-iw5
void signature::process()
{
if (this->signatures_.empty()) return;
const auto start = static_cast<char*>(this->start_);
const unsigned int sig_count = this->signatures_.size();
const auto containers = this->signatures_.data();
for (size_t i = 0; i < this->length_; ++i)
{
const auto address = start + i;
for (unsigned int k = 0; k < sig_count; ++k)
{
const auto container = &containers[k];
unsigned int j;
for (j = 0; j < static_cast<unsigned int>(container->mask.size()); ++j)
{
if (container->mask[j] != '?' && container->signature[j] != address[j])
{
break;
}
}
if (j == container->mask.size())
{
container->callback(address);
}
}
}
}
void signature::add(const container& container)
{
signatures_.push_back(container);
}
namespace
{
[[maybe_unused]] class _

View File

@ -4,6 +4,37 @@
namespace utils::hook
{
class signature final
{
public:
struct container final
{
std::string signature;
std::string mask;
std::function<void(char*)> callback;
};
signature(void* start, const size_t length) : start_(start), length_(length)
{
}
signature(const DWORD start, const size_t length) : signature(reinterpret_cast<void*>(start), length)
{
}
signature() : signature(0x400000, 0x800000)
{
}
void process();
void add(const container& container);
private:
void* start_;
size_t length_;
std::vector<container> signatures_;
};
class detour
{
public: