mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-15 23:20:36 +01:00
Core/Scripts: Added basic types for async script actions
This commit is contained in:
66
src/server/game/Scripting/v2/ScriptActionResult.h
Normal file
66
src/server/game/Scripting/v2/ScriptActionResult.h
Normal 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
|
||||
57
src/server/game/Scripting/v2/ScriptActions.cpp
Normal file
57
src/server/game/Scripting/v2/ScriptActions.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
87
src/server/game/Scripting/v2/ScriptActions.h
Normal file
87
src/server/game/Scripting/v2/ScriptActions.h
Normal 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
|
||||
Reference in New Issue
Block a user