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 "gsc.hpp"
#include "scripting.hpp"
#include "scheduler.hpp"
#include <utils/string.hpp>
#include <utils/memory.hpp>
@ -91,6 +92,16 @@ namespace command
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()
: nesting_(game::sv_cmd_args->nesting)
{
@ -128,6 +139,16 @@ namespace command
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)())
{
game::Cmd_AddCommandInternal(name, callback, utils::memory::get_allocator()->allocate<game::cmd_function_t>());
@ -215,6 +236,9 @@ namespace command
gsc::function::add_multiple([](const std::string& name, const scripting::function& function)
{
command::add_script_command(name, [function](const command::params& params)
{
const auto params_ = params.get_all();
scheduler::once([=]()
{
scripting::array array;
@ -225,11 +249,15 @@ namespace command
function({array});
});
});
}, "addcommand", "command::add");
gsc::function::add_multiple([](const std::string& name, const scripting::function& function)
{
command::add_script_sv_command(name, [function](const int client_num, const command::params_sv& params)
{
const auto params_ = params.get_all();
scheduler::once([=]()
{
const scripting::entity player = game::Scr_GetEntityId(game::SCRIPTINSTANCE_SERVER, client_num, 0, 0);
@ -241,6 +269,7 @@ namespace command
}
function(player, {array});
}, scheduler::pipeline::server);
});
}, "addclientcommand", "command::add_sv");

View File

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

View File

@ -11,6 +11,8 @@ namespace scheduler
{
namespace
{
utils::hook::detour server_frame_hook;
struct task
{
std::function<bool()> handler{};
@ -94,13 +96,10 @@ namespace scheduler
execute(pipeline::server);
}
void server_frame_stub(utils::hook::assembler& a)
void server_frame_stub()
{
a.pushad();
a.call(execute_server);
a.popad();
a.jmp(SELECT_VALUE(0x0, 0x0));
execute(pipeline::server);
server_frame_hook.invoke<void>();
}
}
@ -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<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<int()> Sys_Milliseconds{0x0, 0x0};