diff options
author | Shauren <shauren.trinity@gmail.com> | 2024-04-05 10:41:12 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2024-04-05 10:41:12 +0200 |
commit | eb9375b0b11cac7c3c5f3c57af805f9d3e6e4ebc (patch) | |
tree | 87fbf369300286e7e0815a2c26b23a5e0aa40d8c /src | |
parent | d079a7e267511714babc0c627c3e35b06a1c901e (diff) |
Core/Scripts: Added basic types for async script actions
Diffstat (limited to 'src')
-rw-r--r-- | src/server/game/Scripting/v2/ScriptActionResult.h | 66 | ||||
-rw-r--r-- | src/server/game/Scripting/v2/ScriptActions.cpp | 57 | ||||
-rw-r--r-- | src/server/game/Scripting/v2/ScriptActions.h | 87 |
3 files changed, 210 insertions, 0 deletions
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 <http://www.gnu.org/licenses/>. + */ + +#ifndef TRINITYCORE_SCRIPT_ACTION_RESULT_H +#define TRINITYCORE_SCRIPT_ACTION_RESULT_H + +#include <memory> + +namespace Scripting::v2 +{ +class ActionBase; + +void MarkActionCompleted(ActionBase& action); + +template <typename T> +class ActionResultSetter +{ +public: + explicit ActionResultSetter(std::shared_ptr<ActionBase> action, T* result) : _action(std::move(action)), _result(result) { } + + void SetResult(T result) + { + if (std::shared_ptr<ActionBase> ptr = _action.lock()) + { + *_result = std::move(result); + MarkActionCompleted(*ptr); + } + } + +private: + std::weak_ptr<ActionBase> _action; + T* _result; +}; + +template <> +class ActionResultSetter<void> +{ +public: + explicit ActionResultSetter(std::shared_ptr<ActionBase> action) : _action(std::move(action)) { } + + void SetResult() + { + if (std::shared_ptr<ActionBase> ptr = _action.lock()) + MarkActionCompleted(*ptr); + } + +private: + std::weak_ptr<ActionBase> _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 <http://www.gnu.org/licenses/>.
+ */
+
+#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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TRINITYCORE_SCRIPT_ACTIONS_H
+#define TRINITYCORE_SCRIPT_ACTIONS_H
+
+#include "ScriptActionResult.h"
+#include "Duration.h"
+
+namespace Scripting::v2
+{
+class ActionBase;
+
+template<typename T>
+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<typename T>
+class ActionResult : public ActionBase
+{
+public:
+ [[nodiscard]] static ActionResultSetter<T> GetResultSetter(std::shared_ptr<ActionResult> action)
+ {
+ T* resultPtr = &action->_result;
+ return ActionResultSetter<T>(std::move(action), resultPtr);
+ }
+
+private:
+ T _result = { };
+};
+
+template<>
+class ActionResult<void> : public ActionBase
+{
+public:
+ [[nodiscard]] static ActionResultSetter<void> GetResultSetter(std::shared_ptr<ActionResult> action)
+ {
+ return ActionResultSetter<void>(std::move(action));
+ }
+};
+}
+
+#endif // TRINITYCORE_SCRIPT_ACTIONS_H
|