From 0ac522511427a0291bedc8b5466e2d8048a14cbb Mon Sep 17 00:00:00 2001 From: Federico Cecchetto Date: Mon, 13 Dec 2021 03:05:22 +0100 Subject: [PATCH] #5 --- src/component/gsc.hpp | 2 ++ src/component/io.cpp | 6 ------ src/component/json.cpp | 6 ------ src/component/notifies.cpp | 38 +++++++++++++++++++++++++++++-------- src/component/scripting.cpp | 14 ++++++++++++++ src/component/scripting.hpp | 2 ++ src/component/userinfo.cpp | 6 ------ 7 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/component/gsc.hpp b/src/component/gsc.hpp index 7846caf..712e402 100644 --- a/src/component/gsc.hpp +++ b/src/component/gsc.hpp @@ -1,4 +1,6 @@ #pragma once +#include "game/scripting/array.hpp" +#include "game/scripting/execution.hpp" namespace gsc { diff --git a/src/component/io.cpp b/src/component/io.cpp index 98e2c46..de02ceb 100644 --- a/src/component/io.cpp +++ b/src/component/io.cpp @@ -2,12 +2,6 @@ #include "loader/component_loader.hpp" #include "scheduler.hpp" - -#include "game/scripting/event.hpp" -#include "game/scripting/execution.hpp" -#include "game/scripting/functions.hpp" -#include "game/scripting/array.hpp" - #include "gsc.hpp" #include "json.hpp" diff --git a/src/component/json.cpp b/src/component/json.cpp index e6fba65..d7e85ba 100644 --- a/src/component/json.cpp +++ b/src/component/json.cpp @@ -2,12 +2,6 @@ #include "loader/component_loader.hpp" #include "scheduler.hpp" - -#include "game/scripting/event.hpp" -#include "game/scripting/execution.hpp" -#include "game/scripting/functions.hpp" -#include "game/scripting/array.hpp" - #include "gsc.hpp" #include "json.hpp" diff --git a/src/component/notifies.cpp b/src/component/notifies.cpp index 75686df..2da35d3 100644 --- a/src/component/notifies.cpp +++ b/src/component/notifies.cpp @@ -1,14 +1,15 @@ #include #include "loader/component_loader.hpp" -#include "scheduler.hpp" -#include "game/scripting/entity.hpp" -#include "game/scripting/execution.hpp" +#include "scheduler.hpp" +#include "gsc.hpp" +#include "scripting.hpp" namespace notifies { namespace { + std::vector say_callbacks; utils::hook::detour client_command_hook; void client_command_stub(int clientNum) @@ -17,19 +18,28 @@ namespace notifies 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); - const scripting::entity level{*game::levelEntityId}; - const auto player = scripting::call("getEntByNum", {clientNum}).as(); + 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}); - scripting::notify(level, cmd, {player, message}); - scripting::notify(player, cmd, {message}); + if (result.is()) + { + hidden = result.as() == 0; + } + } } - return client_command_hook.invoke(clientNum); + if (!hidden) + { + client_command_hook.invoke(clientNum); + } } } @@ -39,6 +49,18 @@ namespace notifies 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(); + say_callbacks.push_back(function); + return {}; + }); } }; } diff --git a/src/component/scripting.cpp b/src/component/scripting.cpp index a9e0ecf..5dca0dd 100644 --- a/src/component/scripting.cpp +++ b/src/component/scripting.cpp @@ -29,6 +29,8 @@ namespace scripting std::string current_file; + std::vector> shutdown_callbacks; + void vm_notify_stub(const unsigned int notify_list_owner_id, const unsigned int string_value, game::VariableValue* top) { @@ -77,6 +79,13 @@ namespace scripting { userinfo::clear_overrides(); command::clear_script_commands(); + + for (const auto& callback : shutdown_callbacks) + { + callback(); + } + + shutdown_callbacks = {}; g_shutdown_game_hook.invoke(free_scripts); } @@ -106,6 +115,11 @@ namespace scripting } } + void on_shutdown(const std::function& callback) + { + shutdown_callbacks.push_back(callback); + } + class component final : public component_interface { public: diff --git a/src/component/scripting.hpp b/src/component/scripting.hpp index 1f16a66..446d8ef 100644 --- a/src/component/scripting.hpp +++ b/src/component/scripting.hpp @@ -4,4 +4,6 @@ namespace scripting { extern std::unordered_map> fields_table; extern std::unordered_map> script_function_table; + + void on_shutdown(const std::function& callback); } \ No newline at end of file diff --git a/src/component/userinfo.cpp b/src/component/userinfo.cpp index 82436ae..7c98a5d 100644 --- a/src/component/userinfo.cpp +++ b/src/component/userinfo.cpp @@ -2,12 +2,6 @@ #include "loader/component_loader.hpp" #include "scheduler.hpp" - -#include "game/scripting/event.hpp" -#include "game/scripting/execution.hpp" -#include "game/scripting/functions.hpp" -#include "game/scripting/array.hpp" - #include "gsc.hpp" namespace userinfo