Skip to content

Commit

Permalink
fix: fix crashing issues on Windows
Browse files Browse the repository at this point in the history
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?
  • Loading branch information
mlugg committed Dec 27, 2021
1 parent 0aec794 commit 155c8e2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 49 deletions.
28 changes: 15 additions & 13 deletions src/Checksum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,21 +292,23 @@ static void calcMapSums() {

static void initFileSums() {
std::vector<std::string> paths;
std::vector<std::string> 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) {
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,14 @@ int _FileCompletionFunc(std::string extension, std::string rootdir, int exp_args
std::set<std::string> 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) {
}
}

Expand Down
29 changes: 16 additions & 13 deletions src/Features/Hud/Crosshair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
}
}
}
Expand Down
21 changes: 12 additions & 9 deletions src/Features/WorkshopList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
}

Expand Down
21 changes: 12 additions & 9 deletions src/Modules/EngineDemoPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,15 +363,18 @@ CON_COMMAND_F_COMPLETION(sar_startdemosfolder, "sar_startdemosfolder <folder nam
DemoParser parser;

for (const auto &file : std::filesystem::directory_iterator(dir + args[1])) {
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);
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) {
}
}

Expand Down

0 comments on commit 155c8e2

Please sign in to comment.