Add CopyFile native (#1683)

This commit is contained in:
Nicholas Hastings
2026-07-26 13:45:46 -04:00
parent 1a59e55723
commit bc69fc9f8e
2 changed files with 79 additions and 0 deletions
+62
View File
@@ -32,6 +32,7 @@
#include <assert.h>
#include <sys/stat.h>
#include <string.h>
#include <filesystem>
#include <IHandleSys.h>
#include <ILibrarySys.h>
#include <IPluginSys.h>
@@ -669,6 +670,66 @@ static cell_t sm_RenameFile(IPluginContext *pContext, const cell_t *params)
#endif
}
// Streams the contents of one open file into another. Used for the Valve file
// system copy path, which has no native copy function.
static bool StreamCopyFile(FileObject *src, FileObject *dst)
{
char buffer[8192];
while (!src->EndOfFile())
{
size_t bytes = src->Read(buffer, sizeof(buffer));
if (src->HasError())
return false;
if (bytes == 0)
break;
if (dst->Write(buffer, static_cast<int>(bytes)) != bytes)
return false;
}
return !dst->HasError();
}
static cell_t sm_CopyFile(IPluginContext *pContext, const cell_t *params)
{
char *newpath, *oldpath;
pContext->LocalToString(params[1], &newpath);
pContext->LocalToString(params[2], &oldpath);
if (params[3] == 1)
{
char *sourcePathID;
pContext->LocalToStringNULL(params[4], &sourcePathID);
char *destPathID;
pContext->LocalToStringNULL(params[5], &destPathID);
ValveFile *src = ValveFile::Open(oldpath, "rb", sourcePathID);
if (!src)
return 0;
ValveFile *dst = ValveFile::Open(newpath, "wb", destPathID);
if (!dst)
{
delete src;
return 0;
}
bool success = StreamCopyFile(src, dst);
delete dst;
delete src;
return success ? 1 : 0;
}
char new_realpath[PLATFORM_MAX_PATH];
g_pSM->BuildPath(Path_Game, new_realpath, sizeof(new_realpath), "%s", newpath);
char old_realpath[PLATFORM_MAX_PATH];
g_pSM->BuildPath(Path_Game, old_realpath, sizeof(old_realpath), "%s", oldpath);
std::error_code ec;
std::filesystem::copy_file(old_realpath, new_realpath,
std::filesystem::copy_options::overwrite_existing, ec);
return ec ? 0 : 1;
}
static cell_t sm_DirExists(IPluginContext *pContext, const cell_t *params)
{
char *name;
@@ -1261,6 +1322,7 @@ REGISTER_NATIVES(filesystem)
{"FilePosition", sm_FilePosition},
{"FileExists", sm_FileExists},
{"RenameFile", sm_RenameFile},
{"CopyFile", sm_CopyFile},
{"DirExists", sm_DirExists},
{"FileSize", sm_FileSize},
{"RemoveDir", sm_RemoveDir},
+17
View File
@@ -523,6 +523,23 @@ native bool FileExists(const char[] path, bool use_valve_fs=false, const char[]
*/
native bool RenameFile(const char[] newpath, const char[] oldpath, bool use_valve_fs=false, const char[] valve_path_id="DEFAULT_WRITE_PATH");
/**
* Copies a file. If the destination file already exists, it is overwritten.
*
* @param newpath Path to the destination file.
* @param oldpath Path to the existing source file.
* @param use_valve_fs If true, the Valve file system will be used instead.
* This can be used to copy files in the game's
* Valve search paths, rather than directly in the gamedir.
* @param source_path_id If use_valve_fs, the gameinfo search path used to locate
* the source file, or NULL_STRING for all search paths.
* @param dest_path_id If use_valve_fs, the gameinfo search path used to write
* the destination file, or NULL_STRING for the default
* write path.
* @return True on success, false otherwise.
*/
native bool CopyFile(const char[] newpath, const char[] oldpath, bool use_valve_fs=false, const char[] source_path_id="GAME", const char[] dest_path_id="DEFAULT_WRITE_PATH");
/**
* Checks if a directory exists.
*