feat: move editor to project / add post processing etc
This commit is contained in:
@@ -214,6 +214,53 @@ std::optional<std::string> AssetFS::ToVirtualPath(const std::filesystem::path& f
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void AssetFS::SetSourceRoot(std::string_view scheme, std::filesystem::path sourceRoot) {
|
||||
for (FSMount& mount : mounts) {
|
||||
if (mount.scheme == scheme) {
|
||||
mount.sourceRoot = std::filesystem::absolute(std::move(sourceRoot)).lexically_normal();
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw std::runtime_error("asset mount not found: " + std::string{scheme});
|
||||
}
|
||||
|
||||
std::optional<std::filesystem::path> AssetFS::GetSourcePath(std::string_view vpath) const {
|
||||
const ParsedVirtualPath parsed = ParseVirtualPath(vpath);
|
||||
const FSMount* mount = FindMount(mounts, parsed.scheme);
|
||||
if (mount == nullptr || !mount->sourceRoot) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return (*mount->sourceRoot / parsed.relativePath).lexically_normal();
|
||||
}
|
||||
|
||||
bool AssetFS::WriteFile(std::string_view vpath, std::string_view contents) {
|
||||
std::vector<std::filesystem::path> targets{GetFullPath(vpath)};
|
||||
if (auto source = GetSourcePath(vpath)) {
|
||||
targets.push_back(std::move(*source));
|
||||
}
|
||||
|
||||
for (const auto& target : targets) {
|
||||
std::error_code error;
|
||||
std::filesystem::create_directories(target.parent_path(), error);
|
||||
std::filesystem::path temporary = target;
|
||||
temporary += ".tmp";
|
||||
{
|
||||
std::ofstream file(temporary, std::ios::binary | std::ios::trunc);
|
||||
if (!file.write(contents.data(), static_cast<std::streamsize>(contents.size()))) {
|
||||
spdlog::error("Failed to write {}", temporary.string());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
std::filesystem::rename(temporary, target, error);
|
||||
if (error) {
|
||||
spdlog::error("Failed to replace {}: {}", target.string(), error.message());
|
||||
std::filesystem::remove(temporary, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> AssetFS::ReadFile(const std::filesystem::path& fullPath) {
|
||||
std::ifstream file(fullPath, std::ios::binary);
|
||||
if (!file) {
|
||||
|
||||
Reference in New Issue
Block a user