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 "tools/cpp/runfiles/runfiles.h"
6
7#include <filesystem>
8#include <memory>
9#include <optional>
10#include <string>
11#include <string_view>
12
13namespace pixelbullet
14{
16{
17public:
18 static constexpr std::string_view assets_alias = "assets";
19 static constexpr std::string_view shared_alias = "shared";
20
21 struct Config
22 {
23 std::string workspace = "pixelbullet";
24 std::filesystem::path asset_base_path;
25 std::filesystem::path workspace_root;
26 std::filesystem::path packaged_runtime_root;
27 };
28
29 explicit Filesystem(std::unique_ptr<bazel::tools::cpp::runfiles::Runfiles> runfiles);
30 explicit Filesystem(std::unique_ptr<bazel::tools::cpp::runfiles::Runfiles> runfiles, Config config);
31 ~Filesystem() = default;
32
33 Filesystem(const Filesystem&) = delete;
34 Filesystem& operator=(const Filesystem&) = delete;
35 Filesystem(Filesystem&&) = delete;
36 Filesystem& operator=(Filesystem&&) = delete;
37
38 [[nodiscard]] std::optional<std::filesystem::path> TryResolve(const VirtualPath& path) const;
39 [[nodiscard]] std::filesystem::path Resolve(const VirtualPath& path) const;
40 [[nodiscard]] std::filesystem::path ResolveWritable(const VirtualPath& path) const;
41 [[nodiscard]] bool Exists(const VirtualPath& path) const;
42
43 [[nodiscard]] const std::filesystem::path& GetAssetBasePath() const noexcept
44 {
45 return asset_base_path_;
46 }
47
48 [[nodiscard]] const std::filesystem::path& GetWorkspaceRoot() const noexcept
49 {
50 return workspace_root_;
51 }
52
53 [[nodiscard]] const std::filesystem::path& GetPackagedRuntimeRoot() const noexcept
54 {
55 return packaged_runtime_root_;
56 }
57
58private:
59 enum class VirtualRoot
60 {
61 Direct,
62 Assets,
63 Shared,
64 };
65
66 struct ExpandedPath
67 {
68 std::filesystem::path path;
69 VirtualRoot root = VirtualRoot::Direct;
70 };
71
72 [[nodiscard]] std::optional<ExpandedPath> ExpandPath(const VirtualPath& path) const;
73 [[nodiscard]] std::filesystem::path BuildRuntimeRelativePath(const std::filesystem::path& logicalPath, VirtualRoot root) const;
74 [[nodiscard]] std::filesystem::path BuildRunfilesLookup(const std::filesystem::path& logicalPath, VirtualRoot root) const;
75 [[nodiscard]] std::filesystem::path BuildPackagedLookup(const std::filesystem::path& logicalPath, VirtualRoot root) const;
76 [[nodiscard]] static std::filesystem::path DetectWorkspaceRoot(const std::filesystem::path& startPath);
77
78private:
79 std::unique_ptr<bazel::tools::cpp::runfiles::Runfiles> runfiles_;
80 std::string workspace_name_;
81 std::filesystem::path asset_base_path_;
82 std::filesystem::path workspace_root_;
83 std::filesystem::path packaged_runtime_root_;
84};
85} // namespace pixelbullet
Definition filesystem.h:16
Definition virtual_path.h:10
Definition filesystem.h:22