4#include "pixelbullet/filesystem/filesystem.h"
5#include "pixelbullet/filesystem/native_path.h"
6#include "pixelbullet/filesystem/virtual_path.h"
7#include "pixelbullet/serialization/node.h"
8#include "pixelbullet/serialization/node_file_io.h"
9#include "pixelbullet/serialization/serialization_result.h"
14#include <system_error>
16namespace pixelbullet::serialization_internal
18inline std::string asset_label_string(
const std::string_view asset_label)
20 ASSERT(!asset_label.empty(),
"Asset IO helper labels must not be empty");
21 return std::string(asset_label);
24inline SerializationResult resolve_virtual_asset_read_path(std::filesystem::path& physical_path,
const Filesystem& filesystem,
25 const VirtualPath& path,
const std::string_view asset_label)
27 const std::string label = asset_label_string(asset_label);
30 return {
false,
"Path for " + label +
" is empty." };
33 const auto resolved_path = filesystem.try_resolve(path);
36 return {
false, label +
" file '" + path.logical_path() +
"' not found." };
39 physical_path = *resolved_path;
43inline SerializationResult prepare_virtual_asset_write_path(std::filesystem::path& physical_path,
const Filesystem& filesystem,
44 const VirtualPath& path,
const std::string_view asset_label)
46 const std::string label = asset_label_string(asset_label);
49 return {
false,
"Path for " + label +
" is empty." };
52 physical_path = filesystem.resolve_writable(path);
53 if (physical_path.empty())
55 return {
false,
"Failed to resolve " + label +
" path '" + path.logical_path() +
"'." };
58 const std::filesystem::path native_path = filesystem::to_native_file_io_path(physical_path);
59 const std::filesystem::path parent = native_path.parent_path();
62 std::error_code error_code;
63 std::filesystem::create_directories(parent, error_code);
68 "Failed to create " + label +
" parent directory '" + parent.string() +
"': " + error_code.message(),
76inline SerializationResult read_virtual_node_asset(Node& node,
const Filesystem& filesystem,
const VirtualPath& path,
77 const std::string_view asset_label)
79 const std::string label = asset_label_string(asset_label);
80 std::filesystem::path physical_path;
81 const SerializationResult resolve_result = resolve_virtual_asset_read_path(physical_path, filesystem, path, label);
84 return resolve_result;
87 const SerializationResult read_result = serialization::read_node_file(node, physical_path);
90 return {
false,
"Failed to load " + label +
" '" + path.logical_path() +
"': " + read_result.error_message };
96inline SerializationResult write_virtual_node_asset(
const Node& node,
const Filesystem& filesystem,
const VirtualPath& path,
97 const std::string_view asset_label)
99 const std::string label = asset_label_string(asset_label);
100 std::filesystem::path physical_path;
101 const SerializationResult prepare_result = prepare_virtual_asset_write_path(physical_path, filesystem, path, label);
104 return prepare_result;
107 const SerializationResult write_result = serialization::write_node_file(node, physical_path);
110 return {
false,
"Failed to save " + label +
" '" + path.logical_path() +
"': " + write_result.error_message };
Provides assertion and panic mechanisms with optional custom formatting.
#define ASSERT(condition,...)
Asserts that a condition is true.
Definition assert.h:142