@@ -32,6 +32,18 @@ typedef int FileFindHandle_t;
|
||||
|
||||
namespace SourceMod {
|
||||
|
||||
typedef void *AsyncControl_t;
|
||||
|
||||
enum AsyncStatus
|
||||
{
|
||||
AsyncStatus_FileOpenError = -1,
|
||||
AsyncStatus_Ok = 0,
|
||||
AsyncStatus_Pending,
|
||||
AsyncStatus_InProgress,
|
||||
AsyncStatus_Aborted,
|
||||
AsyncStatus_Unserviced,
|
||||
};
|
||||
|
||||
class IFileSystemBridge
|
||||
{
|
||||
public:
|
||||
@@ -59,6 +71,14 @@ public:
|
||||
virtual void CreateDirHierarchy(const char *path, const char *pathID = 0) = 0;
|
||||
virtual int GetSearchPath(const char* pathID, bool bGetPackFiles, char* pPath, int nMaxLen) = 0;
|
||||
virtual const char * GetGameBinArchSubdirectory() = 0;
|
||||
virtual bool SupportsAsync() = 0;
|
||||
virtual int AsyncRead(const char *filename, const char *pathID, AsyncControl_t *control) = 0;
|
||||
virtual int AsyncWrite(const char *filename, const void *data, int size, bool append,
|
||||
AsyncControl_t *control) = 0;
|
||||
virtual int AsyncStatus(AsyncControl_t control) = 0;
|
||||
virtual int AsyncGetResult(AsyncControl_t control, void **data, int *size) = 0;
|
||||
virtual void AsyncAbort(AsyncControl_t control) = 0;
|
||||
virtual void AsyncRelease(AsyncControl_t control) = 0;
|
||||
};
|
||||
|
||||
} // namespace SourceMod
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
+111
-5
@@ -46,8 +46,11 @@
|
||||
#include "IDBDriver.h"
|
||||
#include "provider.h"
|
||||
#include "sm_convar.h"
|
||||
#include <amtl/os/am-shared-library.h>
|
||||
#include <amtl/os/am-path.h>
|
||||
#include <amtl/os/am-shared-library.h>
|
||||
#include <amtl/os/am-path.h>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <bridge/include/IVEngineServerBridge.h>
|
||||
#include <bridge/include/IPlayerInfoBridge.h>
|
||||
#include <bridge/include/IFileSystemBridge.h>
|
||||
@@ -194,7 +197,7 @@ public:
|
||||
{
|
||||
return filesystem->GetSearchPath(pathID, bGetPackFiles, pPath, nMaxLen);
|
||||
}
|
||||
const char* GetGameBinArchSubdirectory() override
|
||||
const char* GetGameBinArchSubdirectory() override
|
||||
{
|
||||
#if defined( KE_ARCH_X64 ) && SOURCE_ENGINE >= SE_BLADE
|
||||
#if defined( PLATFORM_WINDOWS )
|
||||
@@ -212,8 +215,111 @@ public:
|
||||
// Already included in the GameBin path(s), if required
|
||||
return "";
|
||||
#endif // defined( KE_ARCH_X64 ) && SOURCE_ENGINE >= SE_BLADE
|
||||
}
|
||||
} fs_wrapper;
|
||||
}
|
||||
bool SupportsAsync() override
|
||||
{
|
||||
#if SOURCE_ENGINE == SE_MOCK
|
||||
return false;
|
||||
#else
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
int AsyncRead(const char *filename, const char *pathID, SourceMod::AsyncControl_t *control) override
|
||||
{
|
||||
#if SOURCE_ENGINE == SE_MOCK
|
||||
return SourceMod::AsyncStatus_FileOpenError;
|
||||
#else
|
||||
auto request = std::make_unique<AsyncReadRequest>();
|
||||
request->request.pszFilename = filename;
|
||||
request->request.pszPathID = pathID;
|
||||
request->request.pfnCallback = AsyncReadCallback;
|
||||
request->request.pContext = request.get();
|
||||
FSAsyncControl_t native_control = nullptr;
|
||||
int status = filesystem->AsyncReadMultiple(&request->request, 1, &native_control);
|
||||
if (status >= SourceMod::AsyncStatus_Ok && native_control)
|
||||
async_requests_.emplace(native_control, std::move(request));
|
||||
*control = native_control;
|
||||
return status;
|
||||
#endif
|
||||
}
|
||||
int AsyncWrite(const char *filename, const void *data, int size, bool append,
|
||||
SourceMod::AsyncControl_t *control) override
|
||||
{
|
||||
#if SOURCE_ENGINE == SE_MOCK || SOURCE_ENGINE == SE_DARKMESSIAH
|
||||
return SourceMod::AsyncStatus_FileOpenError;
|
||||
#else
|
||||
return filesystem->AsyncWrite(filename, data, size, false, append,
|
||||
reinterpret_cast<FSAsyncControl_t *>(control));
|
||||
#endif
|
||||
}
|
||||
int AsyncStatus(SourceMod::AsyncControl_t control) override
|
||||
{
|
||||
#if SOURCE_ENGINE == SE_MOCK
|
||||
return SourceMod::AsyncStatus_FileOpenError;
|
||||
#else
|
||||
return filesystem->AsyncStatus(reinterpret_cast<FSAsyncControl_t>(control));
|
||||
#endif
|
||||
}
|
||||
int AsyncGetResult(SourceMod::AsyncControl_t control, void **data, int *size) override
|
||||
{
|
||||
#if SOURCE_ENGINE == SE_MOCK
|
||||
return SourceMod::AsyncStatus_FileOpenError;
|
||||
#else
|
||||
FSAsyncControl_t native_control = reinterpret_cast<FSAsyncControl_t>(control);
|
||||
void *engine_data;
|
||||
int engine_size;
|
||||
int status = filesystem->AsyncGetResult(native_control, &engine_data, &engine_size);
|
||||
if (status != SourceMod::AsyncStatus_Ok)
|
||||
return status;
|
||||
|
||||
auto iter = async_requests_.find(native_control);
|
||||
if (iter == async_requests_.end())
|
||||
return SourceMod::AsyncStatus_FileOpenError;
|
||||
|
||||
if (data)
|
||||
*data = iter->second->contents.empty() ? nullptr : iter->second->contents.data();
|
||||
if (size)
|
||||
*size = static_cast<int>(iter->second->contents.size());
|
||||
return status;
|
||||
#endif
|
||||
}
|
||||
void AsyncAbort(SourceMod::AsyncControl_t control) override
|
||||
{
|
||||
#if SOURCE_ENGINE != SE_MOCK
|
||||
filesystem->AsyncAbort(reinterpret_cast<FSAsyncControl_t>(control));
|
||||
#endif
|
||||
}
|
||||
void AsyncRelease(SourceMod::AsyncControl_t control) override
|
||||
{
|
||||
#if SOURCE_ENGINE != SE_MOCK
|
||||
FSAsyncControl_t native_control = reinterpret_cast<FSAsyncControl_t>(control);
|
||||
filesystem->AsyncRelease(native_control);
|
||||
async_requests_.erase(native_control);
|
||||
#endif
|
||||
}
|
||||
|
||||
private:
|
||||
#if SOURCE_ENGINE != SE_MOCK
|
||||
struct AsyncReadRequest
|
||||
{
|
||||
FileAsyncRequest_t request;
|
||||
std::vector<uint8_t> contents;
|
||||
};
|
||||
|
||||
static void AsyncReadCallback(const FileAsyncRequest_t &request, int bytes, FSAsyncStatus_t status)
|
||||
{
|
||||
auto *async_request = static_cast<AsyncReadRequest *>(request.pContext);
|
||||
if (status != FSASYNC_OK || bytes < 0 || (bytes && !request.pData))
|
||||
return;
|
||||
|
||||
async_request->contents.resize(bytes);
|
||||
if (bytes)
|
||||
memcpy(async_request->contents.data(), request.pData, bytes);
|
||||
}
|
||||
|
||||
std::unordered_map<FSAsyncControl_t, std::unique_ptr<AsyncReadRequest>> async_requests_;
|
||||
#endif
|
||||
} fs_wrapper;
|
||||
|
||||
class VPlayerInfo_Logic : public IPlayerInfoBridge
|
||||
{
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
Reference in New Issue
Block a user