Add async file op natives (#1674) (#2524)

This commit is contained in:
Nicholas Hastings
2026-07-30 06:53:45 -04:00
committed by GitHub
parent e851097670
commit 8bc615f29b
6 changed files with 1742 additions and 5 deletions
+227
View File
@@ -610,6 +610,233 @@ native bool RemoveDir(const char[] path);
*/
native bool CreateDirectory(const char[] path, int mode=FPERM_O_READ|FPERM_O_EXEC|FPERM_G_READ|FPERM_G_EXEC|FPERM_U_READ|FPERM_U_WRITE|FPERM_U_EXEC, bool use_valve_fs=false, const char[] valve_path_id="DEFAULT_WRITE_PATH");
/**
* Results reported by asynchronous filesystem operations.
*/
enum FileOpResult
{
FileOp_Success = 0,
FileOp_NotFound,
FileOp_Error,
};
/**
* Called when an asynchronous filesystem operation completes.
*
* @param result Operation result.
* @param path Path supplied to the operation.
* @param data User data supplied to the operation.
*/
typedef FileOpCallback = function void(FileOpResult result, const char[] path, any data);
/**
* Called when an asynchronous existence check completes.
*
* @param exists Whether the requested entry exists and has the requested type.
* @param path Path supplied to the operation.
* @param data User data supplied to the operation.
*/
typedef FileExistsCallback = function void(bool exists, const char[] path, any data);
/**
* Called when an asynchronous file-size query completes.
*
* @param result Operation result.
* @param path Path supplied to the operation.
* @param size File size in bytes, or -1 on failure.
* @param data User data supplied to the operation.
*/
typedef FileSizeCallback = function void(FileOpResult result, const char[] path, int size, any data);
/**
* Called when an asynchronous whole-file read completes.
*
* contents contains the raw file bytes. It is binary-safe, including embedded
* NUL bytes, and its length is given by size. Do not use string-focused natives
* on it unless the data is known to be a string.
*
* @param result Operation result.
* @param path Path supplied to the operation.
* @param contents Raw-byte buffer.
* @param size Number of file bytes in contents.
* @param data User data supplied to the operation.
*/
typedef FileReadCallback = function void(FileOpResult result, const char[] path, const char[] contents, int size, any data);
/**
* Called when an asynchronous directory snapshot has been materialized.
*
* On success, listing contains a fully-materialized DirectoryListing and must
* be closed by the callback. It is independent of later filesystem changes.
* On failure, listing is null.
*
* @param result Operation result.
* @param path Path supplied to the operation.
* @param listing Materialized directory listing, or null on failure.
* @param data User data supplied to the operation.
*/
typedef FileDirectoryCallback = function void(FileOpResult result, const char[] path, DirectoryListing listing, any data);
/**
* Deletes a file without blocking the game thread.
*
* @param path Path to the file.
* @param callback Callback invoked on completion.
* @param data User data passed to the callback.
* @param use_valve_fs If true, use the Valve filesystem.
* @param valve_path_id Valve filesystem search path when use_valve_fs is true.
* @error The callback is invalid, the async worker is shutting down, or Valve async operations are unavailable.
*/
native void DeleteFileAsync(const char[] path, FileOpCallback callback, any data=0,
bool use_valve_fs=false,
const char[] valve_path_id="DEFAULT_WRITE_PATH");
/**
* Renames a file without blocking the game thread.
*
* @param newpath New path to the file.
* @param oldpath Path to the existing file.
* @param callback Callback invoked on completion.
* @param data User data passed to the callback.
* @param use_valve_fs If true, use the Valve filesystem.
* @param valve_path_id Valve filesystem search path when use_valve_fs is true.
* @error The callback is invalid, the async worker is shutting down, or Valve async operations are unavailable.
*/
native void RenameFileAsync(const char[] newpath, const char[] oldpath,
FileOpCallback callback, any data=0,
bool use_valve_fs=false,
const char[] valve_path_id="DEFAULT_WRITE_PATH");
/**
* Copies a file without blocking the game thread. Existing destinations are
* overwritten.
*
* @param newpath Path to the destination file.
* @param oldpath Path to the existing source file.
* @param callback Callback invoked on completion.
* @param data User data passed to the callback.
* @param use_valve_fs If true, use the Valve filesystem.
* @param source_path_id Valve filesystem search path for the source.
* @param dest_path_id Valve filesystem search path for the destination.
* @error The callback is invalid, the async worker is shutting down, or Valve async operations are unavailable.
*/
native void CopyFileAsync(const char[] newpath, const char[] oldpath,
FileOpCallback callback, any data=0,
bool use_valve_fs=false,
const char[] source_path_id="GAME",
const char[] dest_path_id="DEFAULT_WRITE_PATH");
/**
* Checks for a regular file without blocking the game thread.
*
* @param path Path to the file.
* @param callback Callback invoked on completion.
* @param data User data passed to the callback.
* @param use_valve_fs If true, use the Valve filesystem.
* @param valve_path_id Valve filesystem search path when use_valve_fs is true.
* @error The callback is invalid, the async worker is shutting down, or Valve async operations are unavailable.
*/
native void FileExistsAsync(const char[] path, FileExistsCallback callback, any data=0,
bool use_valve_fs=false,
const char[] valve_path_id="GAME");
/**
* Checks for a directory without blocking the game thread.
*
* @param path Path to the directory. Use "." for the game directory.
* @param callback Callback invoked on completion.
* @param data User data passed to the callback.
* @param use_valve_fs If true, use the Valve filesystem.
* @param valve_path_id Valve filesystem search path when use_valve_fs is true.
* @error The path is empty, the callback is invalid, the async worker is shutting down, or Valve async operations are unavailable.
*/
native void DirExistsAsync(const char[] path, FileExistsCallback callback, any data=0,
bool use_valve_fs=false,
const char[] valve_path_id="GAME");
/**
* Gets a file's size without blocking the game thread.
*
* @param path Path to the file.
* @param callback Callback invoked on completion.
* @param data User data passed to the callback.
* @param use_valve_fs If true, use the Valve filesystem.
* @param valve_path_id Valve filesystem search path when use_valve_fs is true.
* @error The callback is invalid, the async worker is shutting down, or Valve async operations are unavailable.
*/
native void FileSizeAsync(const char[] path, FileSizeCallback callback, any data=0,
bool use_valve_fs=false,
const char[] valve_path_id="GAME");
/**
* Reads a whole file without blocking the game thread.
*
* @param path Path to the file.
* @param callback Callback invoked on completion.
* @param data User data passed to the callback.
* @param use_valve_fs If true, use the Valve filesystem.
* @param valve_path_id Valve filesystem search path when use_valve_fs is true.
* @error The callback is invalid, the async worker is shutting down, or Valve async operations are unavailable.
*/
native void ReadFileAsync(const char[] path, FileReadCallback callback, any data=0,
bool use_valve_fs=false,
const char[] valve_path_id="GAME");
/**
* Enumerates an OS filesystem directory without blocking the game thread.
* The callback receives a fully-materialized DirectoryListing snapshot.
*
* @param path Path to the directory. Use "." for the game directory.
* @param callback Callback invoked on completion.
* @param data User data passed to the callback.
* @error The path is empty, the callback is invalid, or the async worker is shutting down.
*/
native void OpenDirectoryAsync(const char[] path, FileDirectoryCallback callback, any data=0);
/**
* Writes size raw bytes from contents without blocking the game thread.
*
* @param path Path to the file.
* @param contents Raw-byte buffer to write.
* @param size Number of bytes to write from contents.
* @param callback Callback invoked on completion.
* @param data User data passed to the callback.
* @param append If true, append rather than overwrite.
* @param use_valve_fs If true, use the Valve filesystem.
* @param valve_path_id Valve filesystem path; must be DEFAULT_WRITE_PATH.
* @error The size is negative, the callback is invalid, the async worker is shutting down, Valve async operations are unavailable, or valve_path_id is unsupported.
*/
native void WriteFileAsync(const char[] path, const char[] contents, int size,
FileOpCallback callback, any data=0, bool append=false,
bool use_valve_fs=false,
const char[] valve_path_id="DEFAULT_WRITE_PATH");
/**
* Creates a directory without blocking the game thread.
*
* @param path Path to create.
* @param mode Permissions for an OS filesystem directory. Ignored by the Valve filesystem.
* @param callback Callback invoked on completion.
* @param data User data passed to the callback.
* @param use_valve_fs If true, use the Valve filesystem.
* @param valve_path_id Valve filesystem search path when use_valve_fs is true.
* @error The callback is invalid, the async worker is shutting down, or Valve async operations are unavailable.
*/
native void CreateDirectoryAsync(const char[] path, int mode,
FileOpCallback callback, any data=0,
bool use_valve_fs=false,
const char[] valve_path_id="DEFAULT_WRITE_PATH");
/**
* Removes an empty OS filesystem directory without blocking the game thread.
*
* @param path Path to the directory.
* @param callback Callback invoked on completion.
* @param data User data passed to the callback.
* @error The callback is invalid or the async worker is shutting down.
*/
native void RemoveDirectoryAsync(const char[] path, FileOpCallback callback, any data=0);
/**
* Changes a file or directories permissions.
*
+178
View File
@@ -0,0 +1,178 @@
#pragma semicolon 1
#pragma newdecls required
#include <testing>
#include <files>
static const char g_Source[] = "test_async_file_source.txt";
static const char g_Destination[] = "test_async_file_destination.txt";
static const char g_BinarySource[] = "test_async_file_binary_source.dat";
static const char g_BinaryDestination[] = "test_async_file_binary_destination.dat";
static const char g_Directory[] = "test_async_file_dir";
static const char g_DirectoryFirst[] = "test_async_file_dir/first.txt";
static const char g_DirectorySecond[] = "test_async_file_dir/second.txt";
static bool g_NativeReturned;
static void WriteTestFile(const char[] path, const char[] contents)
{
File file = OpenFile(path, "wb");
AssertTrue("open_for_write", file != null);
file.WriteString(contents, false);
delete file;
}
static void ReadTestFile(const char[] path, char[] buffer, int maxlength)
{
File file = OpenFile(path, "rb");
AssertTrue("open_for_read", file != null);
file.ReadString(buffer, maxlength);
delete file;
}
static void WriteBinaryTestFile(const char[] path, const int[] bytes, int count)
{
File file = OpenFile(path, "wb");
AssertTrue("open_binary_for_write", file != null);
AssertTrue("write_binary", file.Write(bytes, count, 1));
delete file;
}
public void OnPluginStart()
{
DeleteFile(g_Source);
DeleteFile(g_Destination);
DeleteFile(g_BinarySource);
DeleteFile(g_BinaryDestination);
DeleteFile(g_DirectoryFirst);
DeleteFile(g_DirectorySecond);
RemoveDir(g_Directory);
WriteTestFile(g_Source, "async copyfile");
AssertTrue("create_directory", CreateDirectory(g_Directory));
WriteTestFile(g_DirectoryFirst, "first");
WriteTestFile(g_DirectorySecond, "second");
SetTestContext("AsyncCopyCreatesDestination");
CopyFileAsync(g_Destination, g_Source, OnCopyComplete, 42);
g_NativeReturned = true;
}
void OnCopyComplete(FileOpResult result, const char[] path, any data)
{
char buffer[64];
AssertTrue("callback_after_native", g_NativeReturned);
AssertEq("copy_result", result, FileOp_Success);
AssertStrEq("copy_path", path, g_Destination);
AssertEq("copy_data", data, 42);
AssertTrue("dest_exists", FileExists(g_Destination));
ReadTestFile(g_Destination, buffer, sizeof(buffer));
AssertStrEq("dest_contents", buffer, "async copyfile");
SetTestContext("AsyncFileExists");
FileExistsAsync(g_Destination, OnExistsComplete, 43);
}
void OnExistsComplete(bool exists, const char[] path, any data)
{
AssertTrue("exists", exists);
AssertStrEq("exists_path", path, g_Destination);
AssertEq("exists_data", data, 43);
SetTestContext("AsyncCopyMissingSource");
CopyFileAsync(g_Destination, "test_async_file_missing.txt", OnMissingCopyComplete, 44);
}
void OnMissingCopyComplete(FileOpResult result, const char[] path, any data)
{
char buffer[64];
AssertEq("copy_result", result, FileOp_NotFound);
AssertStrEq("copy_path", path, g_Destination);
AssertEq("copy_data", data, 44);
AssertTrue("dest_preserved", FileExists(g_Destination));
ReadTestFile(g_Destination, buffer, sizeof(buffer));
AssertStrEq("dest_contents", buffer, "async copyfile");
int bytes[] = {0x00, 0x01, 0x41, 0x00, 0xff, 0x7f};
WriteBinaryTestFile(g_BinarySource, bytes, sizeof(bytes));
SetTestContext("AsyncReadBinary");
ReadFileAsync(g_BinarySource, OnReadComplete, 45);
}
void OnReadComplete(FileOpResult result, const char[] path, const char[] contents, int size, any data)
{
int expected[] = {0x00, 0x01, 0x41, 0x00, 0xff, 0x7f};
AssertEq("read_result", result, FileOp_Success);
AssertStrEq("read_path", path, g_BinarySource);
AssertEq("read_size", size, sizeof(expected));
AssertEq("read_data", data, 45);
for (int i = 0; i < sizeof(expected); i++)
{
AssertEq("read_byte", contents[i] & 0xff, expected[i]);
}
SetTestContext("AsyncWriteBinary");
WriteFileAsync(g_BinaryDestination, contents, size, OnWriteComplete, 46);
}
void OnWriteComplete(FileOpResult result, const char[] path, any data)
{
int expected[] = {0x00, 0x01, 0x41, 0x00, 0xff, 0x7f};
int actual[sizeof(expected)];
File file;
AssertEq("write_result", result, FileOp_Success);
AssertStrEq("write_path", path, g_BinaryDestination);
AssertEq("write_data", data, 46);
file = OpenFile(g_BinaryDestination, "rb");
AssertTrue("open_binary_for_read", file != null);
AssertEq("read_binary", file.Read(actual, sizeof(actual), 1), sizeof(actual));
delete file;
for (int i = 0; i < sizeof(expected); i++)
{
AssertEq("written_byte", actual[i], expected[i]);
}
SetTestContext("AsyncDirectorySnapshot");
OpenDirectoryAsync(g_Directory, OnDirectoryComplete, 47);
}
void OnDirectoryComplete(FileOpResult result, const char[] path, DirectoryListing listing, any data)
{
char name[PLATFORM_MAX_PATH];
FileType type;
bool foundFirst;
bool foundSecond;
AssertEq("directory_result", result, FileOp_Success);
AssertStrEq("directory_path", path, g_Directory);
AssertEq("directory_data", data, 47);
AssertTrue("directory_listing", listing != null);
while (listing.GetNext(name, sizeof(name), type))
{
if (StrEqual(name, "first.txt"))
{
AssertEq("first_type", type, FileType_File);
foundFirst = true;
}
else if (StrEqual(name, "second.txt"))
{
AssertEq("second_type", type, FileType_File);
foundSecond = true;
}
}
delete listing;
AssertTrue("directory_first", foundFirst);
AssertTrue("directory_second", foundSecond);
DeleteFile(g_Source);
DeleteFile(g_Destination);
DeleteFile(g_BinarySource);
DeleteFile(g_BinaryDestination);
DeleteFile(g_DirectoryFirst);
DeleteFile(g_DirectorySecond);
RemoveDir(g_Directory);
PrintToServer("OK");
}
+69
View File
@@ -0,0 +1,69 @@
#pragma semicolon 1
#pragma newdecls required
#include <testing>
#include <files>
// Only the non-Valve-fs path is exercised here. The hl2sdk-mock file system
// reports IsOk() inverted, so ValveFile::HasError() is always true under the
// mock and the Valve-fs streaming copy cannot be meaningfully tested there.
static void WriteTestFile(const char[] path, const char[] contents)
{
File f = OpenFile(path, "wb");
AssertTrue("open_for_write", f != null);
f.WriteString(contents, false);
delete f;
}
static void ReadTestFile(const char[] path, char[] buffer, int maxlength)
{
File f = OpenFile(path, "rb");
AssertTrue("open_for_read", f != null);
f.ReadString(buffer, maxlength);
delete f;
}
public void OnPluginStart()
{
char src[] = "test_copyfile_src.txt";
char dst[] = "test_copyfile_dst.txt";
char buffer[256];
// Clean up any leftovers from a previous run.
DeleteFile(src);
DeleteFile(dst);
// --------------------------------------------------------------------------------
SetTestContext("CopyCreatesDestination");
WriteTestFile(src, "hello copyfile");
AssertTrue("copy_returns_true", CopyFile(dst, src));
AssertTrue("dest_exists", FileExists(dst));
ReadTestFile(dst, buffer, sizeof(buffer));
AssertStrEq("dest_contents", buffer, "hello copyfile");
// --------------------------------------------------------------------------------
SetTestContext("CopyOverwritesDestination");
WriteTestFile(dst, "OLD CONTENT that is longer than the source");
AssertTrue("copy_returns_true", CopyFile(dst, src));
ReadTestFile(dst, buffer, sizeof(buffer));
AssertStrEq("dest_overwritten", buffer, "hello copyfile");
// --------------------------------------------------------------------------------
SetTestContext("CopyMissingSourceFails");
DeleteFile(dst);
AssertFalse("copy_returns_false", CopyFile(dst, "test_copyfile_missing.txt"));
AssertFalse("dest_not_created", FileExists(dst));
// --------------------------------------------------------------------------------
DeleteFile(src);
DeleteFile(dst);
PrintToServer("OK");
}