From eb9375b0b11cac7c3c5f3c57af805f9d3e6e4ebc Mon Sep 17 00:00:00 2001 From: Shauren Date: Fri, 5 Apr 2024 10:41:12 +0200 Subject: Core/Scripts: Added basic types for async script actions --- src/server/game/Scripting/v2/ScriptActionResult.h | 66 +++++++++++++++++ src/server/game/Scripting/v2/ScriptActions.cpp | 57 +++++++++++++++ src/server/game/Scripting/v2/ScriptActions.h | 87 +++++++++++++++++++++++ 3 files changed, 210 insertions(+) create mode 100644 src/server/game/Scripting/v2/ScriptActionResult.h create mode 100644 src/server/game/Scripting/v2/ScriptActions.cpp create mode 100644 src/server/game/Scripting/v2/ScriptActions.h (limited to 'src') diff --git a/src/server/game/Scripting/v2/ScriptActionResult.h b/src/server/game/Scripting/v2/ScriptActionResult.h new file mode 100644 index 00000000000..823aba61208 --- /dev/null +++ b/src/server/game/Scripting/v2/ScriptActionResult.h @@ -0,0 +1,66 @@ +/* + * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef TRINITYCORE_SCRIPT_ACTION_RESULT_H +#define TRINITYCORE_SCRIPT_ACTION_RESULT_H + +#include + +namespace Scripting::v2 +{ +class ActionBase; + +void MarkActionCompleted(ActionBase& action); + +template +class ActionResultSetter +{ +public: + explicit ActionResultSetter(std::shared_ptr action, T* result) : _action(std::move(action)), _result(result) { } + + void SetResult(T result) + { + if (std::shared_ptr ptr = _action.lock()) + { + *_result = std::move(result); + MarkActionCompleted(*ptr); + } + } + +private: + std::weak_ptr _action; + T* _result; +}; + +template <> +class ActionResultSetter +{ +public: + explicit ActionResultSetter(std::shared_ptr action) : _action(std::move(action)) { } + + void SetResult() + { + if (std::shared_ptr ptr = _action.lock()) + MarkActionCompleted(*ptr); + } + +private: + std::weak_ptr _action; +}; +} + +#endif // TRINITYCORE_SCRIPT_ACTION_RESULT_H diff --git a/src/server/game/Scripting/v2/ScriptActions.cpp b/src/server/game/Scripting/v2/ScriptActions.cpp new file mode 100644 index 00000000000..faa0ccc0075 --- /dev/null +++ b/src/server/game/Scripting/v2/ScriptActions.cpp @@ -0,0 +1,57 @@ +/* + * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "ScriptActions.h" +#include "GameTime.h" + +namespace Scripting::v2 +{ +ActionBase::ActionBase() : _isReady(false) +{ +} + +ActionBase::ActionBase(ActionBase const& other) = default; +ActionBase::ActionBase(ActionBase&& other) noexcept = default; +ActionBase& ActionBase::operator=(ActionBase const& other) = default; +ActionBase& ActionBase::operator=(ActionBase&& other) noexcept = default; + +ActionBase::~ActionBase() = default; + +bool ActionBase::IsReady() const noexcept +{ + return _isReady; +} + +void ActionBase::MarkCompleted() noexcept +{ + _isReady = true; +} + +void MarkActionCompleted(ActionBase& action) +{ + action.MarkCompleted(); +} + +WaitAction::WaitAction(TimePoint waitEnd) : _waitEnd(waitEnd) +{ +} + +bool WaitAction::IsReady() const noexcept +{ + return _waitEnd <= GameTime::Now(); +} +} diff --git a/src/server/game/Scripting/v2/ScriptActions.h b/src/server/game/Scripting/v2/ScriptActions.h new file mode 100644 index 00000000000..5c9fe5f204e --- /dev/null +++ b/src/server/game/Scripting/v2/ScriptActions.h @@ -0,0 +1,87 @@ +/* + * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef TRINITYCORE_SCRIPT_ACTIONS_H +#define TRINITYCORE_SCRIPT_ACTIONS_H + +#include "ScriptActionResult.h" +#include "Duration.h" + +namespace Scripting::v2 +{ +class ActionBase; + +template +class ActionResultSetter; + +class ActionBase +{ +public: + ActionBase(); + ActionBase(ActionBase const& other); + ActionBase(ActionBase&& other) noexcept; + ActionBase& operator=(ActionBase const& other); + ActionBase& operator=(ActionBase&& other) noexcept; + virtual ~ActionBase(); + + virtual bool IsReady() const noexcept; + +protected: + friend void MarkActionCompleted(ActionBase& action); + void MarkCompleted() noexcept; + +private: + bool _isReady; +}; + +class WaitAction : public ActionBase +{ +public: + explicit WaitAction(TimePoint waitEnd); + + bool IsReady() const noexcept override; + +private: + TimePoint _waitEnd; +}; + +template +class ActionResult : public ActionBase +{ +public: + [[nodiscard]] static ActionResultSetter GetResultSetter(std::shared_ptr action) + { + T* resultPtr = &action->_result; + return ActionResultSetter(std::move(action), resultPtr); + } + +private: + T _result = { }; +}; + +template<> +class ActionResult : public ActionBase +{ +public: + [[nodiscard]] static ActionResultSetter GetResultSetter(std::shared_ptr action) + { + return ActionResultSetter(std::move(action)); + } +}; +} + +#endif // TRINITYCORE_SCRIPT_ACTIONS_H -- cgit v1.2.3