#ifndef _ASSET_H #define _ASSET_H #include #include #include "util.h" namespace Mauri { enum AssetType { ASSET_VOID = 0, ASSET_SLICE, ASSET_FILE }; enum AssetTypeHint { NONE = 0, TEXTURE, SHADER, FRAGMENT_SHADER, VERTEX_SHADER }; class Asset { public: Asset(); Asset(const std::string &path, byte *begin, byte *end); Asset(const std::string &path); ~Asset(); AssetType type; std::string path; AssetTypeHint ltype = NONE; void *lasset = nullptr; u32 size; u64 hash; auto ptr() const -> byte * { return &this->data[this->pos]; } auto seek(s32 n) -> void { this->pos += n; } auto reset() -> void { this->pos = 0; } template auto read() -> T { T val = *((T *)this->ptr()); this->seek(sizeof(T)); return val; } auto reads(u32 n) -> std::string_view { std::string_view s((char *)this->ptr(), n); this->seek(n); return s; } auto as_string() -> const std::string_view & { if (str.empty()) { str = std::string_view((char *)this->ptr(), this->size); } return str; } private: u32 pos = 0; byte *data; std::string_view str; }; struct PackageFile { std::string_view path; u32 offset; u32 length; }; class AssetManager { public: AssetManager() = default; ~AssetManager() = default; bool prepare_output; auto arm_package(const std::string &path, bool can_output) -> bool; auto add_search_directory(const std::string &directory) -> bool; auto get(const std::string &part, AssetTypeHint hint = NONE) -> Asset *; auto dump(const std::string &output_path) -> void; auto unload_package() -> void; private: std::vector directories; Asset *package; std::vector assets; }; extern auto asset_manager() -> AssetManager *; } // namespace Mauri #endif // _ASSET_H