Skip to content

Commit d7ea677

Browse files
committed
refactor: filesystem function
1 parent 8b53648 commit d7ea677

3 files changed

Lines changed: 30 additions & 25 deletions

File tree

src/Features/ConfigPlus.cpp

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,13 @@ CON_COMMAND_F(sar_download_file, "sar_download_file <url> <filepath> [directory]
6666
}
6767

6868
std::string filepath = args[2];
69-
std::string gamedir = "";
69+
std::string gamedir = args.ArgC() > 3 ? args[3] : "";
7070

71-
auto paths = fileSystem->GetSearchPaths();
72-
if (args.ArgC() > 3 && args[3][0]) {
73-
for (auto path : paths) {
74-
if (strstr(path.c_str(), args[3]) == NULL) continue;
75-
gamedir = path;
76-
break;
77-
}
78-
}
79-
if (gamedir == "") {
80-
gamedir = std::string(engine->GetGameDirectory()) + "/";
81-
for (auto path : paths) {
82-
if (!std::ifstream(path + filepath).good()) continue;
83-
gamedir = path;
84-
break;
85-
}
71+
auto result = fileSystem->FindFileSomewhere(filepath, gamedir);
72+
if (result.has_value()) {
73+
filepath = result.value();
74+
} else {
75+
filepath = std::string(engine->GetGameDirectory()) + "/" + filepath;
8676
}
8777

8878
CURL *curl = curl_easy_init();
@@ -102,13 +92,13 @@ CON_COMMAND_F(sar_download_file, "sar_download_file <url> <filepath> [directory]
10292
if (res == CURLE_OK) {
10393

10494
// Create any intermediate directories
105-
std::string dirpath = gamedir + filepath;
95+
std::string dirpath = filepath;
10696
std::replace(dirpath.begin(), dirpath.end(), '\\', '/');
10797
dirpath = dirpath.substr(0, dirpath.rfind('/'));
10898
std::filesystem::create_directories(dirpath);
10999

110-
std::filesystem::remove(gamedir + filepath);
111-
std::filesystem::copy(temp, gamedir + filepath); // for some reason moving the file doesn't work
100+
std::filesystem::remove(filepath);
101+
std::filesystem::copy(temp, filepath); // for some reason moving the file doesn't work
112102

113103
} else {
114104
console->Print("An error occurred\n");

src/Modules/FileSystem.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "Surface.hpp"
88
#include "Console.hpp"
99
#include "Command.hpp"
10-
#include "Engine.hpp"
10+
#include "Modules/Engine.hpp"
1111

1212
#include <filesystem>
1313
#include <fstream>
@@ -37,11 +37,26 @@ std::vector<std::string> FileSystem::GetSearchPaths() {
3737
}
3838

3939
bool FileSystem::FileExistsSomewhere(std::string filename) {
40-
std::vector<std::string> paths = GetSearchPaths();
41-
for (std::string path : paths) {
42-
if (std::ifstream(path + "/" + filename).good()) return true;
40+
return FindFileSomewhere(filename).has_value();
41+
}
42+
43+
std::optional<std::string> FileSystem::FindFileSomewhere(std::string filepath, std::string gamedir /* = "" */) {
44+
auto paths = fileSystem->GetSearchPaths();
45+
if (gamedir.length() == 0) {
46+
for (auto path : paths) {
47+
if (std::ifstream(path + filepath).good()) return path + filepath;
48+
}
49+
50+
} else {
51+
for (auto path : paths) {
52+
// Fuzzy gamedir search
53+
if (strstr(path.c_str(), gamedir.c_str()) == NULL) continue;
54+
if (std::ifstream(path + filepath).good()) return path + filepath;
55+
break;
56+
}
57+
4358
}
44-
return false;
59+
return std::nullopt;
4560
}
4661

4762
#ifdef _WIN32
@@ -79,7 +94,6 @@ bool FileSystem::MapExists(std::string name) {
7994
return FileExistsSomewhere(name);
8095
}
8196

82-
8397
bool FileSystem::Init() {
8498
g_pFullFileSystem = Interface::Create(this->Name(), "VFileSystem017", false);
8599

src/Modules/FileSystem.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class FileSystem : public Module {
1717
bool FileExistsSomewhere(std::string filename);
1818
std::string GetSaveDirectory();
1919
bool MapExists(std::string name);
20+
std::optional<std::string> FindFileSomewhere(std::string filepath, std::string gamedir = "");
2021

2122
public:
2223
bool Init() override;

0 commit comments

Comments
 (0)