Core/Scripts: Added basic types for async script actions

This commit is contained in:
Shauren
2024-04-05 10:41:12 +02:00
parent d079a7e267
commit eb9375b0b1
3 changed files with 210 additions and 0 deletions

View File

@@ -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

View File

@@ -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();
}
}

View File

@@ -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