Skip to content

Commit aa57337

Browse files
committed
Match GMod map pack and VPK search path handling
1 parent 4649f04 commit aa57337

1 file changed

Lines changed: 69 additions & 40 deletions

File tree

‎Source.Filesystem/BaseFileSystem.cs‎

Lines changed: 69 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,35 @@ public BaseFileSystem() {
5555
AddSearchPath(AppContext.BaseDirectory, "BASE_PATH", name: PathGroupName.EngineCore);
5656
}
5757

58+
private void RemoveAllMapSearchPaths() {
59+
foreach (KeyValuePair<ulong, SearchPathCollection> pair in SearchPaths) {
60+
List<ISearchPath> addOrder = pair.Value.GetAddOrder();
61+
for (int i = addOrder.Count - 1; i >= 0; i--) {
62+
if (addOrder[i] is not ZipPackFileSearchPath)
63+
continue;
64+
65+
RemoveSearchPathFromGroup(addOrder[i], SearchPaths.GetName(pair.Key));
66+
addOrder.RemoveAt(i);
67+
pair.Value.InvalidateOrder();
68+
}
69+
}
70+
}
71+
5872
private void AddMapPackFile(ReadOnlySpan<char> path, ReadOnlySpan<char> pathID, SearchPathAdd addType, PathGroupName groupName) {
73+
Span<char> fullPath = stackalloc char[MAX_PATH];
74+
ReadOnlySpan<char> newPath = RelativePathToFullPath(path, "GAME", fullPath);
75+
if (newPath.IsEmpty)
76+
return;
77+
78+
foreach (SearchPathCollection existing in SearchPaths.Values) {
79+
foreach (ISearchPath searchPath in existing.GetAddOrder()) {
80+
if (searchPath is ZipPackFileSearchPath && searchPath.GetDiskPath().Equals(newPath, StringComparison.OrdinalIgnoreCase))
81+
return;
82+
}
83+
}
84+
85+
RemoveAllMapSearchPaths();
86+
5987
using IFileHandle? file = Open(path, FileOpenOptions.Read | FileOpenOptions.Binary, "GAME");
6088
if (file == null) {
6189
Warning("Couldn't open BSP for embedded pack file\n");
@@ -71,58 +99,39 @@ private void AddMapPackFile(ReadOnlySpan<char> path, ReadOnlySpan<char> pathID,
7199
return;
72100
}
73101

74-
Span<char> fullPath = stackalloc char[MAX_PATH];
75-
ReadOnlySpan<char> newPath = RelativePathToFullPath(path, "GAME", fullPath);
76-
77-
if (!SearchPaths.OpenOrCreateCollection(pathID, out SearchPathCollection collection)) {
78-
for (int i = 0, c = collection.Count; i < c; i++) {
79-
var searchPath = collection.GetAddOrder()[i];
80-
if (searchPath.GetDiskPath() == newPath) {
81-
if ((addType == SearchPathAdd.ToHead && i == 0) || addType == SearchPathAdd.ToTail)
82-
return;
83-
else {
84-
RemoveSearchPathFromGroup(searchPath, pathID);
85-
collection.GetAddOrder().RemoveAt(i);
86-
collection.InvalidateOrder();
87-
i--;
88-
c--;
89-
break;
90-
}
91-
}
92-
}
93-
}
94-
95102
ZipPackFileSearchPath zip = new(this, new(newPath), file.Stream, in pakfile);
96103
if (!zip.IsValid()) {
97104
Warning("ZipPackFileSearchPath not valid\n");
98105
return;
99106
}
100107

108+
SearchPaths.OpenOrCreateCollection(pathID, out SearchPathCollection collection);
101109
AddSearchPathFinal(zip, addType, collection, groupName, pathID);
102110
}
103111

104112
private void AddVPKFile(ReadOnlySpan<char> path, ReadOnlySpan<char> pathID, SearchPathAdd addType, PathGroupName groupName) {
105113
string newPath = Path.IsPathFullyQualified(path) ? new(path) : Path.GetFullPath(new(path));
114+
ISearchPath? createdSearchPath = null;
106115

107116
if (!SearchPaths.OpenOrCreateCollection(pathID, out SearchPathCollection collection)) {
108-
for (int i = 0, c = collection.Count; i < c; i++) {
109-
var searchPath = collection.GetAddOrder()[i];
110-
if (searchPath.GetDiskPath() == newPath) {
111-
if ((addType == SearchPathAdd.ToHead && i == 0) || addType == SearchPathAdd.ToTail)
112-
return;
113-
else {
114-
RemoveSearchPathFromGroup(searchPath, pathID);
115-
collection.GetAddOrder().RemoveAt(i);
116-
collection.InvalidateOrder();
117-
i--;
118-
c--;
119-
break;
120-
}
121-
}
117+
for (int i = collection.Count - 1; i >= 0; i--) {
118+
ISearchPath searchPath = collection.GetAddOrder()[i];
119+
if (searchPath is not PackStoreSearchPath || !searchPath.GetDiskPath().Equals(newPath, StringComparison.OrdinalIgnoreCase))
120+
continue;
121+
122+
#if GMOD_DLL
123+
Msg($"DUPLICATE: [{searchPath.GetDiskPath()}]\n");
124+
RemoveSearchPathFromGroup(searchPath, pathID);
125+
collection.GetAddOrder().RemoveAt(i);
126+
collection.InvalidateOrder();
127+
createdSearchPath = searchPath;
128+
#else
129+
return;
130+
#endif
122131
}
123132
}
124133

125-
ISearchPath createdSearchPath = new PackStoreSearchPath(this, newPath);
134+
createdSearchPath ??= new PackStoreSearchPath(this, newPath);
126135
AddSearchPathFinal(createdSearchPath, addType, collection, groupName, pathID);
127136
}
128137
private void AddPackFiles(ReadOnlySpan<char> path, ReadOnlySpan<char> pathID, SearchPathAdd addType) { } // TODO
@@ -143,7 +152,7 @@ private void AddSearchPathDiskInternal(ReadOnlySpan<char> path, ReadOnlySpan<cha
143152
if (!SearchPaths.OpenOrCreateCollection(pathID, out SearchPathCollection collection)) {
144153
for (int i = 0, c = collection.Count; i < c; i++) {
145154
var searchPath = collection.GetAddOrder()[i];
146-
if (searchPath.GetDiskPath().SequenceEqual(newPath)) {
155+
if (searchPath.GetDiskPath().Equals(newPath, StringComparison.OrdinalIgnoreCase)) {
147156
if ((addType == SearchPathAdd.ToHead && i == 0) || addType == SearchPathAdd.ToTail)
148157
return;
149158
else {
@@ -518,10 +527,29 @@ public bool RemoveSearchPath(ReadOnlySpan<char> path, ReadOnlySpan<char> pathID)
518527
if (!SearchPaths.TryGetValue(hash, out var collection))
519528
return false;
520529

530+
bool matchAll = false;
531+
string newPath = "";
532+
533+
if (path.IsEmpty) {
534+
#if GMOD_DLL
535+
return false;
536+
#else
537+
matchAll = true;
538+
#endif
539+
}
540+
else if (path.Contains(".bsp", StringComparison.OrdinalIgnoreCase)) {
541+
Span<char> fullPath = stackalloc char[MAX_PATH];
542+
newPath = new(RelativePathToFullPath(path, "GAME", fullPath));
543+
}
544+
else {
545+
newPath = Path.IsPathFullyQualified(path) ? new(path) : Path.GetFullPath(new(path));
546+
AddSeparatorAndFixPath(ref newPath);
547+
}
548+
521549
bool ret = false;
522550

523551
for (int i = collection.Count - 1; i >= 0; i--) {
524-
if (collection.GetAddOrder()[i].GetDiskPath() != path)
552+
if (!matchAll && !collection.GetAddOrder()[i].GetDiskPath().Equals(newPath, StringComparison.OrdinalIgnoreCase))
525553
continue;
526554
RemoveSearchPathFromGroup(collection.GetAddOrder()[i], pathID);
527555
collection.GetAddOrder().RemoveAt(i);
@@ -700,8 +728,9 @@ public void PrintSearchPaths() {
700728
ISearchPath spi = searchpath.path;
701729
ReadOnlySpan<char> pack = "";
702730
ReadOnlySpan<char> type = "";
703-
if (false /* TODO: Map-based pack files */) {
704-
// type = "(map)";
731+
if (spi is ZipPackFileSearchPath zip) {
732+
type = "(map)";
733+
pack = zip.GetDiskPath();
705734
}
706735
else if (spi is PackStoreSearchPath pssp) {
707736
type = "(VPK)";

0 commit comments

Comments
 (0)