20 #ifndef ANTKEEPER_RESOURCES_RESOURCE_MANAGER_HPP
21 #define ANTKEEPER_RESOURCES_RESOURCE_MANAGER_HPP
32 #include <unordered_map>
59 bool mount(
const std::filesystem::path& path);
68 bool unmount(
const std::filesystem::path& path);
80 std::shared_ptr<T>
load(
const std::filesystem::path& path);
93 bool save(
const T& resource,
const std::filesystem::path& path)
const;
107 [[nodiscard]]
inline const std::filesystem::path&
get_write_path() const noexcept
120 [[nodiscard]] std::shared_ptr<void> fetch(
const std::filesystem::path& path)
const;
129 [[nodiscard]] std::unique_ptr<deserialize_context> open_read(
const std::filesystem::path& path)
const;
138 [[nodiscard]] std::unique_ptr<serialize_context> open_write(
const std::filesystem::path& path)
const;
140 std::unordered_map<std::filesystem::path, std::weak_ptr<void>> resource_cache;
141 std::filesystem::path write_path;
148 if (
auto resource = fetch(path))
150 return std::static_pointer_cast<T>(resource);
153 const auto path_string = path.string();
160 auto deserialize_ctx = open_read(path);
164 resource_cache[path] = resource;
170 catch (
const std::exception&
e)
181 const auto path_string = path.string();
188 auto serialize_ctx = open_write(path);
196 catch (
const std::exception&
e)
198 debug::log_error(
"Failed to save resource to \"{}\": {}", path_string,
e.what());
static std::unique_ptr< T > load(::resource_manager &resource_manager, deserialize_context &ctx)
Loads a resource.
Manages the loading, caching, and saving of resources.
bool set_write_path(const std::filesystem::path &path)
Sets the path to a directory or archive where files can be written.
bool unmount(const std::filesystem::path &path)
Removes a directory or archive from the search path.
const std::filesystem::path & get_write_path() const noexcept
Returns the path to the directory or archive to which files are written.
bool save(const T &resource, const std::filesystem::path &path) const
Saves a resource to a file.
resource_manager()
Constructs a resource manager.
bool mount(const std::filesystem::path &path)
Adds a directory or archive to the search path.
std::shared_ptr< T > load(const std::filesystem::path &path)
Loads and caches a resource.
~resource_manager()
Destructs a resource manager.
log_message< log_message_severity::trace, Args... > log_trace
Formats and logs a trace message.
log_message< log_message_severity::error, Args... > log_error
Formats and logs an error message.
Specializations of serializer define the serialization process for a given type.
void serialize(const T &value, serialize_context &ctx)
Serializes a value.