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