Refactor directory file-system abstraction in app library

This commit is contained in:
kayomn 2023-02-20 22:29:39 +00:00
parent c4b83082e3
commit fd62ec5966
1 changed files with 45 additions and 19 deletions

View File

@ -12,6 +12,10 @@ import coral.math;
import oar;
namespace app {
struct directory : public coral::fs {
virtual coral::slice<char const> native_path() const = 0;
};
struct file_reader : public coral::file_reader {
enum class [[nodiscard]] close_result {
ok,
@ -25,8 +29,8 @@ namespace app {
not_found,
};
file_reader(coral::fs * fs) : rw_ops{nullptr} {
this->fs = fs;
file_reader(directory * file_directory) : rw_ops{nullptr} {
this->file_directory = file_directory;
}
close_result close() {
@ -48,7 +52,22 @@ namespace app {
default: coral::unreachable();
}
this->rw_ops = ::SDL_RWFromFile(reinterpret_cast<char const *>(this->path_buffer), "r");
// Windows system path max is something like 512 bytes (depending on the version) and
// on Linux it's 4096.
coral::fixed_buffer<4096> path_buffer{0};
if (!path_buffer.write(this->file_directory->native_path().as_bytes()).is_ok())
// What kind of directory path is being used that would be longer than 4096 bytes?
return open_result::not_found;
if (!path_buffer.write(file_path.as_slice().as_bytes()).is_ok())
// This happening is still implausible but slightly more reasonable?
return open_result::not_found;
// No room for zero terminator.
if (path_buffer.is_full()) return open_result::not_found;
this->rw_ops = ::SDL_RWFromFile(reinterpret_cast<char const *>(path_buffer.begin()), "r");
if (this->rw_ops == nullptr) return open_result::not_found;
@ -88,17 +107,13 @@ namespace app {
}
private:
static constexpr coral::usize path_max{4096};
coral::u8 path_buffer[path_max];
coral::fs * fs;
directory * file_directory;
::SDL_RWops * rw_ops;
};
}
struct base_directory : public coral::fs {
struct base_directory : public app::directory {
base_directory() : directory_path{} {
char * const path{::SDL_GetBasePath()};
@ -121,6 +136,10 @@ struct base_directory : public coral::fs {
::SDL_free(this->directory_path.begin());
}
coral::slice<const char> native_path() const override {
return this->directory_path;
}
void read_file(coral::path const & file_path, coral::callable<void(coral::file_reader &)> const & then) override {
if (this->directory_path.length == 0) return;
@ -141,7 +160,7 @@ struct base_directory : public coral::fs {
coral::slice<char> directory_path;
};
struct user_directory : public coral::fs {
struct user_directory : public app::directory {
user_directory(coral::path const & title) : directory_path{} {
char * const path{::SDL_GetPrefPath("ona", title.begin())};
@ -164,6 +183,10 @@ struct user_directory : public coral::fs {
::SDL_free(this->directory_path.begin());
}
coral::slice<const char> native_path() const override {
return this->directory_path;
}
void read_file(coral::path const & file_path, coral::callable<void(coral::file_reader &)> const & then) override {
if (this->directory_path.length == 0) return;
@ -192,10 +215,11 @@ export namespace app {
};
struct system {
system(coral::path const & title) : res{&base, "base_directory.oar"}, user{title} {}
system(coral::path const & title) : user_directory{title},
res_archive{&base_directory, res_archive_path} {}
coral::fs & base_fs() {
return this->base;
app::directory & base_dir() {
return this->base_directory;
}
bool poll() {
@ -209,7 +233,7 @@ export namespace app {
}
coral::fs & res_fs() {
return this->res;
return this->res_archive;
}
void log(app::log_level level, coral::slice<char const> const & message) {
@ -224,11 +248,13 @@ export namespace app {
return this->allocator;
}
coral::fs & user_fs() {
return this->user;
app::directory & user_dir() {
return this->user_directory;
}
private:
static constexpr coral::path res_archive_path{"base.oar"};
::SDL_Event sdl_event;
struct : public coral::allocator {
@ -241,11 +267,11 @@ export namespace app {
}
} allocator;
base_directory base;
struct base_directory base_directory;
user_directory user;
struct user_directory user_directory;
oar::archive res;
oar::archive res_archive;
};
struct graphics {