Add XDG Games desktop shortcut, hicolor icons, and installable asset lookup

This commit is contained in:
Luxferre committed 2026-12-01 09:51:29 +02:00
1 parent 554958db9a
commit d001f214aa
12 files changed
+445 -35

No files matched your search

+101 -25
View File
@@ -38,6 +38,20 @@ std::vector<std::string> split_rel_path(const std::string& rel) {
return parts;
}
std::filesystem::path strip_trailing_sep(std::filesystem::path p) {
while (p.has_relative_path() && p.filename().empty()) {
p = p.parent_path();
}
return p;
}
void append_unique(std::vector<std::filesystem::path>& out, const std::filesystem::path& p) {
if (p.empty()) return;
if (std::find(out.begin(), out.end(), p) == out.end()) {
out.push_back(p);
}
}
bool is_valid_hioctane_dir(const std::filesystem::path& p) {
std::error_code ec;
if (!std::filesystem::is_directory(p, ec)) {
@@ -58,18 +72,100 @@ bool is_valid_hioctane_dir(const std::filesystem::path& p) {
} // namespace
bool Vfs::init(const std::string& explicit_data_dir) {
std::vector<std::filesystem::path> Vfs::build_search_paths(
const std::string& explicit_data_dir,
const std::filesystem::path& cwd,
const std::filesystem::path& base_dir,
const std::filesystem::path& pref_dir) {
std::vector<std::filesystem::path> candidates;
if (!explicit_data_dir.empty()) {
candidates.emplace_back(explicit_data_dir);
append_unique(candidates, std::filesystem::path(explicit_data_dir));
}
if (const char* env_dir = std::getenv("HIOCTANE_DATA")) {
if (*env_dir) {
candidates.emplace_back(env_dir);
append_unique(candidates, std::filesystem::path(env_dir));
}
}
const std::filesystem::path clean_cwd = strip_trailing_sep(cwd);
const std::filesystem::path clean_base = strip_trailing_sep(base_dir);
auto add_folder_candidates = [&](const char* folder_name) {
if (!clean_cwd.empty()) {
append_unique(candidates, clean_cwd / folder_name);
if (clean_cwd.has_parent_path() && clean_cwd.parent_path() != clean_cwd) {
append_unique(candidates, clean_cwd.parent_path() / folder_name);
}
}
if (!clean_base.empty()) {
append_unique(candidates, clean_base / folder_name);
if (clean_base.has_parent_path() && clean_base.parent_path() != clean_base) {
append_unique(candidates, clean_base.parent_path() / folder_name);
}
}
};
add_folder_candidates("data");
add_folder_candidates("HiOctane");
// Relocatable installed prefix paths (<prefix>/bin/top-octane -> <prefix>/share[/games]/top-octane[/data])
if (!clean_base.empty() && clean_base.has_parent_path() && clean_base.parent_path() != clean_base) {
const std::filesystem::path prefix = clean_base.parent_path();
append_unique(candidates, prefix / "share" / "top-octane" / "data");
append_unique(candidates, prefix / "share" / "top-octane");
append_unique(candidates, prefix / "share" / "games" / "top-octane" / "data");
append_unique(candidates, prefix / "share" / "games" / "top-octane");
}
#ifndef _WIN32
// XDG Base Directory Specification asset lookup (XDG_DATA_HOME, then XDG_DATA_DIRS)
std::filesystem::path xdg_data_home;
if (const char* xdh = std::getenv("XDG_DATA_HOME"); xdh && xdh[0] == '/') {
xdg_data_home = strip_trailing_sep(std::filesystem::path(xdh));
} else if (const char* home = std::getenv("HOME"); home && *home) {
xdg_data_home = strip_trailing_sep(std::filesystem::path(home)) / ".local" / "share";
}
if (!xdg_data_home.empty()) {
append_unique(candidates, xdg_data_home / "top-octane" / "data");
append_unique(candidates, xdg_data_home / "top-octane");
}
const char* xdd_env = std::getenv("XDG_DATA_DIRS");
const std::string xdd = (xdd_env && *xdd_env) ? std::string(xdd_env) : std::string("/usr/local/share:/usr/share");
std::stringstream ss(xdd);
std::string entry;
while (std::getline(ss, entry, ':')) {
if (entry.empty() || entry[0] != '/') continue;
const std::filesystem::path base = strip_trailing_sep(std::filesystem::path(entry));
append_unique(candidates, base / "top-octane" / "data");
append_unique(candidates, base / "top-octane");
append_unique(candidates, base / "games" / "top-octane" / "data");
append_unique(candidates, base / "games" / "top-octane");
}
#endif
#ifdef HIOCTANE_INSTALL_DATA_DIR
if (HIOCTANE_INSTALL_DATA_DIR[0] != '\0') {
append_unique(candidates, std::filesystem::path(HIOCTANE_INSTALL_DATA_DIR));
}
#endif
if (!pref_dir.empty()) {
append_unique(candidates, strip_trailing_sep(pref_dir) / "data");
}
return candidates;
}
bool Vfs::init(const std::string& explicit_data_dir) {
std::error_code ec;
if (char* pref = SDL_GetPrefPath("top-octane", "TopOctane")) {
pref_dir_ = pref;
SDL_free(pref);
} else {
pref_dir_ = std::filesystem::current_path(ec) / "save";
}
std::filesystem::create_directories(pref_dir_, ec);
std::filesystem::path cwd = std::filesystem::current_path(ec);
std::filesystem::path base_dir;
if (char* base_p = SDL_GetBasePath()) {
@@ -77,19 +173,7 @@ bool Vfs::init(const std::string& explicit_data_dir) {
SDL_free(base_p);
}
auto add_folder_candidates = [&](const char* folder_name) {
if (!cwd.empty()) {
candidates.emplace_back(cwd / folder_name);
candidates.emplace_back(cwd.parent_path() / folder_name);
}
if (!base_dir.empty()) {
candidates.emplace_back(base_dir / folder_name);
candidates.emplace_back(base_dir.parent_path() / folder_name);
}
};
add_folder_candidates("data");
add_folder_candidates("HiOctane");
const auto candidates = build_search_paths(explicit_data_dir, cwd, base_dir, pref_dir_);
data_root_.clear();
for (const auto& c : candidates) {
if (is_valid_hioctane_dir(c)) {
@@ -99,16 +183,8 @@ bool Vfs::init(const std::string& explicit_data_dir) {
}
}
if (char* pref = SDL_GetPrefPath("top-octane", "TopOctane")) {
pref_dir_ = pref;
SDL_free(pref);
} else {
pref_dir_ = std::filesystem::current_path() / "save";
}
std::filesystem::create_directories(pref_dir_, ec);
if (data_root_.empty()) {
LOG_ERROR("Could not locate Top-Octane asset directory (expected ./data or --data-dir)");
LOG_ERROR("Could not locate Top-Octane asset directory (checked --data-dir, HIOCTANE_DATA, ./data, and XDG data dirs)");
return false;
}
LOG_INFO("Using read-only asset directory: %s", data_root_.string().c_str());