diff --git a/src/component/gsc.cpp b/src/component/gsc.cpp index ed973b6..e51ba4f 100644 --- a/src/component/gsc.cpp +++ b/src/component/gsc.cpp @@ -6,7 +6,6 @@ #include "game/scripting/event.hpp" #include "game/scripting/execution.hpp" -#include "game/scripting/functions.hpp" #include "game/scripting/array.hpp" #include "game/scripting/function.hpp" @@ -16,19 +15,6 @@ namespace gsc { namespace { - function_args get_arguments() - { - std::vector args; - - for (auto i = 0; i < game::scr_VmPub->outparamcount; i++) - { - const auto value = game::scr_VmPub->top[-i]; - args.push_back(value); - } - - return args; - } - void return_value(const scripting::script_value& value) { if (game::scr_VmPub->outparamcount) @@ -108,8 +94,8 @@ namespace gsc return scr_set_object_field_hook.invoke(classnum, entnum, offset); } - const auto args = get_arguments(); - + function_args args; + try { field->setter(entnum, args[0]); @@ -141,7 +127,8 @@ namespace gsc { try { - const auto result = function.operator()(get_arguments()); + function_args args; + const auto result = function.operator()(args); const auto type = result.get_raw().type; if (type) @@ -159,7 +146,8 @@ namespace gsc { try { - const auto result = method.operator()(ent, get_arguments()); + function_args args; + const auto result = method.operator()(ent, args); const auto type = result.get_raw().type; if (type) @@ -188,22 +176,31 @@ namespace gsc } } - function_args::function_args(std::vector values) + function_args::function_args() + { + for (auto i = 0u; i < game::scr_VmPub->outparamcount; i++) + { + const auto value = game::scr_VmPub->top[-i]; + this->values_.emplace_back(value); + } + } + + function_args::function_args(const std::vector& values) : values_(values) { } - unsigned int function_args::size() const + std::uint32_t function_args::size() const { return this->values_.size(); } - std::vector function_args::get_raw() const + const std::vector& function_args::get_raw() const { return this->values_; } - scripting::value_wrap function_args::get(const int index) const + scripting::value_wrap function_args::get(const std::uint32_t index) const { if (index >= this->values_.size()) { diff --git a/src/component/gsc.hpp b/src/component/gsc.hpp index 64347f8..2a1c0be 100644 --- a/src/component/gsc.hpp +++ b/src/component/gsc.hpp @@ -26,13 +26,14 @@ namespace gsc class function_args { public: - function_args(std::vector); + function_args(); + function_args(const std::vector&); - unsigned int size() const; - std::vector get_raw() const; - scripting::value_wrap get(const int index) const; + std::uint32_t size() const; + const std::vector& get_raw() const; + scripting::value_wrap get(const std::uint32_t index) const; - scripting::value_wrap operator[](const int index) const + scripting::value_wrap operator[](const std::uint32_t index) const { return this->get(index); } diff --git a/src/component/io.cpp b/src/component/io.cpp index d7e5fb0..a2dd539 100644 --- a/src/component/io.cpp +++ b/src/component/io.cpp @@ -102,7 +102,8 @@ namespace io }); }); - gsc::function::add("jsonprint", [](const gsc::function_args& args) -> scripting::script_value + gsc::function::add("jsonprint", [](const gsc::function_args& args) + -> scripting::script_value { std::string buffer; @@ -189,7 +190,8 @@ namespace io return scripting::script_value{}; }); - gsc::function::add("httpget", [](const gsc::function_args& args) -> scripting::script_value + gsc::function::add("httpget", [](const gsc::function_args& args) + -> scripting::script_value { const auto url = args[0].as(); const auto object = scripting::entity(scripting::make_object()); diff --git a/src/component/json.cpp b/src/component/json.cpp index eac4ddd..e42dd1b 100644 --- a/src/component/json.cpp +++ b/src/component/json.cpp @@ -20,7 +20,8 @@ namespace json auto string_indexed = -1; const auto keys = array.get_keys(); - for (auto i = 0; i < keys.size(); i++) + + for (auto i = 0u; i < keys.size(); i++) { const auto is_int = keys[i].is(); const auto is_string = keys[i].is(); diff --git a/src/component/scripting.cpp b/src/component/scripting.cpp index 3e7d195..47c728a 100644 --- a/src/component/scripting.cpp +++ b/src/component/scripting.cpp @@ -7,7 +7,6 @@ #include "game/scripting/event.hpp" #include "game/scripting/execution.hpp" -#include "game/scripting/functions.hpp" #include "gsc.hpp" diff --git a/src/component/scripting.hpp b/src/component/scripting.hpp index 003b623..753f5c5 100644 --- a/src/component/scripting.hpp +++ b/src/component/scripting.hpp @@ -3,4 +3,4 @@ namespace scripting { 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 a209c9c..402970f 100644 --- a/src/component/userinfo.cpp +++ b/src/component/userinfo.cpp @@ -7,23 +7,23 @@ namespace userinfo { using userinfo_map = std::unordered_map; - std::unordered_map userinfo_overrides; + std::array userinfo_overrides; namespace { utils::hook::detour sv_getuserinfo_hook; - userinfo_map userinfo_to_map(std::string userinfo) + userinfo_map userinfo_to_map(const char* userinfo) { userinfo_map map{}; if (userinfo[0] == '\\') { - userinfo = userinfo.substr(1); + ++userinfo; } const auto args = utils::string::split(userinfo, '\\'); - for (size_t i = 0; !args.empty() && i < (args.size() - 1); i += 2) + for (auto i = 0u; !args.empty() && i < (args.size() - 1); i += 2) { map[args[i]] = args[i + 1]; } @@ -49,12 +49,8 @@ namespace userinfo void sv_getuserinfo_stub(int index, char* buffer, int bufferSize) { sv_getuserinfo_hook.invoke(index, buffer, bufferSize); - auto map = userinfo_to_map(buffer); - if (userinfo_overrides.find(index) == userinfo_overrides.end()) - { - userinfo_overrides[index] = {}; - } + auto map = userinfo_to_map(buffer); for (const auto& values : userinfo_overrides[index]) { @@ -80,7 +76,10 @@ namespace userinfo void clear_overrides() { - userinfo_overrides.clear(); + for (auto& entry : userinfo_overrides) + { + entry.clear(); + } } class component final : public component_interface @@ -93,7 +92,8 @@ namespace userinfo 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 + gsc::method::add("setname", [](const game::scr_entref_t ent, const gsc::function_args& args) + -> scripting::script_value { if (ent.classnum != 0) { @@ -113,7 +113,8 @@ namespace userinfo return {}; }); - gsc::method::add("resetname", [](const game::scr_entref_t ent, const gsc::function_args&) -> scripting::script_value + gsc::method::add("resetname", [](const game::scr_entref_t ent, const gsc::function_args&) + -> scripting::script_value { if (ent.classnum != 0) { @@ -131,7 +132,8 @@ namespace userinfo return {}; }); - gsc::method::add("setclantag", [](const game::scr_entref_t ent, const gsc::function_args& args) -> scripting::script_value + gsc::method::add("setclantag", [](const game::scr_entref_t ent, const gsc::function_args& args) + -> scripting::script_value { if (ent.classnum != 0) { @@ -153,7 +155,8 @@ namespace userinfo return {}; }); - gsc::method::add("resetclantag", [](const game::scr_entref_t ent, const gsc::function_args&) -> scripting::script_value + gsc::method::add("resetclantag", [](const game::scr_entref_t ent, const gsc::function_args&) + -> scripting::script_value { if (ent.classnum != 0) { @@ -173,7 +176,8 @@ namespace userinfo return {}; }); - gsc::method::add("removeclantag", [](const game::scr_entref_t ent, const gsc::function_args&) -> scripting::script_value + gsc::method::add("removeclantag", [](const game::scr_entref_t ent, const gsc::function_args&) + -> scripting::script_value { if (ent.classnum != 0) { diff --git a/src/component/userinfo.hpp b/src/component/userinfo.hpp index 3c483e8..30fc5e8 100644 --- a/src/component/userinfo.hpp +++ b/src/component/userinfo.hpp @@ -2,6 +2,5 @@ namespace userinfo { - void clear_client_overrides(int client); void clear_overrides(); -} \ No newline at end of file +} diff --git a/src/game/scripting/array.cpp b/src/game/scripting/array.cpp index f6993e1..ed7be06 100644 --- a/src/game/scripting/array.cpp +++ b/src/game/scripting/array.cpp @@ -15,30 +15,30 @@ namespace scripting } const auto value = game::scr_VarGlob->childVariableValue[this->id_ + 0xC800 * (this->parent_id_ & 1)]; - game::VariableValue variable; + game::VariableValue variable{}; variable.u = value.u.u; variable.type = (game::scriptType_e)value.type; this->value_ = variable; } - void array_value::operator=(const script_value& _value) + void array_value::operator=(const script_value& value) { if (!this->id_) { return; } - const auto value = _value.get_raw(); + const auto& value_raw = value.get_raw(); const auto variable = &game::scr_VarGlob->childVariableValue[this->id_ + 0xC800 * (this->parent_id_ & 1)]; - game::AddRefToValue(value.type, value.u); + game::AddRefToValue(value_raw.type, value_raw.u); game::RemoveRefToValue(variable->type, variable->u.u); - variable->type = value.type; - variable->u.u = value.u; + variable->type = value_raw.type; + variable->u.u = value_raw.u; - this->value_ = value; + this->value_ = value_raw; } array::array(const unsigned int id) @@ -170,7 +170,7 @@ namespace scripting return game::Scr_GetSelf(this->id_); } - unsigned int array::push(script_value value) const + unsigned int array::push(const script_value& value) const { this->set(this->size(), value); return this->size(); @@ -225,7 +225,7 @@ namespace scripting } const auto value = game::scr_VarGlob->childVariableValue[variable_id + 0xC800 * (this->id_ & 1)]; - game::VariableValue variable; + game::VariableValue variable{}; variable.u = value.u.u; variable.type = (game::scriptType_e)value.type; @@ -242,7 +242,7 @@ namespace scripting } const auto value = game::scr_VarGlob->childVariableValue[variable_id + 0xC800 * (this->id_ & 1)]; - game::VariableValue variable; + game::VariableValue variable{}; variable.u = value.u.u; variable.type = (game::scriptType_e)value.type; @@ -261,9 +261,9 @@ namespace scripting } } - void array::set(const std::string& key, const script_value& value_) const + void array::set(const std::string& key, const script_value& value) const { - const auto value = value_.get_raw(); + const auto& value_raw = value.get_raw(); const auto variable_id = this->get_value_id(key); if (!variable_id) @@ -273,16 +273,16 @@ namespace scripting const auto variable = &game::scr_VarGlob->childVariableValue[variable_id + 0xC800 * (this->id_ & 1)]; - game::AddRefToValue(value.type, value.u); + game::AddRefToValue(value_raw.type, value_raw.u); game::RemoveRefToValue(variable->type, variable->u.u); - variable->type = value.type; - variable->u.u = value.u; + variable->type = value_raw.type; + variable->u.u = value_raw.u; } - void array::set(const unsigned int index, const script_value& value_) const + void array::set(const unsigned int index, const script_value& value) const { - const auto value = value_.get_raw(); + const auto& value_raw = value.get_raw(); const auto variable_id = this->get_value_id(index); if (!variable_id) @@ -292,11 +292,11 @@ namespace scripting const auto variable = &game::scr_VarGlob->childVariableValue[variable_id + 0xC800 * (this->id_ & 1)]; - game::AddRefToValue(value.type, value.u); + game::AddRefToValue(value_raw.type, value_raw.u); game::RemoveRefToValue(variable->type, variable->u.u); - variable->type = value.type; - variable->u.u = value.u; + variable->type = value_raw.type; + variable->u.u = value_raw.u; } unsigned int array::get_entity_id() const @@ -320,6 +320,7 @@ namespace scripting unsigned int array::get_value_id(const unsigned int index) const { const auto variable_id = game::FindVariable(this->id_, (index - 0x800000) & 0xFFFFFF); + if (!variable_id) { return game::GetNewArrayVariable(this->id_, index); diff --git a/src/game/scripting/array.hpp b/src/game/scripting/array.hpp index adc88d5..482ff0d 100644 --- a/src/game/scripting/array.hpp +++ b/src/game/scripting/array.hpp @@ -34,7 +34,7 @@ namespace scripting std::vector get_keys() const; unsigned int size() const; - unsigned int push(script_value) const; + unsigned int push(const script_value&) const; void erase(const unsigned int) const; void erase(const std::string&) const; script_value pop() const; @@ -69,12 +69,12 @@ namespace scripting { if (key.is()) { - return { this->id_, this->get_value_id(key.as()) }; + return {this->id_, this->get_value_id(key.as())}; } if (key.is()) { - return { this->id_, this->get_value_id(key.as()) }; + return {this->id_, this->get_value_id(key.as())}; } } diff --git a/src/game/scripting/function.cpp b/src/game/scripting/function.cpp index b1f85bc..beb7cec 100644 --- a/src/game/scripting/function.cpp +++ b/src/game/scripting/function.cpp @@ -11,10 +11,9 @@ namespace scripting script_value function::get_raw() const { - game::VariableValue value; + game::VariableValue value{}; value.type = game::SCRIPT_FUNCTION; value.u.codePosValue = this->pos_; - return value; } @@ -23,7 +22,7 @@ namespace scripting return this->pos_; } - script_value function::call(const entity& self, std::vector arguments) const + script_value function::call(const entity& self, const std::vector& arguments) const { return exec_ent_thread(self, this->pos_, arguments); } diff --git a/src/game/scripting/function.hpp b/src/game/scripting/function.hpp index 8d2639b..96416e5 100644 --- a/src/game/scripting/function.hpp +++ b/src/game/scripting/function.hpp @@ -12,14 +12,14 @@ namespace scripting script_value get_raw() const; const char* get_pos() const; - script_value call(const entity& self, std::vector arguments) const; + script_value call(const entity& self, const std::vector& arguments) const; - script_value operator()(const entity& self, std::vector arguments) const + script_value operator()(const entity& self, const std::vector& arguments) const { return this->call(self, arguments); } - script_value operator()(std::vector arguments) const + script_value operator()(const std::vector& arguments) const { return this->call(*game::levelEntityId, arguments); } @@ -28,7 +28,9 @@ namespace scripting { return this->call(*game::levelEntityId, {}); } + private: const char* pos_; + }; } diff --git a/src/game/scripting/functions.cpp b/src/game/scripting/functions.cpp deleted file mode 100644 index 3d88a8e..0000000 --- a/src/game/scripting/functions.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include -#include "functions.hpp" - -#include - -namespace scripting -{ - -} diff --git a/src/game/scripting/functions.hpp b/src/game/scripting/functions.hpp deleted file mode 100644 index 8fbb0fd..0000000 --- a/src/game/scripting/functions.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once -#include "game/game.hpp" - -namespace scripting -{ - using script_function = void(*)(game::scr_entref_t); -} diff --git a/src/game/scripting/script_value.cpp b/src/game/scripting/script_value.cpp index 1a0558f..5ca275f 100644 --- a/src/game/scripting/script_value.cpp +++ b/src/game/scripting/script_value.cpp @@ -321,7 +321,7 @@ namespace scripting return this->value_.get(); } - value_wrap::value_wrap(const scripting::script_value& value, int argument_index) + value_wrap::value_wrap(const scripting::script_value& value, const std::uint32_t argument_index) : value_(value) , argument_index_(argument_index) { diff --git a/src/game/scripting/script_value.hpp b/src/game/scripting/script_value.hpp index 351c26f..75f3d53 100644 --- a/src/game/scripting/script_value.hpp +++ b/src/game/scripting/script_value.hpp @@ -164,7 +164,7 @@ namespace scripting class value_wrap { public: - value_wrap(const scripting::script_value& value, int argument_index); + value_wrap(const scripting::script_value& value, const std::uint32_t); template T as() const @@ -179,7 +179,7 @@ namespace scripting } } - template + template T* as_ptr() { try @@ -203,7 +203,7 @@ namespace scripting return this->value_.get_raw(); } - int argument_index_{}; + std::uint32_t argument_index_{}; scripting::script_value value_; }; } diff --git a/src/game/scripting/vector.hpp b/src/game/scripting/vector.hpp index 0bb1595..fd1f685 100644 --- a/src/game/scripting/vector.hpp +++ b/src/game/scripting/vector.hpp @@ -26,6 +26,6 @@ namespace scripting void set_z(float value); private: - game::vec3_t value_{ 0 }; + game::vec3_t value_{}; }; } \ No newline at end of file