Execute script command callbacks on server frame

This commit is contained in:
Federico Cecchetto 2022-06-12 02:28:12 +02:00
parent 90b3a553db
commit 5fb5d2d1fe
4 changed files with 53 additions and 23 deletions

View File

@ -6,6 +6,7 @@
#include "command.hpp" #include "command.hpp"
#include "gsc.hpp" #include "gsc.hpp"
#include "scripting.hpp" #include "scripting.hpp"
#include "scheduler.hpp"
#include <utils/string.hpp> #include <utils/string.hpp>
#include <utils/memory.hpp> #include <utils/memory.hpp>
@ -91,6 +92,16 @@ namespace command
return result; return result;
} }
std::vector<std::string> params::get_all() const
{
std::vector<std::string> params_;
for (auto i = 0; i < this->size(); i++)
{
params_.push_back(this->get(i));
}
return params_;
}
params_sv::params_sv() params_sv::params_sv()
: nesting_(game::sv_cmd_args->nesting) : nesting_(game::sv_cmd_args->nesting)
{ {
@ -128,6 +139,16 @@ namespace command
return result; return result;
} }
std::vector<std::string> params_sv::get_all() const
{
std::vector<std::string> params_;
for (auto i = 0; i < this->size(); i++)
{
params_.push_back(this->get(i));
}
return params_;
}
void add_raw(const char* name, void (*callback)()) void add_raw(const char* name, void (*callback)())
{ {
game::Cmd_AddCommandInternal(name, callback, utils::memory::get_allocator()->allocate<game::cmd_function_t>()); game::Cmd_AddCommandInternal(name, callback, utils::memory::get_allocator()->allocate<game::cmd_function_t>());
@ -216,14 +237,18 @@ namespace command
{ {
command::add_script_command(name, [function](const command::params& params) command::add_script_command(name, [function](const command::params& params)
{ {
scripting::array array; const auto params_ = params.get_all();
scheduler::once([=]()
for (auto i = 0; i < params.size(); i++)
{ {
array.push(params[i]); scripting::array array;
}
function({array}); for (auto i = 0; i < params.size(); i++)
{
array.push(params[i]);
}
function({array});
});
}); });
}, "addcommand", "command::add"); }, "addcommand", "command::add");
@ -231,16 +256,20 @@ namespace command
{ {
command::add_script_sv_command(name, [function](const int client_num, const command::params_sv& params) command::add_script_sv_command(name, [function](const int client_num, const command::params_sv& params)
{ {
const scripting::entity player = game::Scr_GetEntityId(game::SCRIPTINSTANCE_SERVER, client_num, 0, 0); const auto params_ = params.get_all();
scheduler::once([=]()
scripting::array array;
for (auto i = 0; i < params.size(); i++)
{ {
array.push(params[i]); const scripting::entity player = game::Scr_GetEntityId(game::SCRIPTINSTANCE_SERVER, client_num, 0, 0);
}
function(player, {array}); scripting::array array;
for (auto i = 0; i < params.size(); i++)
{
array.push(params[i]);
}
function(player, {array});
}, scheduler::pipeline::server);
}); });
}, "addclientcommand", "command::add_sv"); }, "addclientcommand", "command::add_sv");

View File

@ -10,6 +10,7 @@ namespace command
int size() const; int size() const;
const char* get(int index) const; const char* get(int index) const;
std::string join(int index) const; std::string join(int index) const;
std::vector<std::string> get_all() const;
const char* operator[](const int index) const const char* operator[](const int index) const
{ {
@ -28,6 +29,7 @@ namespace command
int size() const; int size() const;
const char* get(int index) const; const char* get(int index) const;
std::string join(int index) const; std::string join(int index) const;
std::vector<std::string> get_all() const;
const char* operator[](const int index) const const char* operator[](const int index) const
{ {

View File

@ -11,6 +11,8 @@ namespace scheduler
{ {
namespace namespace
{ {
utils::hook::detour server_frame_hook;
struct task struct task
{ {
std::function<bool()> handler{}; std::function<bool()> handler{};
@ -94,13 +96,10 @@ namespace scheduler
execute(pipeline::server); execute(pipeline::server);
} }
void server_frame_stub(utils::hook::assembler& a) void server_frame_stub()
{ {
a.pushad(); execute(pipeline::server);
a.call(execute_server); server_frame_hook.invoke<void>();
a.popad();
a.jmp(SELECT_VALUE(0x0, 0x0));
} }
} }
@ -151,9 +150,9 @@ namespace scheduler
} }
}); });
server_frame_hook.create(SELECT_VALUE(0x43E340, 0x46B680), server_frame_stub);
} }
}; };
} }
//REGISTER_COMPONENT(scheduler::component) REGISTER_COMPONENT(scheduler::component)

View File

@ -101,7 +101,7 @@ namespace game
WEAK symbol<void(int clientNum, const char* reason)> SV_GameDropClient{0x0, 0x0}; WEAK symbol<void(int clientNum, const char* reason)> SV_GameDropClient{0x0, 0x0};
WEAK symbol<bool(int clientNum)> SV_IsTestClient{0x0, 0x0}; WEAK symbol<bool(int clientNum)> SV_IsTestClient{0x0, 0x0};
WEAK symbol<void(int clientNum, int type, const char* command)> SV_GameSendServerCommand{0x0, 0x0}; WEAK symbol<void(int clientNum, int type, const char* command)> SV_GameSendServerCommand{0x543CF0, 0x6B8730};
WEAK symbol<void*(int valueIndex)> Sys_GetValue{0x67D4F0, 0x529EB0}; WEAK symbol<void*(int valueIndex)> Sys_GetValue{0x67D4F0, 0x529EB0};
WEAK symbol<int()> Sys_Milliseconds{0x0, 0x0}; WEAK symbol<int()> Sys_Milliseconds{0x0, 0x0};