Skip to content

Commit 80f86b9

Browse files
committed
Split into RenameFile and RenameDirectory
1 parent e8aee1c commit 80f86b9

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

Source/Managers/LuaMan.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ namespace RTE {
8080
.def("DirectoryCreate", &LuaStateWrapper::DirectoryCreate2)
8181
.def("DirectoryRemove", &LuaStateWrapper::DirectoryRemove1)
8282
.def("DirectoryRemove", &LuaStateWrapper::DirectoryRemove2)
83-
.def("Rename", &LuaStateWrapper::Rename)
83+
.def("FileRename", &LuaStateWrapper::FileRename)
84+
.def("DirectoryRename", &LuaStateWrapper::DirectoryRename)
8485
.def("FileReadLine", &LuaStateWrapper::FileReadLine)
8586
.def("FileWriteLine", &LuaStateWrapper::FileWriteLine)
8687
.def("FileEOF", &LuaStateWrapper::FileEOF),
@@ -294,7 +295,8 @@ namespace RTE {
294295
bool LuaStateWrapper::DirectoryCreate2(const std::string& path, bool recursive) { return g_LuaMan.DirectoryCreate(path, recursive); }
295296
bool LuaStateWrapper::DirectoryRemove1(const std::string& path) { return g_LuaMan.DirectoryRemove(path, false); }
296297
bool LuaStateWrapper::DirectoryRemove2(const std::string& path, bool recursive) { return g_LuaMan.DirectoryRemove(path, recursive); }
297-
bool LuaStateWrapper::Rename(const std::string& oldPath, const std::string& newPath) { return g_LuaMan.Rename(oldPath, newPath); }
298+
bool LuaStateWrapper::FileRename(const std::string& oldPath, const std::string& newPath) { return g_LuaMan.FileRename(oldPath, newPath); }
299+
bool LuaStateWrapper::DirectoryRename(const std::string& oldPath, const std::string& newPath) { return g_LuaMan.DirectoryRename(oldPath, newPath); }
298300
std::string LuaStateWrapper::FileReadLine(int fileIndex) { return g_LuaMan.FileReadLine(fileIndex); }
299301
void LuaStateWrapper::FileWriteLine(int fileIndex, const std::string& line) { return g_LuaMan.FileWriteLine(fileIndex, line); }
300302
bool LuaStateWrapper::FileEOF(int fileIndex) { return g_LuaMan.FileEOF(fileIndex); }
@@ -1192,7 +1194,7 @@ namespace RTE {
11921194

11931195
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
11941196

1195-
bool LuaMan::Rename(const std::string& oldPath, const std::string& newPath) {
1197+
bool LuaMan::FileRename(const std::string& oldPath, const std::string& newPath) {
11961198
std::string fullOldPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(oldPath);
11971199
std::string fullNewPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(newPath);
11981200
if (IsValidModulePath(fullOldPath) && IsValidModulePath(fullNewPath)) {
@@ -1202,7 +1204,31 @@ namespace RTE {
12021204
#endif
12031205
// Ensures parity between Linux which can overwrite an empty directory, while Windows can't
12041206
// Ensures parity between Linux which can't rename a directory to a newPath that is a file in order to overwrite it, while Windows can
1205-
if (!std::filesystem::exists(fullNewPath))
1207+
if (std::filesystem::is_regular_file(fullOldPath) && !std::filesystem::exists(fullNewPath))
1208+
{
1209+
try {
1210+
std::filesystem::rename(fullOldPath, fullNewPath);
1211+
return true;
1212+
} catch (const std::filesystem::filesystem_error &e) {}
1213+
}
1214+
}
1215+
g_ConsoleMan.PrintString("ERROR: Failed to rename oldPath " + oldPath + " to newPath " + newPath);
1216+
return false;
1217+
}
1218+
1219+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1220+
1221+
bool LuaMan::DirectoryRename(const std::string& oldPath, const std::string& newPath) {
1222+
std::string fullOldPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(oldPath);
1223+
std::string fullNewPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(newPath);
1224+
if (IsValidModulePath(fullOldPath) && IsValidModulePath(fullNewPath)) {
1225+
#ifndef _WIN32
1226+
fullOldPath = GetCaseInsensitiveFullPath(fullOldPath);
1227+
fullNewPath = GetCaseInsensitiveFullPath(fullNewPath);
1228+
#endif
1229+
// Ensures parity between Linux which can overwrite an empty directory, while Windows can't
1230+
// Ensures parity between Linux which can't rename a directory to a newPath that is a file in order to overwrite it, while Windows can
1231+
if (std::filesystem::is_directory(fullOldPath) && !std::filesystem::exists(fullNewPath))
12061232
{
12071233
try {
12081234
std::filesystem::rename(fullOldPath, fullNewPath);

Source/Managers/LuaMan.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ namespace RTE {
299299
bool DirectoryCreate2(const std::string& path, bool recursive);
300300
bool DirectoryRemove1(const std::string& path);
301301
bool DirectoryRemove2(const std::string& path, bool recursive);
302-
bool Rename(const std::string& oldPath, const std::string& newPath);
302+
bool FileRename(const std::string& oldPath, const std::string& newPath);
303+
bool DirectoryRename(const std::string& oldPath, const std::string& newPath);
303304
std::string FileReadLine(int fileIndex);
304305
void FileWriteLine(int fileIndex, const std::string& line);
305306
bool FileEOF(int fileIndex);
@@ -525,14 +526,24 @@ namespace RTE {
525526
bool DirectoryRemove(const std::string &path, bool recursive);
526527

527528
/// <summary>
528-
/// Moves or renames the filesystem object oldPath to newPath.
529+
/// Moves or renames the file oldPath to newPath.
529530
/// In order to get consistent behavior across Windows and Linux across all 4 combinations of oldPath and newPath being a directory/file,
530531
/// the newPath isn't allowed to already exist.
531532
/// </summary>
532533
/// <param name="oldPath">Path to the filesystem object. All paths are made absolute by adding current working directory to the specified path.</param>
533534
/// <param name="newPath">Path to the filesystem object. All paths are made absolute by adding current working directory to the specified path.</param>
534535
/// <returns>Whether or not renaming succeeded.</returns>
535-
bool Rename(const std::string &oldPath, const std::string &newPath);
536+
bool FileRename(const std::string &oldPath, const std::string &newPath);
537+
538+
/// <summary>
539+
/// Moves or renames the directory oldPath to newPath.
540+
/// In order to get consistent behavior across Windows and Linux across all 4 combinations of oldPath and newPath being a directory/file,
541+
/// the newPath isn't allowed to already exist.
542+
/// </summary>
543+
/// <param name="oldPath">Path to the filesystem object. All paths are made absolute by adding current working directory to the specified path.</param>
544+
/// <param name="newPath">Path to the filesystem object. All paths are made absolute by adding current working directory to the specified path.</param>
545+
/// <returns>Whether or not renaming succeeded.</returns>
546+
bool DirectoryRename(const std::string &oldPath, const std::string &newPath);
536547

537548
/// <summary>
538549
/// Reads a line from a file.

0 commit comments

Comments
 (0)