Skip to content

Commit

Permalink
File handling opcodes fixes (#218)
Browse files Browse the repository at this point in the history
* Fix move_file and move_directory opcodes.

* Fixed copy_directory
  • Loading branch information
MiranDMC authored Sep 30, 2024
1 parent 3977148 commit edb8f5d
Showing 1 changed file with 30 additions and 59 deletions.
89 changes: 30 additions & 59 deletions cleo_plugins/FileSystemOperations/FileSystemOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
#include "CLEO_Utils.h"
#include "FileUtils.h"

#include <filesystem>
#include <set>

using namespace CLEO;
using namespace plugin;
namespace FS = std::filesystem;

#define OPCODE_READ_PARAM_FILE_HANDLE(handle) auto handle = (DWORD)OPCODE_READ_PARAM_PTR(); \
if(m_hFiles.find(handle) == m_hFiles.end()) { auto info = ScriptInfoStr(thread); SHOW_ERROR("Invalid or already closed '0x%X' file handle param in script %s \nScript suspended.", handle, info.c_str()); return thread->Suspend(); }
Expand Down Expand Up @@ -556,9 +558,15 @@ class FileSystemOperations
OPCODE_READ_PARAM_FILEPATH(filepath);
OPCODE_READ_PARAM_FILEPATH(newFilepath);

BOOL result = GetFileAttributes(filepath) & FILE_ATTRIBUTE_DIRECTORY;
if (!result)
result = MoveFile(filepath, newFilepath);
bool result = false;

auto fsPath = FS::path(filepath);
if (FS::is_regular_file(fsPath))
{
std::error_code err;
FS::rename(fsPath, newFilepath, err);
result = !err;
}

OPCODE_CONDITION_RESULT(result);
return OR_CONTINUE;
Expand All @@ -570,9 +578,15 @@ class FileSystemOperations
OPCODE_READ_PARAM_FILEPATH(filepath);
OPCODE_READ_PARAM_FILEPATH(newFilepath);

BOOL result = GetFileAttributes(filepath) & FILE_ATTRIBUTE_DIRECTORY;
if (result)
result = MoveFile(filepath, newFilepath);
bool result = false;

auto fsPath = FS::path(filepath);
if (FS::is_directory(fsPath))
{
std::error_code err;
FS::rename(fsPath, newFilepath, err);
result = !err;
}

OPCODE_CONDITION_RESULT(result);
return OR_CONTINUE;
Expand All @@ -596,66 +610,23 @@ class FileSystemOperations
return OR_CONTINUE;
}

static BOOL CopyDir(const char *path, const char *newPath)
{
char mask[MAX_PATH];
HANDLE hSearch = NULL;
WIN32_FIND_DATA wfd;
char subPath[MAX_PATH], newSubPath[MAX_PATH];
DWORD fattr;

//create parent directory
if (!CreateDirectory(newPath, NULL))
return FALSE;

memset(&wfd, 0, sizeof(wfd));
//search mask
sprintf(mask, "%s\\*", path);

//copy all files and folders into new directory
if ((hSearch = FindFirstFile(mask, &wfd)) != INVALID_HANDLE_VALUE)
{
do
{
sprintf(subPath, "%s\\%s", path, wfd.cFileName);
sprintf(newSubPath, "%s\\%s", newPath, wfd.cFileName);
//copy subdirectories
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if ((strcmp(wfd.cFileName, "..") != 0) && (strcmp(wfd.cFileName, ".") != 0))
{
if (!CopyDir(subPath, newSubPath))
return FALSE;
}
}
else
{
//copy file into new directory
if (CopyFile(subPath, newSubPath, FALSE))
{
fattr = GetFileAttributes(subPath);
SetFileAttributes(newSubPath, fattr);
}
else return FALSE;
}


} while (FindNextFile(hSearch, &wfd));
FindClose(hSearch);
}

return TRUE;
}

// 0B05=2, copy_directory %1d% to %2d% //IF and SET
static OpcodeResult WINAPI Script_FS_CopyDir(CScriptThread* thread)
{
OPCODE_READ_PARAM_FILEPATH(filepath);
OPCODE_READ_PARAM_FILEPATH(newFilepath);

BOOL result = CopyDir(filepath, newFilepath);
auto path = FS::path(filepath);
if (!FS::is_directory(path))
{
OPCODE_CONDITION_RESULT(false);
return OR_CONTINUE;
}

OPCODE_CONDITION_RESULT(result);
std::error_code err;
FS::copy(filepath, newFilepath, FS::copy_options::update_existing | FS::copy_options::recursive, err);

OPCODE_CONDITION_RESULT(!err);
return OR_CONTINUE;
}

Expand Down

0 comments on commit edb8f5d

Please sign in to comment.