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 wxLogMessage("ResourcePath: Using resources directory: %s", cachedDir);
53 } else {
54 wxLogWarning("ResourcePath: Could not locate resources directory");
55 }
56 }
57
58 return cachedDir;
59}
60
61bool ResourcePath::Exists(const wxString &relativePath) {
62 wxString fullPath = Get(relativePath);
63 return wxFileName::Exists(fullPath);
64}
65
67 s_initialized = false;
68 GetCachedDir().Clear();
69}
70
72 static wxString cachedDir;
73 return cachedDir;
74}
75
77 wxString resourcesDir;
78
79 // 1. Check environment variable override (highest priority)
80 wxString envPath;
81 if (wxGetEnv("EMBER_RESOURCES", &envPath)) {
82 if (wxFileName::DirExists(envPath)) {
83 wxLogMessage("ResourcePath: Using EMBER_RESOURCES environment variable");
84 resourcesDir = envPath;
85 if (!resourcesDir.EndsWith(wxFileName::GetPathSeparator())) {
86 resourcesDir += wxFileName::GetPathSeparator();
87 }
88 return resourcesDir;
89 } else {
90 wxLogWarning("ResourcePath: EMBER_RESOURCES path does not exist: %s", envPath);
91 }
92 }
93
94 // 2. Get executable directory
95 wxString exePath = wxStandardPaths::Get().GetExecutablePath();
96 wxFileName exeFileName(exePath);
97 wxString exeDir = exeFileName.GetPath();
98
99 // Candidate paths to check (in order of preference)
100 wxArrayString candidates;
101
102 // 2a. resources/ next to executable (installed layout)
103 candidates.Add(exeDir + wxFileName::GetPathSeparator() + "resources" + wxFileName::GetPathSeparator());
104
105 // 2b. ../resources/ from executable (if exe is in bin/)
106 wxFileName parentDir(exeDir);
107 parentDir.RemoveLastDir();
108 candidates.Add(parentDir.GetPath() + wxFileName::GetPathSeparator() + "resources" + wxFileName::GetPathSeparator());
109
110 // 2c. ../../resources/ from executable (if exe is in build/bin/)
111 wxFileName grandparentDir(parentDir.GetPath());
112 grandparentDir.RemoveLastDir();
113 candidates.Add(grandparentDir.GetPath() + wxFileName::GetPathSeparator() + "resources" +
114 wxFileName::GetPathSeparator());
115
116 // 3. Check each candidate
117 for (const wxString &candidate : candidates) {
118 // Verify it's a valid resources directory by checking for a known subdirectory
119 wxString testPath = candidate + "icons";
120 if (wxFileName::DirExists(testPath)) {
121 return candidate;
122 }
123
124 // Also check for config directory as alternative marker
125 testPath = candidate + "config";
126 if (wxFileName::DirExists(testPath)) {
127 return candidate;
128 }
129
130 // Or buttons directory
131 testPath = candidate + "buttons";
132 if (wxFileName::DirExists(testPath)) {
133 return candidate;
134 }
135 }
136
137 // 4. Last resort: check current working directory (legacy behavior)
138 wxString cwdResources = wxGetCwd() + wxFileName::GetPathSeparator() + "resources" + wxFileName::GetPathSeparator();
139 if (wxFileName::DirExists(cwdResources + "icons") || wxFileName::DirExists(cwdResources + "config") ||
140 wxFileName::DirExists(cwdResources + "buttons")) {
141 wxLogWarning("ResourcePath: Falling back to CWD-relative resources (not recommended)");
142 return cwdResources;
143 }
144
145 // Nothing found
146 return wxEmptyString;
147}
148
149} // 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.