#pragma once #include #include #include #include #include "ini.h" #include "resource.h" /* Resources are cached. * A resource of same file with different * type, is considered a seperate resource. * * Each resource needs to inherit from * resource, and have a constructor like this: * (resource_manager& rm, ini_category const* ini, std::string const& path, std::vector const& data) */ struct resource_manager { resource_manager(int argc, char** argv); void mount(std::string const& path, std::string const& mounting_point); template T const* get(std::filesystem::path const& path); private: std::vector read(std::filesystem::path const& path); bool resource_exists(std::filesystem::path const& path); std::map> _map; }; template T const* resource_manager::get(std::filesystem::path const& path) { static_assert(std::is_base_of::value); if(!_map[typeid(T).name()].contains(path.string())) { ini const* ini = 0; if(path.filename() != "assets.ini") { std::filesystem::path ini_path = path; ini_path.replace_filename("assets.ini"); if(resource_exists(ini_path)) { ini = get<::ini>(ini_path); } } ini_category const* cat = &ini_category::empty; if(ini) cat = ini->get_category(path.filename().string()); _map[typeid(T).name()].emplace(path.string(), new T(*this, cat, path.string(), read(path))); } return static_cast(_map[typeid(T).name()].at(path.string())); }