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
27 changes: 23 additions & 4 deletions Game.Client/C_Soundscape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -712,13 +712,32 @@ void AddSoundScapeFile(ReadOnlySpan<char> filename) {
}
}

void TouchPlayLooping(KeyValues? ambient) => throw new NotImplementedException();
void TouchPlayLooping(KeyValues? ambient) {
for (KeyValues? key = ambient?.GetFirstSubKey(); key != null; key = key.GetNextKey()) {
if (stricmp(key.Name, "wave") == 0)
TouchSoundFile(key.GetString());
}
}

void TouchPlayRandom(KeyValues? playRandom) => throw new NotImplementedException();
void TouchPlayRandom(KeyValues? playRandom) {
for (KeyValues? key = playRandom?.GetFirstSubKey(); key != null; key = key.GetNextKey()) {
if (stricmp(key.Name, "rndwave") == 0) {
for (KeyValues? waves = key.GetFirstSubKey(); waves != null; waves = waves.GetNextKey())
TouchSoundFile(waves.GetString());
}
}
}

void TouchWaveFiles(KeyValues? soundScape) => throw new NotImplementedException();
void TouchWaveFiles(KeyValues? soundScape) {
for (KeyValues? key = soundScape?.GetFirstSubKey(); key != null; key = key.GetNextKey()) {
if (stricmp(key.Name, "playlooping") == 0)
TouchPlayLooping(key);
else if (stricmp(key.Name, "playrandom") == 0)
TouchPlayRandom(key);
}
}

void TouchSoundFile(ReadOnlySpan<char> wavefile) => throw new NotImplementedException();
void TouchSoundFile(ReadOnlySpan<char> wavefile) => filesystem.GetFileTime($"sound/{SoundCharsUtils.SkipSoundChars(wavefile)}", "GAME");

