From 6b66701ee513eaa3162ad23ad3311c7f6ed0c306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Sat, 10 Oct 2026 20:06:21 +0300 Subject: [PATCH] Fix nested file finds sharing their results --- Source.Common/Filesystem/IFileSystem.cs | 4 +- Source.Filesystem/BaseFileSystem.cs | 110 ++++++++++-------- Source.Filesystem/BaseSearchPath.cs | 43 +------ Source.Filesystem/DiskSearchPath.cs | 2 +- .../GarrysMod/AddonSearchPath.cs | 2 +- Source.Filesystem/PackStoreSearchPath.cs | 2 +- Source.Filesystem/ZipPackFileSearchPath.cs | 2 +- 7 files changed, 70 insertions(+), 95 deletions(-) diff --git a/Source.Common/Filesystem/IFileSystem.cs b/Source.Common/Filesystem/IFileSystem.cs index 8d92f153d..39434edcc 100644 --- a/Source.Common/Filesystem/IFileSystem.cs +++ b/Source.Common/Filesystem/IFileSystem.cs @@ -124,9 +124,7 @@ public interface ISearchPath ReadOnlySpan GetPathString(); object? GetPackFile(); object? GetPackedStore(); - void UnlockFinds(); - void LockFinds(UtlSymbol wildcard, HashSet foundAlready); - (string, bool)? FindAt(int index); + void PrepareFinds(List files, List dirs, string? wildcard); PathGroupName GetGroupName(); void SetGroupName(PathGroupName name); ReadOnlySpan GetDiskPath(); diff --git a/Source.Filesystem/BaseFileSystem.cs b/Source.Filesystem/BaseFileSystem.cs index 60dfe464d..2f9a0643b 100644 --- a/Source.Filesystem/BaseFileSystem.cs +++ b/Source.Filesystem/BaseFileSystem.cs @@ -718,6 +718,8 @@ public struct FileFindContext SearchPathCollection? currentCollection; ISearchPath? currentPath; HashSet? foundAlready; + List? files; + List? dirs; #if GMOD_DLL List? addonFiles; #endif @@ -752,6 +754,10 @@ public void Reset() { foundAlready ??= []; foundAlready.Clear(); + files ??= []; + files.Clear(); + dirs ??= []; + dirs.Clear(); #if GMOD_DLL addonFiles ??= []; addonFiles.Clear(); @@ -761,62 +767,75 @@ public void Reset() { public ReadOnlySpan Next() { - findCollection: - if (currentCollection == null) { - currentCollection = PathID == 0 - ? system.SearchPaths.At(Interlocked.Increment(ref CollectionIdx)) - : Interlocked.CompareExchange(ref ranAtLeastOnce, 1, 0) == 0 - ? (system.SearchPaths.TryGetValue(PathID.String().Hash(), out var found) ? found : null) - : null; + while (true) { + if (currentPath != null) { + var currentFile = FindAt(Interlocked.Increment(ref FileIdx)); + if (currentFile.HasValue) { + IsDirectory = currentFile.Value.Item2; + return currentFile.Value.Item1; + } + currentPath = null; + } if (currentCollection != null) { - // Reset these parts... - Interlocked.Exchange(ref FileIdx, -1); - Interlocked.Exchange(ref PathIdx, -1); - goto findPath; // We don't need to perform the next check + currentPath = currentCollection.AtSorted(Interlocked.Increment(ref PathIdx)); + if (currentPath != null) { + PrepareFinds(currentPath); + Interlocked.Exchange(ref FileIdx, -1); + continue; + } + currentCollection = null; } - } - if (currentCollection == null) { + + currentCollection = NextCollection(); + if (currentCollection == null) { #if GMOD_DLL - if (addonFiles!.Count != 0) { - SearchFile file = addonFiles[0]; - addonFiles.RemoveAt(0); - IsDirectory = file.Folder; - return file.FileName; - } + if (addonFiles!.Count != 0) { + SearchFile file = addonFiles[0]; + addonFiles.RemoveAt(0); + IsDirectory = file.Folder; + return file.FileName; + } #endif - return null; // Cannot continue. + return null; + } + + Interlocked.Exchange(ref FileIdx, -1); + Interlocked.Exchange(ref PathIdx, -1); } + } - findPath: - if (currentPath == null) { - // Find the next collection. - currentPath = currentCollection.AtSorted(Interlocked.Increment(ref PathIdx)); + SearchPathCollection? NextCollection() { + if (PathID == 0) + return system.SearchPaths.At(Interlocked.Increment(ref CollectionIdx)); - if (currentPath != null) { - currentPath.LockFinds(Wildcard, foundAlready!); - Interlocked.Exchange(ref FileIdx, -1); - // We don't need to perform the next check - goto findFileDir; - } - } + if (Interlocked.CompareExchange(ref ranAtLeastOnce, 1, 0) == 0) + return system.SearchPaths.TryGetValue(PathID.String().Hash(), out var found) ? found : null; - if (currentPath == null) { - // Search for a new collection? - currentCollection = null; - goto findCollection; - } + return null; + } + + void PrepareFinds(ISearchPath path) { + files!.Clear(); + dirs!.Clear(); + path.PrepareFinds(files, dirs, Wildcard.String()); + for (int i = dirs.Count - 1; i >= 0; i--) + if (!foundAlready!.Add(dirs[i].Hash())) + dirs.RemoveAt(i); + for (int i = files.Count - 1; i >= 0; i--) + if (!foundAlready!.Add(files[i].Hash())) + files.RemoveAt(i); + } - findFileDir: - var currentFile = currentPath.FindAt(Interlocked.Increment(ref FileIdx)); - if (!currentFile.HasValue) { - // Search for a new path? - currentPath.UnlockFinds(); - currentPath = null; - goto findPath; + readonly (string, bool)? FindAt(int index) { + if (index >= files!.Count) { + if (index >= (files.Count + dirs!.Count)) + return null; + else + return (dirs[index - files.Count], true); } - IsDirectory = currentFile.Value.Item2; - return currentFile.Value.Item1; + else + return (files[index], false); } public void Close() { @@ -826,7 +845,6 @@ public void Close() { return; } - currentPath?.UnlockFinds(); Locked = 0; Reset(); } diff --git a/Source.Filesystem/BaseSearchPath.cs b/Source.Filesystem/BaseSearchPath.cs index d1ded4edc..98d94927a 100644 --- a/Source.Filesystem/BaseSearchPath.cs +++ b/Source.Filesystem/BaseSearchPath.cs @@ -36,46 +36,5 @@ public void SetDiskPath(scoped ReadOnlySpan diskPath) { public abstract object? GetPackFile(); public abstract object? GetPackedStore(); - protected abstract void PrepareFinds(List files, List dirs, string? wildcard); - - uint FindsIdx; - readonly List files = []; - readonly List dirs = []; - public void LockFinds(UtlSymbol wildcard, HashSet foundAlready) { - if (Interlocked.Increment(ref FindsIdx) == 1) { - // Prepare the find buffers... - // unfortunately requires a lock here. - lock (files) - lock (dirs) { - files.Clear(); - dirs.Clear(); - PrepareFinds(files, dirs, wildcard.String()); - for (int i = dirs.Count - 1; i >= 0; i--) - if (!foundAlready.Add(dirs[i].Hash())) - dirs.RemoveAt(i); - for (int i = files.Count - 1; i >= 0; i--) - if (!foundAlready.Add(files[i].Hash())) - files.RemoveAt(i); - } - - } - } - public void UnlockFinds() { - Interlocked.Decrement(ref FindsIdx); - } - public (string, bool)? FindAt(int index) { - if (Interlocked.CompareExchange(ref FindsIdx, 0, 0) == 0) { - AssertMsg(false, "Unlocked find attempt"); - return null; - } - - if (index >= files.Count) { - if (index >= (files.Count + dirs.Count)) - return null; - else - return (dirs[index - files.Count], true); - } - else - return (files[index], false); - } + public abstract void PrepareFinds(List files, List dirs, string? wildcard); } diff --git a/Source.Filesystem/DiskSearchPath.cs b/Source.Filesystem/DiskSearchPath.cs index cce022187..98fcdb9c0 100644 --- a/Source.Filesystem/DiskSearchPath.cs +++ b/Source.Filesystem/DiskSearchPath.cs @@ -168,7 +168,7 @@ public override DateTime Time(ReadOnlySpan path) { return null; } - protected override void PrepareFinds(List files, List dirs, string? wildcard) { + public override void PrepareFinds(List files, List dirs, string? wildcard) { IEnumerable fileSearch, dirSearch; if (wildcard != null) { diff --git a/Source.Filesystem/GarrysMod/AddonSearchPath.cs b/Source.Filesystem/GarrysMod/AddonSearchPath.cs index 332ddb415..032f7780a 100644 --- a/Source.Filesystem/GarrysMod/AddonSearchPath.cs +++ b/Source.Filesystem/GarrysMod/AddonSearchPath.cs @@ -32,7 +32,7 @@ public AddonSearchPath(AddonFileSystem addons, string absPath) { public override object? GetPackFile() => null; public override object? GetPackedStore() => null; - protected override void PrepareFinds(List files, List dirs, string? wildcard) { + public override void PrepareFinds(List files, List dirs, string? wildcard) { List results = []; Addons.FindFirst(wildcard ?? "*", results, null); diff --git a/Source.Filesystem/PackStoreSearchPath.cs b/Source.Filesystem/PackStoreSearchPath.cs index 4a8f7cbeb..1176f85e7 100644 --- a/Source.Filesystem/PackStoreSearchPath.cs +++ b/Source.Filesystem/PackStoreSearchPath.cs @@ -127,7 +127,7 @@ public override ReadOnlySpan GetPathString() { return DiskPath; } - protected override void PrepareFinds(List files, List dirs, string? wildcard) { + public override void PrepareFinds(List files, List dirs, string? wildcard) { ReadOnlySpan wildcardDir = string.Empty, wildcardFile = string.Empty, wildcardExt = string.Empty; wildcard?.FileInfo(null, out wildcardDir, out wildcardFile, out wildcardExt); diff --git a/Source.Filesystem/ZipPackFileSearchPath.cs b/Source.Filesystem/ZipPackFileSearchPath.cs index c2ae27989..8da0b3680 100644 --- a/Source.Filesystem/ZipPackFileSearchPath.cs +++ b/Source.Filesystem/ZipPackFileSearchPath.cs @@ -109,7 +109,7 @@ public override DateTime Time(ReadOnlySpan path) { public override ReadOnlySpan GetPathString() => this.DiskPath; - protected override void PrepareFinds(List files, List dirs, string? wildcard) { + public override void PrepareFinds(List files, List dirs, string? wildcard) { // TODO. } }