From 9b79843fbcbc237cc92d99f768ad4cde4134e523 Mon Sep 17 00:00:00 2001 From: Death Killer <884052+deathkiller@users.noreply.github.com> Date: Sat, 24 Feb 2024 12:43:15 +0100 Subject: [PATCH] Fixed build --- Sources/Main.cpp | 5 +-- Sources/backward/backward.h | 63 +++++++++++++++++++++++++++++++------ 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/Sources/Main.cpp b/Sources/Main.cpp index 646d598a..021bd327 100644 --- a/Sources/Main.cpp +++ b/Sources/Main.cpp @@ -163,10 +163,7 @@ void GameEventHandler::OnPreInit(AppConfiguration& config) { ZoneScopedC(0x888888); - // TODO: Only to simulate crash - AppConfiguration& config2 = *(AppConfiguration*)123; - - PreferencesCache::Initialize(config2); + PreferencesCache::Initialize(config); config.windowTitle = NCINE_APP_NAME; if (PreferencesCache::MaxFps == PreferencesCache::UseVsync) { diff --git a/Sources/backward/backward.h b/Sources/backward/backward.h index 8e8362de..141482e3 100644 --- a/Sources/backward/backward.h +++ b/Sources/backward/backward.h @@ -599,7 +599,10 @@ namespace backward { size_t next = 0; size_t delimiter_size = sizeof(kBackwardPathDelimiter) - 1; while ((next = s.find(kBackwardPathDelimiter, last)) != std::string::npos) { - out.push_back(s.substr(last, next - last)); + size_t length = next - last; + if (length > 0) { + out.push_back(s.substr(last, length)); + } last = next + delimiter_size; } if (last <= s.length()) { @@ -3596,18 +3599,58 @@ namespace backward { // a colon-separated list of path prefixes. Try prepending each // to the given path until a valid file is found. const std::vector& prefixes = get_paths_from_env_variable(); - for (size_t i = 0; i < prefixes.size(); ++i) { - // Double slashes (//) should not be a problem. - std::string new_path = prefixes[i] + '/' + path; - _file.reset(new std::ifstream(new_path.c_str())); - if (is_open()) { - break; + if (!prefixes.empty()) { + size_t lastOffset = std::string::npos; + while (true) { + size_t offset = path.find_last_of("/\\", lastOffset); + if (offset == std::string::npos) { + break; + } + + for (size_t i = 0; i < prefixes.size(); i++) { + const std::string& prefix = prefixes[i]; + std::string new_path = prefix; + if (prefix[prefix.size() - 1] != '/' && prefix[prefix.size() - 1] != '\\') { +#if defined(BACKWARD_SYSTEM_WINDOWS) + new_path += '\\'; +#else + new_path += '/'; +#endif + } + new_path.append(path, offset + 1); + + _file.reset(new std::ifstream(new_path.c_str())); + if (is_open()) { + return; + } + } + + if (offset == 0) { + break; + } + + lastOffset = offset - 1; + } + + for (size_t i = 0; i < prefixes.size(); ++i) { + // Double slashes (//) should not be a problem. + std::string new_path = prefixes[i]; +#if defined(BACKWARD_SYSTEM_WINDOWS) + new_path += '\\'; +#else + new_path += '/'; +#endif + new_path += path; + + _file.reset(new std::ifstream(new_path.c_str())); + if (is_open()) { + return; + } } } + // 2. If no valid file found then fallback to opening the path as-is. - if (!_file || !is_open()) { - _file.reset(new std::ifstream(path.c_str())); - } + _file.reset(new std::ifstream(path.c_str())); } bool is_open() const { return _file->is_open();