void TouchSoundFiles() {
if (commandLine.FindParm("-makereslists") == 0)
Expand Down
6 changes: 3 additions & 3 deletions Game.Lua/LuaSharedImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ public void CloseLuaInterface(ILuaInterface iface) {
if (handle == null)
return null;

using MemoryStream buffer = new();
handle.Stream.CopyTo(buffer);
return buffer.ToArray();
byte[] data = new byte[handle.Stream.Length];
handle.Stream.ReadExactly(data);
return data;
}

LuaFile? LoadFile_FromDataTable(ReadOnlySpan<char> path, ReadOnlySpan<char> pathId, bool unk) {
Expand Down
16 changes: 11 additions & 5 deletions Game.Server/AI_NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,19 @@ public void LoadNetworkGraph() {

string nrpFilename = $"maps/graphs/{gpGlobals.MapName}.ain";

using IFileHandle? handle = filesystem.Open(nrpFilename, FileOpenOptions.Read | FileOpenOptions.Binary, "GAME");
if (handle == null) {
DevWarning(2, $"Couldn't read {nrpFilename}!\n");
return;
byte[] buf;
using (IFileHandle? handle = filesystem.Open(nrpFilename, FileOpenOptions.Read | FileOpenOptions.Binary, "GAME")) {
if (handle == null) {
DevWarning(2, $"Couldn't read {nrpFilename}!\n");
return;
}

buf = new byte[handle.Stream.Length];
handle.Stream.ReadExactly(buf);
}

LoadNetworkGraph(handle.Stream, nrpFilename, gpGlobals.MapVersion);
using MemoryStream stream = new(buf, false);
LoadNetworkGraph(stream, nrpFilename, gpGlobals.MapVersion);
}

public void LoadNetworkGraph(Stream stream, ReadOnlySpan<char> nrpFilename, int currentMapVersion) {
Expand Down
19 changes: 19 additions & 0 deletions Source.Common/Filesystem/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ public enum FSAsyncFlags
/// </summary>
public delegate void FSAsyncCallbackFunc(in FileAsyncRequest request, int bytesRead, FSAsyncStatus err);
public delegate byte[] FSAllocFunc(string fileName, int bytes);
public delegate void FileSystemLoggingFunc(ReadOnlySpan<char> fileName, ReadOnlySpan<char> accessType);
public delegate void FileSystemWarningFunc(ReadOnlySpan<char> fmt, ReadOnlySpan<object?> args);

public enum FileWarningLevel
{
Warning = -1,
Quiet = 0,
ReportUnclosed,
ReportUsage,
ReportAllAccesses,
ReportAllAccessesRead,
ReportAllAccessesReadWrite,
ReportAllAccessesAsync,
}

/// <summary>
/// Description of an async request
Expand Down Expand Up @@ -300,7 +314,12 @@ public interface IFileSystem : IBaseFileSystem
bool WriteFile(ReadOnlySpan<char> fileName, ReadOnlySpan<char> pathID, ReadOnlySpan<byte> buf);
void MarkAllCRCsUnverified();
ReadOnlySpan<char> WhereIsFile(ReadOnlySpan<char> relativePath, ReadOnlySpan<char> pathID = default);
void PrintOpenedFiles();
void PrintSearchPaths();
void SetWarningFunc(FileSystemWarningFunc? warning);
void SetWarningLevel(FileWarningLevel level);
void AddLoggingFunc(FileSystemLoggingFunc logFunc);
void RemoveLoggingFunc(FileSystemLoggingFunc logFunc);

/// <summary>
/// FileNameHandle_t's are case-insensitive and slash-insensitive.
Expand Down
7 changes: 7 additions & 0 deletions Source.Common/Formats/Keyvalues/KeyValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ public bool LoadFromStream(Stream? stream) {
// Clear();
if (stream == null) return false;

if (stream.CanSeek) {
byte[] data = new byte[stream.Length - stream.Position];
stream.ReadExactly(data);
stream.Dispose();
stream = new MemoryStream(data, false);
}

// Size the buffer to the stream when we can, so the text is decoded in one piece.
int bufferSize = stream.CanSeek ? (int)Math.Clamp(stream.Length - stream.Position, 1024, 1 << 20) : 1024;
using StreamReader streamReader = new(stream, System.Text.Encoding.UTF8, true, bufferSize);
Expand Down
5 changes: 4 additions & 1 deletion Source.Engine/ClientLauncherAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ public IClientLauncherAPI.Result RunListenServer() {


public void SetStartupInfo(in StartupInfo info) {
startupInfo = info;
g_pFileSystem.SetWarningFunc(Warning);

Sys.TextMode = info.TextMode;
host_parms.BaseDir = info.BaseDirectory;
startupInfo = info;
COM.InitFilesystem(info.InitialMod);
}

Expand Down
14 changes: 12 additions & 2 deletions Source.Engine/Cmd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,18 @@ void exec(in TokenizedCommand args) {
}
}

using var execFile = fileSystem.Open(file, FileOpenOptions.Read, pathID);
using StreamReader reader = new StreamReader(execFile!.Stream);
byte[] buf;
using (IFileHandle? execFile = fileSystem.Open(file, FileOpenOptions.Read, pathID)) {
if (execFile == null) {
Dbg.ConMsg($"exec: couldn't exec {args[1]}\n");
return;
}

buf = new byte[execFile.Stream.Length];
execFile.Stream.ReadExactly(buf);
}

using StreamReader reader = new StreamReader(new MemoryStream(buf, false));
Dbg.ConDMsg($"execing {file}\n");
LinkedListNode<CommandBuffer.Command>? hCommand = Cbuf.Buffer.GetNextCommandHandle();
// check to make sure we're not going to overflow later
Expand Down
2 changes: 2 additions & 0 deletions Source.Engine/DedicatedServerAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public bool ModInit(in StartupInfo info) {
eng ??= services.GetRequiredService<IEngine>();
eng.SetQuitting(IEngine.Quit.NotQuitting);

g_pFileSystem.SetWarningFunc(Warning);

host_parms.BaseDir = info.BaseDirectory;
host_parms.Mod = GetModDirFromPath(info.InitialMod);
host_parms.Game = info.InitialGame;
Expand Down
50 changes: 50 additions & 0 deletions Source.Engine/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,54 @@ void path(in TokenizedCommand args) {
fileSystem.PrintSearchPaths();
}

[ConCommand(helpText: "Show all files currently opened by the engine.")]
void fs_printopenfiles() {
fileSystem.PrintOpenedFiles();
}

[ConCommand(helpText: "Set the filesystem warning level.")]
void fs_warning_level(in TokenizedCommand args) {
if (args.ArgC() != 2) {
Warning("\"fs_warning_level n\" where n is one of:\n");
Warning("\t0:\tFILESYSTEM_WARNING_QUIET\n");
Warning("\t1:\tFILESYSTEM_WARNING_REPORTUNCLOSED\n");
Warning("\t2:\tFILESYSTEM_WARNING_REPORTUSAGE\n");
Warning("\t3:\tFILESYSTEM_WARNING_REPORTALLACCESSES\n");
Warning("\t4:\tFILESYSTEM_WARNING_REPORTALLACCESSES_READ\n");
Warning("\t5:\tFILESYSTEM_WARNING_REPORTALLACCESSES_READWRITE\n");
Warning("\t6:\tFILESYSTEM_WARNING_REPORTALLACCESSES_ASYNC\n");
return;
}

int level = atoi(args[1]);
switch ((FileWarningLevel)level) {
case FileWarningLevel.Quiet:
Warning("fs_warning_level = FILESYSTEM_WARNING_QUIET\n");
break;
case FileWarningLevel.ReportUnclosed:
Warning("fs_warning_level = FILESYSTEM_WARNING_REPORTUNCLOSED\n");
break;
case FileWarningLevel.ReportUsage:
Warning("fs_warning_level = FILESYSTEM_WARNING_REPORTUSAGE\n");
break;
case FileWarningLevel.ReportAllAccesses:
Warning("fs_warning_level = FILESYSTEM_WARNING_REPORTALLACCESSES\n");
break;
case FileWarningLevel.ReportAllAccessesRead:
Warning("fs_warning_level = FILESYSTEM_WARNING_REPORTALLACCESSES_READ\n");
break;
case FileWarningLevel.ReportAllAccessesReadWrite:
Warning("fs_warning_level = FILESYSTEM_WARNING_REPORTALLACCESSES_READWRITE\n");
break;
case FileWarningLevel.ReportAllAccessesAsync:
Warning("fs_warning_level = FILESYSTEM_WARNING_REPORTALLACCESSES_ASYNC\n");
break;
default:
Warning("fs_warning_level = UNKNOWN!!!!!!!\n");
return;
}

fileSystem.SetWarningLevel((FileWarningLevel)level);
}

}
101 changes: 95 additions & 6 deletions Source.Filesystem/BaseFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using CommunityToolkit.HighPerformance;

using Source.Common;
using Source.Common.Commands;
using Source.Common.Filesystem;
using Source.Common.Formats.BSP;
using Source.Common.Formats.Keyvalues;
Expand All @@ -12,6 +14,7 @@
using Source.Filesystem.GarrysMod;

using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
Expand Down Expand Up @@ -86,7 +89,7 @@ private void AddMapPackFile(ReadOnlySpan<char> path, ReadOnlySpan<char> pathID,

using IFileHandle? file = Open(path, FileOpenOptions.Read | FileOpenOptions.Binary, "GAME");
if (file == null) {
Warning("Couldn't open BSP for embedded pack file\n");
Warning(FileWarningLevel.Warning, $"Couldn't open bsp {newPath} for embedded pack file check\n");
return;
}

Expand All @@ -101,7 +104,7 @@ private void AddMapPackFile(ReadOnlySpan<char> path, ReadOnlySpan<char> pathID,

ZipPackFileSearchPath zip = new(this, new(newPath), file.Stream, in pakfile);
if (!zip.IsValid()) {
Warning("ZipPackFileSearchPath not valid\n");
Dbg.Warning("ZipPackFileSearchPath not valid\n");
return;
}

Expand Down Expand Up @@ -410,14 +413,14 @@ public bool IsDirectory(ReadOnlySpan<char> fileName, ReadOnlySpan<char> pathID)
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) {
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");
Warning(FileWarningLevel.Warning, $"FS: Specified two path IDs ({fileName}, {pathID}).\n");

int read = 2;
int written = 0;
Expand Down Expand Up @@ -665,7 +668,7 @@ private ReadOnlySpan<char> GetWritePath(ReadOnlySpan<char> filename, ReadOnlySpa
if (searchPath != null)
return searchPath.GetPathString();

Warning("Requested non-existent write path %s!\n", new string(pathID));
Warning(FileWarningLevel.Warning, $"Requested non-existent write path {pathID}!\n");
}

searchPath = FindWritePath(filename, "DEFAULT_WRITE_PATH");
Expand Down Expand Up @@ -713,6 +716,92 @@ public ReadOnlySpan<char> WhereIsFile(ReadOnlySpan<char> fileName, ReadOnlySpan<
return null;
}

static readonly ConVar fs_warning_mode = new("fs_warning_mode", "0", 0, "0:Off, 1:Warn main thread, 2:Warn other threads");

FileWarningLevel WarningLevel = FileWarningLevel.ReportUnclosed;
FileSystemWarningFunc? WarningFunc;

int OpenedFiles;
internal readonly List<FileSystemLoggingFunc> LogFuncs = [];

public void SetWarningFunc(FileSystemWarningFunc? warning) => WarningFunc = warning;

public void SetWarningLevel(FileWarningLevel level) => WarningLevel = level;

internal void Warning(FileWarningLevel level, ReadOnlySpan<char> text) {
if (level > WarningLevel)
return;

if ((fs_warning_mode.GetInt() == 1 && !ThreadInMainThread()) || (fs_warning_mode.GetInt() == 2 && ThreadInMainThread()))
return;

string warningText = new(text.Length > 4095 ? text[..4095] : text);

Console.Write(warningText);

WarningFunc?.Invoke("%s", [warningText]);
}

static string TraceThreadTag() => ThreadInMainThread() ? "" : "[a]";
static string TraceTime() => Platform.Time.ToString("F3", CultureInfo.InvariantCulture);
#if WIN32
static string TracePointer(object file) => RuntimeHelpers.GetHashCode(file).ToString("X16", CultureInfo.InvariantCulture);
#else
static string TracePointer(object file) => "0x" + RuntimeHelpers.GetHashCode(file).ToString("x", CultureInfo.InvariantCulture);
#endif

internal void LogAccessToFile(ReadOnlySpan<char> accesstype, ReadOnlySpan<char> fullpath, ReadOnlySpan<char> options) {
lock (LogFuncs) {
if (WarningLevel >= FileWarningLevel.ReportAllAccesses && !fullpath.Contains("console.log", StringComparison.OrdinalIgnoreCase))
Warning(FileWarningLevel.ReportAllAccesses, $"---FS{TraceThreadTag()}: {accesstype} {fullpath} ({TraceTime()})\n");

for (int i = 0; i < LogFuncs.Count; ++i)
LogFuncs[i](fullpath, options);
}
}

internal void Trace_FOpen(string filename, ReadOnlySpan<char> options) {
Interlocked.Increment(ref OpenedFiles);

LogAccessToFile("open", filename, options);
}

internal void Trace_FClose(object file, string name) {
if (WarningLevel >= FileWarningLevel.ReportAllAccesses && !name.Contains("console.log", StringComparison.OrdinalIgnoreCase))
Warning(FileWarningLevel.ReportAllAccesses, $"---FS{TraceThreadTag()}: close {name} {TracePointer(file)} {OpenedFiles} ({TraceTime()})\n");

Interlocked.Decrement(ref OpenedFiles);
}

internal void Trace_FRead(int size, object file, string name) {
if (WarningLevel < FileWarningLevel.ReportAllAccessesRead)
return;

Warning(FileWarningLevel.ReportAllAccessesRead, $"---FS{TraceThreadTag()}: read {name} {size} {TracePointer(file)} ({TraceTime()})\n");
}

internal void Trace_FWrite(int size, object file, string name) {
if (WarningLevel < FileWarningLevel.ReportAllAccessesReadWrite)
return;

Warning(FileWarningLevel.ReportAllAccessesReadWrite, $"---FS{TraceThreadTag()}: write {name} {size} {TracePointer(file)}\n");
}

public void PrintOpenedFiles() {
}

public void AddLoggingFunc(FileSystemLoggingFunc logFunc) {
lock (LogFuncs) {
Assert(!LogFuncs.Contains(logFunc));
LogFuncs.Add(logFunc);
}
}

public void RemoveLoggingFunc(FileSystemLoggingFunc logFunc) {
lock (LogFuncs)
LogFuncs.Remove(logFunc);
}

public void PrintSearchPaths() {
Msg("Paths:\n");

Expand Down Expand Up @@ -959,7 +1048,7 @@ void PrepareFinds(ISearchPath path) {

public void Close() {
if (Locked == 0) {
Warning("Tried to unlock a file handle that was already unlocked!!!\n");
Dbg.Warning("Tried to unlock a file handle that was already unlocked!!!\n");
Assert(false);
return;
}
Expand Down
Loading
Loading