Skip to content

Commit a35a7d6

Browse files
committed
Only need to write DirectoryCreate() still
1 parent b2c8498 commit a35a7d6

File tree

1 file changed

+188
-49
lines changed

1 file changed

+188
-49
lines changed

Source/Managers/LuaMan.cpp

Lines changed: 188 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -940,21 +940,45 @@ namespace RTE {
940940
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
941941

942942
const std::vector<std::string> * LuaMan::DirectoryList(const std::string &path) {
943+
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
943944
auto *directoryPaths = new std::vector<std::string>();
944945

945-
for (const std::filesystem::directory_entry &directoryEntry : std::filesystem::directory_iterator(System::GetWorkingDirectory() + path)) {
946-
if (directoryEntry.is_directory()) { directoryPaths->emplace_back(directoryEntry.path().filename().generic_string()); }
946+
if (IsValidModulePath(fullPath)) {
947+
#ifndef _WIN32
948+
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath)
949+
if (caseInsensitiveFullPath)
950+
#endif
951+
{
952+
#ifndef _WIN32
953+
fullPath = *caseInsensitiveFullPath;
954+
#endif
955+
for (const std::filesystem::directory_entry &directoryEntry : std::filesystem::directory_iterator(fullPath) {
956+
if (directoryEntry.is_directory()) { directoryPaths->emplace_back(directoryEntry.path().filename().generic_string()); }
957+
}
958+
}
947959
}
948960
return directoryPaths;
949961
}
950962

951963
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
952964

953965
const std::vector<std::string> * LuaMan::FileList(const std::string &path) {
966+
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
954967
auto *filePaths = new std::vector<std::string>();
955968

956-
for (const std::filesystem::directory_entry &directoryEntry : std::filesystem::directory_iterator(System::GetWorkingDirectory() + path)) {
957-
if (directoryEntry.is_regular_file()) { filePaths->emplace_back(directoryEntry.path().filename().generic_string()); }
969+
if (IsValidModulePath(fullPath)) {
970+
#ifndef _WIN32
971+
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath)
972+
if (caseInsensitiveFullPath)
973+
#endif
974+
{
975+
#ifndef _WIN32
976+
fullPath = *caseInsensitiveFullPath;
977+
#endif
978+
for (const std::filesystem::directory_entry &directoryEntry : std::filesystem::directory_iterator(fullPath) {
979+
if (directoryEntry.is_regular_file()) { filePaths->emplace_back(directoryEntry.path().filename().generic_string()); }
980+
}
981+
}
958982
}
959983
return filePaths;
960984
}
@@ -964,7 +988,16 @@ namespace RTE {
964988
bool LuaMan::FileExists(const std::string &path) {
965989
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
966990
if (IsValidModulePath(fullPath)) {
967-
return std::filesystem::is_regular_file(fullPath);
991+
#ifndef _WIN32
992+
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath)
993+
if (caseInsensitiveFullPath)
994+
#endif
995+
{
996+
#ifndef _WIN32
997+
fullPath = *caseInsensitiveFullPath;
998+
#endif
999+
return std::filesystem::is_regular_file(fullPath);
1000+
}
9681001
}
9691002
return false;
9701003
}
@@ -974,7 +1007,16 @@ namespace RTE {
9741007
bool LuaMan::DirectoryExists(const std::string &path) {
9751008
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
9761009
if (IsValidModulePath(fullPath)) {
977-
return std::filesystem::is_directory(fullPath);
1010+
#ifndef _WIN32
1011+
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath)
1012+
if (caseInsensitiveFullPath)
1013+
#endif
1014+
{
1015+
#ifndef _WIN32
1016+
fullPath = *caseInsensitiveFullPath;
1017+
#endif
1018+
return std::filesystem::is_directory(fullPath);
1019+
}
9781020
}
9791021
return false;
9801022
}
@@ -988,6 +1030,35 @@ namespace RTE {
9881030

9891031
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
9901032

1033+
// TODO: Move this shit to some other place
1034+
std::optional<std::string> GetCaseInsensitiveFullPath(const std::string &fullPath) {
1035+
std::filesystem::path inspectedPath = System::GetWorkingDirectory();
1036+
const std::filesystem::path relativeFilePath = std::filesystem::path(fullPath).lexically_relative(inspectedPath);
1037+
1038+
// Iterate over all path parts
1039+
for (std::filesystem::path::const_iterator relativeFilePathIterator = relativeFilePath.begin(); relativeFilePathIterator != relativeFilePath.end(); ++relativeFilePathIterator) {
1040+
bool pathPartExists = false;
1041+
1042+
// Iterate over all entries in the path part's directory,
1043+
// to check if the path part is in there case insensitively
1044+
for (const std::filesystem::path &filesystemEntryPath : std::filesystem::directory_iterator(inspectedPath)) {
1045+
if (StringsEqualCaseInsensitive(filesystemEntryPath.filename().generic_string(), relativeFilePathIterator->generic_string())) {
1046+
inspectedPath = filesystemEntryPath;
1047+
1048+
// If the path part is found, stop looking for it
1049+
pathPartExists = true;
1050+
break;
1051+
}
1052+
}
1053+
1054+
if (!pathPartExists) {
1055+
return std::nullopt;
1056+
}
1057+
}
1058+
1059+
return std::optional<std::string>(inspectedPath);
1060+
}
1061+
9911062
int LuaMan::FileOpen(const std::string &path, const std::string &accessMode) {
9921063
if (c_FileAccessModes.find(accessMode) == c_FileAccessModes.end()) {
9931064
g_ConsoleMan.PrintString("ERROR: Cannot open file, invalid file access mode specified.");
@@ -1008,35 +1079,41 @@ namespace RTE {
10081079

10091080
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
10101081
if (IsValidModulePath(fullPath)) {
1011-
10121082
#ifdef _WIN32
10131083
FILE *file = fopen(fullPath.c_str(), accessMode.c_str());
10141084
#else
10151085
FILE *file = [&fullPath, &accessMode]() -> FILE* {
10161086
std::filesystem::path inspectedPath = System::GetWorkingDirectory();
10171087
const std::filesystem::path relativeFilePath = std::filesystem::path(fullPath).lexically_relative(inspectedPath);
10181088

1089+
// Iterate over all path parts
10191090
for (std::filesystem::path::const_iterator relativeFilePathIterator = relativeFilePath.begin(); relativeFilePathIterator != relativeFilePath.end(); ++relativeFilePathIterator) {
10201091
bool pathPartExists = false;
10211092

1022-
// Check if a path part (directory or file) exists in the filesystem.
1093+
// Iterate over all entries in the path part's directory,
1094+
// to check if the path part is in there case insensitively
10231095
for (const std::filesystem::path &filesystemEntryPath : std::filesystem::directory_iterator(inspectedPath)) {
1024-
if (StringsEqualCaseInsensitive(filesystemEntryPath.filename().generic_string(), (*relativeFilePathIterator).generic_string())) {
1096+
if (StringsEqualCaseInsensitive(filesystemEntryPath.filename().generic_string(), relativeFilePathIterator->generic_string())) {
10251097
inspectedPath = filesystemEntryPath;
1098+
1099+
// If the path part is found, stop looking for it
10261100
pathPartExists = true;
10271101
break;
10281102
}
10291103
}
1104+
10301105
if (!pathPartExists) {
1031-
// If this is the last part, then all directories in relativeFilePath exist, but the file doesn't.
1106+
// If this is the last part, then all directories in relativeFilePath exist, but the file doesn't
10321107
if (std::next(relativeFilePathIterator) == relativeFilePath.end()) {
10331108
return fopen((inspectedPath / relativeFilePath.filename()).generic_string().c_str(), accessMode.c_str());
10341109
}
1035-
// Some directory in relativeFilePath doesn't exist, so the file can't be created.
1110+
1111+
// Some directory in relativeFilePath doesn't exist, so the file can't be created
10361112
return nullptr;
10371113
}
10381114
}
1039-
// If the file exists, open it.
1115+
1116+
// If the file exists, open it
10401117
return fopen(inspectedPath.generic_string().c_str(), accessMode.c_str());
10411118
}();
10421119
#endif
@@ -1070,13 +1147,19 @@ namespace RTE {
10701147

10711148
bool LuaMan::FileRemove(const std::string& path) {
10721149
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-
?
1150+
if (IsValidModulePath(fullPath)) {
1151+
#ifndef _WIN32
1152+
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath)
1153+
if (caseInsensitiveFullPath)
1154+
#endif
1155+
{
1156+
#ifndef _WIN32
1157+
fullPath = *caseInsensitiveFullPath;
10791158
#endif
1159+
if (std::filesystem::is_regular_file(fullPath)) {
1160+
return std::filesystem::remove(fullPath);
1161+
}
1162+
}
10801163
}
10811164
g_ConsoleMan.PrintString("ERROR: Failed to remove file " + path);
10821165
return false;
@@ -1087,18 +1170,62 @@ namespace RTE {
10871170
bool LuaMan::DirectoryCreate(const std::string& path, bool recursive) {
10881171
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
10891172
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-
?
1173+
#ifndef _WIN32
1174+
// TODO: The goal here is to return the same fullPath,
1175+
// but with the parent directories given the real filesys casing
1176+
//
1177+
// 1. There is no way for this lambda to fail
1178+
// 1. Account for the `recursive` argument of this fn
1179+
// 2. Let a lambda true if a parent directory has to be created,
1180+
// since create_directory() will return false for us in that case
1181+
auto caseInsensitiveFullPath = [&fullPath]() -> auto {
1182+
// std::filesystem::path inspectedPath = System::GetWorkingDirectory();
1183+
// const std::filesystem::path relativeFilePath = std::filesystem::path(fullPath).lexically_relative(inspectedPath);
1184+
1185+
// // Iterate over all path parts
1186+
// for (std::filesystem::path::const_iterator relativeFilePathIterator = relativeFilePath.begin(); relativeFilePathIterator != relativeFilePath.end(); ++relativeFilePathIterator) {
1187+
// bool pathPartExists = false;
1188+
1189+
// // Iterate over all entries in the path part's directory,
1190+
// // to check if the path part is in there case insensitively
1191+
// for (const std::filesystem::path &filesystemEntryPath : std::filesystem::directory_iterator(inspectedPath)) {
1192+
// if (StringsEqualCaseInsensitive(filesystemEntryPath.filename().generic_string(), relativeFilePathIterator->generic_string())) {
1193+
// inspectedPath = filesystemEntryPath;
1194+
1195+
// // If the path part is found, stop looking for it
1196+
// pathPartExists = true;
1197+
// break;
1198+
// }
1199+
// }
1200+
1201+
// if (!pathPartExists) {
1202+
// // If this is the last part, then all directories in relativeFilePath exist, but the file doesn't
1203+
// if (std::next(relativeFilePathIterator) == relativeFilePath.end()) {
1204+
// return fopen((inspectedPath / relativeFilePath.filename()).generic_string().c_str(), accessMode.c_str());
1205+
// }
1206+
1207+
// // Some directory in relativeFilePath doesn't exist, so the file can't be created
1208+
// return nullptr;
1209+
// }
1210+
// }
1211+
1212+
// // If the file exists, open it
1213+
// return fopen(inspectedPath.generic_string().c_str(), accessMode.c_str());
1214+
}();
1215+
if (caseInsensitiveFullPath)
1216+
#endif
1217+
{
1218+
#ifndef _WIN32
1219+
fullPath = *caseInsensitiveFullPath;
11001220
#endif
1101-
} catch (const std::filesystem::filesystem_error &e) {}
1221+
try {
1222+
if (recursive) {
1223+
return std::filesystem::create_directories(fullPath);
1224+
} else {
1225+
return std::filesystem::create_directory(fullPath);
1226+
}
1227+
} catch (const std::filesystem::filesystem_error &e) {}
1228+
}
11021229
}
11031230
g_ConsoleMan.PrintString("ERROR: Failed to remove directory " + path);
11041231
return false;
@@ -1108,19 +1235,25 @@ namespace RTE {
11081235

11091236
bool LuaMan::DirectoryRemove(const std::string& path, bool recursive) {
11101237
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-
?
1238+
if (IsValidModulePath(fullPath)) {
1239+
#ifndef _WIN32
1240+
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath)
1241+
if (caseInsensitiveFullPath)
1242+
#endif
1243+
{
1244+
#ifndef _WIN32
1245+
fullPath = *caseInsensitiveFullPath;
11221246
#endif
1123-
} catch (const std::filesystem::filesystem_error &e) {}
1247+
if (std::filesystem::is_directory(fullPath)) {
1248+
try {
1249+
if (recursive) {
1250+
return std::filesystem::remove_all(fullPath) > 0;
1251+
} else {
1252+
return std::filesystem::remove(fullPath);
1253+
}
1254+
} catch (const std::filesystem::filesystem_error &e) {}
1255+
}
1256+
}
11241257
}
11251258
g_ConsoleMan.PrintString("ERROR: Failed to remove directory " + path);
11261259
return false;
@@ -1132,15 +1265,21 @@ namespace RTE {
11321265
std::string fullOldPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(oldPath);
11331266
std::string fullNewPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(newPath);
11341267
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-
?
1268+
#ifndef _WIN32
1269+
auto caseInsensitiveFullOldPath = GetCaseInsensitiveFullPath(fullOldPath)
1270+
auto caseInsensitiveFullNewPath = GetCaseInsensitiveFullPath(fullNewPath)
1271+
if (caseInsensitiveFullOldPath && caseInsensitiveFullNewPath)
1272+
#endif
1273+
{
1274+
#ifndef _WIN32
1275+
fullOldPath = *caseInsensitiveFullOldPath;
1276+
fullNewPath = *caseInsensitiveFullNewPath;
11421277
#endif
1143-
} catch (const std::filesystem::filesystem_error &e) {}
1278+
try {
1279+
std::filesystem::rename(fullOldPath, fullNewPath);
1280+
return true;
1281+
} catch (const std::filesystem::filesystem_error &e) {}
1282+
}
11441283
}
11451284
g_ConsoleMan.PrintString("ERROR: Failed to rename oldPath " + oldPath + " to newPath " + newPath);
11461285
return false;

0 commit comments

Comments
 (0)