mirror of
https://github.com/fedddddd/iw5-gsc-utils.git
synced 2026-08-01 12:50:33 +00:00
fixes
This commit is contained in:
@@ -8,6 +8,8 @@
|
||||
namespace command
|
||||
{
|
||||
std::unordered_map<std::string, std::function<void(params&)>> handlers;
|
||||
std::vector<std::string> script_commands;
|
||||
utils::memory::allocator allocator;
|
||||
|
||||
void main_handler()
|
||||
{
|
||||
@@ -46,7 +48,11 @@ namespace command
|
||||
|
||||
for (auto i = index; i < this->size(); i++)
|
||||
{
|
||||
if (i > index) result.append(" ");
|
||||
if (i > index)
|
||||
{
|
||||
result.append(" ");
|
||||
}
|
||||
|
||||
result.append(this->get(i));
|
||||
}
|
||||
|
||||
@@ -70,14 +76,11 @@ namespace command
|
||||
handlers[command] = callback;
|
||||
}
|
||||
|
||||
std::vector<std::string> script_commands;
|
||||
utils::memory::allocator allocator;
|
||||
|
||||
void add_script_command(const std::string& name, const std::function<void(const params&)>& callback)
|
||||
{
|
||||
script_commands.push_back(name);
|
||||
const auto _name = allocator.duplicate_string(name);
|
||||
add(_name, callback);
|
||||
const auto name_str = allocator.duplicate_string(name);
|
||||
add(name_str, callback);
|
||||
}
|
||||
|
||||
void clear_script_commands()
|
||||
@@ -95,7 +98,7 @@ namespace command
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
void on_startup([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
@@ -25,4 +25,4 @@ namespace command
|
||||
|
||||
void add_script_command(const std::string& name, const std::function<void(const params&)>& callback);
|
||||
void clear_script_commands();
|
||||
}
|
||||
}
|
||||
|
||||
+64
-191
@@ -14,41 +14,8 @@
|
||||
|
||||
namespace gsc
|
||||
{
|
||||
std::unordered_map<unsigned, script_function> functions;
|
||||
std::unordered_map<unsigned, script_method> methods;
|
||||
|
||||
namespace
|
||||
{
|
||||
std::string method_name(unsigned int id)
|
||||
{
|
||||
const auto& map = (*game::plutonium::gsc_ctx)->meth_map();
|
||||
|
||||
for (const auto& function : map)
|
||||
{
|
||||
if (function.second == id)
|
||||
{
|
||||
return function.first.data();
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string function_name(unsigned int id)
|
||||
{
|
||||
const auto& map = (*game::plutonium::gsc_ctx)->func_map();
|
||||
|
||||
for (const auto& function : map)
|
||||
{
|
||||
if (function.second == id)
|
||||
{
|
||||
return function.first.data();
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
function_args get_arguments()
|
||||
{
|
||||
std::vector<scripting::script_value> args;
|
||||
@@ -72,9 +39,6 @@ namespace gsc
|
||||
scripting::push_value(value);
|
||||
}
|
||||
|
||||
auto function_map_start = 0x200;
|
||||
auto method_map_start = 0x8400;
|
||||
auto token_map_start = 0x8000;
|
||||
auto field_offset_start = 0xA000;
|
||||
|
||||
struct entity_field
|
||||
@@ -87,16 +51,11 @@ namespace gsc
|
||||
std::vector<std::function<void()>> post_load_callbacks;
|
||||
std::unordered_map<unsigned int, std::unordered_map<unsigned int, entity_field>> custom_fields;
|
||||
|
||||
void call_function(unsigned int id)
|
||||
void call_function(const script_function* function, const char* name)
|
||||
{
|
||||
if (id < 0x200)
|
||||
{
|
||||
return reinterpret_cast<builtin_function*>(game::plutonium::function_table.get())[id]();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
const auto result = functions[id](get_arguments());
|
||||
const auto result = function->operator()(get_arguments());
|
||||
const auto type = result.get_raw().type;
|
||||
|
||||
if (type)
|
||||
@@ -106,23 +65,18 @@ namespace gsc
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
printf("************** Script execution error **************\n");
|
||||
printf("Error executing function %s:\n", function_name(id).data());
|
||||
printf(" %s\n", e.what());
|
||||
printf("****************************************************\n");
|
||||
printf("************** Script execution error **************");
|
||||
printf("Error executing function %s:", name);
|
||||
printf(" %s", e.what());
|
||||
printf("****************************************************");
|
||||
}
|
||||
}
|
||||
|
||||
void call_method(game::scr_entref_t ent, unsigned int id)
|
||||
void call_method(const script_method* method, const char* name, game::scr_entref_t ent)
|
||||
{
|
||||
if (id < 0x8400)
|
||||
{
|
||||
return reinterpret_cast<builtin_method*>(game::plutonium::method_table.get())[id - 0x8000](ent);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
const auto result = methods[id](ent, get_arguments());
|
||||
const auto result = method->operator()(ent, get_arguments());
|
||||
const auto type = result.get_raw().type;
|
||||
|
||||
if (type)
|
||||
@@ -132,55 +86,10 @@ namespace gsc
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
printf("************** Script execution error **************\n");
|
||||
printf("Error executing method %s:\n", method_name(id).data());
|
||||
printf(" %s\n", e.what());
|
||||
printf("****************************************************\n");
|
||||
}
|
||||
}
|
||||
|
||||
__declspec(naked) void call_builtin_stub()
|
||||
{
|
||||
__asm
|
||||
{
|
||||
push eax
|
||||
|
||||
mov eax, 0x20B4A5C
|
||||
mov [eax], esi
|
||||
|
||||
mov eax, 0x20B4A90
|
||||
mov [eax], edx
|
||||
|
||||
pop eax
|
||||
|
||||
pushad
|
||||
push eax
|
||||
call call_function
|
||||
pop eax
|
||||
popad
|
||||
|
||||
push 0x56C900
|
||||
retn
|
||||
}
|
||||
}
|
||||
|
||||
__declspec(naked) void call_builtin_method_stub()
|
||||
{
|
||||
__asm
|
||||
{
|
||||
pushad
|
||||
push ecx
|
||||
push ebx
|
||||
call call_method
|
||||
pop ebx
|
||||
pop ecx
|
||||
popad
|
||||
|
||||
push ebx
|
||||
add esp, 0xC
|
||||
|
||||
push 0x56CBE9
|
||||
retn
|
||||
printf("************** Script execution error **************");
|
||||
printf("Error executing method %s:", name);
|
||||
printf(" %s", e.what());
|
||||
printf("****************************************************");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -201,10 +110,10 @@ namespace gsc
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
printf("************** Script execution error **************\n");
|
||||
printf("Error getting field %s:\n", field.name.data());
|
||||
printf(" %s\n", e.what());
|
||||
printf("****************************************************\n");
|
||||
printf("************** Script execution error **************");
|
||||
printf("Error getting field %s:", field.name.data());
|
||||
printf(" %s", e.what());
|
||||
printf("****************************************************");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,10 +134,10 @@ namespace gsc
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
printf("************** Script execution error **************\n");
|
||||
printf("Error setting field %s:\n", field.name.data());
|
||||
printf(" %s\n", e.what());
|
||||
printf("****************************************************\n");
|
||||
printf("************** Script execution error **************");
|
||||
printf("Error setting field %s:", field.name.data());
|
||||
printf(" %s", e.what());
|
||||
printf("****************************************************");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -244,53 +153,51 @@ namespace gsc
|
||||
}
|
||||
}
|
||||
|
||||
namespace function
|
||||
void* make_function_thunk(const char* name, const script_function* func)
|
||||
{
|
||||
void add(const std::string& name, const script_function& func)
|
||||
static std::uint8_t bytes[] =
|
||||
{
|
||||
auto index = 0u;
|
||||
auto& ctx = (*game::plutonium::gsc_ctx);
|
||||
|
||||
if (ctx->func_exists(name))
|
||||
{
|
||||
printf("[iw5-gsc-utils] Warning: function '%s' already defined\n", name.data());
|
||||
index = ctx->func_id(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
index = function_map_start++;
|
||||
ctx->func_add(name, index);
|
||||
}
|
||||
0x68, 0x44, 0x33, 0x22, 0x11, // push
|
||||
0x68, 0x44, 0x33, 0x22, 0x11, // push
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, // call
|
||||
0x83, 0xC4, 0x08, // add esp, 8
|
||||
0xC3 // ret
|
||||
};
|
||||
|
||||
functions.insert(std::make_pair(index, func));
|
||||
}
|
||||
const auto stub = utils::memory::allocate_array<std::uint8_t>(sizeof(bytes));
|
||||
std::memcpy(stub, bytes, sizeof(bytes));
|
||||
|
||||
utils::hook::unprotect(stub, sizeof(bytes));
|
||||
|
||||
*reinterpret_cast<std::size_t*>(stub + 1) = reinterpret_cast<size_t>(name);
|
||||
*reinterpret_cast<std::size_t*>(stub + 6) = reinterpret_cast<size_t>(func);
|
||||
utils::hook::call(stub + 10, call_function);
|
||||
|
||||
return stub;
|
||||
}
|
||||
|
||||
namespace method
|
||||
void* make_method_thunk(const char* name, const script_method* func)
|
||||
{
|
||||
void add(const std::string& name, const script_method& func)
|
||||
static std::uint8_t bytes[] =
|
||||
{
|
||||
if (true)
|
||||
{
|
||||
return;
|
||||
}
|
||||
0xFF, 0x74, 0x24, 0x04, // push [esp+4]
|
||||
0x68, 0x44, 0x33, 0x22, 0x11, // push
|
||||
0x68, 0x44, 0x33, 0x22, 0x11, // push
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, // call
|
||||
0x83, 0xC4, 0x0C, // add esp, 8
|
||||
0xC3 // ret
|
||||
};
|
||||
|
||||
auto index = 0u;
|
||||
auto& ctx = (*game::plutonium::gsc_ctx);
|
||||
const auto stub = utils::memory::allocate_array<std::uint8_t>(sizeof(bytes));
|
||||
std::memcpy(stub, bytes, sizeof(bytes));
|
||||
|
||||
if (ctx->meth_exists(name))
|
||||
{
|
||||
printf("[iw5-gsc-utils] Warning: method '%s' already defined\n", name.data());
|
||||
index = ctx->meth_id(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
index = method_map_start++;
|
||||
ctx->meth_add(name, index);
|
||||
}
|
||||
utils::hook::unprotect(stub, sizeof(bytes));
|
||||
|
||||
methods.insert(std::make_pair(index, func));
|
||||
}
|
||||
*reinterpret_cast<std::size_t*>(stub + 5) = reinterpret_cast<size_t>(name);
|
||||
*reinterpret_cast<std::size_t*>(stub + 10) = reinterpret_cast<size_t>(func);
|
||||
utils::hook::call(stub + 14, call_method);
|
||||
|
||||
return stub;
|
||||
}
|
||||
|
||||
namespace field
|
||||
@@ -338,7 +245,7 @@ namespace gsc
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
void on_startup([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
scr_get_object_field_hook.create(0x52BDB0, scr_get_object_field_stub);
|
||||
scr_set_object_field_hook.create(0x52BCC0, scr_set_object_field_stub);
|
||||
@@ -394,14 +301,6 @@ namespace gsc
|
||||
return {};
|
||||
});
|
||||
|
||||
function::add("say", [](const function_args& args) -> scripting::script_value
|
||||
{
|
||||
const auto message = args[0].as<std::string>();
|
||||
game::SV_GameSendServerCommand(-1, 0, utils::string::va("%c \"%s\"", 84, message.data()));
|
||||
|
||||
return {};
|
||||
});
|
||||
|
||||
function::add("dropallbots", [](const function_args&) -> scripting::script_value
|
||||
{
|
||||
for (auto i = 0; i < *game::svs_clientCount; i++)
|
||||
@@ -416,45 +315,26 @@ namespace gsc
|
||||
return {};
|
||||
});
|
||||
|
||||
method::add("tell", [](const game::scr_entref_t ent, const function_args& args) -> scripting::script_value
|
||||
{
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
}
|
||||
|
||||
const auto client = ent.entnum;
|
||||
|
||||
if (game::g_entities[client].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Not a player entity");
|
||||
}
|
||||
|
||||
const auto message = args[0].as<std::string>();
|
||||
game::SV_GameSendServerCommand(client, 0, utils::string::va("%c \"%s\"", 84, message.data()));
|
||||
|
||||
return {};
|
||||
});
|
||||
|
||||
method::add("specialtymarathon", [](const game::scr_entref_t ent, const function_args& args) -> scripting::script_value
|
||||
{
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
throw std::runtime_error("invalid entity");
|
||||
}
|
||||
|
||||
const auto client = ent.entnum;
|
||||
|
||||
if (game::g_entities[client].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Not a player entity");
|
||||
throw std::runtime_error("not a player entity");
|
||||
}
|
||||
|
||||
const auto toggle = args[0].as<int>();
|
||||
auto flags = game::g_entities[client].client->ps.perks[0];
|
||||
|
||||
game::g_entities[client].client->ps.perks[0] = toggle
|
||||
? flags | 0x4000u : flags & ~0x4000u;
|
||||
? flags | 0x4000u
|
||||
: flags & ~0x4000u;
|
||||
|
||||
return {};
|
||||
});
|
||||
@@ -463,14 +343,14 @@ namespace gsc
|
||||
{
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
throw std::runtime_error("invalid entity");
|
||||
}
|
||||
|
||||
const auto client = ent.entnum;
|
||||
|
||||
if (game::g_entities[client].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Not a player entity");
|
||||
throw std::runtime_error("not a player entity");
|
||||
}
|
||||
|
||||
return game::svs_clients[client].bIsTestClient;
|
||||
@@ -480,25 +360,18 @@ namespace gsc
|
||||
{
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
throw std::runtime_error("invalid entity");
|
||||
}
|
||||
|
||||
const auto client = ent.entnum;
|
||||
|
||||
if (game::g_entities[client].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Not a player entity");
|
||||
throw std::runtime_error("not a player entity");
|
||||
}
|
||||
|
||||
return {(game::g_entities[client].client->flags & 4) != 0};
|
||||
});
|
||||
|
||||
// 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);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
+21
-2
@@ -36,14 +36,33 @@ namespace gsc
|
||||
using script_function = std::function<scripting::script_value(const function_args&)>;
|
||||
using script_method = std::function<scripting::script_value(const game::scr_entref_t, const function_args&)>;
|
||||
|
||||
void* make_function_thunk(const char* name, const script_function* func);
|
||||
void* make_method_thunk(const char* name, const script_method* meth);
|
||||
|
||||
namespace function
|
||||
{
|
||||
void add(const std::string& name, const script_function& func);
|
||||
template <typename F>
|
||||
void add(const std::string& name, F&& f)
|
||||
{
|
||||
const auto name_str = utils::memory::duplicate_string(name);
|
||||
const auto func = new script_function(std::forward<F>(f));
|
||||
const auto thunk = reinterpret_cast<
|
||||
plutonium::sdk::interfaces::gsc::function_callback>(make_function_thunk(name_str, func));
|
||||
plugin::get()->get_interface()->gsc()->register_function(name, thunk);
|
||||
}
|
||||
}
|
||||
|
||||
namespace method
|
||||
{
|
||||
void add(const std::string& name, const script_method& func);
|
||||
template <typename F>
|
||||
void add(const std::string& name, F&& f)
|
||||
{
|
||||
const auto name_str = utils::memory::duplicate_string(name);
|
||||
const auto func = new script_method(std::forward<F>(f));
|
||||
const auto thunk = reinterpret_cast<
|
||||
plutonium::sdk::interfaces::gsc::method_callback>(make_method_thunk(name_str, func));
|
||||
plugin::get()->get_interface()->gsc()->register_method(name, thunk);
|
||||
}
|
||||
}
|
||||
|
||||
namespace field
|
||||
|
||||
+15
-60
@@ -10,79 +10,34 @@ namespace io
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
void on_after_dvar_init([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
const auto path = game::Dvar_FindVar("fs_basegame")->current.string;
|
||||
std::filesystem::current_path(path);
|
||||
const auto fs_basegame = game::Dvar_FindVar("fs_basegame");
|
||||
if (fs_basegame == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
printf("working directory: %s", fs_basegame->current.string);
|
||||
std::filesystem::current_path(fs_basegame->current.string);
|
||||
}
|
||||
|
||||
void on_startup([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
gsc::function::add("jsonprint", [](const gsc::function_args& args) -> scripting::script_value
|
||||
{
|
||||
std::string buffer;
|
||||
|
||||
for (const auto arg : args.get_raw())
|
||||
for (const auto& arg : args.get_raw())
|
||||
{
|
||||
buffer.append(json::gsc_to_string(arg));
|
||||
buffer.append("\t");
|
||||
}
|
||||
|
||||
printf("%s\n", buffer.data());
|
||||
printf("%s", buffer.data());
|
||||
return {};
|
||||
});
|
||||
|
||||
gsc::function::add("fremove", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto path = args[0].as<const char*>();
|
||||
return std::remove(path);
|
||||
});
|
||||
|
||||
gsc::function::add("fopen", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto* path = args[0].as<const char*>();
|
||||
const auto* mode = args[1].as<const char*>();
|
||||
|
||||
const auto handle = fopen(path, mode);
|
||||
|
||||
if (!handle)
|
||||
{
|
||||
printf("fopen: Invalid path\n");
|
||||
}
|
||||
|
||||
return handle;
|
||||
});
|
||||
|
||||
gsc::function::add("fclose", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto handle = args[0].as_ptr<FILE>();
|
||||
return fclose(handle);
|
||||
});
|
||||
|
||||
gsc::function::add("fwrite", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto handle = args[0].as_ptr<FILE>();
|
||||
const auto text = args[1].as<const char*>();
|
||||
|
||||
return fprintf(handle, text);
|
||||
});
|
||||
|
||||
gsc::function::add("fread", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto handle = args[0].as_ptr<FILE>();
|
||||
|
||||
fseek(handle, 0, SEEK_END);
|
||||
const auto length = ftell(handle);
|
||||
|
||||
fseek(handle, 0, SEEK_SET);
|
||||
char* buffer = (char*)calloc(length, sizeof(char));
|
||||
|
||||
fread(buffer, sizeof(char), length, handle);
|
||||
|
||||
const std::string result = buffer;
|
||||
|
||||
free(buffer);
|
||||
|
||||
return result;
|
||||
});
|
||||
|
||||
gsc::function::add("fileexists", [](const gsc::function_args& args)
|
||||
{
|
||||
const auto path = args[0].as<std::string>();
|
||||
@@ -179,4 +134,4 @@ namespace io
|
||||
};
|
||||
}
|
||||
|
||||
//REGISTER_COMPONENT(io::component)
|
||||
REGISTER_COMPONENT(io::component)
|
||||
|
||||
@@ -144,7 +144,7 @@ namespace json
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
void on_startup([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
gsc::function::add("array", [](const gsc::function_args& args)
|
||||
{
|
||||
@@ -194,4 +194,4 @@ namespace json
|
||||
};
|
||||
}
|
||||
|
||||
//REGISTER_COMPONENT(json::component)
|
||||
REGISTER_COMPONENT(json::component)
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "loader/component_loader.hpp"
|
||||
|
||||
#include "scheduler.hpp"
|
||||
#include "gsc.hpp"
|
||||
#include "scripting.hpp"
|
||||
|
||||
namespace notifies
|
||||
{
|
||||
namespace
|
||||
{
|
||||
std::vector<scripting::function> say_callbacks;
|
||||
utils::hook::detour client_command_hook;
|
||||
|
||||
void client_command_stub(int clientNum)
|
||||
{
|
||||
char cmd[1024] = {0};
|
||||
const auto* entity = &game::g_entities[clientNum];
|
||||
|
||||
if (entity->client == nullptr)
|
||||
{
|
||||
return; // Client is not fully in game yet
|
||||
}
|
||||
|
||||
game::SV_Cmd_ArgvBuffer(0, cmd, 1024);
|
||||
|
||||
auto hidden = false;
|
||||
if (cmd == "say"s || cmd == "say_team"s)
|
||||
{
|
||||
std::string message = game::ConcatArgs(1);
|
||||
message.erase(0, 1);
|
||||
|
||||
for (const auto& callback : say_callbacks)
|
||||
{
|
||||
const auto entity_id = game::Scr_GetEntityId(clientNum, 0);
|
||||
const auto result = callback(entity_id, {message, cmd == "say_team"s});
|
||||
|
||||
if (result.is<int>() && !hidden)
|
||||
{
|
||||
hidden = result.as<int>() == 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!hidden)
|
||||
{
|
||||
client_command_hook.invoke<void>(clientNum);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
{
|
||||
client_command_hook.create(0x502CB0, client_command_stub);
|
||||
|
||||
scripting::on_shutdown([]()
|
||||
{
|
||||
say_callbacks.clear();
|
||||
});
|
||||
|
||||
gsc::function::add("onplayersay", [](const gsc::function_args& args) -> scripting::script_value
|
||||
{
|
||||
const auto function = args[0].as<scripting::function>();
|
||||
say_callbacks.push_back(function);
|
||||
return {};
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//REGISTER_COMPONENT(notifies::component)
|
||||
@@ -128,7 +128,7 @@ namespace scheduler
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
void on_startup([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
thread = std::thread([]()
|
||||
{
|
||||
@@ -144,4 +144,4 @@ namespace scheduler
|
||||
};
|
||||
}
|
||||
|
||||
//REGISTER_COMPONENT(scheduler::component)
|
||||
REGISTER_COMPONENT(scheduler::component)
|
||||
|
||||
@@ -13,68 +13,12 @@
|
||||
|
||||
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;
|
||||
|
||||
namespace
|
||||
{
|
||||
utils::hook::detour vm_notify_hook;
|
||||
utils::hook::detour scr_add_class_field_hook;
|
||||
|
||||
utils::hook::detour scr_load_level_hook;
|
||||
utils::hook::detour g_shutdown_game_hook;
|
||||
|
||||
utils::hook::detour scr_set_thread_position_hook;
|
||||
utils::hook::detour process_script_hook;
|
||||
|
||||
std::string current_file;
|
||||
|
||||
std::vector<std::function<void()>> shutdown_callbacks;
|
||||
|
||||
void vm_notify_stub(const unsigned int notify_list_owner_id, const unsigned int string_value,
|
||||
game::VariableValue* top)
|
||||
{
|
||||
const auto* name = game::SL_ConvertToString(string_value);
|
||||
|
||||
if (name)
|
||||
{
|
||||
event e;
|
||||
e.name = name;
|
||||
e.entity = notify_list_owner_id;
|
||||
|
||||
for (auto* value = top; value->type != game::SCRIPT_END; --value)
|
||||
{
|
||||
e.arguments.emplace_back(*value);
|
||||
}
|
||||
|
||||
if (e.name == "connected")
|
||||
{
|
||||
const auto player = e.arguments[0].as<scripting::entity>();
|
||||
const auto client = player.call("getentitynumber").as<int>();
|
||||
userinfo::clear_client_overrides(client);
|
||||
}
|
||||
}
|
||||
|
||||
vm_notify_hook.invoke<void>(notify_list_owner_id, string_value, top);
|
||||
}
|
||||
|
||||
void scr_add_class_field_stub(unsigned int classnum, unsigned int _name, unsigned int canonicalString, unsigned int offset)
|
||||
{
|
||||
const auto name = game::SL_ConvertToString(_name);
|
||||
|
||||
if (fields_table[classnum].find(name) == fields_table[classnum].end())
|
||||
{
|
||||
fields_table[classnum][name] = offset;
|
||||
}
|
||||
|
||||
scr_add_class_field_hook.invoke<void>(classnum, _name, canonicalString, offset);
|
||||
}
|
||||
|
||||
void scr_load_level_stub()
|
||||
{
|
||||
scr_load_level_hook.invoke<void>();
|
||||
}
|
||||
|
||||
void g_shutdown_game_stub(const int free_scripts)
|
||||
{
|
||||
userinfo::clear_overrides();
|
||||
@@ -87,31 +31,6 @@ namespace scripting
|
||||
|
||||
g_shutdown_game_hook.invoke<void>(free_scripts);
|
||||
}
|
||||
|
||||
void process_script_stub(const char* filename)
|
||||
{
|
||||
current_file = filename;
|
||||
|
||||
const auto file_id = atoi(filename);
|
||||
if (file_id)
|
||||
{
|
||||
current_file = scripting::find_file(file_id);
|
||||
}
|
||||
|
||||
process_script_hook.invoke<void>(filename);
|
||||
}
|
||||
|
||||
void scr_set_thread_position_stub(unsigned int threadName, const char* codePos)
|
||||
{
|
||||
const auto function_name = scripting::find_token(threadName);
|
||||
|
||||
if (!function_name.empty())
|
||||
{
|
||||
script_function_table[current_file][function_name] = codePos;
|
||||
}
|
||||
|
||||
scr_set_thread_position_hook.invoke<void>(threadName, codePos);
|
||||
}
|
||||
}
|
||||
|
||||
void on_shutdown(const std::function<void()>& callback)
|
||||
@@ -122,18 +41,11 @@ namespace scripting
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
void on_startup([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
scr_load_level_hook.create(0x527AF0, scr_load_level_stub);
|
||||
g_shutdown_game_hook.create(0x50C100, g_shutdown_game_stub);
|
||||
|
||||
scr_add_class_field_hook.create(0x567CD0, scr_add_class_field_stub);
|
||||
vm_notify_hook.create(0x569720, vm_notify_stub);
|
||||
|
||||
scr_set_thread_position_hook.create(0x5616D0, scr_set_thread_position_stub);
|
||||
process_script_hook.create(0x56B130, process_script_stub);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//REGISTER_COMPONENT(scripting::component)
|
||||
REGISTER_COMPONENT(scripting::component)
|
||||
|
||||
@@ -2,8 +2,5 @@
|
||||
|
||||
namespace scripting
|
||||
{
|
||||
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;
|
||||
|
||||
void on_shutdown(const std::function<void()>& callback);
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
#include <stdinc.hpp>
|
||||
#include "signatures.hpp"
|
||||
#include <utils/hook.hpp>
|
||||
|
||||
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');
|
||||
const auto base = reinterpret_cast<size_t>(GetModuleHandle("plutonium-bootstrapper-win32.exe"));
|
||||
utils::hook::signature signature(base, get_image_size() - base);
|
||||
OutputDebugString(utils::string::va("%p %p\n", base, get_image_size()));
|
||||
auto found = false;
|
||||
signature.add({
|
||||
string,
|
||||
mask,
|
||||
[&](char* address)
|
||||
{
|
||||
if (found)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
found = true;
|
||||
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);
|
||||
if (!string_ptr)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::memcpy(bytes, &string_ptr, sizeof(bytes));
|
||||
return find_string_ptr({bytes, 4});
|
||||
}
|
||||
|
||||
bool process_gsc_ctx()
|
||||
{
|
||||
OutputDebugString("HELLOOO");
|
||||
|
||||
const auto string_ref = find_string_ref("in call to builtin %s \"%s\"");
|
||||
if (!string_ref)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto gsc_ctx_ptr = *reinterpret_cast<size_t*>(string_ref + 215);
|
||||
OutputDebugString(utils::string::va("gsc_ctx_ptr: %p\n", gsc_ctx_ptr));
|
||||
game::plutonium::gsc_ctx.set(gsc_ctx_ptr);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool process()
|
||||
{
|
||||
load_function_tables();
|
||||
return process_gsc_ctx();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
namespace signatures
|
||||
{
|
||||
bool process();
|
||||
}
|
||||
@@ -10,13 +10,8 @@ namespace string
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
void on_startup([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
gsc::function::add("toupper", [](const gsc::function_args& args)
|
||||
{
|
||||
return utils::string::to_upper(args[0].as<std::string>());
|
||||
});
|
||||
|
||||
gsc::function::add("getchar", [](const gsc::function_args& args)
|
||||
{
|
||||
auto index = 0;
|
||||
@@ -28,7 +23,7 @@ namespace string
|
||||
const auto string = args[0].as<std::string>();
|
||||
if (index >= static_cast<int>(string.size()))
|
||||
{
|
||||
throw std::runtime_error("Char index out of bounds");
|
||||
throw std::runtime_error("char index out of bounds");
|
||||
}
|
||||
|
||||
return static_cast<int>(string[index]);
|
||||
@@ -43,4 +38,4 @@ namespace string
|
||||
};
|
||||
}
|
||||
|
||||
//REGISTER_COMPONENT(string::component)
|
||||
REGISTER_COMPONENT(string::component)
|
||||
|
||||
+19
-16
@@ -71,11 +71,11 @@ namespace userinfo
|
||||
const auto userinfo = map_to_userinfo(map);
|
||||
strcpy_s(buffer, 1024, userinfo.data());
|
||||
}
|
||||
}
|
||||
|
||||
void clear_client_overrides(int client)
|
||||
{
|
||||
userinfo_overrides[client].clear();
|
||||
void clear_client_overrides(unsigned int client)
|
||||
{
|
||||
userinfo_overrides[client].clear();
|
||||
}
|
||||
}
|
||||
|
||||
void clear_overrides()
|
||||
@@ -86,20 +86,23 @@ namespace userinfo
|
||||
class component final : public component_interface
|
||||
{
|
||||
public:
|
||||
void post_unpack() override
|
||||
void on_startup([[maybe_unused]] plugin::plugin* plugin) override
|
||||
{
|
||||
sv_getuserinfo_hook.create(0x573E00, sv_getuserinfo_stub);
|
||||
|
||||
plugin->get_interface()->callbacks()->on_player_connect(clear_client_overrides);
|
||||
plugin->get_interface()->callbacks()->on_player_disconnect(clear_client_overrides);
|
||||
|
||||
gsc::method::add("setname", [](const game::scr_entref_t ent, const gsc::function_args& args) -> scripting::script_value
|
||||
{
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
throw std::runtime_error("invalid entity");
|
||||
}
|
||||
|
||||
if (game::g_entities[ent.entnum].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Not a player entity");
|
||||
throw std::runtime_error("not a player entity");
|
||||
}
|
||||
|
||||
const auto name = args[0].as<std::string>();
|
||||
@@ -114,12 +117,12 @@ namespace userinfo
|
||||
{
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
throw std::runtime_error("invalid entity");
|
||||
}
|
||||
|
||||
if (game::g_entities[ent.entnum].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Not a player entity");
|
||||
throw std::runtime_error("not a player entity");
|
||||
}
|
||||
|
||||
userinfo_overrides[ent.entnum].erase("name");
|
||||
@@ -132,12 +135,12 @@ namespace userinfo
|
||||
{
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
throw std::runtime_error("invalid entity");
|
||||
}
|
||||
|
||||
if (game::g_entities[ent.entnum].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Not a player entity");
|
||||
throw std::runtime_error("not a player entity");
|
||||
}
|
||||
|
||||
const auto name = args[0].as<std::string>();
|
||||
@@ -154,12 +157,12 @@ namespace userinfo
|
||||
{
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
throw std::runtime_error("invalid entity");
|
||||
}
|
||||
|
||||
if (game::g_entities[ent.entnum].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Not a player entity");
|
||||
throw std::runtime_error("not a player entity");
|
||||
}
|
||||
|
||||
userinfo_overrides[ent.entnum].erase("clantag");
|
||||
@@ -174,12 +177,12 @@ namespace userinfo
|
||||
{
|
||||
if (ent.classnum != 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid entity");
|
||||
throw std::runtime_error("invalid entity");
|
||||
}
|
||||
|
||||
if (game::g_entities[ent.entnum].client == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Not a player entity");
|
||||
throw std::runtime_error("not a player entity");
|
||||
}
|
||||
|
||||
userinfo_overrides[ent.entnum]["clantag"] = "";
|
||||
@@ -193,4 +196,4 @@ namespace userinfo
|
||||
};
|
||||
}
|
||||
|
||||
//REGISTER_COMPONENT(userinfo::component)
|
||||
REGISTER_COMPONENT(userinfo::component)
|
||||
|
||||
Reference in New Issue
Block a user