diff --git a/server/src/core/abstractmethod.cpp b/server/src/core/abstractmethod.cpp index 5079bae3..f5cb4ee1 100644 --- a/server/src/core/abstractmethod.cpp +++ b/server/src/core/abstractmethod.cpp @@ -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} { } diff --git a/server/src/core/abstractmethod.hpp b/server/src/core/abstractmethod.hpp index 04ab02b4..eb31b004 100644 --- a/server/src/core/abstractmethod.hpp +++ b/server/src/core/abstractmethod.hpp @@ -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 #include #include @@ -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; using Result = std::variant; - 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 argumentTypes() const = 0; diff --git a/server/src/core/method.hpp b/server/src/core/method.hpp index f2c50205..50891185 100644 --- a/server/src/core/method.hpp +++ b/server/src/core/method.hpp @@ -96,6 +96,12 @@ class Method : public AbstractMethod std::function m_function; public: + Method(Object& object, const std::string& name, MethodFlags flags, std::function function) : + AbstractMethod(object, name, flags), + m_function{std::move(function)} + { + } + Method(Object& object, const std::string& name, std::function function) : AbstractMethod(object, name), m_function{std::move(function)} diff --git a/server/src/core/methodflags.hpp b/server/src/core/methodflags.hpp new file mode 100644 index 00000000..80402bb6 --- /dev/null +++ b/server/src/core/methodflags.hpp @@ -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(0); + +#endif diff --git a/server/src/lua/object.cpp b/server/src/lua/object.cpp index 5e534001..4f33ce0c 100644 --- a/server/src/lua/object.cpp +++ b/server/src/lua/object.cpp @@ -159,7 +159,10 @@ int Object::__index(lua_State* L) } else if(AbstractMethod* method = dynamic_cast(item)) { - Method::push(L, *method); + if(method->isScriptCallable()) + Method::push(L, *method); + else + lua_pushnil(L); } else {