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
9 changes: 9 additions & 0 deletions Source.Common/Exts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,15 @@ public static void FixSlashes(Span<char> name, char separator = CORRECT_PATH_SEP
name[i] = separator;
}
}
public static void StripTrailingSlash(Span<char> path) {
int len = (int)strlen(path);
if (len > 0 && path[len - 1].IsPathSeparator())
path[len - 1] = '\0';
}
public static bool IsAbsolutePath(ReadOnlySpan<char> str) {
char first = str.IsEmpty ? '\0' : str[0];
return (first != '\0' && str.Length > 1 && str[1] == ':') || first == '/' || first == '\\';
}

public static int StrLen(ReadOnlySpan<char> str) {
int i = str.IndexOf('\0');
Expand Down
171 changes: 116 additions & 55 deletions Source.Filesystem/BaseFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -54,7 +55,35 @@ public BaseFileSystem() {
AddSearchPath(AppContext.BaseDirectory, "BASE_PATH", name: PathGroupName.EngineCore);
}

private void RemoveAllMapSearchPaths() {
foreach (KeyValuePair<ulong, SearchPathCollection> pair in SearchPaths) {
List<ISearchPath> 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<char> path, ReadOnlySpan<char> pathID, SearchPathAdd addType, PathGroupName groupName) {
Span<char> fullPath = stackalloc char[MAX_PATH];
ReadOnlySpan<char> 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");
Expand All @@ -70,58 +99,39 @@ private void AddMapPackFile(ReadOnlySpan<char> path, ReadOnlySpan<char> pathID,
return;
}

Span<char> fullPath = stackalloc char[MAX_PATH];
ReadOnlySpan<char> 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<char> path, ReadOnlySpan<char> pathID, SearchPathAdd addType, PathGroupName groupName) {
string newPath = Path.IsPathFullyQualified(path) ? new(path) : Path.GetFullPath(new(path), AppContext.BaseDirectory);
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;
}
}
if (!SearchPaths.OpenOrCreateCollection(pathID, out SearchPathCollection collection)) {
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<char> path, ReadOnlySpan<char> pathID, SearchPathAdd addType) { } // TODO
Expand All @@ -142,7 +152,7 @@ private void AddSearchPathDiskInternal(ReadOnlySpan<char> path, ReadOnlySpan<cha
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 (searchPath.GetDiskPath().Equals(newPath, StringComparison.OrdinalIgnoreCase)) {
if ((addType == SearchPathAdd.ToHead && i == 0) || addType == SearchPathAdd.ToTail)
return;
else {
Expand Down Expand Up @@ -226,7 +236,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++;
Expand Down Expand Up @@ -345,6 +355,9 @@ public bool FullPathToRelativePathEx(ReadOnlySpan<char> fullPath, ReadOnlySpan<c
if (!pathID.IsEmpty && searchPaths.Key != pathIDHash)
continue;

if (pathID.IsEmpty && searchPaths.Value.RequestOnly)
continue;

foreach (var searchPath in searchPaths.Value.GetSortOrder()) {
if (searchPath is not DiskSearchPath)
continue;
Expand Down Expand Up @@ -383,7 +396,37 @@ readonly ref struct IsDirectory_Op() : IFirstToThePostOp<bool>
}

public bool IsDirectory(ReadOnlySpan<char> fileName, ReadOnlySpan<char> pathID) {
return FirstToThePost(fileName, pathID, new IsDirectory_Op(), false, out _);
Span<char> tempBuf = stackalloc char[MAX_PATH];
strcpy(tempBuf, fileName);
StrTools.StripTrailingSlash(tempBuf);
scoped ReadOnlySpan<char> name = tempBuf.SliceNullTerminatedString();

Span<char> tempPathID = stackalloc char[MAX_PATH];
scoped ReadOnlySpan<char> 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<char> fileName, scoped ref ReadOnlySpan<char> pathID, Span<char> 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<bool>
Expand All @@ -399,12 +442,7 @@ public bool IsFileWritable(ReadOnlySpan<char> fileName, ReadOnlySpan<char> pathI

public void MarkPathIDByRequestOnly(ReadOnlySpan<char> 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<char> pathID, bool getPackFiles, Span<char> dest) {
Expand Down Expand Up @@ -457,8 +495,7 @@ private static ReadOnlySpan<char> FixUpPath(ReadOnlySpan<char> fileName, Span<ch
StrTools.FixDoubleSlashes(fixedUpFileName);

Span<char> 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]);
}
Expand Down Expand Up @@ -490,10 +527,29 @@ public bool RemoveSearchPath(ReadOnlySpan<char> path, ReadOnlySpan<char> 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)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a RemoveVPKFile branch I believe

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(may need a double check)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checked in the gmod binaries there's no RemoveVPKFile branch in RemoveSearchPath (checked on windows/linux/macos)
See CBaseFileSystem::RemoveSearchPath in IDA

Span<char> fullPath = stackalloc char[MAX_PATH];
newPath = new(RelativePathToFullPath(path, "GAME", fullPath));
}
else {
newPath = 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);
Expand Down Expand Up @@ -585,7 +641,6 @@ public void CreateDirHierarchy(ReadOnlySpan<char> relativePath, ReadOnlySpan<cha
}
private ISearchPath? FindWritePath(ReadOnlySpan<char> filename, ReadOnlySpan<char> pathID) {
ulong hash = pathID.Hash();
if (hash == 0) return null;

foreach (var searchPaths in SearchPaths) {
foreach (var searchPath in searchPaths.Value.GetSortOrder()) {
Expand All @@ -603,7 +658,7 @@ private ReadOnlySpan<char> GetWritePath(ReadOnlySpan<char> 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);
Expand Down Expand Up @@ -673,8 +728,9 @@ public void PrintSearchPaths() {
ISearchPath spi = searchpath.path;
ReadOnlySpan<char> pack = "";
ReadOnlySpan<char> 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)";
Expand Down Expand Up @@ -864,8 +920,13 @@ public ReadOnlySpan<char> 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;
Expand Down
13 changes: 10 additions & 3 deletions Source.Filesystem/DiskSearchPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CaseInsensitiveCache {
public required string RealPath;
}
static readonly Dictionary<ulong, CaseInsensitiveCache> 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;
Expand Down Expand Up @@ -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
Expand All @@ -71,7 +71,14 @@ public override bool Exists(ReadOnlySpan<char> path) {

public bool ExistsOnDisk(ReadOnlySpan<char> path) => Path.Exists(GetAbsPath(path));
public string GetFullPath(ReadOnlySpan<char> path) => GetAbsPath(path).Replace('\\', '/');
public override bool IsDirectory(ReadOnlySpan<char> path) => Directory.Exists(GetAbsPath(path));
public override bool IsDirectory(ReadOnlySpan<char> 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<char> path) {
var info = new FileInfo(GetAbsPath(path));
Expand Down
13 changes: 12 additions & 1 deletion Source.Filesystem/SearchPathIDCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class SearchPathIDCollection : Dictionary<ulong, SearchPathCollection>
{
readonly List<ulong> pathOrder = [];
readonly Dictionary<ulong, string> searchPathNames = [];
readonly HashSet<ulong> requestOnlyPathIDs = [];

/// <summary>
///
Expand All @@ -20,13 +21,23 @@ public bool OpenOrCreateCollection(in ReadOnlySpan<char> 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<char> pathID) => Remove(pathID.Hash());

public new bool Remove(ulong hashID) {
Expand Down
Loading