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
5#include <filesystem>
7
9
10namespace unravel
11{
12namespace asset_writer
13{
14//------------------------------------------------------------------------------
15// Atomically rename src -> dst, overwriting dst if it exists.
16//------------------------------------------------------------------------------
17auto atomic_rename_file(const fs::path& src, const fs::path& dst, fs::error_code& ec) noexcept -> bool;
18auto make_temp_path(const fs::path& dir, fs::path& out, fs::error_code& ec) noexcept -> bool;
19
20//------------------------------------------------------------------------------
21// Atomically copy src -> dst, overwriting dst if it exists.
22//------------------------------------------------------------------------------
23auto atomic_copy_file(const fs::path& src, const fs::path& dst, fs::error_code& ec) noexcept -> bool;
24void atomic_write_file(const fs::path& dst,
25 const std::function<void(const fs::path&)>& callback,
26 fs::error_code& ec) noexcept;
27
28template<typename T>
29auto resolve_meta_file(const asset_handle<T>& asset) -> fs::path
30{
31 const fs::path& key = asset.id();
32 fs::path absolute_path = fs::convert_to_protocol(key);
33 absolute_path =
35 if(absolute_path.extension() != ".meta")
36 {
37 absolute_path += ".meta";
38 }
39 return absolute_path;
40}
41
42template<typename T>
43auto atomic_save_to_file(const fs::path& key, const asset_handle<T>& obj) -> bool
44{
45 try
46 {
47 fs::path absolute_key = fs::absolute(fs::resolve_protocol(key));
48
49 fs::error_code err;
51 absolute_key,
52 [&](const fs::path& temp)
53 {
54 save_to_file(temp.string(), obj.get());
55 },
56 err);
57
58 return !err;
59 }
60 catch(const std::exception& e)
61 {
62 APPLOG_ERROR("Failed to save object to file: {0}", e.what());
63 return false;
64 }
65}
66
67template<typename T>
68auto atomic_save_to_file(const fs::path& key, const T& obj) -> bool
69{
70 try
71 {
72 fs::path absolute_key = fs::absolute(fs::resolve_protocol(key));
73
74 fs::error_code err;
76 absolute_key,
77 [&](const fs::path& temp)
78 {
79 save_to_file(temp.string(), obj);
80 },
81 err);
82
83 return !err;
84 }
85 catch(const std::exception& e)
86 {
87 APPLOG_ERROR("Failed to save object to file: {0}", e.what());
88 return false;
89 }
90}
91
92} // namespace asset_writer
93} // 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
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.
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 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)
Represents a handle to an asset, providing access and management functions.