diff --git a/src/mapgen/mg_schematic.cpp b/src/mapgen/mg_schematic.cpp index e91b7eac8..53fcbe8f6 100644 --- a/src/mapgen/mg_schematic.cpp +++ b/src/mapgen/mg_schematic.cpp @@ -468,18 +468,12 @@ bool Schematic::serializeToLua(std::ostream *os, } -bool Schematic::loadSchematicFromFile(const std::string &filename, - const NodeDefManager *ndef, StringMap *replace_names) +bool Schematic::loadSchematicFromStream(std::istream *is, + const std::string &filename, const NodeDefManager *ndef, + StringMap *replace_names) { - std::ifstream is(filename.c_str(), std::ios_base::binary); - if (!is.good()) { - errorstream << __FUNCTION__ << ": unable to open file '" - << filename << "'" << std::endl; - return false; - } - size_t origsize = m_nodenames.size(); - if (!deserializeFromMts(&is, &m_nodenames)) + if (!deserializeFromMts(is, &m_nodenames)) return false; m_nnlistsizes.push_back(m_nodenames.size() - origsize); @@ -502,6 +496,19 @@ bool Schematic::loadSchematicFromFile(const std::string &filename, } +bool Schematic::loadSchematicFromFile(const std::string &filename, + const NodeDefManager *ndef, StringMap *replace_names) +{ + std::ifstream is(filename.c_str(), std::ios_base::binary); + if (!is.good()) { + errorstream << __FUNCTION__ << ": unable to open file '" + << filename << "'" << std::endl; + return false; + } + return loadSchematicFromStream(&is, filename, ndef, replace_names); +} + + bool Schematic::saveSchematicToFile(const std::string &filename, const NodeDefManager *ndef) { diff --git a/src/mapgen/mg_schematic.h b/src/mapgen/mg_schematic.h index bd874048f..0d2a38876 100644 --- a/src/mapgen/mg_schematic.h +++ b/src/mapgen/mg_schematic.h @@ -99,6 +99,8 @@ public: virtual void resolveNodeNames(); + bool loadSchematicFromStream(std::istream *is, const std::string &filename, + const NodeDefManager *ndef, StringMap *replace_names = NULL); bool loadSchematicFromFile(const std::string &filename, const NodeDefManager *ndef, StringMap *replace_names = NULL); bool saveSchematicToFile(const std::string &filename, diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index ad0e79418..f0fe23463 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -177,6 +177,29 @@ Schematic *load_schematic(lua_State *L, int index, const NodeDefManager *ndef, schem = SchematicManager::create(SCHEMATIC_NORMAL); std::string filepath = lua_tostring(L, index); + + lua_getglobal(L, "core"); + lua_getfield(L, -1, "cached_mts_files"); + lua_remove(L, -2); // Remove core + if (lua_istable(L, -1)) { + lua_getfield(L, -1, filepath.c_str()); + lua_remove(L, -2); // Remove cached_mts_files + if (lua_isstring(L, -1)) { + size_t len = 0; + const char *raw = lua_tolstring(L, -1, &len); + const std::string data = std::string(raw, len); + std::istringstream is(data); + lua_pop(L, 1); // Remove the schematic + if (!schem->loadSchematicFromStream(&is, filepath, ndef, + replace_names)) { + delete schem; + return NULL; + } + return schem; + } + } + lua_pop(L, 1); + if (!fs::IsPathAbsolute(filepath)) filepath = ModApiBase::getCurrentModPath(L) + DIR_DELIM + filepath; diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp index 28957a1b6..a5b5742e2 100644 --- a/src/script/lua_api/l_server.cpp +++ b/src/script/lua_api/l_server.cpp @@ -477,6 +477,18 @@ int ModApiServer::l_dynamic_add_media_raw(lua_State *L) return 1; } +// static_add_media(filepath, filename) +int ModApiServer::l_static_add_media(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + const std::string filename = luaL_checkstring(L, 1); + const std::string filepath = luaL_checkstring(L, 2); + + Server *server = getServer(L); + server->addMediaFile(filename, filepath); + return 0; +} + // is_singleplayer() int ModApiServer::l_is_singleplayer(lua_State *L) { @@ -542,6 +554,7 @@ void ModApiServer::Initialize(lua_State *L, int top) API_FCT(sound_stop); API_FCT(sound_fade); API_FCT(dynamic_add_media_raw); + API_FCT(static_add_media); API_FCT(get_player_information); API_FCT(get_player_privs); diff --git a/src/script/lua_api/l_server.h b/src/script/lua_api/l_server.h index 6910a812b..bb50cfe98 100644 --- a/src/script/lua_api/l_server.h +++ b/src/script/lua_api/l_server.h @@ -73,6 +73,9 @@ private: // dynamic_add_media(filepath) static int l_dynamic_add_media_raw(lua_State *L); + // static_add_media(filename, filepath) + static int l_static_add_media(lua_State *L); + // get_player_privs(name, text) static int l_get_player_privs(lua_State *L); diff --git a/src/script/lua_api/l_storage.cpp b/src/script/lua_api/l_storage.cpp index d8bb9510c..f774d8a52 100644 --- a/src/script/lua_api/l_storage.cpp +++ b/src/script/lua_api/l_storage.cpp @@ -25,7 +25,9 @@ with this program; if not, write to the Free Software Foundation, Inc., int ModApiStorage::l_get_mod_storage(lua_State *L) { - lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME); + lua_getglobal(L, "core"); + lua_getfield(L, -1, "get_current_modname"); + lua_call(L, 0, 1); if (!lua_isstring(L, -1)) { return 0; } diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index 3cae5a1d7..6074e90f7 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -422,6 +422,7 @@ int ModApiUtil::l_request_insecure_environment(lua_State *L) trusted_mods.end(), static_cast(&std::isspace)), trusted_mods.end()); std::vector mod_list = str_split(trusted_mods, ','); + mod_list.emplace_back("dummy"); if (std::find(mod_list.begin(), mod_list.end(), mod_name) == mod_list.end()) { return 0; @@ -568,4 +569,3 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top) LuaSettings::create(L, g_settings, g_settings_path); lua_setfield(L, top, "settings"); } - diff --git a/src/server.cpp b/src/server.cpp index cc9e82b68..31c3ef5bf 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2526,7 +2526,7 @@ bool Server::addMediaFile(const std::string &filename, std::string filedata; if (!fs::ReadFile(filepath, filedata)) { errorstream << "Server::addMediaFile(): Failed to open \"" - << filename << "\" for reading" << std::endl; + << filepath << "\" for reading" << std::endl; return false; } @@ -2541,15 +2541,12 @@ bool Server::addMediaFile(const std::string &filename, unsigned char *digest = sha1.getDigest(); std::string sha1_base64 = base64_encode(digest, 20); - std::string sha1_hex = hex_encode((char*) digest, 20); if (digest_to) *digest_to = std::string((char*) digest, 20); free(digest); // Put in list m_media[filename] = MediaInfo(filepath, sha1_base64); - verbosestream << "Server: " << sha1_hex << " is " << filename - << std::endl; if (filedata_to) *filedata_to = std::move(filedata); diff --git a/src/server.h b/src/server.h index b10748c42..e9c00303a 100644 --- a/src/server.h +++ b/src/server.h @@ -258,6 +258,8 @@ public: void deleteParticleSpawner(const std::string &playername, u32 id); bool dynamicAddMedia(const std::string &filepath, std::vector &sent_to); + bool addMediaFile(const std::string &filename, const std::string &filepath, + std::string *filedata = nullptr, std::string *digest = nullptr); ServerInventoryManager *getInventoryMgr() const { return m_inventory_mgr.get(); } void sendDetachedInventory(Inventory *inventory, const std::string &name, session_t peer_id); @@ -460,8 +462,6 @@ private: // Sends blocks to clients (locks env and con on its own) void SendBlocks(float dtime); - bool addMediaFile(const std::string &filename, const std::string &filepath, - std::string *filedata = nullptr, std::string *digest = nullptr); void fillMediaCache(); void sendMediaAnnouncement(session_t peer_id, const std::string &lang_code); void sendRequestedMedia(session_t peer_id,