added method flags to control method access within the Lua scripting engine
Dieser Commit ist enthalten in:
Ursprung
b9897c996d
Commit
803ca40ec6
@ -3,7 +3,7 @@
|
|||||||
*
|
*
|
||||||
* This file is part of the traintastic source code.
|
* This file is part of the traintastic source code.
|
||||||
*
|
*
|
||||||
* Copyright (C) 2019-2020 Reinder Feenstra
|
* Copyright (C) 2019-2021 Reinder Feenstra
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or
|
* This program is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU General Public License
|
* modify it under the terms of the GNU General Public License
|
||||||
@ -22,7 +22,8 @@
|
|||||||
|
|
||||||
#include "abstractmethod.hpp"
|
#include "abstractmethod.hpp"
|
||||||
|
|
||||||
AbstractMethod::AbstractMethod(Object& object, const std::string& name) :
|
AbstractMethod::AbstractMethod(Object& object, const std::string& name, MethodFlags flags)
|
||||||
InterfaceItem(object, name)
|
: InterfaceItem(object, name)
|
||||||
|
, m_flags{flags}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
*
|
*
|
||||||
* This file is part of the traintastic source code.
|
* This file is part of the traintastic source code.
|
||||||
*
|
*
|
||||||
* Copyright (C) 2019-2020 Reinder Feenstra
|
* Copyright (C) 2019-2021 Reinder Feenstra
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or
|
* This program is free software; you can redistribute it and/or
|
||||||
* modify it under the terms of the GNU General Public License
|
* modify it under the terms of the GNU General Public License
|
||||||
@ -24,6 +24,7 @@
|
|||||||
#define TRAINTASTIC_SERVER_CORE_ABSTRACTMETHOD_HPP
|
#define TRAINTASTIC_SERVER_CORE_ABSTRACTMETHOD_HPP
|
||||||
|
|
||||||
#include "interfaceitem.hpp"
|
#include "interfaceitem.hpp"
|
||||||
|
#include "methodflags.hpp"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
@ -31,6 +32,9 @@
|
|||||||
|
|
||||||
class AbstractMethod : public InterfaceItem
|
class AbstractMethod : public InterfaceItem
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
const MethodFlags m_flags;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
class MethodCallError : public std::runtime_error
|
class MethodCallError : public std::runtime_error
|
||||||
{
|
{
|
||||||
@ -94,7 +98,11 @@ class AbstractMethod : public InterfaceItem
|
|||||||
using Argument = std::variant<bool, int64_t, double, std::string, ObjectPtr>;
|
using Argument = std::variant<bool, int64_t, double, std::string, ObjectPtr>;
|
||||||
using Result = std::variant<std::monostate, bool, int64_t, double, std::string, ObjectPtr>;
|
using Result = std::variant<std::monostate, bool, int64_t, double, std::string, ObjectPtr>;
|
||||||
|
|
||||||
AbstractMethod(Object& object, const std::string& name);
|
AbstractMethod(Object& object, const std::string& name, MethodFlags m_flags = noMethodFlags);
|
||||||
|
|
||||||
|
inline bool isScriptCallable() const { return m_flags == MethodFlags::ScriptCallable; }
|
||||||
|
|
||||||
|
inline MethodFlags flags() const { return m_flags; }
|
||||||
|
|
||||||
virtual std::size_t argumentCount() const = 0;
|
virtual std::size_t argumentCount() const = 0;
|
||||||
virtual std::vector<ValueType> argumentTypes() const = 0;
|
virtual std::vector<ValueType> argumentTypes() const = 0;
|
||||||
|
|||||||
@ -96,6 +96,12 @@ class Method<R(A...)> : public AbstractMethod
|
|||||||
std::function<R(A...)> m_function;
|
std::function<R(A...)> m_function;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
Method(Object& object, const std::string& name, MethodFlags flags, std::function<R(A...)> function) :
|
||||||
|
AbstractMethod(object, name, flags),
|
||||||
|
m_function{std::move(function)}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
Method(Object& object, const std::string& name, std::function<R(A...)> function) :
|
Method(Object& object, const std::string& name, std::function<R(A...)> function) :
|
||||||
AbstractMethod(object, name),
|
AbstractMethod(object, name),
|
||||||
m_function{std::move(function)}
|
m_function{std::move(function)}
|
||||||
|
|||||||
36
server/src/core/methodflags.hpp
Normale Datei
36
server/src/core/methodflags.hpp
Normale Datei
@ -0,0 +1,36 @@
|
|||||||
|
/**
|
||||||
|
* server/src/core/methodflags.hpp
|
||||||
|
*
|
||||||
|
* This file is part of the traintastic source code.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2021 Reinder Feenstra
|
||||||
|
*
|
||||||
|
* 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, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef TRAINTASTIC_SERVER_CORE_METHODFLAGS_HPP
|
||||||
|
#define TRAINTASTIC_SERVER_CORE_METHODFLAGS_HPP
|
||||||
|
|
||||||
|
enum class MethodFlags
|
||||||
|
{
|
||||||
|
// bit 0..1
|
||||||
|
NoScript = 1 << 0,
|
||||||
|
ScriptCallable = 2 << 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// temporary placeholder, should be removed in the future when all method have their flags set
|
||||||
|
constexpr MethodFlags noMethodFlags = static_cast<MethodFlags>(0);
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -159,7 +159,10 @@ int Object::__index(lua_State* L)
|
|||||||
}
|
}
|
||||||
else if(AbstractMethod* method = dynamic_cast<AbstractMethod*>(item))
|
else if(AbstractMethod* method = dynamic_cast<AbstractMethod*>(item))
|
||||||
{
|
{
|
||||||
|
if(method->isScriptCallable())
|
||||||
Method::push(L, *method);
|
Method::push(L, *method);
|
||||||
|
else
|
||||||
|
lua_pushnil(L);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
Laden…
x
In neuem Issue referenzieren
Einen Benutzer sperren