From 29b95f3625466698a3fccb020eec84c79fb5f8ed Mon Sep 17 00:00:00 2001 From: Federico Cecchetto Date: Mon, 25 Apr 2022 22:41:02 +0200 Subject: [PATCH] Cleanup --- src/client/component/ui_scripting.hpp | 1 - src/client/game/structs.hpp | 10 +- src/client/game/ui_scripting/event.hpp | 11 -- src/client/game/ui_scripting/execution.cpp | 2 +- src/client/game/ui_scripting/lua/context.hpp | 4 - src/client/game/ui_scripting/lua/engine.hpp | 2 - .../game/ui_scripting/lua/event_handler.cpp | 175 ------------------ .../game/ui_scripting/lua/event_handler.hpp | 58 ------ .../game/ui_scripting/lua/scheduler.cpp | 49 ----- .../game/ui_scripting/lua/scheduler.hpp | 4 - src/client/game/ui_scripting/script_value.cpp | 12 +- 11 files changed, 16 insertions(+), 312 deletions(-) delete mode 100644 src/client/game/ui_scripting/event.hpp delete mode 100644 src/client/game/ui_scripting/lua/event_handler.cpp delete mode 100644 src/client/game/ui_scripting/lua/event_handler.hpp diff --git a/src/client/component/ui_scripting.hpp b/src/client/component/ui_scripting.hpp index 417619fe..d00036f1 100644 --- a/src/client/component/ui_scripting.hpp +++ b/src/client/component/ui_scripting.hpp @@ -1,6 +1,5 @@ #pragma once #include "game/ui_scripting/lua/value_conversion.hpp" -#include "game/ui_scripting/event.hpp" namespace ui_scripting { diff --git a/src/client/game/structs.hpp b/src/client/game/structs.hpp index 8c3017c2..c76c760d 100644 --- a/src/client/game/structs.hpp +++ b/src/client/game/structs.hpp @@ -1206,11 +1206,11 @@ namespace game enum HksError { HKS_NO_ERROR = 0x0, - LUA_ERRSYNTAX = 0xFFFFFFFC, - LUA_ERRFILE = 0xFFFFFFFB, - LUA_ERRRUN = 0xFFFFFF9C, - LUA_ERRMEM = 0xFFFFFF38, - LUA_ERRERR = 0xFFFFFED4, + HKS_ERRSYNTAX = 0xFFFFFFFC, + HKS_ERRFILE = 0xFFFFFFFB, + HKS_ERRRUN = 0xFFFFFF9C, + HKS_ERRMEM = 0xFFFFFF38, + HKS_ERRERR = 0xFFFFFED4, HKS_THROWING_ERROR = 0xFFFFFE0C, HKS_GC_YIELD = 0x1, }; diff --git a/src/client/game/ui_scripting/event.hpp b/src/client/game/ui_scripting/event.hpp deleted file mode 100644 index f82c8a79..00000000 --- a/src/client/game/ui_scripting/event.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once -#include "script_value.hpp" - -namespace ui_scripting -{ - struct event - { - std::string name; - arguments arguments; - }; -} diff --git a/src/client/game/ui_scripting/execution.cpp b/src/client/game/ui_scripting/execution.cpp index e6c8c115..a0b0de13 100644 --- a/src/client/game/ui_scripting/execution.cpp +++ b/src/client/game/ui_scripting/execution.cpp @@ -40,7 +40,7 @@ namespace ui_scripting bool notify(const std::string& name, const event_arguments& arguments) { const auto state = *game::hks::lua_state; - if (!state) + if (state == nullptr) { return false; } diff --git a/src/client/game/ui_scripting/lua/context.hpp b/src/client/game/ui_scripting/lua/context.hpp index 5ad4fb7c..585d70e3 100644 --- a/src/client/game/ui_scripting/lua/context.hpp +++ b/src/client/game/ui_scripting/lua/context.hpp @@ -1,7 +1,5 @@ #pragma once -#include "../event.hpp" - #pragma warning(push) #pragma warning(disable: 4702) @@ -10,7 +8,6 @@ #include #include "scheduler.hpp" -#include "event_handler.hpp" namespace ui_scripting::lua { @@ -33,7 +30,6 @@ namespace ui_scripting::lua context& operator=(const context&) = delete; void run_frame(); - void notify(const event& e); private: sol::state state_{}; diff --git a/src/client/game/ui_scripting/lua/engine.hpp b/src/client/game/ui_scripting/lua/engine.hpp index e098f192..bbcf427c 100644 --- a/src/client/game/ui_scripting/lua/engine.hpp +++ b/src/client/game/ui_scripting/lua/engine.hpp @@ -1,7 +1,5 @@ #pragma once -#include "../event.hpp" - namespace ui_scripting::lua::engine { void start(); diff --git a/src/client/game/ui_scripting/lua/event_handler.cpp b/src/client/game/ui_scripting/lua/event_handler.cpp deleted file mode 100644 index 8d123f29..00000000 --- a/src/client/game/ui_scripting/lua/event_handler.cpp +++ /dev/null @@ -1,175 +0,0 @@ -#include "std_include.hpp" -#include "context.hpp" -#include "error.hpp" - -#include "event_handler.hpp" -#include "value_conversion.hpp" - -namespace ui_scripting::lua -{ - event_handler::event_handler(sol::state& state) - : state_(state) - { - auto event_listener_handle_type = state.new_usertype("event_listener_handle"); - - event_listener_handle_type["clear"] = [this](const event_listener_handle& handle) - { - this->remove(handle); - }; - - event_listener_handle_type["endon"] = [this](const event_listener_handle& handle, const std::string& event) - { - this->add_endon_condition(handle, event); - }; - } - - void event_handler::dispatch(const event& event) - { - bool has_built_arguments = false; - event_arguments arguments{}; - - callbacks_.access([&](task_list& tasks) - { - this->merge_callbacks(); - this->handle_endon_conditions(event); - - for (auto i = tasks.begin(); i != tasks.end();) - { - if (i->event != event.name) - { - ++i; - continue; - } - - if (!i->is_deleted) - { - if (!has_built_arguments) - { - has_built_arguments = true; - arguments = this->build_arguments(event); - } - - handle_error(i->callback(sol::as_args(arguments))); - } - - if (i->is_volatile || i->is_deleted) - { - i = tasks.erase(i); - } - else - { - ++i; - } - } - }); - } - - event_listener_handle event_handler::add_event_listener(event_listener&& listener) - { - const uint64_t id = ++this->current_listener_id_; - listener.id = id; - listener.is_deleted = false; - - new_callbacks_.access([&listener](task_list& tasks) - { - tasks.emplace_back(std::move(listener)); - }); - - return {id}; - } - - void event_handler::add_endon_condition(const event_listener_handle& handle, const std::string& event) - { - auto merger = [&](task_list& tasks) - { - for (auto& task : tasks) - { - if (task.id == handle.id) - { - task.endon_conditions.emplace_back(event); - } - } - }; - - callbacks_.access([&](task_list& tasks) - { - merger(tasks); - new_callbacks_.access(merger); - }); - } - - void event_handler::clear() - { - callbacks_.access([&](task_list& tasks) - { - new_callbacks_.access([&](task_list& new_tasks) - { - new_tasks.clear(); - tasks.clear(); - }); - }); - } - - void event_handler::remove(const event_listener_handle& handle) - { - auto mask_as_deleted = [&](task_list& tasks) - { - for (auto& task : tasks) - { - if (task.id == handle.id) - { - task.is_deleted = true; - break; - } - } - }; - - callbacks_.access(mask_as_deleted); - new_callbacks_.access(mask_as_deleted); - } - - void event_handler::merge_callbacks() - { - callbacks_.access([&](task_list& tasks) - { - new_callbacks_.access([&](task_list& new_tasks) - { - tasks.insert(tasks.end(), std::move_iterator(new_tasks.begin()), - std::move_iterator(new_tasks.end())); - new_tasks = {}; - }); - }); - } - - void event_handler::handle_endon_conditions(const event& event) - { - auto deleter = [&](task_list& tasks) - { - for (auto& task : tasks) - { - for (auto& condition : task.endon_conditions) - { - if (condition == event.name) - { - task.is_deleted = true; - break; - } - } - } - }; - - callbacks_.access(deleter); - } - - event_arguments event_handler::build_arguments(const event& event) const - { - event_arguments arguments; - - for (const auto& argument : event.arguments) - { - arguments.emplace_back(convert(this->state_, argument)); - } - - return arguments; - } -} diff --git a/src/client/game/ui_scripting/lua/event_handler.hpp b/src/client/game/ui_scripting/lua/event_handler.hpp deleted file mode 100644 index abf1415e..00000000 --- a/src/client/game/ui_scripting/lua/event_handler.hpp +++ /dev/null @@ -1,58 +0,0 @@ -#pragma once -#include - -namespace ui_scripting::lua -{ - using event_arguments = std::vector; - using event_callback = sol::protected_function; - - class event_listener_handle - { - public: - uint64_t id = 0; - }; - - class event_listener final : public event_listener_handle - { - public: - std::string event = {}; - event_callback callback = {}; - bool is_volatile = false; - bool is_deleted = false; - std::vector endon_conditions{}; - }; - - class event_handler final - { - public: - event_handler(sol::state& state); - - event_handler(event_handler&&) noexcept = delete; - event_handler& operator=(event_handler&&) noexcept = delete; - - event_handler(const scheduler&) = delete; - event_handler& operator=(const event_handler&) = delete; - - void dispatch(const event& event); - - event_listener_handle add_event_listener(event_listener&& listener); - - void clear(); - - private: - sol::state& state_; - std::atomic_int64_t current_listener_id_ = 0; - - using task_list = std::vector; - utils::concurrency::container new_callbacks_; - utils::concurrency::container callbacks_; - - void remove(const event_listener_handle& handle); - void merge_callbacks(); - void handle_endon_conditions(const event& event); - - void add_endon_condition(const event_listener_handle& handle, const std::string& event); - - event_arguments build_arguments(const event& event) const; - }; -} diff --git a/src/client/game/ui_scripting/lua/scheduler.cpp b/src/client/game/ui_scripting/lua/scheduler.cpp index 99cac9a5..18e779b6 100644 --- a/src/client/game/ui_scripting/lua/scheduler.cpp +++ b/src/client/game/ui_scripting/lua/scheduler.cpp @@ -12,35 +12,6 @@ namespace ui_scripting::lua { this->remove(handle); }; - - task_handle_type["endon"] = [this](const task_handle& handle, const std::string& event) - { - this->add_endon_condition(handle, event); - }; - } - - void scheduler::dispatch(const event& event) - { - auto deleter = [&](task_list& tasks) - { - for (auto& task : tasks) - { - for (auto& condition : task.endon_conditions) - { - if (condition == event.name) - { - task.is_deleted = true; - break; - } - } - } - }; - - callbacks_.access([&](task_list& tasks) - { - deleter(tasks); - new_callbacks_.access(deleter); - }); } void scheduler::run_frame() @@ -118,26 +89,6 @@ namespace ui_scripting::lua return {id}; } - void scheduler::add_endon_condition(const task_handle& handle, const std::string& event) - { - auto merger = [&](task_list& tasks) - { - for (auto& task : tasks) - { - if (task.id == handle.id) - { - task.endon_conditions.emplace_back(event); - } - } - }; - - callbacks_.access([&](task_list& tasks) - { - merger(tasks); - new_callbacks_.access(merger); - }); - } - void scheduler::remove(const task_handle& handle) { auto mask_as_deleted = [&](task_list& tasks) diff --git a/src/client/game/ui_scripting/lua/scheduler.hpp b/src/client/game/ui_scripting/lua/scheduler.hpp index 26ab9c32..1935e25e 100644 --- a/src/client/game/ui_scripting/lua/scheduler.hpp +++ b/src/client/game/ui_scripting/lua/scheduler.hpp @@ -19,7 +19,6 @@ namespace ui_scripting::lua std::chrono::milliseconds delay{}; bool is_volatile = false; bool is_deleted = false; - std::vector endon_conditions{}; }; class scheduler final @@ -33,7 +32,6 @@ namespace ui_scripting::lua scheduler(const scheduler&) = delete; scheduler& operator=(const scheduler&) = delete; - void dispatch(const event& event); void run_frame(); void clear(); @@ -46,8 +44,6 @@ namespace ui_scripting::lua utils::concurrency::container callbacks_; std::atomic_int64_t current_task_id_ = 0; - void add_endon_condition(const task_handle& handle, const std::string& event); - void remove(const task_handle& handle); void merge_callbacks(); }; diff --git a/src/client/game/ui_scripting/script_value.cpp b/src/client/game/ui_scripting/script_value.cpp index faf66202..fcde49c3 100644 --- a/src/client/game/ui_scripting/script_value.cpp +++ b/src/client/game/ui_scripting/script_value.cpp @@ -60,8 +60,12 @@ namespace ui_scripting game::hks::HksObject obj{}; const auto state = *game::hks::lua_state; - state->m_apistack.top = state->m_apistack.base; + if (state == nullptr) + { + return; + } + state->m_apistack.top = state->m_apistack.base; game::hks::hksi_lua_pushlstring(state, value, static_cast(strlen(value))); obj = state->m_apistack.top[-1]; @@ -73,8 +77,12 @@ namespace ui_scripting game::hks::HksObject obj{}; const auto state = *game::hks::lua_state; - state->m_apistack.top = state->m_apistack.base; + if (state == nullptr) + { + return; + } + state->m_apistack.top = state->m_apistack.base; game::hks::hksi_lua_pushlstring(state, value, len); obj = state->m_apistack.top[-1];