Repository navigation
Expand file tree
/
Copy pathFileSystem.cs
More file actions
168 lines (142 loc) · 5.41 KB
/
Copy pathFileSystem.cs
File metadata and controls
168 lines (142 loc) · 5.41 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using Microsoft.Extensions.DependencyInjection;
using Source.Common;
using Source.Common.Commands;
using Source.Common.Filesystem;
using Source.Common.Formats.Keyvalues;
namespace Source.Engine;
public struct CFSSearchPathsInit
{
public string? DirectoryName;
public string Language;
public IFileSystem FileSystem;
public bool MountHDContent;
public bool LowViolence;
public string ModPath;
}
public enum FSReturnCode
{
OK,
MissingGameInfoFile,
InvalidGameInfoFile,
InvalidParameters,
UnableToInit,
MissingSteamDLL,
}
/// <summary>
/// Internal engine filesystem initializer.
/// </summary>
public class FileSystem(IFileSystem fileSystem, IServiceProvider services)
{
FSReturnCode SetupFileSystemError(bool run, FSReturnCode ret, ReadOnlySpan<char> msg) {
Dbg.Error($"{msg}\n");
return ret;
}
public FSReturnCode LoadSearchPaths(ref CFSSearchPathsInit initInfo) {
if (initInfo.FileSystem == null || initInfo.DirectoryName == null)
return SetupFileSystemError(false, FSReturnCode.InvalidParameters, "FileSystem.LoadSearchPaths: Invalid parameters specified.");
KeyValues? mainFile, fileSystemInfo, searchPaths;
FSReturnCode retVal = LoadGameInfoFile(initInfo.DirectoryName, out mainFile, out fileSystemInfo, out searchPaths);
if (retVal != FSReturnCode.OK)
return retVal;
string baseDir;
if (!GetBaseDir(out baseDir))
return SetupFileSystemError(false, FSReturnCode.InvalidParameters, "FileSystem.GetBaseDir: failed.");
initInfo.ModPath = initInfo.DirectoryName;
const string GAMEINFOPATH_TOKEN = "|gameinfo_path|";
const string BASESOURCEPATHS_TOKEN = "|all_source_engine_paths|";
// todo: extraSearchPath.
bool lowViolence = initInfo.LowViolence;
// bool firstGamePath = true;
foreach (KeyValues cur in searchPaths!) {
ReadOnlySpan<char> location = cur.GetString();
string lBaseDir = baseDir;
if (location.Contains(GAMEINFOPATH_TOKEN, StringComparison.OrdinalIgnoreCase)) {
location = location[GAMEINFOPATH_TOKEN.Length..];
lBaseDir = initInfo.DirectoryName;
}
else if (location.Contains(BASESOURCEPATHS_TOKEN, StringComparison.OrdinalIgnoreCase)) {
location = location[BASESOURCEPATHS_TOKEN.Length..];
}
ReadOnlySpan<char> absSearchPath;
if (Path.IsPathFullyQualified(location))
absSearchPath = location;
else
absSearchPath = Path.GetFullPath(Path.Combine(lBaseDir, new string(location)));
// TODO; theres a lot of weird logic here I don't fully understand yet.
// So just do what we can here
string[] pathIDs = cur.Name.Split('+');
for (int i = 0; i < pathIDs.Length; i++) {
pathIDs[i] = pathIDs[i].Trim();
#if GMOD_DLL
initInfo.FileSystem.AddSearchPath(absSearchPath, pathIDs[i], SearchPathAdd.ToTail, PathGroupName.GModCore);
#else
initInfo.FileSystem.AddSearchPath(absSearchPath, pathIDs[i]);
#endif
}
}
initInfo.FileSystem.MarkPathIDByRequestOnly("executable_path", true);
initInfo.FileSystem.MarkPathIDByRequestOnly("gamebin", true);
initInfo.FileSystem.MarkPathIDByRequestOnly("download", true);
initInfo.FileSystem.MarkPathIDByRequestOnly("mod", true);
initInfo.FileSystem.MarkPathIDByRequestOnly("game_write", true);
initInfo.FileSystem.MarkPathIDByRequestOnly("mod_write", true);
#if GMOD_DLL
initInfo.FileSystem.GMOD_SetupDefaultPaths(baseDir, initInfo.DirectoryName);
initInfo.FileSystem.DoFilesystemRefresh();
#endif
return FSReturnCode.OK;
}
private bool GetBaseDir(out string baseDir) {
baseDir = AppContext.BaseDirectory;
return true;
}
public const string GAMEINFO_FILENAME = "gameinfo.txt";
public FSReturnCode LoadGameInfoFile(string directoryName, out KeyValues? mainFile, out KeyValues? fileSystemInfo, out KeyValues? searchPaths) {
mainFile = null;
fileSystemInfo = null;
searchPaths = null;
string gameInfoFilename = Path.Combine(directoryName, GAMEINFO_FILENAME);
mainFile = ReadKeyValuesFile(gameInfoFilename);
if (mainFile == null)
return SetupFileSystemError(true, FSReturnCode.MissingGameInfoFile, $"{gameInfoFilename} is missing.");
// Load ModInfo for other things so everything else doesn't need to parse gameinfo.txt.
services.GetRequiredService<ModInfo>().LoadGameInfoFromKeyValues(mainFile);
fileSystemInfo = mainFile.FindKey("FileSystem");
if (fileSystemInfo == null)
return SetupFileSystemError(true, FSReturnCode.InvalidGameInfoFile, $"{gameInfoFilename} is not a valid format (missing FileSystem).");
searchPaths = fileSystemInfo.FindKey("SearchPaths");
if (searchPaths == null)
return SetupFileSystemError(true, FSReturnCode.InvalidGameInfoFile, $"{gameInfoFilename} is not a valid format (missing SearchPaths).");
return FSReturnCode.OK;
}
private KeyValues? ReadKeyValuesFile(string filename) {
KeyValues kv = new("");
if (!kv.LoadFromFile(filename)) {
return null;
}
return kv;
}
[ConCommand]
void whereis(in TokenizedCommand args) {
ReadOnlySpan<char> where = fileSystem.WhereIsFile(args.ArgS(1), "GAME");
if (where.IsEmpty)
ConWarning($"File '{args.ArgS(1)}' not found\n");
else
ConMsg($"{where}\n");
}
[ConCommand]
void dir(in TokenizedCommand args) {
ReadOnlySpan<char> input = args.Arg(1);
ReadOnlySpan<char> filename = fileSystem.FindFirstEx(input, args.Arg(2), out FileFindHandle_t findHandle);
while (!filename.IsEmpty) {
Msg(filename);
Msg("\n");
filename = fileSystem.FindNext(findHandle);
}
fileSystem.FindClose(findHandle);
}
[ConCommand]
void path(in TokenizedCommand args) {
fileSystem.PrintSearchPaths();
}
}