mirror of
https://github.com/alicealys/t5-gsc-utils.git
synced 2025-04-19 20:42:54 +00:00
Fix printf + improve wrap_function_call
This commit is contained in:
parent
964d6dc0b7
commit
ddf3edb2ed
@ -46,19 +46,13 @@ namespace gsc
|
|||||||
scripting::push_value(value);
|
scripting::push_value(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void call_function(const char* name)
|
void call_function(const function_t* function)
|
||||||
{
|
{
|
||||||
if (functions.find(name) == functions.end())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto args = get_arguments();
|
const auto args = get_arguments();
|
||||||
const auto& function = functions[name];
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
const auto value = function(args);
|
const auto value = function->operator()(args);
|
||||||
return_value(value);
|
return_value(value);
|
||||||
}
|
}
|
||||||
catch (const std::exception& e)
|
catch (const std::exception& e)
|
||||||
@ -67,15 +61,9 @@ namespace gsc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void call_method(const char* name, const game::scr_entref_t entref)
|
void call_method(const function_t* method, const game::scr_entref_t entref)
|
||||||
{
|
{
|
||||||
if (methods.find(name) == methods.end())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto args = get_arguments();
|
const auto args = get_arguments();
|
||||||
const auto& method = methods[name];
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -89,7 +77,7 @@ namespace gsc
|
|||||||
args_.push_back(arg);
|
args_.push_back(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto value = method(args_);
|
const auto value = method->operator()(args_);
|
||||||
return_value(value);
|
return_value(value);
|
||||||
}
|
}
|
||||||
catch (const std::exception& e)
|
catch (const std::exception& e)
|
||||||
@ -98,13 +86,12 @@ namespace gsc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void* wrap_function_call(const std::string& name)
|
void* wrap_function_call(const function_t* function)
|
||||||
{
|
{
|
||||||
const auto name_ = utils::memory::get_allocator()->duplicate_string(name);
|
return utils::hook::assemble([&](utils::hook::assembler& a)
|
||||||
return utils::hook::assemble([name_](utils::hook::assembler& a)
|
|
||||||
{
|
{
|
||||||
a.pushad();
|
a.pushad();
|
||||||
a.push(name_);
|
a.push(function);
|
||||||
a.call(call_function);
|
a.call(call_function);
|
||||||
a.add(esp, 0x4);
|
a.add(esp, 0x4);
|
||||||
a.popad();
|
a.popad();
|
||||||
@ -113,14 +100,13 @@ namespace gsc
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void* wrap_method_call(const std::string& name)
|
void* wrap_method_call(const function_t* method)
|
||||||
{
|
{
|
||||||
const auto name_ = utils::memory::get_allocator()->duplicate_string(name);
|
return utils::hook::assemble([&](utils::hook::assembler& a)
|
||||||
return utils::hook::assemble([name_](utils::hook::assembler& a)
|
|
||||||
{
|
{
|
||||||
a.pushad();
|
a.pushad();
|
||||||
a.push(dword_ptr(esp, 0x24));
|
a.push(dword_ptr(esp, 0x24));
|
||||||
a.push(name_);
|
a.push(method);
|
||||||
a.call(call_method);
|
a.call(call_method);
|
||||||
a.add(esp, 0x8);
|
a.add(esp, 0x8);
|
||||||
a.popad();
|
a.popad();
|
||||||
@ -222,10 +208,11 @@ namespace gsc
|
|||||||
{
|
{
|
||||||
void add_internal(const std::string& name, const function_t& function)
|
void add_internal(const std::string& name, const function_t& function)
|
||||||
{
|
{
|
||||||
const auto name_ = utils::string::to_lower(name);
|
const auto lower = utils::string::to_lower(name);
|
||||||
functions[name_] = function;
|
const auto [iterator, was_inserted] = functions.insert(std::make_pair(lower, function));
|
||||||
const auto call_wrap = wrap_function_call(name_);
|
const auto function_ptr = &iterator->second;
|
||||||
function_wraps[name_] = call_wrap;
|
const auto call_wrap = wrap_function_call(function_ptr);
|
||||||
|
function_wraps[lower] = call_wrap;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -233,10 +220,11 @@ namespace gsc
|
|||||||
{
|
{
|
||||||
void add_internal(const std::string& name, const function_t& method)
|
void add_internal(const std::string& name, const function_t& method)
|
||||||
{
|
{
|
||||||
const auto name_ = utils::string::to_lower(name);
|
const auto lower = utils::string::to_lower(name);
|
||||||
methods[name_] = method;
|
const auto [iterator, was_inserted] = methods.insert(std::make_pair(lower, method));
|
||||||
const auto call_wrap = wrap_method_call(name_);
|
const auto method_ptr = &iterator->second;
|
||||||
method_wraps[name_] = call_wrap;
|
const auto call_wrap = wrap_method_call(method_ptr);
|
||||||
|
method_wraps[lower] = call_wrap;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,11 +5,28 @@
|
|||||||
|
|
||||||
#include <utils/hook.hpp>
|
#include <utils/hook.hpp>
|
||||||
|
|
||||||
BOOL APIENTRY DllMain(HMODULE /*module_*/, DWORD ul_reason_for_call, LPVOID /*reserved_*/)
|
namespace
|
||||||
|
{
|
||||||
|
void printf_stub(const char* fmt, ...)
|
||||||
|
{
|
||||||
|
char buffer[2048]{};
|
||||||
|
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
|
||||||
|
vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, fmt, ap);
|
||||||
|
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
game::Com_Printf(0, "%s", buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOL APIENTRY DllMain(HMODULE /*module*/, DWORD ul_reason_for_call, LPVOID /*reserved*/)
|
||||||
{
|
{
|
||||||
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
|
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
|
||||||
{
|
{
|
||||||
utils::hook::jump(reinterpret_cast<size_t>(&printf), game::Com_Printf);
|
utils::hook::jump(reinterpret_cast<size_t>(&printf), printf_stub);
|
||||||
component_loader::post_unpack();
|
component_loader::post_unpack();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user