#include "core/vfs.hpp" #include "core/log.hpp" #include #include #include #include #include #include namespace top_octane { namespace { std::string to_lower_ascii(std::string s) { for (char& c : s) { c = static_cast(std::tolower(static_cast(c))); } return s; } std::vector split_rel_path(const std::string& rel) { std::vector parts; std::string cur; for (char c : rel) { if (c == '/' || c == '\\') { if (!cur.empty()) { parts.push_back(cur); cur.clear(); } } else { cur.push_back(c); } } if (!cur.empty()) { parts.push_back(cur); } 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& 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)) { return false; } // Check case-insensitively for both DATA and MAPS subdirectories bool has_data = false; bool has_maps = false; for (const auto& ent : std::filesystem::directory_iterator(p, ec)) { if (ent.is_directory(ec)) { std::string name = to_lower_ascii(ent.path().filename().string()); if (name == "data") has_data = true; else if (name == "maps") has_maps = true; } } return has_data && has_maps; } } // namespace std::vector 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 candidates; if (!explicit_data_dir.empty()) { append_unique(candidates, std::filesystem::path(explicit_data_dir)); } if (const char* env_dir = std::getenv("HIOCTANE_DATA")) { if (*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 (/bin/top-octane -> /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()) { base_dir = std::filesystem::path(base_p); SDL_free(base_p); } 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)) { data_root_ = std::filesystem::weakly_canonical(c, ec); if (ec) data_root_ = c; break; } } if (data_root_.empty()) { 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()); LOG_INFO("Using user save/config directory: %s", pref_dir_.string().c_str()); return true; } bool Vfs::resolve(const std::string& rel_path, std::filesystem::path& out_path) const { if (data_root_.empty()) return false; std::filesystem::path cur = data_root_; std::error_code ec; for (const std::string& part : split_rel_path(rel_path)) { std::filesystem::path direct = cur / part; if (std::filesystem::exists(direct, ec)) { cur = direct; continue; } std::string target = to_lower_ascii(part); bool matched = false; if (std::filesystem::is_directory(cur, ec)) { for (const auto& ent : std::filesystem::directory_iterator(cur, ec)) { if (to_lower_ascii(ent.path().filename().string()) == target) { cur = ent.path(); matched = true; break; } } } if (!matched) { return false; } } out_path = cur; return std::filesystem::is_regular_file(out_path, ec); } bool Vfs::exists(const std::string& rel_path) const { std::filesystem::path p; return resolve(rel_path, p); } bool Vfs::read_file(const std::string& rel_path, std::vector& out_data) const { std::filesystem::path p; if (!resolve(rel_path, p)) { return false; } return read_raw_file(p, out_data); } bool Vfs::read_raw_file(const std::filesystem::path& path, std::vector& out_data) { std::ifstream ifs(path, std::ios::binary | std::ios::ate); if (!ifs) return false; const auto sz = ifs.tellg(); if (sz < 0) return false; out_data.resize(static_cast(sz)); ifs.seekg(0, std::ios::beg); if (sz > 0 && !ifs.read(reinterpret_cast(out_data.data()), sz)) { out_data.clear(); return false; } return true; } bool Vfs::write_raw_file(const std::filesystem::path& path, const void* data, size_t size) { std::error_code ec; if (path.has_parent_path()) { std::filesystem::create_directories(path.parent_path(), ec); } std::ofstream ofs(path, std::ios::binary | std::ios::trunc); if (!ofs) return false; if (size > 0 && !ofs.write(reinterpret_cast(data), static_cast(size))) { return false; } return ofs.good(); } } // namespace top_octane