]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/cpp_api/s_base.cpp
Pass clang-format on various cpp/header files (#5559)
[dragonfireclient.git] / src / script / cpp_api / s_base.cpp
index 679a517ee0b84c9c4685b9abf69126560ceb05ab..6a843810f9b4b9f3a1d7ff76d6dd1ac0989f675b 100644 (file)
@@ -23,12 +23,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "lua_api/l_object.h"
 #include "common/c_converter.h"
 #include "serverobject.h"
-#include "debug.h"
 #include "filesys.h"
-#include "log.h"
 #include "mods.h"
 #include "porting.h"
 #include "util/string.h"
+#include "server.h"
+#ifndef SERVER
+#include "client.h"
+#endif
 
 
 extern "C" {
@@ -40,6 +42,7 @@ extern "C" {
 
 #include <stdio.h>
 #include <cstdarg>
+#include <sstream>
 
 
 class ModNameStorer
@@ -68,7 +71,8 @@ class ModNameStorer
 */
 
 ScriptApiBase::ScriptApiBase() :
-       m_luastackmutex()
+       m_luastackmutex(),
+       m_gamedef(NULL)
 {
 #ifdef SCRIPTAPI_LOCK_DEBUG
        m_lock_recursion_count = 0;
@@ -77,6 +81,8 @@ ScriptApiBase::ScriptApiBase() :
        m_luastack = luaL_newstate();
        FATAL_ERROR_IF(!m_luastack, "luaL_newstate() failed");
 
+       lua_atpanic(m_luastack, &luaPanic);
+
        luaL_openlibs(m_luastack);
 
        // Make the ScriptApiBase* accessible to ModApiBase
@@ -110,7 +116,6 @@ ScriptApiBase::ScriptApiBase() :
        // Default to false otherwise
        m_secure = false;
 
-       m_server = NULL;
        m_environment = NULL;
        m_guiengine = NULL;
 }
@@ -120,6 +125,16 @@ ScriptApiBase::~ScriptApiBase()
        lua_close(m_luastack);
 }
 
+int ScriptApiBase::luaPanic(lua_State *L)
+{
+       std::ostringstream oss;
+       oss << "LUA PANIC: unprotected error in call to Lua API ("
+               << lua_tostring(L, -1) << ")";
+       FATAL_ERROR(oss.str().c_str());
+       // NOTREACHED
+       return 0;
+}
+
 void ScriptApiBase::loadMod(const std::string &script_path,
                const std::string &mod_name)
 {
@@ -320,3 +335,14 @@ void ScriptApiBase::objectrefGet(lua_State *L, u16 id)
        lua_remove(L, -2); // object_refs
        lua_remove(L, -2); // core
 }
+
+Server* ScriptApiBase::getServer()
+{
+       return dynamic_cast<Server *>(m_gamedef);
+}
+#ifndef SERVER
+Client* ScriptApiBase::getClient()
+{
+       return dynamic_cast<Client *>(m_gamedef);
+}
+#endif