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#ifdef __APPLE__
7#include <TargetConditionals.h>
8#endif
9
10#include "absl/strings/str_format.h"
11#include "util/file_util.h"
12
13namespace yaze {
14
15std::vector<std::filesystem::path> AssetLoader::GetSearchPaths(
16 const std::string& relative_path) {
17 std::vector<std::filesystem::path> search_paths;
18
19#ifdef __APPLE__
20 // macOS bundle resource paths
21 std::string bundle_root = yaze::util::GetBundleResourcePath();
22
23#if TARGET_OS_IOS == 1
24 // iOS app bundle resources live at the root.
25 search_paths.push_back(std::filesystem::path(bundle_root) / "assets" /
26 relative_path);
27 search_paths.push_back(std::filesystem::path(bundle_root) / relative_path);
28#else
29 // Try Contents/Resources first (standard bundle location)
30 search_paths.push_back(std::filesystem::path(bundle_root) / "Contents" /
31 "Resources" / relative_path);
32 search_paths.push_back(std::filesystem::path(bundle_root) / "Contents" /
33 "Resources" / "assets" / relative_path);
34
35 // Try without Contents (if app is at root)
36 search_paths.push_back(std::filesystem::path(bundle_root) / "Resources" /
37 relative_path);
38 search_paths.push_back(std::filesystem::path(bundle_root) / "Resources" /
39 "assets" / relative_path);
40
41 // Assets folder next to the app bundle (nightly installs)
42 search_paths.push_back(std::filesystem::path(bundle_root) / ".." / "assets" /
43 relative_path);
44
45 // Development paths (when running from build dir)
46 search_paths.push_back(std::filesystem::path(bundle_root) / ".." / ".." /
47 ".." / "assets" / relative_path);
48 search_paths.push_back(std::filesystem::path(bundle_root) / ".." / ".." /
49 ".." / ".." / "assets" / relative_path);
50#endif
51#endif
52
53 // Standard relative paths (works for all platforms)
54 search_paths.push_back(std::filesystem::path("assets") / relative_path);
55 search_paths.push_back(std::filesystem::path("../assets") / relative_path);
56 search_paths.push_back(std::filesystem::path("../../assets") / relative_path);
57 search_paths.push_back(std::filesystem::path("../../../assets") /
58 relative_path);
59 search_paths.push_back(std::filesystem::path("../../../../assets") /
60 relative_path);
61
62 // Build directory paths
63 search_paths.push_back(std::filesystem::path("build/assets") / relative_path);
64 search_paths.push_back(std::filesystem::path("../build/assets") /
65 relative_path);
66
67 return search_paths;
68}
69
70absl::StatusOr<std::filesystem::path> AssetLoader::FindAssetFile(
71 const std::string& relative_path) {
72 auto search_paths = GetSearchPaths(relative_path);
73
74 for (const auto& path : search_paths) {
75 if (std::filesystem::exists(path)) {
76 return path;
77 }
78 }
79
80 // Debug: Print searched paths
81 std::string searched_paths;
82 for (const auto& path : search_paths) {
83 searched_paths += "\n - " + path.string();
84 }
85
86 return absl::NotFoundError(
87 absl::StrFormat("Asset file not found: %s\nSearched paths:%s",
88 relative_path, searched_paths));
89}
90
91absl::StatusOr<std::string> AssetLoader::LoadTextFile(
92 const std::string& relative_path) {
93 auto path_result = FindAssetFile(relative_path);
94 if (!path_result.ok()) {
95 return path_result.status();
96 }
97
98 const auto& path = *path_result;
99 std::ifstream file(path);
100 if (!file.is_open()) {
101 return absl::InternalError(
102 absl::StrFormat("Failed to open file: %s", path.string()));
103 }
104
105 std::stringstream buffer;
106 buffer << file.rdbuf();
107 std::string content = buffer.str();
108
109 if (content.empty()) {
110 return absl::InternalError(
111 absl::StrFormat("File is empty: %s", path.string()));
112 }
113
114 return content;
115}
116
117bool AssetLoader::AssetExists(const std::string& relative_path) {
118 return FindAssetFile(relative_path).ok();
119}
120
121} // 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.