diff --git a/src/engine/EnginePaths.cpp b/src/engine/EnginePaths.cpp index 8a786140..4fdaa7f9 100644 --- a/src/engine/EnginePaths.cpp +++ b/src/engine/EnginePaths.cpp @@ -2,6 +2,7 @@ #include "debug/Logger.hpp" #include "io/devices/StdfsDevice.hpp" +#include "io/devices/MemoryDevice.hpp" #include "io/devices/ZipFileDevice.hpp" #include "maths/util.hpp" #include "typedefs.hpp" @@ -168,6 +169,18 @@ void EnginePaths::unmount(const std::string& name) { mounted.erase(found); } +std::string EnginePaths::createMemoryDevice() { + auto device = std::make_unique(); + std::string name; + do { + name = std::string("M.") + generate_random_base64<6>(); + } while (std::find(mounted.begin(), mounted.end(), name) != mounted.end()); + + io::set_device(name, std::move(device)); + mounted.push_back(name); + return name; +} + std::string EnginePaths::createWriteableDevice(const std::string& name) { const auto& found = writeables.find(name); if (found != writeables.end()) { diff --git a/src/engine/EnginePaths.hpp b/src/engine/EnginePaths.hpp index 41d02236..8e5f71a2 100644 --- a/src/engine/EnginePaths.hpp +++ b/src/engine/EnginePaths.hpp @@ -58,6 +58,7 @@ public: void unmount(const std::string& name); std::string createWriteableDevice(const std::string& name); + std::string createMemoryDevice(); void setEntryPoints(std::vector entryPoints); diff --git a/src/logic/scripting/lua/libs/libapp.cpp b/src/logic/scripting/lua/libs/libapp.cpp index 4bd38f4b..8aab35c7 100644 --- a/src/logic/scripting/lua/libs/libapp.cpp +++ b/src/logic/scripting/lua/libs/libapp.cpp @@ -1,6 +1,21 @@ #include "api_lua.hpp" +#include "io/io.hpp" +#include "io/devices/MemoryDevice.hpp" + +static int l_create_memory_device(lua::State* L) { + auto name = lua::require_string(L, 1); + if (io::get_device(name)) { + throw std::runtime_error( + "entry-point '" + std::string(name) + "' is already used" + ); + } + io::set_device(name, std::make_unique()); + return 0; +} + const luaL_Reg applib[] = { + {"create_memory_device", lua::wrap}, // see libcore.cpp an stdlib.lua {nullptr, nullptr} }; diff --git a/src/logic/scripting/lua/libs/libfile.cpp b/src/logic/scripting/lua/libs/libfile.cpp index c3ee4bdb..6c215fff 100644 --- a/src/logic/scripting/lua/libs/libfile.cpp +++ b/src/logic/scripting/lua/libs/libfile.cpp @@ -221,6 +221,16 @@ static int l_unmount(lua::State* L) { return 0; } +static int l_create_memory_device(lua::State* L) { + if (lua::isstring(L, 1)) { + throw std::runtime_error( + "name must not be specified, use app.create_memory_device instead" + ); + } + auto& paths = engine->getPaths(); + return lua::pushstring(L, paths.createMemoryDevice()); +} + static int l_create_zip(lua::State* L) { io::path folder = lua::require_string(L, 1); io::path outFile = lua::require_string(L, 2); @@ -336,7 +346,6 @@ static int l_write_descriptor(lua::State* L) { if (!stream->good()) { throw std::runtime_error("failed to write to stream"); } - return 0; } @@ -352,7 +361,6 @@ static int l_flush_descriptor(lua::State* L) { } scripting::descriptors_manager::flush(descriptor); - return 0; } @@ -364,13 +372,11 @@ static int l_close_descriptor(lua::State* L) { } scripting::descriptors_manager::close(descriptor); - return 0; } static int l_close_all_descriptors(lua::State* L) { scripting::descriptors_manager::close_all_descriptors(); - return 0; } @@ -396,6 +402,7 @@ const luaL_Reg filelib[] = { {"is_writeable", lua::wrap}, {"mount", lua::wrap}, {"unmount", lua::wrap}, + {"create_memory_device", lua::wrap}, {"create_zip", lua::wrap}, {"__open_descriptor", lua::wrap}, {"__has_descriptor", lua::wrap},