-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathFileSystem.cpp
More file actions
94 lines (76 loc) · 2.19 KB
/
FileSystem.cpp
File metadata and controls
94 lines (76 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "FileSystem.hpp"
#include "Game.hpp"
#include "Interface.hpp"
#include "Offsets.hpp"
#include "Utils.hpp"
#include "Surface.hpp"
#include "Console.hpp"
#include "Command.hpp"
#include "Engine.hpp"
#include <filesystem>
#include <fstream>
#ifndef _WIN32
# include <dirent.h>
#endif
FileSystem *fileSystem;
std::vector<std::string> FileSystem::GetSearchPaths() {
std::vector<std::string> paths;
char allpaths[4096];
int len = fileSystem->GetSearchPath(fileSystem->g_pFullFileSystem->ThisPtr(), "GAME", false, allpaths, sizeof(allpaths));
int start = 0;
for (int i = 0; i <= len; i++) {
if (i != len && allpaths[i] != ';') continue;
std::string path(allpaths + start, i - start);
paths.push_back(path);
start = i + 1;
}
return paths;
}
bool FileSystem::FileExistsSomewhere(std::string filename) {
std::vector<std::string> paths = GetSearchPaths();
for (std::string path : paths) {
if (std::ifstream(path + "/" + filename).good()) return true;
}
return false;
}
#ifdef _WIN32
std::string FileSystem::GetSaveDirectory() {
return engine->GetSaveDirName();
}
#else
static std::string findSubCapitalization(const char *base, const char *sub) {
DIR *d = opendir(base);
if (!d) return std::string(sub);
struct dirent *ent;
while ((ent = readdir(d))) {
if (!strcasecmp(ent->d_name, sub)) {
closedir(d);
return std::string(ent->d_name);
}
}
closedir(d);
return std::string(sub);
}
// Apparently, GetSaveDirName has the wrong capitalization sometimes
// kill me
std::string FileSystem::GetSaveDirectory() {
std::string path = std::string(engine->GetGameDirectory()) + "/";
std::string dir = findSubCapitalization(path.c_str(), "save");
dir += (engine->GetSaveDirName() + 4);
return dir;
}
#endif
bool FileSystem::MapExists(std::string name) {
name = "/maps/" + name + ".bsp";
return FileExistsSomewhere(name);
}
bool FileSystem::Init() {
g_pFullFileSystem = Interface::Create(this->Name(), "VFileSystem017", false);
if (g_pFullFileSystem != nullptr) {
GetSearchPath = this->g_pFullFileSystem->Original<_GetSearchPath>(Offsets::GetSearchPath);
}
return this->hasLoaded = this->g_pFullFileSystem;
}
void FileSystem::Shutdown() {
Interface::Delete(this->g_pFullFileSystem);
}