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
33 // Try without Contents (if app is at root)
34 search_paths.push_back(std::filesystem::path(bundle_root) / "Resources" /
35 relative_path);
36
37 // Development paths (when running from build dir)
38 search_paths.push_back(std::filesystem::path(bundle_root) / ".." / ".." /
39 ".." / "assets" / relative_path);
40 search_paths.push_back(std::filesystem::path(bundle_root) / ".." / ".." /
41 ".." / ".." / "assets" / relative_path);
42#endif
43#endif
44
45 // Standard relative paths (works for all platforms)
46 search_paths.push_back(std::filesystem::path("assets") / relative_path);
47 search_paths.push_back(std::filesystem::path("../assets") / relative_path);
48 search_paths.push_back(std::filesystem::path("../../assets") / relative_path);
49 search_paths.push_back(std::filesystem::path("../../../assets") /
50 relative_path);
51 search_paths.push_back(std::filesystem::path("../../../../assets") /
52 relative_path);
53
54 // Build directory paths
55 search_paths.push_back(std::filesystem::path("build/assets") / relative_path);
56 search_paths.push_back(std::filesystem::path("../build/assets") /
57 relative_path);
58
59 return search_paths;
60}
61
62absl::StatusOr<std::filesystem::path> AssetLoader::FindAssetFile(
63 const std::string& relative_path) {
64 auto search_paths = GetSearchPaths(relative_path);
65
66 for (const auto& path : search_paths) {
67 if (std::filesystem::exists(path)) {
68 return path;
69 }
70 }
71
72 // Debug: Print searched paths
73 std::string searched_paths;
74 for (const auto& path : search_paths) {
75 searched_paths += "\n - " + path.string();
76 }
77
78 return absl::NotFoundError(
79 absl::StrFormat("Asset file not found: %s\nSearched paths:%s",
80 relative_path, searched_paths));
81}
82
83absl::StatusOr<std::string> AssetLoader::LoadTextFile(
84 const std::string& relative_path) {
85 auto path_result = FindAssetFile(relative_path);
86 if (!path_result.ok()) {
87 return path_result.status();
88 }
89
90 const auto& path = *path_result;
91 std::ifstream file(path);
92 if (!file.is_open()) {
93 return absl::InternalError(
94 absl::StrFormat("Failed to open file: %s", path.string()));
95 }
96
97 std::stringstream buffer;
98 buffer << file.rdbuf();
99 std::string content = buffer.str();
100
101 if (content.empty()) {
102 return absl::InternalError(
103 absl::StrFormat("File is empty: %s", path.string()));
104 }
105
106 return content;
107}
108
109bool AssetLoader::AssetExists(const std::string& relative_path) {
110 return FindAssetFile(relative_path).ok();
111}
112
113} // 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.