yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
asset_loader.cc
Go to the documentation of this file.
2
3#include <fstream>
4#include <sstream>
5
6#include "absl/strings/str_format.h"
7#include "util/file_util.h"
8
9namespace yaze {
10
11std::vector<std::filesystem::path> AssetLoader::GetSearchPaths(
12 const std::string& relative_path) {
13 std::vector<std::filesystem::path> search_paths;
14
15#ifdef __APPLE__
16 // macOS bundle resource paths
17 std::string bundle_root = yaze::util::GetBundleResourcePath();
18
19 // Try Contents/Resources first (standard bundle location)
20 search_paths.push_back(std::filesystem::path(bundle_root) / "Contents" /
21 "Resources" / relative_path);
22
23 // Try without Contents (if app is at root)
24 search_paths.push_back(std::filesystem::path(bundle_root) / "Resources" /
25 relative_path);
26
27 // Development paths (when running from build dir)
28 search_paths.push_back(std::filesystem::path(bundle_root) / ".." / ".." /
29 ".." / "assets" / relative_path);
30 search_paths.push_back(std::filesystem::path(bundle_root) / ".." / ".." /
31 ".." / ".." / "assets" / relative_path);
32#endif
33
34 // Standard relative paths (works for all platforms)
35 search_paths.push_back(std::filesystem::path("assets") / relative_path);
36 search_paths.push_back(std::filesystem::path("../assets") / relative_path);
37 search_paths.push_back(std::filesystem::path("../../assets") / relative_path);
38 search_paths.push_back(std::filesystem::path("../../../assets") /
39 relative_path);
40 search_paths.push_back(std::filesystem::path("../../../../assets") /
41 relative_path);
42
43 // Build directory paths
44 search_paths.push_back(std::filesystem::path("build/assets") / relative_path);
45 search_paths.push_back(std::filesystem::path("../build/assets") /
46 relative_path);
47
48 return search_paths;
49}
50
51absl::StatusOr<std::filesystem::path> AssetLoader::FindAssetFile(
52 const std::string& relative_path) {
53 auto search_paths = GetSearchPaths(relative_path);
54
55 for (const auto& path : search_paths) {
56 if (std::filesystem::exists(path)) {
57 return path;
58 }
59 }
60
61 // Debug: Print searched paths
62 std::string searched_paths;
63 for (const auto& path : search_paths) {
64 searched_paths += "\n - " + path.string();
65 }
66
67 return absl::NotFoundError(
68 absl::StrFormat("Asset file not found: %s\nSearched paths:%s",
69 relative_path, searched_paths));
70}
71
72absl::StatusOr<std::string> AssetLoader::LoadTextFile(
73 const std::string& relative_path) {
74 auto path_result = FindAssetFile(relative_path);
75 if (!path_result.ok()) {
76 return path_result.status();
77 }
78
79 const auto& path = *path_result;
80 std::ifstream file(path);
81 if (!file.is_open()) {
82 return absl::InternalError(
83 absl::StrFormat("Failed to open file: %s", path.string()));
84 }
85
86 std::stringstream buffer;
87 buffer << file.rdbuf();
88 std::string content = buffer.str();
89
90 if (content.empty()) {
91 return absl::InternalError(
92 absl::StrFormat("File is empty: %s", path.string()));
93 }
94
95 return content;
96}
97
98bool AssetLoader::AssetExists(const std::string& relative_path) {
99 return FindAssetFile(relative_path).ok();
100}
101
102} // namespace yaze
static absl::StatusOr< std::string > LoadTextFile(const std::string &relative_path)
static absl::StatusOr< std::filesystem::path > FindAssetFile(const std::string &relative_path)
static std::vector< std::filesystem::path > GetSearchPaths(const std::string &relative_path)
static bool AssetExists(const std::string &relative_path)
std::string GetBundleResourcePath()
GetBundleResourcePath returns the path to the bundle resource directory. Specific to MacOS.