Add app::storage path and set it to WASMFS-mounted directory on web builds

This commit is contained in:
Nikita Lisitsa 2026-07-06 22:38:31 +03:00
parent 8587ba6bc0
commit 55d18aaa33
5 changed files with 68 additions and 1 deletions

View file

@ -0,0 +1,18 @@
#pragma once
#include <psemek/io/stream.hpp>
#include <filesystem>
#include <memory>
namespace psemek::app
{
std::filesystem::path storage_root();
void set_storage_root(std::filesystem::path const & root);
// Implemented by a backend library
std::unique_ptr<io::istream> read_storage(std::filesystem::path const & relative_path);
std::unique_ptr<io::ostream> write_storage(std::filesystem::path const & relative_path);
}

View file

@ -0,0 +1,24 @@
#include <psemek/app/storage.hpp>
#include <psemek/util/executable_path.hpp>
namespace psemek::app
{
namespace
{
static std::filesystem::path global_storage_root = util::executable_path().parent_path();
}
std::filesystem::path storage_root()
{
return global_storage_root;
}
void set_storage_root(std::filesystem::path const & root)
{
global_storage_root = root;
}
}

View file

@ -1,5 +1,6 @@
#include <psemek/app/application.hpp>
#include <psemek/app/resource.hpp>
#include <psemek/app/storage.hpp>
#include <psemek/gfx/init.hpp>
#include <psemek/sdl2/init.hpp>
#include <psemek/sdl2/window.hpp>
@ -56,6 +57,10 @@ int main(int argc, char ** argv) try
// In web builds, resources are stored in
// the Emscripten virtual filesystem
app::set_resource_root("/resources");
backend_t opfs = wasmfs_create_opfs_backend();
wasmfs_create_directory("/storage", 0777, opfs);
app::set_storage_root("/storage");
#endif
auto const factory = app::make_application_factory();

View file

@ -0,0 +1,20 @@
#include <psemek/app/storage.hpp>
#include <psemek/io/file_stream.hpp>
#include <psemek/log/log.hpp>
namespace psemek::app
{
std::unique_ptr<io::istream> read_storage(std::filesystem::path const & relative_path)
{
log::info() << "Reading storage " << relative_path;
return std::make_unique<io::file_istream>(app::storage_root() / relative_path);
}
std::unique_ptr<io::ostream> write_storage(std::filesystem::path const & relative_path)
{
log::info() << "Writing storage " << relative_path;
return std::make_unique<io::file_ostream>(app::storage_root() / relative_path);
}
}

View file

@ -8,7 +8,7 @@ if(PSEMEK_PACKAGE_MODE)
set(PSEMEK_PACKAGE_LINK_FLAGS_RAW)
if(PSEMEK_PLATFORM_WEB)
list(APPEND PSEMEK_PACKAGE_LINK_FLAGS_RAW "-sMIN_WEBGL_VERSION=2" "-sNO_DISABLE_EXCEPTION_CATCHING" "-sFULL_ES3")
list(APPEND PSEMEK_PACKAGE_LINK_FLAGS_RAW -sUSE_SDL=2 -sMIN_WEBGL_VERSION=2 -sNO_DISABLE_EXCEPTION_CATCHING -sFULL_ES3 -sWASMFS -sALLOW_MEMORY_GROWTH -sGROWABLE_ARRAYBUFFERS=0 -sASYNCIFY)
list(APPEND PSEMEK_PACKAGE_LINK_FLAGS_RAW "-sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE='$autoResumeAudioContext','$dynCall'")
if(CMAKE_BUILD_TYPE STREQUAL Debug)
list(APPEND PSEMEK_PACKAGE_LINK_FLAGS_RAW "-g")