2013-05-24 15:51:02 -07: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 13:19:39 -07:00
|
|
|
#pragma once
|
2013-05-24 15:51:02 -07:00
|
|
|
|
|
|
|
#include <iostream>
|
2013-08-10 19:09:45 -07:00
|
|
|
#include <string>
|
2017-06-11 00:43:05 -07:00
|
|
|
#include <thread>
|
2017-08-16 13:11:45 -07:00
|
|
|
#include <mutex>
|
2018-01-03 23:21:12 -08:00
|
|
|
#include <unordered_map>
|
2018-06-30 08:11:38 -07:00
|
|
|
#include "common/helper.h"
|
2017-06-10 04:49:15 -07:00
|
|
|
#include "util/basic_macros.h"
|
2013-08-10 19:09:45 -07:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <lua.h>
|
2018-01-03 23:21:12 -08:00
|
|
|
#include <lualib.h>
|
2013-08-10 19:09:45 -07:00
|
|
|
}
|
2013-05-24 15:51:02 -07:00
|
|
|
|
|
|
|
#include "irrlichttypes.h"
|
|
|
|
#include "common/c_types.h"
|
2013-11-05 09:06:15 -08:00
|
|
|
#include "common/c_internal.h"
|
2018-01-03 23:21:12 -08:00
|
|
|
#include "debug.h"
|
2018-01-21 13:26:04 -08:00
|
|
|
#include "config.h"
|
2013-05-24 15:51:02 -07:00
|
|
|
|
2013-08-10 19:09:45 -07:00
|
|
|
#define SCRIPTAPI_LOCK_DEBUG
|
2015-08-11 19:27:54 -07:00
|
|
|
#define SCRIPTAPI_DEBUG
|
2013-05-24 15:51:02 -07:00
|
|
|
|
2014-09-05 17:08:51 -07: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 04:30:50 -07:00
|
|
|
#define PCALL_RES(RES) { \
|
2015-08-04 21:49:35 -07:00
|
|
|
int result_ = (RES); \
|
|
|
|
if (result_ != 0) { \
|
|
|
|
scriptError(result_, __FUNCTION__); \
|
|
|
|
} \
|
2017-08-20 04:30:50 -07:00
|
|
|
}
|
2014-09-05 17:08:51 -07:00
|
|
|
|
2015-08-11 19:27:54 -07:00
|
|
|
#define runCallbacks(nargs, mode) \
|
|
|
|
runCallbacksRaw((nargs), (mode), __FUNCTION__)
|
|
|
|
|
|
|
|
#define setOriginFromTable(index) \
|
|
|
|
setOriginFromTableRaw(index, __FUNCTION__)
|
|
|
|
|
2017-06-30 11:14:39 -07:00
|
|
|
enum class ScriptingType: u8 {
|
2018-01-03 23:21:12 -08:00
|
|
|
Async,
|
2017-06-30 11:14:39 -07:00
|
|
|
Client,
|
2018-01-03 23:21:12 -08:00
|
|
|
MainMenu,
|
|
|
|
Server
|
2017-06-30 11:14:39 -07:00
|
|
|
};
|
|
|
|
|
2013-05-24 15:51:02 -07:00
|
|
|
class Server;
|
2017-01-21 06:02:08 -08:00
|
|
|
#ifndef SERVER
|
|
|
|
class Client;
|
|
|
|
#endif
|
|
|
|
class IGameDef;
|
2013-05-24 15:51:02 -07:00
|
|
|
class Environment;
|
2013-08-10 19:09:45 -07:00
|
|
|
class GUIEngine;
|
2013-05-24 15:51:02 -07:00
|
|
|
class ServerActiveObject;
|
2018-03-29 04:36:01 -07:00
|
|
|
struct PlayerHPChangeReason;
|
2013-05-24 15:51:02 -07:00
|
|
|
|
2018-06-30 08:11:38 -07:00
|
|
|
class ScriptApiBase : protected LuaHelper {
|
2013-05-24 15:51:02 -07:00
|
|
|
public:
|
2018-01-03 23:21:12 -08: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-10 19:09:45 -07:00
|
|
|
virtual ~ScriptApiBase();
|
2017-06-10 04:49:15 -07:00
|
|
|
DISABLE_CLASS_COPY(ScriptApiBase);
|
2013-08-10 19:09:45 -07:00
|
|
|
|
2015-10-29 11:48:10 -07: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-10 19:09:45 -07:00
|
|
|
|
2017-06-30 11:14:39 -07:00
|
|
|
#ifndef SERVER
|
|
|
|
void loadModFromMemory(const std::string &mod_name);
|
|
|
|
#endif
|
|
|
|
|
2015-08-11 19:27:54 -07:00
|
|
|
void runCallbacksRaw(int nargs,
|
|
|
|
RunCallbacksMode mode, const char *fxn);
|
|
|
|
|
2013-05-24 15:51:02 -07:00
|
|
|
/* object */
|
|
|
|
void addObjectReference(ServerActiveObject *cobj);
|
|
|
|
void removeObjectReference(ServerActiveObject *cobj);
|
|
|
|
|
2017-01-21 06:02:08 -08:00
|
|
|
IGameDef *getGameDef() { return m_gamedef; }
|
|
|
|
Server* getServer();
|
2017-06-30 11:14:39 -07:00
|
|
|
ScriptingType getType() { return m_type; }
|
2017-01-21 06:02:08 -08:00
|
|
|
#ifndef SERVER
|
|
|
|
Client* getClient();
|
|
|
|
#endif
|
2014-09-05 17:08:51 -07:00
|
|
|
|
2015-08-11 19:27:54 -07:00
|
|
|
std::string getOrigin() { return m_last_run_mod; }
|
|
|
|
void setOriginDirect(const char *origin);
|
|
|
|
void setOriginFromTableRaw(int index, const char *fxn);
|
|
|
|
|
2018-01-03 23:21:12 -08:00
|
|
|
void clientOpenLibs(lua_State *L);
|
|
|
|
|
2013-05-24 15:51:02 -07:00
|
|
|
protected:
|
|
|
|
friend class LuaABM;
|
2016-02-08 22:08:31 -08:00
|
|
|
friend class LuaLBM;
|
2013-05-24 15:51:02 -07:00
|
|
|
friend class InvRef;
|
|
|
|
friend class ObjectRef;
|
|
|
|
friend class NodeMetaRef;
|
|
|
|
friend class ModApiBase;
|
|
|
|
friend class ModApiEnvMod;
|
2013-06-25 08:02:02 -07:00
|
|
|
friend class LuaVoxelManip;
|
2013-05-24 15:51:02 -07:00
|
|
|
|
2013-08-10 19:09:45 -07:00
|
|
|
lua_State* getStack()
|
2013-05-24 15:51:02 -07:00
|
|
|
{ return m_luastack; }
|
|
|
|
|
|
|
|
void realityCheck();
|
2015-08-04 21:49:35 -07:00
|
|
|
void scriptError(int result, const char *fxn);
|
2013-05-24 15:51:02 -07:00
|
|
|
void stackDump(std::ostream &o);
|
|
|
|
|
2017-01-21 06:02:08 -08:00
|
|
|
void setGameDef(IGameDef* gamedef) { m_gamedef = gamedef; }
|
2013-05-24 15:51:02 -07:00
|
|
|
|
|
|
|
Environment* getEnv() { return m_environment; }
|
|
|
|
void setEnv(Environment* env) { m_environment = env; }
|
|
|
|
|
2013-08-10 19:09:45 -07:00
|
|
|
GUIEngine* getGuiEngine() { return m_guiengine; }
|
|
|
|
void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
|
|
|
|
|
2014-10-02 12:58:13 -07:00
|
|
|
void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
|
2013-05-24 15:51:02 -07:00
|
|
|
|
2018-03-28 08:05:18 -07:00
|
|
|
void pushPlayerHPChangeReason(lua_State *L, const PlayerHPChangeReason& reason);
|
|
|
|
|
2017-06-06 07:29:28 -07:00
|
|
|
std::recursive_mutex m_luastackmutex;
|
2015-08-11 19:27:54 -07:00
|
|
|
std::string m_last_run_mod;
|
2017-06-19 14:54:58 -07:00
|
|
|
bool m_secure = false;
|
2013-08-10 19:09:45 -07:00
|
|
|
#ifdef SCRIPTAPI_LOCK_DEBUG
|
2018-03-29 04:36:01 -07:00
|
|
|
int m_lock_recursion_count{};
|
2017-06-11 00:43:05 -07:00
|
|
|
std::thread::id m_owning_thread;
|
2013-05-24 15:51:02 -07:00
|
|
|
#endif
|
2014-04-15 10:30:46 -07:00
|
|
|
|
2013-05-24 15:51:02 -07:00
|
|
|
private:
|
2016-07-25 09:43:15 -07:00
|
|
|
static int luaPanic(lua_State *L);
|
|
|
|
|
2017-06-19 14:54:58 -07:00
|
|
|
lua_State *m_luastack = nullptr;
|
2013-05-24 15:51:02 -07:00
|
|
|
|
2017-06-19 14:54:58 -07:00
|
|
|
IGameDef *m_gamedef = nullptr;
|
|
|
|
Environment *m_environment = nullptr;
|
|
|
|
GUIEngine *m_guiengine = nullptr;
|
2017-06-30 11:14:39 -07:00
|
|
|
ScriptingType m_type;
|
2013-05-24 15:51:02 -07:00
|
|
|
};
|