From bc69fc9f8ef73784c2b2d56efbd098c6c4a35bf2 Mon Sep 17 00:00:00 2001 From: Nicholas Hastings Date: Sun, 26 Jul 2026 12:02:34 -0400 Subject: [PATCH] Add CopyFile native (#1683) --- core/logic/smn_filesystem.cpp | 62 +++++++++++++++++++++++++++++++++++ plugins/include/files.inc | 17 ++++++++++ 2 files changed, 79 insertions(+) diff --git a/core/logic/smn_filesystem.cpp b/core/logic/smn_filesystem.cpp index c990a1ad..30c8d6ab 100644 --- a/core/logic/smn_filesystem.cpp +++ b/core/logic/smn_filesystem.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -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(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}, diff --git a/plugins/include/files.inc b/plugins/include/files.inc index 180c6050..ec88d76a 100644 --- a/plugins/include/files.inc +++ b/plugins/include/files.inc @@ -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. *