init commit
This commit is contained in:
commit
15ab48343a
187 files changed
+12644
No files matched your search
@@ -0,0 +1,190 @@
|
||||
#include "core/vfs.hpp"
|
||||
#include "core/log.hpp"
|
||||
|
||||
#include <SDL.h>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
namespace top_octane {
|
||||
|
||||
namespace {
|
||||
|
||||
std::string to_lower_ascii(std::string s) {
|
||||
for (char& c : s) {
|
||||
c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
std::vector<std::string> split_rel_path(const std::string& rel) {
|
||||
std::vector<std::string> 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;
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
bool Vfs::init(const std::string& explicit_data_dir) {
|
||||
std::vector<std::filesystem::path> candidates;
|
||||
if (!explicit_data_dir.empty()) {
|
||||
candidates.emplace_back(explicit_data_dir);
|
||||
}
|
||||
if (const char* env_dir = std::getenv("HIOCTANE_DATA")) {
|
||||
if (*env_dir) {
|
||||
candidates.emplace_back(env_dir);
|
||||
}
|
||||
}
|
||||
|
||||
std::error_code 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);
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
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 (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)");
|
||||
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<uint8_t>& 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<uint8_t>& 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<size_t>(sz));
|
||||
ifs.seekg(0, std::ios::beg);
|
||||
if (sz > 0 && !ifs.read(reinterpret_cast<char*>(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<const char*>(data), static_cast<std::streamsize>(size))) {
|
||||
return false;
|
||||
}
|
||||
return ofs.good();
|
||||
}
|
||||
|
||||
} // namespace top_octane
|
||||
Reference in new issue
Block a user