PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
filesystem.h
1#pragma once
2
3#include "pixelbullet/filesystem/asset_path.h"
4#include "pixelbullet/filesystem/virtual_path.h"
5
6#include <filesystem>
7#include <functional>
8#include <optional>
9#include <string>
10#include <string_view>
11
12namespace pixelbullet
13{
14namespace filesystem_internal
15{
17}
18
19class Filesystem
20{
21public:
22 static constexpr std::string_view assets_alias = "assets";
23 static constexpr std::string_view shared_alias = "shared";
24
25 struct Config
26 {
27 std::string workspace = "pixelbullet";
28 std::filesystem::path asset_base_path;
29 std::filesystem::path workspace_root;
30 std::filesystem::path packaged_runtime_root;
31 };
32
33 Filesystem();
34 explicit Filesystem(Config config);
35 ~Filesystem() = default;
36
37 Filesystem(const Filesystem&) = delete;
38 Filesystem& operator=(const Filesystem&) = delete;
39 Filesystem(Filesystem&&) = delete;
40 Filesystem& operator=(Filesystem&&) = delete;
41
42 [[nodiscard]] std::optional<std::filesystem::path> try_resolve(const VirtualPath& path) const;
43 [[nodiscard]] std::optional<std::filesystem::path> try_resolve(const AssetPath& path) const;
44 [[nodiscard]] std::filesystem::path resolve(const VirtualPath& path) const;
45 [[nodiscard]] std::filesystem::path resolve(const AssetPath& path) const;
46 [[nodiscard]] std::filesystem::path resolve_writable(const VirtualPath& path) const;
47 [[nodiscard]] std::filesystem::path resolve_writable(const AssetPath& path) const;
48 [[nodiscard]] bool exists(const VirtualPath& path) const;
49 [[nodiscard]] bool exists(const AssetPath& path) const;
50
51 [[nodiscard]] const std::filesystem::path& asset_base_path() const noexcept
52 {
53 return asset_base_path_;
54 }
55
56 [[nodiscard]] const std::filesystem::path& workspace_root() const noexcept
57 {
58 return workspace_root_;
59 }
60
61 [[nodiscard]] const std::filesystem::path& packaged_runtime_root() const noexcept
62 {
63 return packaged_runtime_root_;
64 }
65
66private:
67 using RuntimeLookup = std::function<std::optional<std::filesystem::path>(const std::filesystem::path&)>;
68 friend class filesystem_internal::FilesystemRuntimeAccess;
69
70 explicit Filesystem(Config config, RuntimeLookup runtime_lookup);
71
72 enum class VirtualRoot
73 {
74 Direct,
75 Assets,
76 Shared,
77 };
78
79 struct ExpandedPath
80 {
81 std::filesystem::path path;
82 VirtualRoot root = VirtualRoot::Direct;
83 };
84
85 [[nodiscard]] std::optional<ExpandedPath> expand_path(const VirtualPath& path) const;
86 [[nodiscard]] std::filesystem::path build_runtime_relative_path(const std::filesystem::path& logical_path, VirtualRoot root) const;
87 [[nodiscard]] std::filesystem::path build_runtime_lookup_path(const std::filesystem::path& logical_path, VirtualRoot root) const;
88 [[nodiscard]] std::filesystem::path build_packaged_lookup(const std::filesystem::path& logical_path, VirtualRoot root) const;
89 [[nodiscard]] static std::filesystem::path detect_workspace_root(const std::filesystem::path& start_path);
90
91private:
92 RuntimeLookup runtime_lookup_;
93 std::string workspace_name_;
94 std::filesystem::path asset_base_path_;
95 std::filesystem::path workspace_root_;
96 std::filesystem::path packaged_runtime_root_;
97};
98} // namespace pixelbullet
Definition asset_path.h:28
Definition virtual_path.h:10
Definition filesystem_runtime_access_internal.h:11
Definition filesystem.h:26