NoJobEngine 2.0.7
Loading...
Searching...
No Matches
NeuralAssetPaths.h
Go to the documentation of this file.
1
6#pragma once
8#include <algorithm>
9#include <cctype>
10#include <filesystem>
11#include <stdexcept>
12#include <string>
13#include <vector>
14
15namespace NoJob {
16inline std::filesystem::path NeuralAssetDirectory() {
17 // Use the same project-root resolution as Behavior Trees.
18 const auto root = BTPathsDetail::FindProjectRoot();
19 if (!root.empty()) return root / "Assets" / "Neural";
20 std::error_code ec;
21 const auto cwd = std::filesystem::current_path(ec);
22 if (ec) return std::filesystem::path("Assets") / "Neural";
23 for (auto base = cwd;; base = base.parent_path()) {
24 const auto folder = base / "Assets" / "Neural";
25 if (std::filesystem::is_directory(folder, ec)) return folder;
26 if (base == base.root_path() || base == base.parent_path()) break;
27 }
28 return cwd / "Assets" / "Neural";
29}
30inline bool IsValidNeuralAssetName(const std::string& name) {
31 return IsValidBTAssetName(name);
32}
33inline std::filesystem::path NeuralAssetPath(const std::string& name) {
34 if (!IsValidNeuralAssetName(name))
35 throw std::invalid_argument("Invalid neural network name");
36 return NeuralAssetDirectory() / (name + ".njn");
37}
38inline std::vector<std::string> ListNeuralAssetNames() {
39 std::vector<std::string> names;
40 const auto folder = NeuralAssetDirectory();
41 std::error_code ec;
42 if (!std::filesystem::is_directory(folder, ec)) return names;
43 for (std::filesystem::directory_iterator it(folder, std::filesystem::directory_options::skip_permission_denied, ec), end;
44 it != end && !ec; it.increment(ec)) {
45 if (!it->is_regular_file(ec)) continue;
46 std::string ext = it->path().extension().string();
47 std::transform(ext.begin(), ext.end(), ext.begin(),
48 [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
49 if (ext == ".njn") names.push_back(it->path().stem().string());
50 }
51 std::sort(names.begin(), names.end());
52 names.erase(std::unique(names.begin(), names.end()), names.end());
53 return names;
54}
55} // namespace NoJob
BTAssetPaths declarations for the Behavior Trees subsystem.
fs::path FindProjectRoot()
bool IsValidBTAssetName(const std::string &name)
std::filesystem::path NeuralAssetDirectory()
bool IsValidNeuralAssetName(const std::string &name)
std::vector< std::string > ListNeuralAssetNames()
std::filesystem::path NeuralAssetPath(const std::string &name)