@@ -940,21 +940,45 @@ namespace RTE {
940
940
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
941
941
942
942
const std::vector<std::string> * LuaMan::DirectoryList (const std::string &path) {
943
+ std::string fullPath = System::GetWorkingDirectory () + g_PresetMan.GetFullModulePath (path);
943
944
auto *directoryPaths = new std::vector<std::string>();
944
945
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
+ }
947
959
}
948
960
return directoryPaths;
949
961
}
950
962
951
963
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
952
964
953
965
const std::vector<std::string> * LuaMan::FileList (const std::string &path) {
966
+ std::string fullPath = System::GetWorkingDirectory () + g_PresetMan.GetFullModulePath (path);
954
967
auto *filePaths = new std::vector<std::string>();
955
968
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
+ }
958
982
}
959
983
return filePaths;
960
984
}
@@ -964,7 +988,16 @@ namespace RTE {
964
988
bool LuaMan::FileExists (const std::string &path) {
965
989
std::string fullPath = System::GetWorkingDirectory () + g_PresetMan.GetFullModulePath (path);
966
990
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
+ }
968
1001
}
969
1002
return false ;
970
1003
}
@@ -974,7 +1007,16 @@ namespace RTE {
974
1007
bool LuaMan::DirectoryExists (const std::string &path) {
975
1008
std::string fullPath = System::GetWorkingDirectory () + g_PresetMan.GetFullModulePath (path);
976
1009
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
+ }
978
1020
}
979
1021
return false ;
980
1022
}
@@ -988,6 +1030,35 @@ namespace RTE {
988
1030
989
1031
// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
990
1032
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
+
991
1062
int LuaMan::FileOpen (const std::string &path, const std::string &accessMode) {
992
1063
if (c_FileAccessModes.find (accessMode) == c_FileAccessModes.end ()) {
993
1064
g_ConsoleMan.PrintString (" ERROR: Cannot open file, invalid file access mode specified." );
@@ -1008,35 +1079,41 @@ namespace RTE {
1008
1079
1009
1080
std::string fullPath = System::GetWorkingDirectory () + g_PresetMan.GetFullModulePath (path);
1010
1081
if (IsValidModulePath (fullPath)) {
1011
-
1012
1082
#ifdef _WIN32
1013
1083
FILE *file = fopen (fullPath.c_str (), accessMode.c_str ());
1014
1084
#else
1015
1085
FILE *file = [&fullPath, &accessMode]() -> FILE* {
1016
1086
std::filesystem::path inspectedPath = System::GetWorkingDirectory ();
1017
1087
const std::filesystem::path relativeFilePath = std::filesystem::path (fullPath).lexically_relative (inspectedPath);
1018
1088
1089
+ // Iterate over all path parts
1019
1090
for (std::filesystem::path::const_iterator relativeFilePathIterator = relativeFilePath.begin (); relativeFilePathIterator != relativeFilePath.end (); ++relativeFilePathIterator) {
1020
1091
bool pathPartExists = false ;
1021
1092
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
1023
1095
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 ())) {
1025
1097
inspectedPath = filesystemEntryPath;
1098
+
1099
+ // If the path part is found, stop looking for it
1026
1100
pathPartExists = true ;
1027
1101
break ;
1028
1102
}
1029
1103
}
1104
+
1030
1105
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
1032
1107
if (std::next (relativeFilePathIterator) == relativeFilePath.end ()) {
1033
1108
return fopen ((inspectedPath / relativeFilePath.filename ()).generic_string ().c_str (), accessMode.c_str ());
1034
1109
}
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
1036
1112
return nullptr ;
1037
1113
}
1038
1114
}
1039
- // If the file exists, open it.
1115
+
1116
+ // If the file exists, open it
1040
1117
return fopen (inspectedPath.generic_string ().c_str (), accessMode.c_str ());
1041
1118
}();
1042
1119
#endif
@@ -1070,13 +1147,19 @@ namespace RTE {
1070
1147
1071
1148
bool LuaMan::FileRemove (const std::string& path) {
1072
1149
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;
1079
1158
#endif
1159
+ if (std::filesystem::is_regular_file (fullPath)) {
1160
+ return std::filesystem::remove (fullPath);
1161
+ }
1162
+ }
1080
1163
}
1081
1164
g_ConsoleMan.PrintString (" ERROR: Failed to remove file " + path);
1082
1165
return false ;
@@ -1087,18 +1170,62 @@ namespace RTE {
1087
1170
bool LuaMan::DirectoryCreate (const std::string& path, bool recursive) {
1088
1171
std::string fullPath = System::GetWorkingDirectory () + g_PresetMan.GetFullModulePath (path);
1089
1172
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;
1100
1220
#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
+ }
1102
1229
}
1103
1230
g_ConsoleMan.PrintString (" ERROR: Failed to remove directory " + path);
1104
1231
return false ;
@@ -1108,19 +1235,25 @@ namespace RTE {
1108
1235
1109
1236
bool LuaMan::DirectoryRemove (const std::string& path, bool recursive) {
1110
1237
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;
1122
1246
#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
+ }
1124
1257
}
1125
1258
g_ConsoleMan.PrintString (" ERROR: Failed to remove directory " + path);
1126
1259
return false ;
@@ -1132,15 +1265,21 @@ namespace RTE {
1132
1265
std::string fullOldPath = System::GetWorkingDirectory () + g_PresetMan.GetFullModulePath (oldPath);
1133
1266
std::string fullNewPath = System::GetWorkingDirectory () + g_PresetMan.GetFullModulePath (newPath);
1134
1267
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;
1142
1277
#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
+ }
1144
1283
}
1145
1284
g_ConsoleMan.PrintString (" ERROR: Failed to rename oldPath " + oldPath + " to newPath " + newPath);
1146
1285
return false ;
0 commit comments