Skip to content

Commit 2e25608

Browse files
committed
Passing all Lua filesystem tests on Windows
1 parent a35a7d6 commit 2e25608

File tree

1 file changed

+38
-42
lines changed

1 file changed

+38
-42
lines changed

Source/Managers/LuaMan.cpp

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ namespace RTE {
952952
#ifndef _WIN32
953953
fullPath = *caseInsensitiveFullPath;
954954
#endif
955-
for (const std::filesystem::directory_entry &directoryEntry : std::filesystem::directory_iterator(fullPath) {
955+
for (const std::filesystem::directory_entry &directoryEntry : std::filesystem::directory_iterator(fullPath)) {
956956
if (directoryEntry.is_directory()) { directoryPaths->emplace_back(directoryEntry.path().filename().generic_string()); }
957957
}
958958
}
@@ -975,7 +975,7 @@ namespace RTE {
975975
#ifndef _WIN32
976976
fullPath = *caseInsensitiveFullPath;
977977
#endif
978-
for (const std::filesystem::directory_entry &directoryEntry : std::filesystem::directory_iterator(fullPath) {
978+
for (const std::filesystem::directory_entry &directoryEntry : std::filesystem::directory_iterator(fullPath)) {
979979
if (directoryEntry.is_regular_file()) { filePaths->emplace_back(directoryEntry.path().filename().generic_string()); }
980980
}
981981
}
@@ -1056,7 +1056,7 @@ namespace RTE {
10561056
}
10571057
}
10581058

1059-
return std::optional<std::string>(inspectedPath);
1059+
return std::optional<std::string>(inspectedPath.generic_string());
10601060
}
10611061

10621062
int LuaMan::FileOpen(const std::string &path, const std::string &accessMode) {
@@ -1171,46 +1171,42 @@ namespace RTE {
11711171
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
11721172
if (IsValidModulePath(fullPath)) {
11731173
#ifndef _WIN32
1174-
// TODO: The goal here is to return the same fullPath,
1174+
// The goal here is to return the same fullPath,
11751175
// 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());
1176+
std::string caseInsensitiveFullPath = [&fullPath]() -> std::string {
1177+
std::filesystem::path inspectedPath = System::GetWorkingDirectory();
1178+
const std::filesystem::path relativeFilePath = std::filesystem::path(fullPath).lexically_relative(inspectedPath);
1179+
1180+
// Iterate over all path parts
1181+
for (std::filesystem::path::const_iterator relativeFilePathIterator = relativeFilePath.begin(); relativeFilePathIterator != relativeFilePath.end(); ++relativeFilePathIterator) {
1182+
bool pathPartExists = false;
1183+
1184+
// Iterate over all entries in the path part's directory,
1185+
// to check if the path part is in there case insensitively
1186+
for (const std::filesystem::path &filesystemEntryPath : std::filesystem::directory_iterator(inspectedPath)) {
1187+
if (StringsEqualCaseInsensitive(filesystemEntryPath.filename().generic_string(), relativeFilePathIterator->generic_string())) {
1188+
inspectedPath = filesystemEntryPath;
1189+
1190+
// If the path part is found, stop looking for it
1191+
pathPartExists = true;
1192+
break;
1193+
}
1194+
}
1195+
1196+
if (!pathPartExists) {
1197+
// If part of the path exists, append the rest of fullPath its parts
1198+
relativeFilePathIterator++;
1199+
while (relativeFilePathIterator != relativeFilePath.end()) {
1200+
inspectedPath += "/" + relativeFilePathIterator->generic_string();
1201+
relativeFilePathIterator++;
1202+
}
1203+
1204+
return inspectedPath;
1205+
}
1206+
}
1207+
1208+
// If the entire path exists
1209+
return inspectedPath;
12141210
}();
12151211
if (caseInsensitiveFullPath)
12161212
#endif

0 commit comments

Comments
 (0)