Unravel Engine C++ Reference
Loading...
Searching...
No Matches
asset_actions.cpp
Go to the documentation of this file.
1#include "asset_actions.h"
2
5
7#include <logging/logging.h>
9
10#include <algorithm>
11#include <cctype>
12#include <fstream>
13#include <sstream>
14#include <stdexcept>
15
17{
18
19auto resolve_asset_source_path(const std::string& asset_key) -> fs::path
20{
21 return fs::absolute(fs::resolve_protocol(asset_key).string());
22}
23
24auto resolve_asset_source_path(const fs::path& path) -> fs::path
25{
27 {
28 return resolve_asset_source_path(path.generic_string());
29 }
30 return fs::absolute(path);
31}
32
33auto can_reimport(const fs::path& absolute_path) -> bool
34{
35 fs::error_code ec;
36 if(!fs::exists(absolute_path, ec) || !fs::is_regular_file(absolute_path, ec))
37 {
38 return false;
39 }
40
41 const auto extension = absolute_path.extension().string();
42 for(const auto& formats : ex::get_all_formats())
43 {
44 for(const auto& format : formats)
45 {
46 if(format == extension)
47 {
48 return true;
49 }
50 }
51 }
52
53 return false;
54}
55
56void reimport_path(const fs::path& absolute_path)
57{
58 const fs::path resolved = resolve_asset_source_path(absolute_path);
59 fs::error_code ec;
60 if(!fs::exists(resolved, ec) || !fs::is_regular_file(resolved, ec))
61 {
62 APPLOG_WARNING("Reimport skipped: '{}' is not a regular file", resolved.generic_string());
63 return;
64 }
65
66 fs::watcher::touch(resolved, false);
67}
68
69void reimport_key(const std::string& asset_key)
70{
72}
73
74auto is_valid_csharp_identifier(const std::string& name) -> bool
75{
76 if(name.empty() || (std::isdigit(static_cast<unsigned char>(name.front())) != 0))
77 {
78 return false;
79 }
80 return std::all_of(name.begin(),
81 name.end(),
82 [](unsigned char c)
83 {
84 return std::isalnum(c) != 0 || c == '_';
85 });
86}
87
88auto create_script_from_template(const fs::path& template_path, const fs::path& dst, std::string* error) -> bool
89{
90 std::ifstream input(template_path, std::ios::binary);
91 if(!input.is_open())
92 {
93 const auto msg = "Script template not found: " + template_path.generic_string();
94 APPLOG_ERROR("{}", msg);
95 if(error)
96 {
97 *error = msg;
98 }
99 return false;
100 }
101
102 std::ostringstream buffer;
103 buffer << input.rdbuf();
104 auto content = buffer.str();
105 string_utils::alterable::replace(content, "#SCRIPTNAME#", dst.stem().string());
106
107 fs::error_code err;
108 const auto absolute = fs::absolute(dst);
109 fs::create_directories(absolute.parent_path(), err);
110 err.clear();
111
112 try
113 {
115 absolute,
116 [&](const fs::path& temp)
117 {
118 std::ofstream output(temp, std::ios::binary | std::ios::trunc);
119 if(!output.is_open())
120 {
121 throw std::runtime_error("Failed to open temp file for write: " + temp.generic_string());
122 }
123 output.write(content.data(), static_cast<std::streamsize>(content.size()));
124 if(!output.good())
125 {
126 throw std::runtime_error("Failed writing temp file: " + temp.generic_string());
127 }
128 },
129 err);
130 }
131 catch(const std::exception& ex)
132 {
133 const auto msg = std::string(ex.what());
134 APPLOG_ERROR("{}", msg);
135 if(error)
136 {
137 *error = msg;
138 }
139 return false;
140 }
141
142 if(err)
143 {
144 const auto msg = "Atomic write failed: " + absolute.generic_string() + " (" + err.message() + ")";
145 APPLOG_ERROR("{}", msg);
146 if(error)
147 {
148 *error = msg;
149 }
150 return false;
151 }
152
153 return true;
154}
155
156} // namespace unravel::asset_actions
gfx::texture_format format
static void touch(const fs::path &path, bool recursive, fs::file_time_type time=fs::now())
Sets the last modification time of a file or directory. by default sets the time to the current time.
Definition watcher.cpp:57
std::string name
Definition hub.cpp:33
#define APPLOG_WARNING(...)
Definition logging.h:19
#define APPLOG_ERROR(...)
Definition logging.h:20
std::string error
Definition mcp_async.cpp:33
auto get_all_formats() -> const std::vector< std::vector< 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 ...
bool has_known_protocol(const path &_path)
Checks whether the path has a known protocol.
void replace(std::string &str, const std::string &search, const std::string &replace)
Definition utils.cpp:105
auto is_valid_csharp_identifier(const std::string &name) -> bool
auto resolve_asset_source_path(const std::string &asset_key) -> fs::path
void reimport_key(const std::string &asset_key)
auto create_script_from_template(const fs::path &template_path, const fs::path &dst, std::string *error) -> bool
Instantiate a ScriptComponent template (.cs.in), substituting #SCRIPTNAME# with the destination file ...
void reimport_path(const fs::path &absolute_path)
auto can_reimport(const fs::path &absolute_path) -> bool
void atomic_write_file(const fs::path &dst, const std::function< void(const fs::path &)> &callback, fs::error_code &ec) noexcept