From 155c8e21c8eedf0e9288651996ef104d70da6622 Mon Sep 17 00:00:00 2001 From: mlugg Date: Mon, 27 Dec 2021 00:43:04 +0000 Subject: [PATCH] fix: fix crashing issues on Windows From the bottom of my heart: fuck you, Microsoft. Why in the everloving *fuck* does stringifying a filepath just throw an undocumented system_error if the string contains certain Unicode characters? --- src/Checksum.cpp | 28 +++++++++++++++------------- src/Command.cpp | 13 ++++++++----- src/Features/Hud/Crosshair.cpp | 29 ++++++++++++++++------------- src/Features/WorkshopList.cpp | 21 ++++++++++++--------- src/Modules/EngineDemoPlayer.cpp | 21 ++++++++++++--------- 5 files changed, 63 insertions(+), 49 deletions(-) diff --git a/src/Checksum.cpp b/src/Checksum.cpp index e77d55384..7cf1cb42a 100644 --- a/src/Checksum.cpp +++ b/src/Checksum.cpp @@ -292,21 +292,23 @@ static void calcMapSums() { static void initFileSums() { std::vector paths; - std::vector maps; for (auto &ent : std::filesystem::recursive_directory_iterator(".")) { - if (ent.status().type() == std::filesystem::file_type::regular || ent.status().type() == std::filesystem::file_type::symlink) { - auto path = ent.path().string(); - std::replace(path.begin(), path.end(), '\\', '/'); - if (Utils::EndsWith(path, ".nut") - || (Utils::EndsWith(path, ".vpk") && path.find("portal2_dlc3") != std::string::npos) - || path.find("scripts/talker") != std::string::npos) - { - paths.push_back(path); - } - - if (Utils::EndsWith(path, ".bsp") && path.find("/workshop/") == std::string::npos) { - g_mapfiles.push_back(path); + try { + if (ent.status().type() == std::filesystem::file_type::regular || ent.status().type() == std::filesystem::file_type::symlink) { + auto path = ent.path().string(); + std::replace(path.begin(), path.end(), '\\', '/'); + if (Utils::EndsWith(path, ".nut") + || (Utils::EndsWith(path, ".vpk") && path.find("portal2_dlc3") != std::string::npos) + || path.find("scripts/talker") != std::string::npos) + { + paths.push_back(path); + } + + if (Utils::EndsWith(path, ".bsp") && path.find("/workshop/") == std::string::npos) { + g_mapfiles.push_back(path); + } } + } catch (std::system_error &e) { } } diff --git a/src/Command.cpp b/src/Command.cpp index 58bf4cf5a..d061811e4 100644 --- a/src/Command.cpp +++ b/src/Command.cpp @@ -217,11 +217,14 @@ int _FileCompletionFunc(std::string extension, std::string rootdir, int exp_args std::set sorted; for (auto &file : std::filesystem::directory_iterator(rootdir + std::string("/") + dirpart)) { - if (file.is_directory() || Utils::EndsWith(file.path().extension().string(), extension)) { - std::string path = dirpart + file.path().stem().string(); - std::replace(path.begin(), path.end(), '\\', '/'); - if (file.is_directory()) path += "/"; - sorted.insert(path); + try { + if (file.is_directory() || Utils::EndsWith(file.path().extension().string(), extension)) { + std::string path = dirpart + file.path().stem().string(); + std::replace(path.begin(), path.end(), '\\', '/'); + if (file.is_directory()) path += "/"; + sorted.insert(path); + } + } catch (std::system_error &e) { } } diff --git a/src/Features/Hud/Crosshair.cpp b/src/Features/Hud/Crosshair.cpp index f3ab7d3b6..8086ff47f 100644 --- a/src/Features/Hud/Crosshair.cpp +++ b/src/Features/Hud/Crosshair.cpp @@ -334,9 +334,21 @@ void Crosshair::UpdateImages() { // Scan through all directories and find the image file for (auto &dir : std::filesystem::recursive_directory_iterator(path)) { - if (dir.status().type() == std::filesystem::file_type::directory) { - auto curdir = dir.path().string(); - for (auto &dirdir : std::filesystem::directory_iterator(curdir)) { + try { + if (dir.status().type() == std::filesystem::file_type::directory) { + auto curdir = dir.path().string(); + for (auto &dirdir : std::filesystem::directory_iterator(curdir)) { + auto file = dir.path().string(); + if (Utils::EndsWith(file, std::string(".png"))) { + auto img = file.substr(index); + if (std::isdigit(img[img.length() - 5])) { //Take only images with a digit as last character + img = img.substr(0, img.length() - 5); + this->images.push_back(img); + } + break; + } + } + } else { auto file = dir.path().string(); if (Utils::EndsWith(file, std::string(".png"))) { auto img = file.substr(index); @@ -347,16 +359,7 @@ void Crosshair::UpdateImages() { break; } } - } else { - auto file = dir.path().string(); - if (Utils::EndsWith(file, std::string(".png"))) { - auto img = file.substr(index); - if (std::isdigit(img[img.length() - 5])) { //Take only images with a digit as last character - img = img.substr(0, img.length() - 5); - this->images.push_back(img); - } - break; - } + } catch (std::system_error &e) { } } } diff --git a/src/Features/WorkshopList.cpp b/src/Features/WorkshopList.cpp index 4b3db2a38..0fd858672 100644 --- a/src/Features/WorkshopList.cpp +++ b/src/Features/WorkshopList.cpp @@ -24,17 +24,20 @@ int WorkshopList::Update() { // Scan through all directories and find the map file for (auto &dir : std::filesystem::recursive_directory_iterator(path)) { - if (dir.status().type() == std::filesystem::file_type::directory) { - auto curdir = dir.path().string(); - for (auto &dirdir : std::filesystem::directory_iterator(curdir)) { - auto file = dirdir.path().string(); - if (Utils::EndsWith(file, std::string(".bsp"))) { - auto map = file.substr(index); - map = map.substr(0, map.length() - 4); - this->maps.push_back(map); - break; + try { + if (dir.status().type() == std::filesystem::file_type::directory) { + auto curdir = dir.path().string(); + for (auto &dirdir : std::filesystem::directory_iterator(curdir)) { + auto file = dirdir.path().string(); + if (Utils::EndsWith(file, std::string(".bsp"))) { + auto map = file.substr(index); + map = map.substr(0, map.length() - 4); + this->maps.push_back(map); + break; + } } } + } catch (std::system_error &e) { } } diff --git a/src/Modules/EngineDemoPlayer.cpp b/src/Modules/EngineDemoPlayer.cpp index c8293a9e9..bae388b6c 100644 --- a/src/Modules/EngineDemoPlayer.cpp +++ b/src/Modules/EngineDemoPlayer.cpp @@ -363,15 +363,18 @@ CON_COMMAND_F_COMPLETION(sar_startdemosfolder, "sar_startdemosfolder Print("%s\n", filepath.c_str()); - if (parser.Parse(dir + filepath, &demo)) { - engine->demoplayer->demoQueue.push_back(filepath); + try { + if (file.path().extension() != ".dem") + continue; + + filepath = args[1]; + if (filepath[filepath.size() - 1] != '/') filepath += "/"; + filepath += file.path().filename().string(); + console->Print("%s\n", filepath.c_str()); + if (parser.Parse(dir + filepath, &demo)) { + engine->demoplayer->demoQueue.push_back(filepath); + } + } catch (std::system_error &e) { } }