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