Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions Source.Common/Filesystem/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ public interface ISearchPath
ReadOnlySpan<char> GetPathString();
object? GetPackFile();
object? GetPackedStore();
void UnlockFinds();
void LockFinds(UtlSymbol wildcard, HashSet<ulong> foundAlready);
(string, bool)? FindAt(int index);
void PrepareFinds(List<string> files, List<string> dirs, string? wildcard);
PathGroupName GetGroupName();
void SetGroupName(PathGroupName name);
ReadOnlySpan<char> GetDiskPath();
Expand Down
110 changes: 64 additions & 46 deletions Source.Filesystem/BaseFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,8 @@ public struct FileFindContext
SearchPathCollection? currentCollection;
ISearchPath? currentPath;
HashSet<FileNameHandle_t>? foundAlready;
List<string>? files;
List<string>? dirs;
#if GMOD_DLL
List<SearchFile>? addonFiles;
#endif
Expand Down Expand Up @@ -752,6 +754,10 @@ public void Reset() {

foundAlready ??= [];
foundAlready.Clear();
files ??= [];
files.Clear();
dirs ??= [];
dirs.Clear();
#if GMOD_DLL
addonFiles ??= [];
addonFiles.Clear();
Expand All @@ -761,62 +767,75 @@ public void Reset() {


public ReadOnlySpan<char> 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() {
Expand All @@ -826,7 +845,6 @@ public void Close() {
return;
}

currentPath?.UnlockFinds();
Locked = 0;
Reset();
}
Expand Down
43 changes: 1 addition & 42 deletions Source.Filesystem/BaseSearchPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,46 +36,5 @@ public void SetDiskPath(scoped ReadOnlySpan<char> diskPath) {
public abstract object? GetPackFile();
public abstract object? GetPackedStore();

protected abstract void PrepareFinds(List<string> files, List<string> dirs, string? wildcard);

uint FindsIdx;
readonly List<string> files = [];
readonly List<string> dirs = [];
public void LockFinds(UtlSymbol wildcard, HashSet<UtlSymId_t> 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<string> files, List<string> dirs, string? wildcard);
}
2 changes: 1 addition & 1 deletion Source.Filesystem/DiskSearchPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public override DateTime Time(ReadOnlySpan<char> path) {
return null;
}

protected override void PrepareFinds(List<string> files, List<string> dirs, string? wildcard) {
public override void PrepareFinds(List<string> files, List<string> dirs, string? wildcard) {
IEnumerable<string> fileSearch, dirSearch;

if (wildcard != null) {
Expand Down
2 changes: 1 addition & 1 deletion Source.Filesystem/GarrysMod/AddonSearchPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> files, List<string> dirs, string? wildcard) {
public override void PrepareFinds(List<string> files, List<string> dirs, string? wildcard) {
List<SearchFile> results = [];
Addons.FindFirst(wildcard ?? "*", results, null);

Expand Down
2 changes: 1 addition & 1 deletion Source.Filesystem/PackStoreSearchPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public override ReadOnlySpan<char> GetPathString() {
return DiskPath;
}

protected override void PrepareFinds(List<string> files, List<string> dirs, string? wildcard) {
public override void PrepareFinds(List<string> files, List<string> dirs, string? wildcard) {
ReadOnlySpan<char> wildcardDir = string.Empty, wildcardFile = string.Empty, wildcardExt = string.Empty;
wildcard?.FileInfo(null, out wildcardDir, out wildcardFile, out wildcardExt);

Expand Down
2 changes: 1 addition & 1 deletion Source.Filesystem/ZipPackFileSearchPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public override DateTime Time(ReadOnlySpan<char> path) {

public override ReadOnlySpan<char> GetPathString() => this.DiskPath;

protected override void PrepareFinds(List<string> files, List<string> dirs, string? wildcard) {
public override void PrepareFinds(List<string> files, List<string> dirs, string? wildcard) {
// TODO.
}
}
Loading