PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
node_asset_io_internal.h
1#pragma once
2
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"
10
11#include <filesystem>
12#include <string>
13#include <string_view>
14#include <system_error>
15
16namespace pixelbullet::serialization_internal
17{
18inline std::string asset_label_string(const std::string_view asset_label)
19{
20 ASSERT(!asset_label.empty(), "Asset IO helper labels must not be empty");
21 return std::string(asset_label);
22}
23
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)
26{
27 const std::string label = asset_label_string(asset_label);
28 if (path.empty())
29 {
30 return { false, "Path for " + label + " is empty." };
31 }
32
33 const auto resolved_path = filesystem.try_resolve(path);
34 if (!resolved_path)
35 {
36 return { false, label + " file '" + path.logical_path() + "' not found." };
37 }
38
39 physical_path = *resolved_path;
40 return { true, {} };
41}
42
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)
45{
46 const std::string label = asset_label_string(asset_label);
47 if (path.empty())
48 {
49 return { false, "Path for " + label + " is empty." };
50 }
51
52 physical_path = filesystem.resolve_writable(path);
53 if (physical_path.empty())
54 {
55 return { false, "Failed to resolve " + label + " path '" + path.logical_path() + "'." };
56 }
57
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();
60 if (!parent.empty())
61 {
62 std::error_code error_code;
63 std::filesystem::create_directories(parent, error_code);
64 if (error_code)
65 {
66 return {
67 false,
68 "Failed to create " + label + " parent directory '" + parent.string() + "': " + error_code.message(),
69 };
70 }
71 }
72
73 return { true, {} };
74}
75
76inline SerializationResult read_virtual_node_asset(Node& node, const Filesystem& filesystem, const VirtualPath& path,
77 const std::string_view asset_label)
78{
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);
82 if (!resolve_result)
83 {
84 return resolve_result;
85 }
86
87 const SerializationResult read_result = serialization::read_node_file(node, physical_path);
88 if (!read_result)
89 {
90 return { false, "Failed to load " + label + " '" + path.logical_path() + "': " + read_result.error_message };
91 }
92
93 return read_result;
94}
95
96inline SerializationResult write_virtual_node_asset(const Node& node, const Filesystem& filesystem, const VirtualPath& path,
97 const std::string_view asset_label)
98{
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);
102 if (!prepare_result)
103 {
104 return prepare_result;
105 }
106
107 const SerializationResult write_result = serialization::write_node_file(node, physical_path);
108 if (!write_result)
109 {
110 return { false, "Failed to save " + label + " '" + path.logical_path() + "': " + write_result.error_message };
111 }
112
113 return write_result;
114}
115} // namespace pixelbullet::serialization_internal
Provides assertion and panic mechanisms with optional custom formatting.
#define ASSERT(condition,...)
Asserts that a condition is true.
Definition assert.h:142