From d478c5eeeba51e8bd58b55c976622b52c5c6b4e2 Mon Sep 17 00:00:00 2001 From: Space V <40030799+spacevx@users.noreply.github.com> Date: Mon, 16 Nov 2026 03:10:32 +0100 Subject: [PATCH 1/7] Honor request-only path IDs in filesystem lookups --- Source.Filesystem/BaseFileSystem.cs | 21 ++++++++++++--------- Source.Filesystem/SearchPathIDCollection.cs | 13 ++++++++++++- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/Source.Filesystem/BaseFileSystem.cs b/Source.Filesystem/BaseFileSystem.cs index 91901b55..16389120 100644 --- a/Source.Filesystem/BaseFileSystem.cs +++ b/Source.Filesystem/BaseFileSystem.cs @@ -226,7 +226,7 @@ public bool MoveNext() { if (currentCollection == null) return false; - currentSearchPath = currentCollection.AtSorted(currentSearchPathIdx); + currentSearchPath = iterateCollections && currentCollection.RequestOnly ? null : currentCollection.AtSorted(currentSearchPathIdx); if (currentSearchPath == null) { if (iterateCollections) { currentCollectionIdx++; @@ -345,6 +345,9 @@ public bool FullPathToRelativePathEx(ReadOnlySpan fullPath, ReadOnlySpan fileName, ReadOnlySpan pathI public void MarkPathIDByRequestOnly(ReadOnlySpan pathID, bool requestOnly) { using Lock.Scope scope = SearchPathLock.EnterScope(); - ulong hashID = pathID.Hash(); - - if (!SearchPaths.TryGetValue(hashID, out var collection)) - return; - - collection.RequestOnly = requestOnly; + SearchPaths.MarkPathIDByRequestOnly(pathID.Hash(), requestOnly); } public int GetSearchPath(ReadOnlySpan pathID, bool getPackFiles, Span dest) { @@ -864,8 +862,13 @@ public ReadOnlySpan Next() { } SearchPathCollection? NextCollection() { - if (PathID == 0) - return system.SearchPaths.At(Interlocked.Increment(ref CollectionIdx)); + if (PathID == 0) { + SearchPathCollection? collection; + do + collection = system.SearchPaths.At(Interlocked.Increment(ref CollectionIdx)); + while (collection != null && collection.RequestOnly); + return collection; + } if (Interlocked.CompareExchange(ref ranAtLeastOnce, 1, 0) == 0) return system.SearchPaths.TryGetValue(PathID.String().Hash(), out var found) ? found : null; diff --git a/Source.Filesystem/SearchPathIDCollection.cs b/Source.Filesystem/SearchPathIDCollection.cs index 03dca6d2..4eed8a71 100644 --- a/Source.Filesystem/SearchPathIDCollection.cs +++ b/Source.Filesystem/SearchPathIDCollection.cs @@ -6,6 +6,7 @@ public class SearchPathIDCollection : Dictionary { readonly List pathOrder = []; readonly Dictionary searchPathNames = []; + readonly HashSet requestOnlyPathIDs = []; /// /// @@ -20,13 +21,23 @@ public bool OpenOrCreateCollection(in ReadOnlySpan pathID, out SearchPathC return false; } - collection = new(); + collection = new() { RequestOnly = requestOnlyPathIDs.Contains(hashID) }; this[hashID] = collection; pathOrder.Add(hashID); searchPathNames[hashID] = new(pathID); return true; } + public void MarkPathIDByRequestOnly(ulong hashID, bool requestOnly) { + if (requestOnly) + requestOnlyPathIDs.Add(hashID); + else + requestOnlyPathIDs.Remove(hashID); + + if (TryGetValue(hashID, out SearchPathCollection? collection)) + collection.RequestOnly = requestOnly; + } + public bool Remove(in ReadOnlySpan pathID) => Remove(pathID.Hash()); public new bool Remove(ulong hashID) { From 0e39654a21bb0a108cc52bbb3e522c750513821a Mon Sep 17 00:00:00 2001 From: Space V <40030799+spacevx@users.noreply.github.com> Date: Mon, 16 Nov 2026 03:10:32 +0100 Subject: [PATCH 2/7] Match GMod IsDirectory path handling --- Source.Common/Exts.cs | 9 ++++++++ Source.Filesystem/BaseFileSystem.cs | 35 ++++++++++++++++++++++++++--- Source.Filesystem/DiskSearchPath.cs | 13 ++++++++--- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/Source.Common/Exts.cs b/Source.Common/Exts.cs index 7b8a8b7c..7fadc8c3 100644 --- a/Source.Common/Exts.cs +++ b/Source.Common/Exts.cs @@ -298,6 +298,15 @@ public static void FixSlashes(Span name, char separator = CORRECT_PATH_SEP name[i] = separator; } } + public static void StripTrailingSlash(Span path) { + int len = (int)strlen(path); + if (len > 0 && path[len - 1].IsPathSeparator()) + path[len - 1] = '\0'; + } + public static bool IsAbsolutePath(ReadOnlySpan str) { + char first = str.IsEmpty ? '\0' : str[0]; + return (first != '\0' && str.Length > 1 && str[1] == ':') || first == '/' || first == '\\'; + } public static int StrLen(ReadOnlySpan str) { int i = str.IndexOf('\0'); diff --git a/Source.Filesystem/BaseFileSystem.cs b/Source.Filesystem/BaseFileSystem.cs index 16389120..0595378d 100644 --- a/Source.Filesystem/BaseFileSystem.cs +++ b/Source.Filesystem/BaseFileSystem.cs @@ -386,7 +386,37 @@ readonly ref struct IsDirectory_Op() : IFirstToThePostOp } public bool IsDirectory(ReadOnlySpan fileName, ReadOnlySpan pathID) { - return FirstToThePost(fileName, pathID, new IsDirectory_Op(), false, out _); + Span tempBuf = stackalloc char[MAX_PATH]; + strcpy(tempBuf, fileName); + StrTools.StripTrailingSlash(tempBuf); + scoped ReadOnlySpan name = tempBuf.SliceNullTerminatedString(); + + Span tempPathID = stackalloc char[MAX_PATH]; + scoped ReadOnlySpan id = pathID; + ParsePathID(ref name, ref id, tempPathID); + if (StrTools.IsAbsolutePath(name)) + return Directory.Exists(DiskSearchPath.ResolveDiskPath(new(name))); + + return FirstToThePost(name, id, new IsDirectory_Op(), false, out _); + } + + static void ParsePathID(scoped ref ReadOnlySpan fileName, scoped ref ReadOnlySpan pathID, Span tempPathID) { + tempPathID[0] = '\0'; + + if (fileName.Length < 2 || fileName[0] != '/' || fileName[1] != '/') + return; + + if (!pathID.IsEmpty) + Warning($"FS: Specified two path IDs ({fileName}, {pathID}).\n"); + + int read = 2; + int written = 0; + while (read < fileName.Length && !fileName[read].IsPathSeparator() && written < MAX_PATH - 1) + tempPathID[written++] = fileName[read++]; + + tempPathID[written] = '\0'; + pathID = tempPathID[0] == '*' ? default : tempPathID[..written]; + fileName = read == fileName.Length ? fileName[read..] : fileName[(read + 1)..]; } readonly ref struct IsFileWritable_Op() : IFirstToThePostOp @@ -455,8 +485,7 @@ private static ReadOnlySpan FixUpPath(ReadOnlySpan fileName, Span fixedUp = fixedUpFileName[..(int)strlen(fixedUpFileName)]; - bool isAbsolute = (fixedUp.Length > 1 && fixedUp[1] == ':') || (fixedUp.Length > 0 && (fixedUp[0] == '/' || fixedUp[0] == '\\')); - if (!isAbsolute) { + if (!StrTools.IsAbsolutePath(fixedUp)) { for (int i = 0; i < fixedUp.Length; i++) fixedUp[i] = char.ToLowerInvariant(fixedUp[i]); } diff --git a/Source.Filesystem/DiskSearchPath.cs b/Source.Filesystem/DiskSearchPath.cs index 1c4343ac..d5372402 100644 --- a/Source.Filesystem/DiskSearchPath.cs +++ b/Source.Filesystem/DiskSearchPath.cs @@ -15,7 +15,7 @@ class CaseInsensitiveCache { public required string RealPath; } static readonly Dictionary cache = []; - static string ResolveDiskPath(string path) { + internal static string ResolveDiskPath(string path) { ulong hash = path.Hash(invariant: true); if(cache.TryGetValue(hash, out CaseInsensitiveCache? found)) return found.RealPath; @@ -44,7 +44,7 @@ static string ResolveDiskPath(string path) { return path; } #else - static string ResolveDiskPath(string path) { + internal static string ResolveDiskPath(string path) { return path; } #endif @@ -71,7 +71,14 @@ public override bool Exists(ReadOnlySpan path) { public bool ExistsOnDisk(ReadOnlySpan path) => Path.Exists(GetAbsPath(path)); public string GetFullPath(ReadOnlySpan path) => GetAbsPath(path).Replace('\\', '/'); - public override bool IsDirectory(ReadOnlySpan path) => Directory.Exists(GetAbsPath(path)); + public override bool IsDirectory(ReadOnlySpan path) { + string absPath = GetAbsPath(path); +#if GMOD_DLL + if (Workshop && BaseFileSystem.g_AddonFileSystem.IsDirectory(absPath)) + return true; +#endif + return Directory.Exists(absPath); + } public override bool IsFileWritable(ReadOnlySpan path) { var info = new FileInfo(GetAbsPath(path)); From 4dcabf400291f4f7e33272deb14e289f6f760823 Mon Sep 17 00:00:00 2001 From: Space V <40030799+spacevx@users.noreply.github.com> Date: Mon, 16 Nov 2026 03:10:32 +0100 Subject: [PATCH 3/7] Fix GetWritePath mod_write and null path ID fallback --- Source.Filesystem/BaseFileSystem.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Source.Filesystem/BaseFileSystem.cs b/Source.Filesystem/BaseFileSystem.cs index 0595378d..453b9e01 100644 --- a/Source.Filesystem/BaseFileSystem.cs +++ b/Source.Filesystem/BaseFileSystem.cs @@ -612,7 +612,6 @@ public void CreateDirHierarchy(ReadOnlySpan relativePath, ReadOnlySpan filename, ReadOnlySpan pathID) { ulong hash = pathID.Hash(); - if (hash == 0) return null; foreach (var searchPaths in SearchPaths) { foreach (var searchPath in searchPaths.Value.GetSortOrder()) { @@ -630,7 +629,7 @@ private ReadOnlySpan GetWritePath(ReadOnlySpan filename, ReadOnlySpa if (!pathID.IsEmpty && pathID.Length > 0) { if (pathID.Equals("game", StringComparison.OrdinalIgnoreCase)) searchPath = FindWritePath(filename, "game_write"); - else if (pathID.Equals("game", StringComparison.OrdinalIgnoreCase)) + else if (pathID.Equals("mod", StringComparison.OrdinalIgnoreCase)) searchPath = FindWritePath(filename, "mod_write"); searchPath ??= FindWritePath(filename, pathID); From 8ea736a7ed424479ad0ff101bd574727bc3ce90d Mon Sep 17 00:00:00 2001 From: Space V <40030799+spacevx@users.noreply.github.com> Date: Mon, 16 Nov 2026 03:45:50 +0100 Subject: [PATCH 4/7] Add filesystem performance todo --- Source.Filesystem/BaseFileSystem.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Source.Filesystem/BaseFileSystem.cs b/Source.Filesystem/BaseFileSystem.cs index 453b9e01..112206a1 100644 --- a/Source.Filesystem/BaseFileSystem.cs +++ b/Source.Filesystem/BaseFileSystem.cs @@ -19,6 +19,7 @@ namespace Source.FileSystem; // Maybe we redo this one day... +// todo: performance improvements from https://github.com/Facepunch/garrysmod-requests/issues/2530 public partial class BaseFileSystem : IFileSystem { readonly Lock SearchPathLock = new(); From 4649f04d7e9c41fe455d915e98120ec94e2a7b2c Mon Sep 17 00:00:00 2001 From: Space V <40030799+spacevx@users.noreply.github.com> Date: Mon, 16 Nov 2026 05:43:16 +0100 Subject: [PATCH 5/7] Fix disk search path deduplication --- Source.Filesystem/BaseFileSystem.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source.Filesystem/BaseFileSystem.cs b/Source.Filesystem/BaseFileSystem.cs index 112206a1..a5f4d691 100644 --- a/Source.Filesystem/BaseFileSystem.cs +++ b/Source.Filesystem/BaseFileSystem.cs @@ -143,7 +143,7 @@ private void AddSearchPathDiskInternal(ReadOnlySpan path, ReadOnlySpan Date: Mon, 16 Nov 2026 06:15:01 +0100 Subject: [PATCH 6/7] Match GMod map pack and VPK search path handling --- Source.Filesystem/BaseFileSystem.cs | 109 ++++++++++++++++++---------- 1 file changed, 69 insertions(+), 40 deletions(-) diff --git a/Source.Filesystem/BaseFileSystem.cs b/Source.Filesystem/BaseFileSystem.cs index a5f4d691..cf07f0f1 100644 --- a/Source.Filesystem/BaseFileSystem.cs +++ b/Source.Filesystem/BaseFileSystem.cs @@ -55,7 +55,35 @@ public BaseFileSystem() { AddSearchPath(AppContext.BaseDirectory, "BASE_PATH", name: PathGroupName.EngineCore); } + private void RemoveAllMapSearchPaths() { + foreach (KeyValuePair pair in SearchPaths) { + List addOrder = pair.Value.GetAddOrder(); + for (int i = addOrder.Count - 1; i >= 0; i--) { + if (addOrder[i] is not ZipPackFileSearchPath) + continue; + + RemoveSearchPathFromGroup(addOrder[i], SearchPaths.GetName(pair.Key)); + addOrder.RemoveAt(i); + pair.Value.InvalidateOrder(); + } + } + } + private void AddMapPackFile(ReadOnlySpan path, ReadOnlySpan pathID, SearchPathAdd addType, PathGroupName groupName) { + Span fullPath = stackalloc char[MAX_PATH]; + ReadOnlySpan newPath = RelativePathToFullPath(path, "GAME", fullPath); + if (newPath.IsEmpty) + return; + + foreach (SearchPathCollection existing in SearchPaths.Values) { + foreach (ISearchPath searchPath in existing.GetAddOrder()) { + if (searchPath is ZipPackFileSearchPath && searchPath.GetDiskPath().Equals(newPath, StringComparison.OrdinalIgnoreCase)) + return; + } + } + + RemoveAllMapSearchPaths(); + using IFileHandle? file = Open(path, FileOpenOptions.Read | FileOpenOptions.Binary, "GAME"); if (file == null) { Warning("Couldn't open BSP for embedded pack file\n"); @@ -71,58 +99,39 @@ private void AddMapPackFile(ReadOnlySpan path, ReadOnlySpan pathID, return; } - Span fullPath = stackalloc char[MAX_PATH]; - ReadOnlySpan newPath = RelativePathToFullPath(path, "GAME", fullPath); - - if (!SearchPaths.OpenOrCreateCollection(pathID, out SearchPathCollection collection)) { - for (int i = 0, c = collection.Count; i < c; i++) { - var searchPath = collection.GetAddOrder()[i]; - if (searchPath.GetDiskPath() == newPath) { - if ((addType == SearchPathAdd.ToHead && i == 0) || addType == SearchPathAdd.ToTail) - return; - else { - RemoveSearchPathFromGroup(searchPath, pathID); - collection.GetAddOrder().RemoveAt(i); - collection.InvalidateOrder(); - i--; - c--; - break; - } - } - } - } - ZipPackFileSearchPath zip = new(this, new(newPath), file.Stream, in pakfile); if (!zip.IsValid()) { Warning("ZipPackFileSearchPath not valid\n"); return; } + SearchPaths.OpenOrCreateCollection(pathID, out SearchPathCollection collection); AddSearchPathFinal(zip, addType, collection, groupName, pathID); } private void AddVPKFile(ReadOnlySpan path, ReadOnlySpan pathID, SearchPathAdd addType, PathGroupName groupName) { string newPath = Path.IsPathFullyQualified(path) ? new(path) : Path.GetFullPath(new(path)); + ISearchPath? createdSearchPath = null; if (!SearchPaths.OpenOrCreateCollection(pathID, out SearchPathCollection collection)) { - for (int i = 0, c = collection.Count; i < c; i++) { - var searchPath = collection.GetAddOrder()[i]; - if (searchPath.GetDiskPath() == newPath) { - if ((addType == SearchPathAdd.ToHead && i == 0) || addType == SearchPathAdd.ToTail) - return; - else { - RemoveSearchPathFromGroup(searchPath, pathID); - collection.GetAddOrder().RemoveAt(i); - collection.InvalidateOrder(); - i--; - c--; - break; - } - } + for (int i = collection.Count - 1; i >= 0; i--) { + ISearchPath searchPath = collection.GetAddOrder()[i]; + if (searchPath is not PackStoreSearchPath || !searchPath.GetDiskPath().Equals(newPath, StringComparison.OrdinalIgnoreCase)) + continue; + +#if GMOD_DLL + Msg($"DUPLICATE: [{searchPath.GetDiskPath()}]\n"); + RemoveSearchPathFromGroup(searchPath, pathID); + collection.GetAddOrder().RemoveAt(i); + collection.InvalidateOrder(); + createdSearchPath = searchPath; +#else + return; +#endif } } - ISearchPath createdSearchPath = new PackStoreSearchPath(this, newPath); + createdSearchPath ??= new PackStoreSearchPath(this, newPath); AddSearchPathFinal(createdSearchPath, addType, collection, groupName, pathID); } private void AddPackFiles(ReadOnlySpan path, ReadOnlySpan pathID, SearchPathAdd addType) { } // TODO @@ -143,7 +152,7 @@ private void AddSearchPathDiskInternal(ReadOnlySpan path, ReadOnlySpan path, ReadOnlySpan pathID) if (!SearchPaths.TryGetValue(hash, out var collection)) return false; + bool matchAll = false; + string newPath = ""; + + if (path.IsEmpty) { +#if GMOD_DLL + return false; +#else + matchAll = true; +#endif + } + else if (path.Contains(".bsp", StringComparison.OrdinalIgnoreCase)) { + Span fullPath = stackalloc char[MAX_PATH]; + newPath = new(RelativePathToFullPath(path, "GAME", fullPath)); + } + else { + newPath = Path.IsPathFullyQualified(path) ? new(path) : Path.GetFullPath(new(path)); + AddSeparatorAndFixPath(ref newPath); + } + bool ret = false; for (int i = collection.Count - 1; i >= 0; i--) { - if (collection.GetAddOrder()[i].GetDiskPath() != path) + if (!matchAll && !collection.GetAddOrder()[i].GetDiskPath().Equals(newPath, StringComparison.OrdinalIgnoreCase)) continue; RemoveSearchPathFromGroup(collection.GetAddOrder()[i], pathID); collection.GetAddOrder().RemoveAt(i); @@ -700,8 +728,9 @@ public void PrintSearchPaths() { ISearchPath spi = searchpath.path; ReadOnlySpan pack = ""; ReadOnlySpan type = ""; - if (false /* TODO: Map-based pack files */) { - // type = "(map)"; + if (spi is ZipPackFileSearchPath zip) { + type = "(map)"; + pack = zip.GetDiskPath(); } else if (spi is PackStoreSearchPath pssp) { type = "(VPK)"; From 7cf2d30150aec90fd862a2e8ebdcffad6fb1d695 Mon Sep 17 00:00:00 2001 From: Space V <40030799+spacevx@users.noreply.github.com> Date: Tue, 17 Nov 2026 03:44:35 +0100 Subject: [PATCH 7/7] Don't make RemoveSearchPath paths absolute like GMod --- Source.Filesystem/BaseFileSystem.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source.Filesystem/BaseFileSystem.cs b/Source.Filesystem/BaseFileSystem.cs index 1cbf097d..b355a356 100644 --- a/Source.Filesystem/BaseFileSystem.cs +++ b/Source.Filesystem/BaseFileSystem.cs @@ -542,7 +542,7 @@ public bool RemoveSearchPath(ReadOnlySpan path, ReadOnlySpan pathID) newPath = new(RelativePathToFullPath(path, "GAME", fullPath)); } else { - newPath = Path.IsPathFullyQualified(path) ? new(path) : Path.GetFullPath(new(path)); + newPath = new(path); AddSeparatorAndFixPath(ref newPath); }