2013-05-25 00:51:02 +02:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser 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.
|
|
|
|
*/
|
|
|
|
|
2017-08-17 22:19:39 +02:00
|
|
|
#pragma once
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
#include <iostream>
|
2013-08-11 04:09:45 +02:00
|
|
|
#include <string>
|
2017-06-11 09:43:05 +02:00
|
|
|
#include <thread>
|
2017-08-16 22:11:45 +02:00
|
|
|
#include <mutex>
|
2018-01-04 08:21:12 +01:00
|
|
|
#include <unordered_map>
|
2018-06-30 17:11:38 +02:00
|
|
|
#include "common/helper.h"
|
2017-06-10 13:49:15 +02:00
|
|
|
#include "util/basic_macros.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <lua.h>
|
2018-01-04 08:21:12 +01:00
|
|
|
#include <lualib.h>
|
2013-08-11 04:09:45 +02:00
|
|
|
}
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
#include "irrlichttypes.h"
|
|
|
|
#include "common/c_types.h"
|
2013-11-05 18:06:15 +01:00
|
|
|
#include "common/c_internal.h"
|
2018-01-04 08:21:12 +01:00
|
|
|
#include "debug.h"
|
2018-01-21 22:26:04 +01:00
|
|
|
#include "config.h"
|
2013-05-25 00:51:02 +02:00
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
#define SCRIPTAPI_LOCK_DEBUG
|
2013-05-25 00:51:02 +02:00
|
|
|
|
2014-09-06 02:08:51 +02:00
|
|
|
// MUST be an invalid mod name so that mods can't
|
|
|
|
// use that name to bypass security!
|
|
|
|
#define BUILTIN_MOD_NAME "*builtin*"
|
|
|
|
|
2017-08-20 13:30:50 +02:00
|
|
|
#define PCALL_RES(RES) { \
|
2015-08-05 06:49:35 +02:00
|
|
|
int result_ = (RES); \
|
|
|
|
if (result_ != 0) { \
|
|
|
|
scriptError(result_, __FUNCTION__); \
|
|
|
|
} \
|
2017-08-20 13:30:50 +02:00
|
|
|
}
|
2014-09-06 02:08:51 +02:00
|
|
|
|
2015-08-12 04:27:54 +02:00
|
|
|
#define runCallbacks(nargs, mode) \
|
|
|
|
runCallbacksRaw((nargs), (mode), __FUNCTION__)
|
|
|
|
|
|
|
|
#define setOriginFromTable(index) \
|
|
|
|
setOriginFromTableRaw(index, __FUNCTION__)
|
|
|
|
|
2017-06-30 20:14:39 +02:00
|
|
|
enum class ScriptingType: u8 {
|
2018-01-04 08:21:12 +01:00
|
|
|
Async,
|
2017-06-30 20:14:39 +02:00
|
|
|
Client,
|
2018-01-04 08:21:12 +01:00
|
|
|
MainMenu,
|
|
|
|
Server
|
2017-06-30 20:14:39 +02:00
|
|
|
};
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
class Server;
|
2017-01-21 15:02:08 +01:00
|
|
|
#ifndef SERVER
|
|
|
|
class Client;
|
|
|
|
#endif
|
|
|
|
class IGameDef;
|
2013-05-25 00:51:02 +02:00
|
|
|
class Environment;
|
2013-08-11 04:09:45 +02:00
|
|
|
class GUIEngine;
|
2013-05-25 00:51:02 +02:00
|
|
|
class ServerActiveObject;
|
2018-03-29 13:36:01 +02:00
|
|
|
struct PlayerHPChangeReason;
|
2013-05-25 00:51:02 +02:00
|
|
|
|
2018-06-30 17:11:38 +02:00
|
|
|
class ScriptApiBase : protected LuaHelper {
|
2013-05-25 00:51:02 +02:00
|
|
|
public:
|
2018-01-04 08:21:12 +01:00
|
|
|
ScriptApiBase(ScriptingType type);
|
|
|
|
// fake constructor to allow script API classes (e.g ScriptApiEnv) to virtually inherit from this one.
|
|
|
|
ScriptApiBase()
|
|
|
|
{
|
|
|
|
FATAL_ERROR("ScriptApiBase created without ScriptingType!");
|
|
|
|
}
|
2013-08-11 04:09:45 +02:00
|
|
|
virtual ~ScriptApiBase();
|
2017-06-10 13:49:15 +02:00
|
|
|
DISABLE_CLASS_COPY(ScriptApiBase);
|
2013-08-11 04:09:45 +02:00
|
|
|
|
2015-10-29 19:48:10 +01:00
|
|
|
// These throw a ModError on failure
|
|
|
|
void loadMod(const std::string &script_path, const std::string &mod_name);
|
|
|
|
void loadScript(const std::string &script_path);
|
2013-08-11 04:09:45 +02:00
|
|
|
|
2017-06-30 20:14:39 +02:00
|
|
|
#ifndef SERVER
|
|
|
|
void loadModFromMemory(const std::string &mod_name);
|
|
|
|
#endif
|
|
|
|
|
2015-08-12 04:27:54 +02:00
|
|
|
void runCallbacksRaw(int nargs,
|
|
|
|
RunCallbacksMode mode, const char *fxn);
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
/* object */
|
|
|
|
void addObjectReference(ServerActiveObject *cobj);
|
|
|
|
void removeObjectReference(ServerActiveObject *cobj);
|
|
|
|
|
2017-01-21 15:02:08 +01:00
|
|
|
IGameDef *getGameDef() { return m_gamedef; }
|
|
|
|
Server* getServer();
|
2017-06-30 20:14:39 +02:00
|
|
|
ScriptingType getType() { return m_type; }
|
2017-01-21 15:02:08 +01:00
|
|
|
#ifndef SERVER
|
|
|
|
Client* getClient();
|
|
|
|
#endif
|
2014-09-06 02:08:51 +02:00
|
|
|
|
2021-08-28 12:15:12 +02:00
|
|
|
// IMPORTANT: these cannot be used for any security-related uses, they exist
|
|
|
|
// only to enrich error messages
|
|
|
|
const std::string &getOrigin() { return m_last_run_mod; }
|
2015-08-12 04:27:54 +02:00
|
|
|
void setOriginDirect(const char *origin);
|
|
|
|
void setOriginFromTableRaw(int index, const char *fxn);
|
|
|
|
|
2018-01-04 08:21:12 +01:00
|
|
|
void clientOpenLibs(lua_State *L);
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
protected:
|
|
|
|
friend class LuaABM;
|
2016-02-09 07:08:31 +01:00
|
|
|
friend class LuaLBM;
|
2013-05-25 00:51:02 +02:00
|
|
|
friend class InvRef;
|
|
|
|
friend class ObjectRef;
|
|
|
|
friend class NodeMetaRef;
|
|
|
|
friend class ModApiBase;
|
|
|
|
friend class ModApiEnvMod;
|
2013-06-25 17:02:02 +02:00
|
|
|
friend class LuaVoxelManip;
|
2013-05-25 00:51:02 +02:00
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
lua_State* getStack()
|
2013-05-25 00:51:02 +02:00
|
|
|
{ return m_luastack; }
|
|
|
|
|
|
|
|
void realityCheck();
|
2015-08-05 06:49:35 +02:00
|
|
|
void scriptError(int result, const char *fxn);
|
2013-05-25 00:51:02 +02:00
|
|
|
void stackDump(std::ostream &o);
|
|
|
|
|
2017-01-21 15:02:08 +01:00
|
|
|
void setGameDef(IGameDef* gamedef) { m_gamedef = gamedef; }
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
Environment* getEnv() { return m_environment; }
|
|
|
|
void setEnv(Environment* env) { m_environment = env; }
|
|
|
|
|
2020-04-26 20:57:27 +02:00
|
|
|
#ifndef SERVER
|
2013-08-11 04:09:45 +02:00
|
|
|
GUIEngine* getGuiEngine() { return m_guiengine; }
|
|
|
|
void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
|
2020-04-26 20:57:27 +02:00
|
|
|
#endif
|
2013-08-11 04:09:45 +02:00
|
|
|
|
2014-10-02 21:58:13 +02:00
|
|
|
void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
|
2013-05-25 00:51:02 +02:00
|
|
|
|
2018-03-28 17:05:18 +02:00
|
|
|
void pushPlayerHPChangeReason(lua_State *L, const PlayerHPChangeReason& reason);
|
|
|
|
|
2017-06-06 16:29:28 +02:00
|
|
|
std::recursive_mutex m_luastackmutex;
|
2015-08-12 04:27:54 +02:00
|
|
|
std::string m_last_run_mod;
|
2017-06-19 23:54:58 +02:00
|
|
|
bool m_secure = false;
|
2013-08-11 04:09:45 +02:00
|
|
|
#ifdef SCRIPTAPI_LOCK_DEBUG
|
2018-03-29 13:36:01 +02:00
|
|
|
int m_lock_recursion_count{};
|
2017-06-11 09:43:05 +02:00
|
|
|
std::thread::id m_owning_thread;
|
2013-05-25 00:51:02 +02:00
|
|
|
#endif
|
2014-04-15 19:30:46 +02:00
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
private:
|
2016-07-25 18:43:15 +02:00
|
|
|
static int luaPanic(lua_State *L);
|
|
|
|
|
2017-06-19 23:54:58 +02:00
|
|
|
lua_State *m_luastack = nullptr;
|
2013-05-25 00:51:02 +02:00
|
|
|
|
2017-06-19 23:54:58 +02:00
|
|
|
IGameDef *m_gamedef = nullptr;
|
|
|
|
Environment *m_environment = nullptr;
|
2020-04-26 20:57:27 +02:00
|
|
|
#ifndef SERVER
|
2017-06-19 23:54:58 +02:00
|
|
|
GUIEngine *m_guiengine = nullptr;
|
2020-04-26 20:57:27 +02:00
|
|
|
#endif
|
2017-06-30 20:14:39 +02:00
|
|
|
ScriptingType m_type;
|
2013-05-25 00:51:02 +02:00
|
|
|
};
|