From 536035441aaf58a91aadcc8aa20f91da6c18fb25 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 16 Nov 2025 18:35:04 +0300 Subject: [PATCH] fixes --- src/engine/EnginePaths.cpp | 2 +- src/io/devices/MemoryDevice.cpp | 9 +++++++++ src/io/devices/MemoryDevice.hpp | 1 + src/logic/scripting/lua/libs/libapp.cpp | 8 ++++++-- src/logic/scripting/lua/libs/libfile.cpp | 9 +++++++++ 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/engine/EnginePaths.cpp b/src/engine/EnginePaths.cpp index 4fdaa7f9..82aea253 100644 --- a/src/engine/EnginePaths.cpp +++ b/src/engine/EnginePaths.cpp @@ -173,7 +173,7 @@ std::string EnginePaths::createMemoryDevice() { auto device = std::make_unique(); std::string name; do { - name = std::string("M.") + generate_random_base64<6>(); + name = std::string("W.") + generate_random_base64<6>(); } while (std::find(mounted.begin(), mounted.end(), name) != mounted.end()); io::set_device(name, std::move(device)); diff --git a/src/io/devices/MemoryDevice.cpp b/src/io/devices/MemoryDevice.cpp index 8f46d6b7..fac50e57 100644 --- a/src/io/devices/MemoryDevice.cpp +++ b/src/io/devices/MemoryDevice.cpp @@ -59,10 +59,16 @@ io::file_time_type io::MemoryDevice::lastWriteTime(std::string_view path) { } bool io::MemoryDevice::exists(std::string_view path) { + if (path.empty()) { + return true; + } return nodes.find(std::string(path)) != nodes.end(); } bool io::MemoryDevice::isdir(std::string_view path) { + if (path.empty()) { + return true; + } const auto& found = nodes.find(std::string(path)); if (found == nodes.end()) { return false; @@ -201,6 +207,9 @@ io::MemoryDevice::Node* io::MemoryDevice::createFile( } io::MemoryDevice::Dir* io::MemoryDevice::getDir(std::string_view path) { + if (path.empty()) { + return &rootDir; + } const auto& found = nodes.find(std::string(path)); if (found == nodes.end()) { return nullptr; diff --git a/src/io/devices/MemoryDevice.hpp b/src/io/devices/MemoryDevice.hpp index b8eef5fb..d6f97c65 100644 --- a/src/io/devices/MemoryDevice.hpp +++ b/src/io/devices/MemoryDevice.hpp @@ -62,6 +62,7 @@ namespace io { std::unique_ptr list(std::string_view path) override; private: std::unordered_map nodes; + Dir rootDir {}; Node* createFile(std::string path, util::Buffer&& content); Dir* createDir(std::string path); diff --git a/src/logic/scripting/lua/libs/libapp.cpp b/src/logic/scripting/lua/libs/libapp.cpp index 8aab35c7..fae18bb6 100644 --- a/src/logic/scripting/lua/libs/libapp.cpp +++ b/src/logic/scripting/lua/libs/libapp.cpp @@ -4,12 +4,16 @@ #include "io/devices/MemoryDevice.hpp" static int l_create_memory_device(lua::State* L) { - auto name = lua::require_string(L, 1); + std::string name = lua::require_string(L, 1); if (io::get_device(name)) { throw std::runtime_error( - "entry-point '" + std::string(name) + "' is already used" + "entry-point '" + name + "' is already used" ); } + if (name.find(':') != std::string::npos) { + throw std::runtime_error("invalid entry point name"); + } + io::set_device(name, std::make_unique()); return 0; } diff --git a/src/logic/scripting/lua/libs/libfile.cpp b/src/logic/scripting/lua/libs/libfile.cpp index 6c215fff..197cd7db 100644 --- a/src/logic/scripting/lua/libs/libfile.cpp +++ b/src/logic/scripting/lua/libs/libfile.cpp @@ -5,6 +5,7 @@ #include "engine/Engine.hpp" #include "engine/EnginePaths.hpp" #include "io/io.hpp" +#include "io/devices/MemoryDevice.hpp" #include "io/devices/ZipFileDevice.hpp" #include "util/stringutil.hpp" #include "api_lua.hpp" @@ -49,6 +50,14 @@ static bool is_writeable(const std::string& entryPoint) { if (entryPoint.substr(0, 2) == "W.") { return true; } + // todo: do better + auto device = io::get_device(entryPoint); + if (device == nullptr) { + return false; + } + if (dynamic_cast(device.get())) { + return true; + } if (writeable_entry_points.find(entryPoint) != writeable_entry_points.end()) { return true; }