aboutsummaryrefslogtreecommitdiff
path: root/src/script/cpp_api
Commit message (Expand)AuthorAge
* Consistent HP and damage types (#8167)SmallJoker2019-02-10
* Fix wrong code comment (#8061)DS2019-01-06
* Move client-specific files to 'src/client' (#7902)Quentin Bazin2018-11-28
* Raycast: export exact pointing location (#6304)Dániel Juhász2018-08-16
* Add a MSVC / Windows compatible snprintf function (#7353)nOOb31672018-07-22
* Modernize lua read (part 2 & 3): C++ templating assurance (#7410)Loïc Blot2018-06-30
* CSM/SSM: Add on_mods_loaded callback (#7411)Loïc Blot2018-06-06
* Fix builtin inventory list crash when size = 0 (#7297)SmallJoker2018-05-05
* Allow damage for attached objects, add attach/detach callbacks (#6786)SmallJoker2018-04-30
* Add online content repositoryrubenwardy2018-04-19
* Run callback in IDropAction, refactor function argumentsSmallJoker2018-04-02
* Add player inventory callbacksSmallJoker2018-04-02
* LINT: add clang-tidy step (#6295)Loïc Blot2018-04-01
* Fix a warning reported by clangLoic Blot2018-03-29
* Add reasons to on_dieplayer and on_hpchangeAndrew Ward2018-03-28
* Add `on_auth_fail` callback (#7039)red-0012018-02-15
* Node definition manager refactor (#7016)Dániel Juhász2018-02-10
* Move `setlocale` from Lua to C++.red-0012018-02-08
* [CSM] Don't Load the package library (#6944)red-0012018-01-22
* Change include from "cmake_config.h" to "config.h"Wayward One2018-01-21
* [CSM] Remove `on_connect` callback (#6941)red-0012018-01-21
* [CSM] Don't load the IO library. (#6087)red-0012018-01-04
* Fix crash on can_bypass_userlimit returning non-booleanrubenwardy2018-01-03
* Move files to subdirectories (#6599)Vitaliy2017-11-08
* Unkown nodes: Provide position on interact (#6505)SmallJoker2017-10-07
* [CSM] Add callback on open inventory (#5793)Vincent Glize2017-10-02
* Implement mod communication channels (#6351)Loïc Blot2017-09-26
* Set placer to nil instead of a non-functional one in item_OnPlace (#6449)DTA72017-09-21
* on_death: Fix callback number of pushed arguments (Fixes #6451)SmallJoker2017-09-21
* ServerEnv: Clean up object lifecycle handling (#6414)sfan52017-09-15
* Make INodeDefManager::getIds return a vector, not a setKahrl2017-09-12
* Implement minetest.register_can_bypass_userlimit (#6369)Loïc Blot2017-09-04
* Remove nodeupdate completely (#6358)Rui2017-09-01
* Modernize source code: last part (#6285)Loïc Blot2017-08-20
* Optimize headers (part 2) (#6272)Loïc Blot2017-08-18
* C++ modernize: Pragma once (#6264)Loïc Blot2017-08-17
* Cleanup various headers to reduce compilation times (#6255)Loïc Blot2017-08-16
* Add LuaEntity on_death callback (#6177)Loïc Blot2017-07-27
* Add 'plantlike_rooted' drawtypenumber Zero2017-07-11
* Expose getPointedThing to LuaDániel Juhász2017-07-07
* Create a filesystem abstraction layer for CSM and only allow accessing files ...red-0012017-06-30
* C++11 cleanup on constructors (#6000)Vincent Glize2017-06-19
* Remove threads.h and replace its definitions with their C++11 equivalents (#5...ShadowNinja2017-06-11
* C++11 patchset 6: forbid object copy using assigment/copy function deleters (...Loïc Blot2017-06-10
* Order es_DrawType exactly like enum NodeDrawType in nodedef.h (#5946)Thomas--S2017-06-10
* CSM: Fix documentation error for register_on_*_chat_messages (#5917)DS2017-06-09
* Use C++11 mutexes only (remove compat code) (#5922)Loïc Blot2017-06-06
* Fix CSM crash (#5779)red-0012017-05-20
* Fix a warning pointed by GCC 7.1Loic Blot2017-05-18
* Use a settings object for the main settingsShadowNinja2017-05-06
">std::string *dst_password, std::set<std::string> *dst_privs) { SCRIPTAPI_PRECHECKHEADER getAuthHandler(); lua_getfield(L, -1, "get_auth"); if(lua_type(L, -1) != LUA_TFUNCTION) throw LuaError(L, "Authentication handler missing get_auth"); lua_pushstring(L, playername.c_str()); if(lua_pcall(L, 1, 1, 0)) scriptError("error: %s", lua_tostring(L, -1)); // nil = login not allowed if(lua_isnil(L, -1)) return false; luaL_checktype(L, -1, LUA_TTABLE); std::string password; bool found = getstringfield(L, -1, "password", password); if(!found) throw LuaError(L, "Authentication handler didn't return password"); if(dst_password) *dst_password = password; lua_getfield(L, -1, "privileges"); if(!lua_istable(L, -1)) throw LuaError(L, "Authentication handler didn't return privilege table"); if(dst_privs) readPrivileges(-1, *dst_privs); lua_pop(L, 1); return true; } void ScriptApi::getAuthHandler() { lua_State *L = getStack(); lua_getglobal(L, "minetest"); lua_getfield(L, -1, "registered_auth_handler"); if(lua_isnil(L, -1)){ lua_pop(L, 1); lua_getfield(L, -1, "builtin_auth_handler"); } if(lua_type(L, -1) != LUA_TTABLE) throw LuaError(L, "Authentication handler table not valid"); } void ScriptApi::readPrivileges(int index,std::set<std::string> &result) { lua_State *L = getStack(); result.clear(); lua_pushnil(L); if(index < 0) index -= 1; while(lua_next(L, index) != 0){ // key at index -2 and value at index -1 std::string key = luaL_checkstring(L, -2); bool value = lua_toboolean(L, -1); if(value) result.insert(key); // removes value, keeps key for next iteration lua_pop(L, 1); } } void ScriptApi::createAuth(const std::string &playername, const std::string &password) { SCRIPTAPI_PRECHECKHEADER getAuthHandler(); lua_getfield(L, -1, "create_auth"); if(lua_type(L, -1) != LUA_TFUNCTION) throw LuaError(L, "Authentication handler missing create_auth"); lua_pushstring(L, playername.c_str()); lua_pushstring(L, password.c_str()); if(lua_pcall(L, 2, 0, 0)) scriptError("error: %s", lua_tostring(L, -1)); } bool ScriptApi::setPassword(const std::string &playername, const std::string &password) { SCRIPTAPI_PRECHECKHEADER getAuthHandler(); lua_getfield(L, -1, "set_password"); if(lua_type(L, -1) != LUA_TFUNCTION) throw LuaError(L, "Authentication handler missing set_password"); lua_pushstring(L, playername.c_str()); lua_pushstring(L, password.c_str()); if(lua_pcall(L, 2, 1, 0)) scriptError("error: %s", lua_tostring(L, -1)); return lua_toboolean(L, -1); } bool ScriptApi::on_chat_message(const std::string &name, const std::string &message) { SCRIPTAPI_PRECHECKHEADER // Get minetest.registered_on_chat_messages lua_getglobal(L, "minetest"); lua_getfield(L, -1, "registered_on_chat_messages"); // Call callbacks lua_pushstring(L, name.c_str()); lua_pushstring(L, message.c_str()); runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC); bool ate = lua_toboolean(L, -1); return ate; } void ScriptApi::on_shutdown() { SCRIPTAPI_PRECHECKHEADER // Get registered shutdown hooks lua_getglobal(L, "minetest"); lua_getfield(L, -1, "registered_on_shutdown"); // Call callbacks runCallbacks(0, RUN_CALLBACKS_MODE_FIRST); } bool ScriptApi::loadMod(const std::string &scriptpath,const std::string &modname) { ModNameStorer modnamestorer(getStack(), modname); if(!string_allowed(modname, MODNAME_ALLOWED_CHARS)){ errorstream<<"Error loading mod \""<<modname <<"\": modname does not follow naming conventions: " <<"Only chararacters [a-z0-9_] are allowed."<<std::endl; return false; } bool success = false; try{ success = scriptLoad(scriptpath.c_str()); } catch(LuaError &e){ errorstream<<"Error loading mod \""<<modname <<"\": "<<e.what()<<std::endl; } return success; } ScriptApi::ScriptApi() { assert("Invalid call to default constructor of scriptapi!" == 0); } ScriptApi::ScriptApi(Server* server) { setServer(server); setStack(luaL_newstate()); assert(getStack()); //TODO add security luaL_openlibs(getStack()); SCRIPTAPI_PRECHECKHEADER lua_pushlightuserdata(L, this); lua_setfield(L, LUA_REGISTRYINDEX, "scriptapi"); lua_newtable(L); lua_setglobal(L, "minetest"); for (std::vector<ModApiBase*>::iterator i = m_mod_api_modules->begin(); i != m_mod_api_modules->end(); i++) { //initializers are called within minetest global table! lua_getglobal(L, "minetest"); int top = lua_gettop(L); bool ModInitializedSuccessfull = (*i)->Initialize(L,top); assert(ModInitializedSuccessfull); } infostream << "SCRIPTAPI: initialized " << m_mod_api_modules->size() << " modules" << std::endl; // Get the main minetest table lua_getglobal(L, "minetest"); // Add tables to minetest lua_newtable(L); lua_setfield(L, -2, "object_refs"); lua_newtable(L); lua_setfield(L, -2, "luaentities"); } ScriptApi::~ScriptApi() { lua_close(getStack()); } bool ScriptApi::scriptLoad(const char *path) { lua_State* L = getStack(); setStack(0); verbosestream<<"Loading and running script from "<<path<<std::endl; lua_pushcfunction(L, script_ErrorHandler); int errorhandler = lua_gettop(L); int ret = luaL_loadfile(L, path) || lua_pcall(L, 0, 0, errorhandler); if(ret){ errorstream<<"========== ERROR FROM LUA ==========="<<std::endl; errorstream<<"Failed to load and run script from "<<std::endl; errorstream<<path<<":"<<std::endl; errorstream<<std::endl; errorstream<<lua_tostring(L, -1)<<std::endl; errorstream<<std::endl; errorstream<<"=======END OF ERROR FROM LUA ========"<<std::endl; lua_pop(L, 1); // Pop error message from stack lua_pop(L, 1); // Pop the error handler from stack return false; } lua_pop(L, 1); // Pop the error handler from stack return true; } bool ScriptApi::registerModApiModule(ModApiBase* ptr) { if (ScriptApi::m_mod_api_modules == 0) ScriptApi::m_mod_api_modules = new std::vector<ModApiBase*>(); assert(ScriptApi::m_mod_api_modules != 0); ScriptApi::m_mod_api_modules->push_back(ptr); return true; } std::vector<ModApiBase*>* ScriptApi::m_mod_api_modules = 0;