NoJobEngine 2.0.7
Loading...
Searching...
No Matches
BTAssetPaths.h
Go to the documentation of this file.
1
6#pragma once
7#include <algorithm>
8#include <cctype>
9#include <filesystem>
10#include <stdexcept>
11#include <string>
12#include <vector>
13
14namespace NoJob {
15namespace BTPathsDetail {
16namespace fs = std::filesystem;
17
18inline fs::path FindProjectRoot() {
19 // The editor may run from out/build/x64-Debug. Never choose that build
20 // folder merely because it contains an old Assets/AI/Default.njbt.
21 std::error_code ec;
22 fs::path base = fs::current_path(ec);
23 if (ec) return {};
24 for (;;) {
25 if (fs::is_regular_file(base / "CMakeLists.txt", ec) &&
26 fs::is_directory(base / "Engine", ec) &&
27 fs::is_directory(base / "Editor", ec))
28 return base;
29 if (base == base.root_path() || base == base.parent_path()) break;
30 base = base.parent_path();
31 }
32 return {};
33}
34inline bool IsTreeFile(const fs::directory_entry& entry) {
35 std::error_code ec;
36 if (!entry.is_regular_file(ec)) return false;
37 std::string ext = entry.path().extension().string();
38 std::transform(ext.begin(), ext.end(), ext.begin(),
39 [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
40 return ext == ".njbt";
41}
42} // namespace BTPathsDetail
43
44inline std::filesystem::path BTAssetDirectory() {
45 namespace fs = std::filesystem;
46 const auto root = BTPathsDetail::FindProjectRoot();
47 if (!root.empty()) return root / "Assets" / "AI";
48
49 // Fallback for standalone distributions without a CMake project root.
50 std::error_code ec;
51 const auto cwd = fs::current_path(ec);
52 if (ec) return fs::path("Assets") / "AI";
53 for (auto base = cwd; ; base = base.parent_path()) {
54 const auto folder = base / "Assets" / "AI";
55 if (fs::is_directory(folder, ec)) return folder;
56 if (base == base.root_path() || base == base.parent_path()) break;
57 base = base.parent_path();
58 }
59 return cwd / "Assets" / "AI";
60}
61
62inline bool IsValidBTAssetName(const std::string& name) {
63 return !name.empty() && name != "." && name != ".." &&
64 name.back() != '.' && name.back() != ' ' &&
65 name.find_first_of("/\\:*?\"<>|") == std::string::npos;
66}
67
68inline std::vector<std::string> ListBTAssetNames() {
69 namespace fs = std::filesystem;
70 std::vector<std::string> names;
71 const auto folder = BTAssetDirectory();
72 std::error_code ec;
73 if (!fs::is_directory(folder, ec)) return names;
74 for (fs::directory_iterator it(folder, fs::directory_options::skip_permission_denied, ec), end;
75 it != end && !ec; it.increment(ec)) {
77 names.push_back(it->path().stem().string());
78 }
79 std::sort(names.begin(), names.end());
80 names.erase(std::unique(names.begin(), names.end()), names.end());
81 return names;
82}
83
84inline std::filesystem::path BTAssetPath(const std::string& name) {
85 if (!IsValidBTAssetName(name))
86 throw std::invalid_argument("Invalid behavior tree asset name");
87 return BTAssetDirectory() / (name + ".njbt");
88}
89} // namespace NoJob
bool IsTreeFile(const fs::directory_entry &entry)
fs::path FindProjectRoot()
bool IsValidBTAssetName(const std::string &name)
std::filesystem::path BTAssetPath(const std::string &name)
std::filesystem::path BTAssetDirectory()
std::vector< std::string > ListBTAssetNames()