Skip to content

Commit b2c8498

Browse files
committed
Fix FileExists() so it doesn't return true on dirs
Add DirectoryExists() Add IsValidModulePath() Add FileRemove() Add DirectoryCreate() Add DirectoryRemove() Add Rename()
1 parent 04df2fa commit b2c8498

File tree

2 files changed

+198
-35
lines changed

2 files changed

+198
-35
lines changed

Source/Managers/LuaMan.cpp

Lines changed: 128 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,16 @@ namespace RTE {
7171
.def("GetDirectoryList", &LuaStateWrapper::DirectoryList, luabind::adopt(luabind::return_value) + luabind::return_stl_iterator)
7272
.def("GetFileList", &LuaStateWrapper::FileList, luabind::adopt(luabind::return_value) + luabind::return_stl_iterator)
7373
.def("FileExists", &LuaStateWrapper::FileExists)
74+
.def("DirectoryExists", &LuaStateWrapper::DirectoryExists)
75+
.def("IsValidModulePath", &LuaStateWrapper::IsValidModulePath)
7476
.def("FileOpen", &LuaStateWrapper::FileOpen)
7577
.def("FileClose", &LuaStateWrapper::FileClose)
78+
.def("FileRemove", &LuaStateWrapper::FileRemove)
79+
.def("DirectoryCreate", &LuaStateWrapper::DirectoryCreate1)
80+
.def("DirectoryCreate", &LuaStateWrapper::DirectoryCreate2)
81+
.def("DirectoryRemove", &LuaStateWrapper::DirectoryRemove1)
82+
.def("DirectoryRemove", &LuaStateWrapper::DirectoryRemove2)
83+
.def("Rename", &LuaStateWrapper::Rename)
7684
.def("FileReadLine", &LuaStateWrapper::FileReadLine)
7785
.def("FileWriteLine", &LuaStateWrapper::FileWriteLine)
7886
.def("FileEOF", &LuaStateWrapper::FileEOF),
@@ -273,12 +281,20 @@ namespace RTE {
273281
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
274282

275283
// Passthrough LuaMan Functions
276-
const std::vector<std::string>* LuaStateWrapper::DirectoryList(const std::string& relativeDirectory) { return g_LuaMan.DirectoryList(relativeDirectory); }
277-
const std::vector<std::string>* LuaStateWrapper::FileList(const std::string& relativeDirectory) { return g_LuaMan.FileList(relativeDirectory); }
278-
bool LuaStateWrapper::FileExists(const std::string &fileName) { return g_LuaMan.FileExists(fileName); }
279-
int LuaStateWrapper::FileOpen(const std::string& fileName, const std::string& accessMode) { return g_LuaMan.FileOpen(fileName, accessMode); }
284+
const std::vector<std::string>* LuaStateWrapper::DirectoryList(const std::string& path) { return g_LuaMan.DirectoryList(path); }
285+
const std::vector<std::string>* LuaStateWrapper::FileList(const std::string& path) { return g_LuaMan.FileList(path); }
286+
bool LuaStateWrapper::FileExists(const std::string &path) { return g_LuaMan.FileExists(path); }
287+
bool LuaStateWrapper::DirectoryExists(const std::string &path) { return g_LuaMan.DirectoryExists(path); }
288+
bool LuaStateWrapper::IsValidModulePath(const std::string &path) { return g_LuaMan.IsValidModulePath(path); }
289+
int LuaStateWrapper::FileOpen(const std::string& path, const std::string& accessMode) { return g_LuaMan.FileOpen(path, accessMode); }
280290
void LuaStateWrapper::FileClose(int fileIndex) { return g_LuaMan.FileClose(fileIndex); }
281291
void LuaStateWrapper::FileCloseAll() { return g_LuaMan.FileCloseAll(); }
292+
bool LuaStateWrapper::FileRemove(const std::string& path) { return g_LuaMan.FileRemove(path); }
293+
bool LuaStateWrapper::DirectoryCreate1(const std::string& path) { return g_LuaMan.DirectoryCreate(path, false); }
294+
bool LuaStateWrapper::DirectoryCreate2(const std::string& path, bool recursive) { return g_LuaMan.DirectoryCreate(path, recursive); }
295+
bool LuaStateWrapper::DirectoryRemove1(const std::string& path) { return g_LuaMan.DirectoryRemove(path, false); }
296+
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); }
282298
std::string LuaStateWrapper::FileReadLine(int fileIndex) { return g_LuaMan.FileReadLine(fileIndex); }
283299
void LuaStateWrapper::FileWriteLine(int fileIndex, const std::string& line) { return g_LuaMan.FileWriteLine(fileIndex, line); }
284300
bool LuaStateWrapper::FileEOF(int fileIndex) { return g_LuaMan.FileEOF(fileIndex); }
@@ -923,40 +939,56 @@ namespace RTE {
923939

924940
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
925941

926-
const std::vector<std::string> * LuaMan::DirectoryList(const std::string &filePath) {
942+
const std::vector<std::string> * LuaMan::DirectoryList(const std::string &path) {
927943
auto *directoryPaths = new std::vector<std::string>();
928944

929-
for (const std::filesystem::directory_entry &directoryEntry : std::filesystem::directory_iterator(System::GetWorkingDirectory() + filePath)) {
945+
for (const std::filesystem::directory_entry &directoryEntry : std::filesystem::directory_iterator(System::GetWorkingDirectory() + path)) {
930946
if (directoryEntry.is_directory()) { directoryPaths->emplace_back(directoryEntry.path().filename().generic_string()); }
931947
}
932948
return directoryPaths;
933949
}
934950

935951
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
936952

937-
const std::vector<std::string> * LuaMan::FileList(const std::string &filePath) {
953+
const std::vector<std::string> * LuaMan::FileList(const std::string &path) {
938954
auto *filePaths = new std::vector<std::string>();
939955

940-
for (const std::filesystem::directory_entry &directoryEntry : std::filesystem::directory_iterator(System::GetWorkingDirectory() + filePath)) {
956+
for (const std::filesystem::directory_entry &directoryEntry : std::filesystem::directory_iterator(System::GetWorkingDirectory() + path)) {
941957
if (directoryEntry.is_regular_file()) { filePaths->emplace_back(directoryEntry.path().filename().generic_string()); }
942958
}
943959
return filePaths;
944960
}
945961

946962
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
947963

948-
bool LuaMan::FileExists(const std::string &fileName) {
949-
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(fileName);
950-
if ((fullPath.find("..") == std::string::npos) && (fullPath.find(System::GetModulePackageExtension()) != std::string::npos)) {
951-
return std::filesystem::exists(fullPath);
964+
bool LuaMan::FileExists(const std::string &path) {
965+
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
966+
if (IsValidModulePath(fullPath)) {
967+
return std::filesystem::is_regular_file(fullPath);
952968
}
969+
return false;
970+
}
971+
972+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
953973

974+
bool LuaMan::DirectoryExists(const std::string &path) {
975+
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
976+
if (IsValidModulePath(fullPath)) {
977+
return std::filesystem::is_directory(fullPath);
978+
}
954979
return false;
955980
}
956981

957982
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
958983

959-
int LuaMan::FileOpen(const std::string &fileName, const std::string &accessMode) {
984+
// TODO: Move to ModuleMan, once the ModuleMan PR has been merged
985+
bool LuaMan::IsValidModulePath(const std::string &path) {
986+
return (path.find("..") == std::string::npos) && (path.find(System::GetModulePackageExtension()) != std::string::npos);
987+
}
988+
989+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
990+
991+
int LuaMan::FileOpen(const std::string &path, const std::string &accessMode) {
960992
if (c_FileAccessModes.find(accessMode) == c_FileAccessModes.end()) {
961993
g_ConsoleMan.PrintString("ERROR: Cannot open file, invalid file access mode specified.");
962994
return -1;
@@ -974,8 +1006,8 @@ namespace RTE {
9741006
return -1;
9751007
}
9761008

977-
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(fileName);
978-
if ((fullPath.find("..") == std::string::npos) && (fullPath.find(System::GetModulePackageExtension()) != std::string::npos)) {
1009+
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
1010+
if (IsValidModulePath(fullPath)) {
9791011

9801012
#ifdef _WIN32
9811013
FILE *file = fopen(fullPath.c_str(), accessMode.c_str());
@@ -1013,7 +1045,7 @@ namespace RTE {
10131045
return fileIndex;
10141046
}
10151047
}
1016-
g_ConsoleMan.PrintString("ERROR: Failed to open file " + fileName);
1048+
g_ConsoleMan.PrintString("ERROR: Failed to open file " + path);
10171049
return -1;
10181050
}
10191051

@@ -1034,6 +1066,86 @@ namespace RTE {
10341066
}
10351067
}
10361068

1069+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1070+
1071+
bool LuaMan::FileRemove(const std::string& path) {
1072+
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
1073+
if (IsValidModulePath(fullPath) && std::filesystem::is_regular_file(fullPath)) {
1074+
#ifdef _WIN32
1075+
return std::filesystem::remove(fullPath);
1076+
#else
1077+
// TODO: Make sure to try this on Ubuntu
1078+
?
1079+
#endif
1080+
}
1081+
g_ConsoleMan.PrintString("ERROR: Failed to remove file " + path);
1082+
return false;
1083+
}
1084+
1085+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1086+
1087+
bool LuaMan::DirectoryCreate(const std::string& path, bool recursive) {
1088+
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
1089+
if (IsValidModulePath(fullPath)) {
1090+
try {
1091+
#ifdef _WIN32
1092+
if (recursive) {
1093+
return std::filesystem::create_directories(fullPath);
1094+
} else {
1095+
return std::filesystem::create_directory(fullPath);
1096+
}
1097+
#else
1098+
// TODO: Make sure to try this on Ubuntu
1099+
?
1100+
#endif
1101+
} catch (const std::filesystem::filesystem_error &e) {}
1102+
}
1103+
g_ConsoleMan.PrintString("ERROR: Failed to remove directory " + path);
1104+
return false;
1105+
}
1106+
1107+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1108+
1109+
bool LuaMan::DirectoryRemove(const std::string& path, bool recursive) {
1110+
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
1111+
if (IsValidModulePath(fullPath) && std::filesystem::is_directory(fullPath)) {
1112+
try {
1113+
#ifdef _WIN32
1114+
if (recursive) {
1115+
return std::filesystem::remove_all(fullPath) > 0;
1116+
} else {
1117+
return std::filesystem::remove(fullPath);
1118+
}
1119+
#else
1120+
// TODO: Make sure to try this on Ubuntu
1121+
?
1122+
#endif
1123+
} catch (const std::filesystem::filesystem_error &e) {}
1124+
}
1125+
g_ConsoleMan.PrintString("ERROR: Failed to remove directory " + path);
1126+
return false;
1127+
}
1128+
1129+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1130+
1131+
bool LuaMan::Rename(const std::string& oldPath, const std::string& newPath) {
1132+
std::string fullOldPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(oldPath);
1133+
std::string fullNewPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(newPath);
1134+
if (IsValidModulePath(fullOldPath) && IsValidModulePath(fullNewPath)) {
1135+
try {
1136+
#ifdef _WIN32
1137+
std::filesystem::rename(fullOldPath, fullNewPath);
1138+
return true;
1139+
#else
1140+
// TODO: Make sure to try this on Ubuntu
1141+
?
1142+
#endif
1143+
} catch (const std::filesystem::filesystem_error &e) {}
1144+
}
1145+
g_ConsoleMan.PrintString("ERROR: Failed to rename oldPath " + oldPath + " to newPath " + newPath);
1146+
return false;
1147+
}
1148+
10371149
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
10381150

10391151
std::string LuaMan::FileReadLine(int fileIndex) {

Source/Managers/LuaMan.h

Lines changed: 70 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,20 @@ namespace RTE {
284284
double PosRand();
285285

286286
#pragma region Passthrough LuaMan Functions
287-
const std::vector<std::string>* DirectoryList(const std::string& relativeDirectory);
288-
const std::vector<std::string>* FileList(const std::string& relativeDirectory);
289-
bool FileExists(const std::string &fileName);
290-
int FileOpen(const std::string& fileName, const std::string& accessMode);
287+
const std::vector<std::string>* DirectoryList(const std::string& path);
288+
const std::vector<std::string>* FileList(const std::string& path);
289+
bool FileExists(const std::string &path);
290+
bool DirectoryExists(const std::string &path);
291+
bool IsValidModulePath(const std::string &path);
292+
int FileOpen(const std::string& path, const std::string& accessMode);
291293
void FileClose(int fileIndex);
292294
void FileCloseAll();
295+
bool FileRemove(const std::string& path);
296+
bool DirectoryCreate1(const std::string& path);
297+
bool DirectoryCreate2(const std::string& path, bool recursive);
298+
bool DirectoryRemove1(const std::string& path);
299+
bool DirectoryRemove2(const std::string& path, bool recursive);
300+
bool Rename(const std::string& oldPath, const std::string& newPath);
293301
std::string FileReadLine(int fileIndex);
294302
void FileWriteLine(int fileIndex, const std::string& line);
295303
bool FileEOF(int fileIndex);
@@ -428,36 +436,48 @@ namespace RTE {
428436

429437
#pragma region File I/O Handling
430438
/// <summary>
431-
/// Returns a vector of all the directories in relativeDirectory, which is relative to the working directory.
432-
/// Note that a call to this method overwrites any previously returned vector from DirectoryList() or FileList().
439+
/// Returns a vector of all the directories in path, which is relative to the working directory.
433440
/// </summary>
434-
/// <param name="relativeDirectory">Directory path relative to the working directory.</param>
435-
/// <returns>A vector of the directories in relativeDirectory.</returns>
436-
const std::vector<std::string> * DirectoryList(const std::string &relativeDirectory);
441+
/// <param name="path">Directory path relative to the working directory.</param>
442+
/// <returns>A vector of the directories in path.</returns>
443+
const std::vector<std::string> * DirectoryList(const std::string &path);
437444

438445
/// <summary>
439-
/// Returns a vector of all the files in relativeDirectory, which is relative to the working directory.
440-
/// Note that a call to this method overwrites any previously returned vector from DirectoryList() or FileList().
446+
/// Returns a vector of all the files in path, which is relative to the working directory.
441447
/// </summary>
442-
/// <param name="relativeDirectory">Directory path relative to the working directory.</param>
443-
/// <returns>A vector of the files in relativeDirectory.</returns>
444-
const std::vector<std::string> * FileList(const std::string &relativeDirectory);
448+
/// <param name="path">Directory path relative to the working directory.</param>
449+
/// <returns>A vector of the files in path.</returns>
450+
const std::vector<std::string> * FileList(const std::string &path);
445451

446452
/// <summary>
447453
/// Returns whether or not the specified file exists. You can only check for files inside .rte folders in the working directory.
448454
/// </summary>
449-
/// <param name="fileName">Path to the file. All paths are made absolute by adding current working directory to the specified path.</param>
455+
/// <param name="path">Path to the file. All paths are made absolute by adding current working directory to the specified path.</param>
450456
/// <returns>Whether or not the specified file exists.</returns>
451-
bool FileExists(const std::string &fileName);
457+
bool FileExists(const std::string &path);
452458

453459
/// <summary>
454-
/// Opens a file or creates one if it does not exist, depending on access mode. You can open files only inside .rte folders in the working directly. You can't open more that c_MaxOpenFiles file simultaneously.
460+
/// Returns whether or not the specified directory exists. You can only check for directories inside .rte folders in the working directory.
461+
/// </summary>
462+
/// <param name="path">Path to the directory. All paths are made absolute by adding current working directory to the specified path.</param>
463+
/// <returns>Whether or not the specified file exists.</returns>
464+
bool DirectoryExists(const std::string &path);
465+
466+
/// <summary>
467+
/// Returns whether or not the path refers to an accessible file or directory. You can only check for files or directories inside .rte directories in the working directory.
468+
/// </summary>
469+
/// <param name="path">Path to the file or directory. All paths are made absolute by adding current working directory to the specified path.</param>
470+
/// <returns>Whether or not the specified file exists.</returns>
471+
bool IsValidModulePath(const std::string &path);
472+
473+
/// <summary>
474+
/// Opens a file or creates one if it does not exist, depending on access mode. You can open files only inside .rte folders in the working directory. You can't open more that c_MaxOpenFiles file simultaneously.
455475
/// On Linux will attempt to open a file case insensitively.
456476
/// </summary>
457-
/// <param name="filename">Path to the file. All paths are made absolute by adding current working directory to the specified path.</param>
477+
/// <param name="path">Path to the file. All paths are made absolute by adding current working directory to the specified path.</param>
458478
/// <param name="mode">File access mode. See 'fopen' for list of modes.</param>
459479
/// <returns>File index in the opened files array.</returns>
460-
int FileOpen(const std::string &fileName, const std::string &accessMode);
480+
int FileOpen(const std::string &path, const std::string &accessMode);
461481

462482
/// <summary>
463483
/// Closes a previously opened file.
@@ -470,6 +490,37 @@ namespace RTE {
470490
/// </summary>
471491
void FileCloseAll();
472492

493+
/// <summary>
494+
/// Removes a file.
495+
/// </summary>
496+
/// <param name="path">Path to the file. All paths are made absolute by adding current working directory to the specified path.</param>
497+
/// <returns>Whether or not the file was removed.</returns>
498+
bool FileRemove(const std::string &path);
499+
500+
/// <summary>
501+
/// Creates a directory, optionally recursively.
502+
/// </summary>
503+
/// <param name="path">Path to the directory to be created. All paths are made absolute by adding current working directory to the specified path.</param>
504+
/// <param name="recursive">Whether to recursively create parent directories.</param>
505+
/// <returns>Whether or not the directory was removed.</returns>
506+
bool DirectoryCreate(const std::string &path, bool recursive);
507+
508+
/// <summary>
509+
/// Removes a directory, optionally recursively.
510+
/// </summary>
511+
/// <param name="path">Path to the directory to be removed. All paths are made absolute by adding current working directory to the specified path.</param>
512+
/// <param name="recursive">Whether to recursively remove files and directories.</param>
513+
/// <returns>Whether or not the directory was removed.</returns>
514+
bool DirectoryRemove(const std::string &path, bool recursive);
515+
516+
/// <summary>
517+
/// Moves or renames the filesystem object oldPath to newPath.
518+
/// </summary>
519+
/// <param name="oldPath">Path to the filesystem object. All paths are made absolute by adding current working directory to the specified path.</param>
520+
/// <param name="newPath">Path to the filesystem object. All paths are made absolute by adding current working directory to the specified path.</param>
521+
/// <returns>Whether or not renaming succeeded.</returns>
522+
bool Rename(const std::string &oldPath, const std::string &newPath);
523+
473524
/// <summary>
474525
/// Reads a line from a file.
475526
/// </summary>

0 commit comments

Comments
 (0)