Unravel Engine C++ Reference
Loading...
Searching...
No Matches
asset_writer.h
Go to the documentation of this file.
1#pragma once
2#include "../asset_handle.h"
3
4#include <chrono>
5#include <cstddef>
7#include <filesystem>
9
11
12namespace unravel
13{
14namespace asset_writer
15{
16//------------------------------------------------------------------------------
17// Atomically rename src -> dst, overwriting dst if it exists.
18//------------------------------------------------------------------------------
19auto atomic_rename_file(const fs::path& src, const fs::path& dst, fs::error_code& ec) noexcept -> bool;
20auto make_temp_path(const fs::path& dir, fs::path& out, fs::error_code& ec) noexcept -> bool;
21
22//------------------------------------------------------------------------------
23// Atomically copy src -> dst, overwriting dst if it exists.
24//------------------------------------------------------------------------------
25auto atomic_copy_file(const fs::path& src, const fs::path& dst, fs::error_code& ec) noexcept -> bool;
26void atomic_write_file(const fs::path& dst,
27 const std::function<void(const fs::path&)>& callback,
28 fs::error_code& ec) noexcept;
29
30//------------------------------------------------------------------------------
31// Scan a directory for stale temp files (matching `.<UUID>.temp`) and remove any
32// older than `min_age`. Use this at startup to recover from crashes or aborted
33// writes that left orphaned temp files behind.
34//
35// Returns the number of files successfully removed.
36//------------------------------------------------------------------------------
37auto cleanup_stale_temp_files(const fs::path& dir,
38 bool recursive,
39 std::chrono::seconds min_age = std::chrono::seconds(60)) noexcept -> std::size_t;
40
41template<typename T>
42auto resolve_meta_file(const asset_handle<T>& asset) -> fs::path
43{
44 const fs::path& key = asset.id();
45 fs::path absolute_path = fs::convert_to_protocol(key);
46 absolute_path =
48 if(absolute_path.extension() != ".meta")
49 {
50 absolute_path += ".meta";
51 }
52 return absolute_path;
53}
54
55template<typename T>
56auto atomic_save_to_file(const fs::path& key, const asset_handle<T>& obj) -> bool
57{
58 try
59 {
60 fs::path absolute_key = fs::absolute(fs::resolve_protocol(key));
61
62 fs::error_code err;
64 absolute_key,
65 [&](const fs::path& temp)
66 {
67 save_to_file(temp.string(), obj.get());
68 },
69 err);
70
71 return !err;
72 }
73 catch(const std::exception& e)
74 {
75 APPLOG_ERROR("Failed to save object to file: {0}", e.what());
76 return false;
77 }
78}
79
80template<typename T>
81auto atomic_save_to_file(const fs::path& key, const T& obj) -> bool
82{
83 try
84 {
85 fs::path absolute_key = fs::absolute(fs::resolve_protocol(key));
86
87 fs::error_code err;
89 absolute_key,
90 [&](const fs::path& temp)
91 {
92 save_to_file(temp.string(), obj);
93 },
94 err);
95
96 return !err;
97 }
98 catch(const std::exception& e)
99 {
100 APPLOG_ERROR("Failed to save object to file: {0}", e.what());
101 return false;
102 }
103}
104
105} // namespace asset_writer
106} // namespace unravel
#define APPLOG_ERROR(...)
Definition logging.h:20
auto get_data_directory(const std::string &prefix={}) -> std::string
auto get_meta_directory(const std::string &prefix={}) -> std::string
Definition cache.hpp:11
path resolve_protocol(const path &_path)
Given the specified path/filename, resolve the final full filename. This will be based on either the ...
path replace(const path &_path, const path &_sequence, const path &_new_sequence)
Replacing any occurences of the specified path sequence with another.
path convert_to_protocol(const path &_path)
Oposite of the resolve_protocol this function tries to convert to protocol path from an absolute one.
Hash specialization for batch_key to enable use in std::unordered_map.
auto make_temp_path(const fs::path &dir, fs::path &out, fs::error_code &ec) noexcept -> bool
auto atomic_rename_file(const fs::path &src, const fs::path &dst, fs::error_code &ec) noexcept -> bool
auto atomic_save_to_file(const fs::path &key, const asset_handle< T > &obj) -> bool
auto cleanup_stale_temp_files(const fs::path &dir, bool recursive, std::chrono::seconds min_age) noexcept -> std::size_t
auto atomic_copy_file(const fs::path &src, const fs::path &dst, fs::error_code &ec) noexcept -> bool
auto resolve_meta_file(const asset_handle< T > &asset) -> fs::path
void atomic_write_file(const fs::path &dst, const std::function< void(const fs::path &)> &callback, fs::error_code &ec) noexcept
void save_to_file(const std::string &absolute_path, const animation_clip &obj)
Thread-safe handle to an asset.