From 97371d8f44ab9f07bd49cc352f90f4e9b7e370a5 Mon Sep 17 00:00:00 2001 From: Federico Cecchetto Date: Sat, 19 Jun 2021 22:17:41 +0200 Subject: [PATCH] Use function class --- src/component/gsc.cpp | 28 ++++++++++-------------- src/component/gsc.hpp | 6 ++--- src/component/io.cpp | 28 ++++++++++++------------ src/component/json.cpp | 8 +++---- src/game/scripting/function.cpp | 30 +++++++++++++++++++++++++ src/game/scripting/function.hpp | 34 +++++++++++++++++++++++++++++ src/game/scripting/script_value.cpp | 9 +++++++- 7 files changed, 105 insertions(+), 38 deletions(-) create mode 100644 src/game/scripting/function.cpp create mode 100644 src/game/scripting/function.hpp diff --git a/src/component/gsc.cpp b/src/component/gsc.cpp index 1f5a960..ceeaff8 100644 --- a/src/component/gsc.cpp +++ b/src/component/gsc.cpp @@ -8,12 +8,13 @@ #include "game/scripting/execution.hpp" #include "game/scripting/functions.hpp" #include "game/scripting/array.hpp" +#include "game/scripting/function.hpp" #include "gsc.hpp" namespace gsc { - std::unordered_map replaced_functions; + std::unordered_map replaced_functions; function_args::function_args(std::vector values) : values_(values) @@ -201,9 +202,9 @@ namespace gsc } } - unsigned int replaced_pos = 0; + const char* replaced_pos = 0; - void get_replaced_pos(unsigned int pos) + void get_replaced_pos(const char* pos) { if (replaced_functions.find(pos) != replaced_functions.end()) { @@ -285,28 +286,23 @@ namespace gsc public: void post_unpack() override { - function::add("executecommand", [](function_args args) -> scripting::script_value + function::add("executecommand", [](const function_args& args) -> scripting::script_value { game::Cbuf_AddText(0, args[0].as()); return {}; }); - function::add("replacefunc", [](function_args args) -> scripting::script_value + function::add("replacefunc", [](const function_args& args) -> scripting::script_value { - const auto what = args[0].get_raw(); - const auto with = args[1].get_raw(); + const auto what = args[0].as(); + const auto with = args[1].as(); - if (what.type != game::SCRIPT_FUNCTION || with.type != game::SCRIPT_FUNCTION) - { - throw std::runtime_error("Invalid type"); - } - - replaced_functions[what.u.uintValue] = with.u.uintValue; + replaced_functions[what.get_pos()] = with.get_pos(); return {}; }); - function::add("addcommand", [](function_args args) -> scripting::script_value + function::add("addcommand", [](const function_args& args) -> scripting::script_value { const auto name = args[0].as(); const auto function = args[1].get_raw(); @@ -331,7 +327,7 @@ namespace gsc return {}; }); - function::add("say", [](function_args args) -> scripting::script_value + function::add("say", [](const function_args& args) -> scripting::script_value { const auto message = args[0].as(); game::SV_GameSendServerCommand(-1, 0, utils::string::va("%c \"%s\"", 84, message.data())); @@ -339,7 +335,7 @@ namespace gsc return {}; }); - method::add("tell", [](game::scr_entref_t ent, function_args args) -> scripting::script_value + method::add("tell", [](const game::scr_entref_t ent, const function_args& args) -> scripting::script_value { if (ent.classnum != 0) { diff --git a/src/component/gsc.hpp b/src/component/gsc.hpp index a357eb7..6834b56 100644 --- a/src/component/gsc.hpp +++ b/src/component/gsc.hpp @@ -2,7 +2,7 @@ namespace gsc { - extern std::unordered_map replaced_functions; + extern std::unordered_map replaced_functions; class function_args { @@ -24,8 +24,8 @@ namespace gsc using builtin_function = void(*)(); using builtin_method = void(*)(game::scr_entref_t); - using script_function = std::function; - using script_method = std::function; + using script_function = std::function; + using script_method = std::function; namespace function { diff --git a/src/component/io.cpp b/src/component/io.cpp index 34a98b3..4a01a2d 100644 --- a/src/component/io.cpp +++ b/src/component/io.cpp @@ -20,13 +20,13 @@ namespace io const auto path = game::Dvar_FindVar("fs_basegame")->current.string; std::filesystem::current_path(path); - gsc::function::add("fremove", [](gsc::function_args args) + gsc::function::add("fremove", [](const gsc::function_args& args) { const auto path = args[0].as(); return std::remove(path); }); - gsc::function::add("fopen", [](gsc::function_args args) + gsc::function::add("fopen", [](const gsc::function_args& args) { const auto* path = args[0].as(); const auto* mode = args[1].as(); @@ -41,13 +41,13 @@ namespace io return handle; }); - gsc::function::add("fclose", [](gsc::function_args args) + gsc::function::add("fclose", [](const gsc::function_args& args) { const auto handle = args[0].as_ptr(); return fclose(handle); }); - gsc::function::add("fwrite", [](gsc::function_args args) + gsc::function::add("fwrite", [](const gsc::function_args& args) { const auto handle = args[0].as_ptr(); const auto text = args[1].as(); @@ -55,7 +55,7 @@ namespace io return fprintf(handle, text); }); - gsc::function::add("fread", [](gsc::function_args args) + gsc::function::add("fread", [](const gsc::function_args& args) { const auto handle = args[0].as_ptr(); @@ -74,13 +74,13 @@ namespace io return result; }); - gsc::function::add("fileexists", [](gsc::function_args args) + gsc::function::add("fileexists", [](const gsc::function_args& args) { const auto path = args[0].as(); return utils::io::file_exists(path); }); - gsc::function::add("writefile", [](gsc::function_args args) + gsc::function::add("writefile", [](const gsc::function_args& args) { const auto path = args[0].as(); const auto data = args[1].as(); @@ -94,37 +94,37 @@ namespace io return utils::io::write_file(path, data, append); }); - gsc::function::add("readfile", [](gsc::function_args args) + gsc::function::add("readfile", [](const gsc::function_args& args) { const auto path = args[0].as(); return utils::io::read_file(path); }); - gsc::function::add("filesize", [](gsc::function_args args) + gsc::function::add("filesize", [](const gsc::function_args& args) { const auto path = args[0].as(); return utils::io::file_size(path); }); - gsc::function::add("createdirectory", [](gsc::function_args args) + gsc::function::add("createdirectory", [](const gsc::function_args& args) { const auto path = args[0].as(); return utils::io::create_directory(path); }); - gsc::function::add("directoryexists", [](gsc::function_args args) + gsc::function::add("directoryexists", [](const gsc::function_args& args) { const auto path = args[0].as(); return utils::io::directory_exists(path); }); - gsc::function::add("directoryisempty", [](gsc::function_args args) + gsc::function::add("directoryisempty", [](const gsc::function_args& args) { const auto path = args[0].as(); return utils::io::directory_is_empty(path); }); - gsc::function::add("listfiles", [](gsc::function_args args) + gsc::function::add("listfiles", [](const gsc::function_args& args) { const auto path = args[0].as(); const auto files = utils::io::list_files(path); @@ -138,7 +138,7 @@ namespace io return array.get_raw(); }); - gsc::function::add("copyfolder", [](gsc::function_args args) + gsc::function::add("copyfolder", [](const gsc::function_args& args) { const auto source = args[0].as(); const auto target = args[1].as(); diff --git a/src/component/json.cpp b/src/component/json.cpp index 6344533..ce91499 100644 --- a/src/component/json.cpp +++ b/src/component/json.cpp @@ -141,13 +141,13 @@ namespace json public: void post_unpack() override { - gsc::function::add("array", [](gsc::function_args args) + gsc::function::add("array", [](const gsc::function_args& args) { scripting::array array(args.get_raw()); return array.get_raw(); }); - gsc::function::add("map", [](gsc::function_args args) + gsc::function::add("map", [](const gsc::function_args& args) { scripting::array array; @@ -165,7 +165,7 @@ namespace json return array.get_raw(); }); - gsc::function::add("jsonparse", [](gsc::function_args args) + gsc::function::add("jsonparse", [](const gsc::function_args& args) { const auto json = args[0].as(); const auto obj = nlohmann::json::parse(json); @@ -173,7 +173,7 @@ namespace json return json_to_gsc(obj); }); - gsc::function::add("jsonserialize", [](gsc::function_args args) + gsc::function::add("jsonserialize", [](const gsc::function_args& args) { const auto value = args[0]; auto indent = -1; diff --git a/src/game/scripting/function.cpp b/src/game/scripting/function.cpp new file mode 100644 index 0000000..d63387e --- /dev/null +++ b/src/game/scripting/function.cpp @@ -0,0 +1,30 @@ +#include +#include "function.hpp" +#include "execution.hpp" + +namespace scripting +{ + function::function(const char* pos) + : pos_(pos) + { + } + + script_value function::get_raw() const + { + game::VariableValue value; + value.type = game::SCRIPT_FUNCTION; + value.u.codePosValue = this->pos_; + + return value; + } + + const char* function::get_pos() const + { + return this->pos_; + } + + script_value function::call(entity self, 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 new file mode 100644 index 0000000..f42b4eb --- /dev/null +++ b/src/game/scripting/function.hpp @@ -0,0 +1,34 @@ +#pragma once +#include "entity.hpp" +#include "script_value.hpp" + +namespace scripting +{ + class function + { + public: + function(const char*); + + script_value get_raw() const; + const char* get_pos() const; + + script_value call(entity self, std::vector arguments) const; + + script_value operator()(entity self, std::vector arguments) const + { + return this->call(self, arguments); + } + + script_value operator()(std::vector arguments) const + { + return this->call(*game::levelEntityId, arguments); + } + + script_value operator()() const + { + return this->call(*game::levelEntityId, {}); + } + private: + const char* pos_; + }; +} diff --git a/src/game/scripting/script_value.cpp b/src/game/scripting/script_value.cpp index 1d606bb..8b85e5e 100644 --- a/src/game/scripting/script_value.cpp +++ b/src/game/scripting/script_value.cpp @@ -2,6 +2,7 @@ #include "script_value.hpp" #include "entity.hpp" #include "array.hpp" +#include "function.hpp" namespace scripting { @@ -261,11 +262,17 @@ namespace scripting **************************************************************/ template <> - bool script_value::is>() const + bool script_value::is() const { return this->get_raw().type == game::SCRIPT_FUNCTION; } + template <> + function script_value::get() const + { + return function(this->get_raw().u.codePosValue); + } + /*************************************************************** * Vector **************************************************************/