added method flags to control method access within the Lua scripting engine

Dieser Commit ist enthalten in:
Reinder Feenstra 2021-11-10 23:19:49 +01:00
Ursprung b9897c996d
Commit 803ca40ec6
5 geänderte Dateien mit 60 neuen und 6 gelöschten Zeilen

Datei anzeigen

@ -3,7 +3,7 @@
*
* 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
* modify it under the terms of the GNU General Public License
@ -22,7 +22,8 @@
#include "abstractmethod.hpp"
AbstractMethod::AbstractMethod(Object& object, const std::string& name) :
InterfaceItem(object, name)
AbstractMethod::AbstractMethod(Object& object, const std::string& name, MethodFlags flags)
: InterfaceItem(object, name)
, m_flags{flags}
{
}

Datei anzeigen

@ -3,7 +3,7 @@
*
* 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
* modify it under the terms of the GNU General Public License
@ -24,6 +24,7 @@
#define TRAINTASTIC_SERVER_CORE_ABSTRACTMETHOD_HPP
#include "interfaceitem.hpp"
#include "methodflags.hpp"
#include <vector>
#include <variant>
#include <stdexcept>
@ -31,6 +32,9 @@
class AbstractMethod : public InterfaceItem
{
private:
const MethodFlags m_flags;
public:
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 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::vector<ValueType> argumentTypes() const = 0;

Datei anzeigen

@ -96,6 +96,12 @@ class Method<R(A...)> : public AbstractMethod
std::function<R(A...)> m_function;
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) :
AbstractMethod(object, name),
m_function{std::move(function)}

Datei anzeigen

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

Datei anzeigen

@ -159,7 +159,10 @@ int Object::__index(lua_State* L)
}
else if(AbstractMethod* method = dynamic_cast<AbstractMethod*>(item))
{
Method::push(L, *method);
if(method->isScriptCallable())
Method::push(L, *method);
else
lua_pushnil(L);
}
else
{