fixes for mac music
This commit is contained in:
@ -2,17 +2,27 @@
|
||||
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <initializer_list>
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
namespace AssetPath {
|
||||
|
||||
inline bool fileExists(const std::string& path) {
|
||||
if (path.empty()) {
|
||||
inline std::string& baseDirectory() {
|
||||
static std::string base;
|
||||
return base;
|
||||
}
|
||||
|
||||
inline void setBasePath(std::string path) {
|
||||
baseDirectory() = std::move(path);
|
||||
}
|
||||
|
||||
inline bool tryOpenFile(const std::string& candidate) {
|
||||
if (candidate.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SDL_IOStream* file = SDL_IOFromFile(path.c_str(), "rb");
|
||||
SDL_IOStream* file = SDL_IOFromFile(candidate.c_str(), "rb");
|
||||
if (file) {
|
||||
SDL_CloseIO(file);
|
||||
return true;
|
||||
@ -20,13 +30,58 @@ inline bool fileExists(const std::string& path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool fileExists(const std::string& path) {
|
||||
if (path.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (tryOpenFile(path)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::filesystem::path p(path);
|
||||
if (!p.is_absolute()) {
|
||||
const std::string& base = baseDirectory();
|
||||
if (!base.empty()) {
|
||||
std::filesystem::path combined = std::filesystem::path(base) / p;
|
||||
if (tryOpenFile(combined.string())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline std::string resolveWithBase(const std::string& path) {
|
||||
if (path.empty()) {
|
||||
return path;
|
||||
}
|
||||
|
||||
if (tryOpenFile(path)) {
|
||||
return path;
|
||||
}
|
||||
|
||||
std::filesystem::path p(path);
|
||||
if (!p.is_absolute()) {
|
||||
const std::string& base = baseDirectory();
|
||||
if (!base.empty()) {
|
||||
std::filesystem::path combined = std::filesystem::path(base) / p;
|
||||
std::string combinedStr = combined.string();
|
||||
if (tryOpenFile(combinedStr)) {
|
||||
return combinedStr;
|
||||
}
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
inline std::string resolveImagePath(const std::string& originalPath) {
|
||||
if (originalPath.empty()) {
|
||||
return originalPath;
|
||||
}
|
||||
|
||||
if (fileExists(originalPath)) {
|
||||
return originalPath;
|
||||
if (auto resolved = resolveWithBase(originalPath); resolved != originalPath || fileExists(resolved)) {
|
||||
return resolved;
|
||||
}
|
||||
|
||||
const std::size_t dot = originalPath.find_last_of('.');
|
||||
@ -45,12 +100,41 @@ inline std::string resolveImagePath(const std::string& originalPath) {
|
||||
if (candidate == originalPath) {
|
||||
continue;
|
||||
}
|
||||
if (fileExists(candidate)) {
|
||||
return candidate;
|
||||
std::string resolvedCandidate = resolveWithBase(candidate);
|
||||
if (resolvedCandidate != candidate || fileExists(resolvedCandidate)) {
|
||||
return resolvedCandidate;
|
||||
}
|
||||
}
|
||||
|
||||
return originalPath;
|
||||
}
|
||||
|
||||
inline std::string resolveWithExtensions(const std::string& basePathWithoutExt, std::initializer_list<const char*> extensions) {
|
||||
for (const char* ext : extensions) {
|
||||
std::string candidate = basePathWithoutExt + ext;
|
||||
std::string resolved = resolveWithBase(candidate);
|
||||
if (resolved != candidate || fileExists(resolved)) {
|
||||
return resolved;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
inline std::string resolveAudioPath(const std::string& basePathWithoutExt) {
|
||||
static constexpr std::array<const char*, 4> AUDIO_EXTENSIONS = {
|
||||
".ogg",
|
||||
".flac",
|
||||
".wav",
|
||||
".mp3"
|
||||
};
|
||||
for (const char* ext : AUDIO_EXTENSIONS) {
|
||||
std::string candidate = basePathWithoutExt + ext;
|
||||
std::string resolved = resolveWithBase(candidate);
|
||||
if (resolved != candidate || fileExists(resolved)) {
|
||||
return resolved;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace AssetPath
|
||||
|
||||
Reference in New Issue
Block a user