Ember
Loading...
Searching...
No Matches
ResourcePath.cpp
Go to the documentation of this file.
1
5
7
8#include <wx/filename.h>
9#include <wx/log.h>
10#include <wx/stdpaths.h>
11#include <wx/utils.h>
12
13namespace EmberForge {
14
16
17wxString ResourcePath::Get(const wxString &relativePath) {
18 wxString base = GetResourcesDir();
19 if (base.IsEmpty()) {
20 wxLogWarning("ResourcePath: Resources directory not found, using relative path");
21 return relativePath;
22 }
23
24 wxString fullPath = base + relativePath;
25
26 // Normalize path separators
27 fullPath.Replace("/", wxString(wxFileName::GetPathSeparator()));
28 fullPath.Replace("\\", wxString(wxFileName::GetPathSeparator()));
29
30 return fullPath;
31}
32
33wxString ResourcePath::GetDir(const wxString &relativeDir) {
34 wxString path = Get(relativeDir);
35
36 // Ensure trailing separator for directories
37 if (!path.IsEmpty() && !path.EndsWith(wxFileName::GetPathSeparator())) {
38 path += wxFileName::GetPathSeparator();
39 }
40
41 return path;
42}
43
45 wxString &cachedDir = GetCachedDir();
46
47 if (!s_initialized) {
48 cachedDir = FindResourcesDir();
49 s_initialized = true;
50
51 if (cachedDir.IsEmpty()) {
52 wxLogWarning("ResourcePath: Could not locate resources directory");
53 }
54 }
55
56 return cachedDir;
57}
58
59bool ResourcePath::Exists(const wxString &relativePath) {
60 wxString fullPath = Get(relativePath);
61 return wxFileName::Exists(fullPath);
62}
63
65 s_initialized = false;
66 GetCachedDir().Clear();
67}
68
70 static wxString cachedDir;
71 return cachedDir;
72}
73
75 wxString resourcesDir;
76
77 // 1. Check environment variable override (highest priority)
78 wxString envPath;
79 if (wxGetEnv("EMBER_RESOURCES", &envPath)) {
80 if (wxFileName::DirExists(envPath)) {
81 wxLogDebug("ResourcePath: Using EMBER_RESOURCES environment variable");
82 resourcesDir = envPath;
83 if (!resourcesDir.EndsWith(wxFileName::GetPathSeparator())) {
84 resourcesDir += wxFileName::GetPathSeparator();
85 }
86 return resourcesDir;
87 } else {
88 wxLogWarning("ResourcePath: EMBER_RESOURCES path does not exist: %s", envPath);
89 }
90 }
91
92 // 2. Get executable directory
93 wxString exePath = wxStandardPaths::Get().GetExecutablePath();
94 wxFileName exeFileName(exePath);
95 wxString exeDir = exeFileName.GetPath();
96
97 // Candidate paths to check (in order of preference)
98 wxArrayString candidates;
99
100 // 2a. resources/ next to executable (installed layout)
101 candidates.Add(exeDir + wxFileName::GetPathSeparator() + "resources" + wxFileName::GetPathSeparator());
102
103 // 2b. ../resources/ from executable (if exe is in bin/)
104 wxFileName parentDir(exeDir);
105 parentDir.RemoveLastDir();
106 candidates.Add(parentDir.GetPath() + wxFileName::GetPathSeparator() + "resources" + wxFileName::GetPathSeparator());
107
108 // 2c. ../../resources/ from executable (if exe is in build/bin/)
109 wxFileName grandparentDir(parentDir.GetPath());
110 grandparentDir.RemoveLastDir();
111 candidates.Add(grandparentDir.GetPath() + wxFileName::GetPathSeparator() + "resources" +
112 wxFileName::GetPathSeparator());
113
114 // 3. Check each candidate
115 for (const wxString &candidate : candidates) {
116 // Verify it's a valid resources directory by checking for a known subdirectory
117 wxString testPath = candidate + "icons";
118 if (wxFileName::DirExists(testPath)) {
119 return candidate;
120 }
121
122 // Also check for config directory as alternative marker
123 testPath = candidate + "config";
124 if (wxFileName::DirExists(testPath)) {
125 return candidate;
126 }
127
128 // Or buttons directory
129 testPath = candidate + "buttons";
130 if (wxFileName::DirExists(testPath)) {
131 return candidate;
132 }
133 }
134
135 // 4. Last resort: check current working directory (legacy behavior)
136 wxString cwdResources = wxGetCwd() + wxFileName::GetPathSeparator() + "resources" + wxFileName::GetPathSeparator();
137 if (wxFileName::DirExists(cwdResources + "icons") || wxFileName::DirExists(cwdResources + "config") ||
138 wxFileName::DirExists(cwdResources + "buttons")) {
139 wxLogWarning("ResourcePath: Falling back to CWD-relative resources (not recommended)");
140 return cwdResources;
141 }
142
143 // Nothing found
144 return wxEmptyString;
145}
146
147} // namespace EmberForge
Centralized resource path management for EmberForge.
static wxString GetResourcesDir()
Get the base resources directory.
static wxString FindResourcesDir()
static wxString & GetCachedDir()
static wxString Get(const wxString &relativePath)
Get the full path to a resource file.
static wxString GetDir(const wxString &relativeDir)
Get the full path to a resource directory.
static void Refresh()
Force re-detection of resources directory.
static bool Exists(const wxString &relativePath)
Check if a resource exists.