8#include <entt/locator/locator.hpp>
11#include <tinyfiledialogs.h>
22#ifdef GALAXY_WIN_PLATFORM
39 PHYSFS_Allocator a = {};
44 a.Realloc = mi_realloc;
50 PHYSFS_permitSymbolicLinks(
false);
53 write_dir.make_preferred();
57 read_dir.make_preferred();
88 PHYSFS_File* f = PHYSFS_openRead(file.c_str());
91 const auto len = PHYSFS_fileLength(f);
124 PHYSFS_File* f = PHYSFS_openRead(file.c_str());
127 const auto len = PHYSFS_fileLength(f);
131 std::vector<std::uint8_t> buffer;
160 return write_raw(data.data(), data.size() *
sizeof(
char), file);
165 return write_raw(data.data(), data.size_bytes(), file);
170 auto path = std::filesystem::path(file);
171 if (path.is_absolute())
173 path = path.filename();
176 PHYSFS_File* f = PHYSFS_openWrite(path.string().c_str());
179 const auto len = PHYSFS_fileLength(f);
203 if (extension.has_value())
208 ray::Image image = ray::LoadImageFromMemory(extension.value().c_str(), data.data(),
static_cast<int>(data.size()));
211 return std::make_optional(image);
234 if (extension.has_value())
239 ray::Image image = ray::LoadImageAnimFromMemory(extension.value().c_str(), data.data(),
static_cast<int>(data.size()), frames);
242 return std::make_optional(image);
265 if (image.has_value())
267 ray::Texture2D texture = ray::LoadTextureFromImage(image.value());
270 ray::UnloadImage(image.value());
271 return std::make_optional(texture);
289 if (extension.has_value())
294 ray::Wave wave = ray::LoadWaveFromMemory(extension.value().c_str(), data.data(),
static_cast<int>(data.size()));
297 return std::make_optional(wave);
320 if (extension.has_value())
325 ray::Music music = ray::LoadMusicStreamFromMemory(extension.value().c_str(), data.data(),
static_cast<int>(data.size()));
328 return std::make_optional(music);
351 if (extension.has_value())
356 ray::Font font = ray::LoadFontFromMemory(
357 extension.value().c_str(),
359 static_cast<int>(data.size()),
362 static_cast<int>(font_chars.size())
364 if (font.texture.id != 0)
366 return std::make_optional(font);
401 return PHYSFS_exists(file.c_str());
406 return PHYSFS_isDirectory(path.c_str());
411 std::vector<std::string>
list;
415 [](
void* data,
const char* origdir,
const char* fname) -> PHYSFS_EnumerateCallbackResult {
416 if (data != nullptr && fname != nullptr)
418 std::string o = origdir;
419 std::string f = fname;
421 auto* my_list = static_cast<std::vector<std::string>*>(data);
422 my_list->emplace_back(o + f);
425 return PHYSFS_ENUM_OK;
440 std::string tinyfd_icon {magic_enum::enum_name(icon)};
441 tinyfd_notifyPopup(title.c_str(), msg.c_str(), tinyfd_icon.c_str());
446 std::string tinyfd_type {magic_enum::enum_name(type)};
447 std::string tinyfd_icon {magic_enum::enum_name(icon)};
449 return tinyfd_messageBox(title.c_str(), msg.c_str(), tinyfd_type.c_str(), tinyfd_icon.c_str(),
static_cast<int>(btn));
452 std::string
VirtualFileSystem::input_box(
const std::string& title,
const std::string& msg,
const std::string& default_text,
const bool password)
noexcept
454 const char* dt = password ? nullptr : default_text.c_str();
455 return tinyfd_inputBox(title.c_str(), msg.c_str(), dt);
460 const char*
const* filter_patterns = (filters.size() > 0) ? filters.data() :
nullptr;
461 const char* result = tinyfd_saveFileDialog(
"Save file", default_filename.c_str(),
static_cast<int>(filters.size()), filter_patterns,
nullptr);
463 if (result !=
nullptr)
477 const char*
const* filter_patterns = (filters.size() > 0) ? filters.data() :
nullptr;
478 const char* result = tinyfd_openFileDialog(
"Open file", default_path.c_str(),
static_cast<int>(filters.size()), filter_patterns,
"Select a file",
false);
480 if (result !=
nullptr)
493 const char* result = tinyfd_selectFolderDialog(
"Select folder", default_path.c_str());
495 if (result !=
nullptr)
506#ifdef GALAXY_WIN_PLATFORM
#define GALAXY_LOG(level, msg,...)
#define GALAXY_DISABLE_WARNING_POP
#define GALAXY_DISABLE_WARNING(x)
#define GALAXY_DISABLE_WARNING_PUSH
Macro for windows platform detection.
void alert() noexcept
Trigger a standard filesystem audio alert.
int message_box(const std::string &title, const std::string &msg, const DialogType type, const DialogIcon icon, const DialogButton btn) noexcept
Opens a platform specific message box.
~VirtualFileSystem() noexcept
Destructor.
bool is_dir(const std::string &path) noexcept
Checks if a file is a folder.
bool write_binary(std::span< std::uint8_t > data, const std::string &file)
Writes a binary file to disk.
std::vector< std::string > list(const std::string &dir)
Get assets in an asset folder.
std::optional< ray::Image > load_ray_image_anim(const std::string &filename, int *frames) noexcept
Load an animated image from VFS (e.g. GIF). You must manually free the data after.
std::string read(const std::string &file)
Read a file.
std::optional< ray::Wave > load_ray_wave(const std::string &filename) noexcept
Load wave data from VFS. You must manually free the data after.
std::optional< ray::Texture2D > load_ray_texture(const std::string &filename) noexcept
Load a texture from VFS. You must manually free the data after.
bool write_raw(const void *data, const std::size_t size, const std::string &file)
Raw data writing.
bool write(const std::string &data, const std::string &file)
Writes a file to disk.
void notification(const std::string &title, const std::string &msg, const DialogIcon icon) noexcept
Trigger a system notification.
std::string input_box(const std::string &title, const std::string &msg, const std::string &default_text="", const bool password=false) noexcept
Opens a platform specific text input box.
bool exists(const std::string &file) noexcept
Does the file exist in the vfs.
std::string open_file_dialog(const std::vector< const char * > &filters={}, const std::string &def_path="")
Open a file dialog.
std::optional< ray::Image > load_ray_image(const std::string &filename) noexcept
Load an image from VFS. You must manually free the data after.
std::optional< ray::Music > load_ray_music_stream(const std::string &filename) noexcept
Load module music from VFS. You must manually free the data after.
std::string open_save_dialog(const std::string &default_filename, const std::vector< const char * > &filters={})
Open a save file dialog.
std::string select_folder_dialog(const std::string &def_path="")
Select a folder using a dialog.
void remove(const std::string &path) noexcept
Delete a file or folder.
std::optional< ray::Font > load_ray_font(const std::string &filename, int font_size, std::span< int > font_chars) noexcept
Load font from VFS. You must manually free the data after.
VirtualFileSystem() noexcept
Constructor.
std::vector< std::uint8_t > read_binary(const std::string &file)
Read a binary file.
void mkdir(const std::string &dir) noexcept
Creates an empty folder in the filesystem relative to the root.
std::optional< std::string > extension(const std::string &filepath) noexcept
Get a file or path's extension.
bool physfs_check(const int code) noexcept
Call a physfs function with error handling and logs a message for you.
DialogButton
Type of button for tinyfd boxes.
DialogType
Type of dialog box for use with tinyfd.
DialogIcon
Type of native system icon to display on file dialogs.
static auto root_dir() noexcept -> std::filesystem::path
Current root directory of application, unless it has been changed.
static auto assets_dir_prefabs() noexcept -> const std::string &
Prefab asset location.
static auto assets_dir_texture() noexcept -> const std::string &
Textures asset location.
static auto assets_dir_voice() noexcept -> const std::string &
Voice asset location.
static auto assets_dir_shaders() noexcept -> const std::string &
Shaders asset location.
static auto assets_dir_ui() noexcept -> const std::string &
UI asset location.
static auto use_loose_assets() noexcept -> bool
Should asset data be read from pack or assets dir.
static auto assets_dir_animation() noexcept -> const std::string &
Animation data location.
static auto assets_dir_script() noexcept -> const std::string &
Scripts asset location.
static auto assets_dir_music() noexcept -> const std::string &
Music asset location.
static auto assets_dir() noexcept -> std::filesystem::path
Main data directory.
static auto asset_pack() noexcept -> const std::string &
Name of packed assets file.
static auto assets_dir_sfx() noexcept -> const std::string &
SFX asset location.
static auto assets_dir_maps() noexcept -> const std::string &
Maps asset location.
static auto assets_dir_video() noexcept -> const std::string &
Video asset location.
static auto assets_dir_font() noexcept -> const std::string &
Font asset location.