Skip to content

Commit ba1922e

Browse files
committed
Pass all Lua tests
1 parent f2dfa03 commit ba1922e

File tree

2 files changed

+45
-116
lines changed

2 files changed

+45
-116
lines changed

Source/Managers/LuaMan.cpp

Lines changed: 42 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ namespace RTE {
940940
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
941941

942942
// TODO: Ask Causeless whether this is an appropriate spot for this method
943-
std::optional<std::string> LuaMan::GetCaseInsensitiveFullPath(const std::string &fullPath) {
943+
std::string LuaMan::GetCaseInsensitiveFullPath(const std::string &fullPath) {
944944
std::filesystem::path inspectedPath = System::GetWorkingDirectory();
945945
const std::filesystem::path relativeFilePath = std::filesystem::path(fullPath).lexically_relative(inspectedPath);
946946

@@ -961,16 +961,16 @@ namespace RTE {
961961
}
962962

963963
if (!pathPartExists) {
964-
// TODO: This should return the same thing DirectoryCreate() does,
965-
// where it just appends the rest of fullPath.
966-
// This way it's the responsibility of the caller to use std::filestem::exists(),
967-
// which allows DirectoryCreate() and Rename() to call this function too,
968-
// since they allow the fullPath and newPath argument to not exist yet.
969-
return NOTasdstd::nullopt;
964+
// If part of the path exists, append the rest of fullPath its parts
965+
while (relativeFilePathIterator != relativeFilePath.end()) {
966+
inspectedPath /= relativeFilePathIterator->generic_string();
967+
relativeFilePathIterator++;
968+
}
969+
break;
970970
}
971971
}
972972

973-
return std::optional<std::string>(inspectedPath.generic_string());
973+
return inspectedPath.generic_string();
974974
}
975975

976976
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -981,13 +981,10 @@ namespace RTE {
981981

982982
if (IsValidModulePath(fullPath)) {
983983
#ifndef _WIN32
984-
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath);
985-
if (caseInsensitiveFullPath)
984+
fullPath = GetCaseInsensitiveFullPath(fullPath);
986985
#endif
986+
if (std::filesystem::exists(fullPath))
987987
{
988-
#ifndef _WIN32
989-
fullPath = *caseInsensitiveFullPath;
990-
#endif
991988
for (const std::filesystem::directory_entry &directoryEntry : std::filesystem::directory_iterator(fullPath)) {
992989
if (directoryEntry.is_directory()) { directoryPaths->emplace_back(directoryEntry.path().filename().generic_string()); }
993990
}
@@ -1004,13 +1001,10 @@ namespace RTE {
10041001

10051002
if (IsValidModulePath(fullPath)) {
10061003
#ifndef _WIN32
1007-
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath);
1008-
if (caseInsensitiveFullPath)
1004+
fullPath = GetCaseInsensitiveFullPath(fullPath);
10091005
#endif
1006+
if (std::filesystem::exists(fullPath))
10101007
{
1011-
#ifndef _WIN32
1012-
fullPath = *caseInsensitiveFullPath;
1013-
#endif
10141008
for (const std::filesystem::directory_entry &directoryEntry : std::filesystem::directory_iterator(fullPath)) {
10151009
if (directoryEntry.is_regular_file()) { filePaths->emplace_back(directoryEntry.path().filename().generic_string()); }
10161010
}
@@ -1025,15 +1019,9 @@ namespace RTE {
10251019
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
10261020
if (IsValidModulePath(fullPath)) {
10271021
#ifndef _WIN32
1028-
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath);
1029-
if (caseInsensitiveFullPath)
1022+
fullPath = GetCaseInsensitiveFullPath(fullPath);
10301023
#endif
1031-
{
1032-
#ifndef _WIN32
1033-
fullPath = *caseInsensitiveFullPath;
1034-
#endif
1035-
return std::filesystem::is_regular_file(fullPath);
1036-
}
1024+
return std::filesystem::is_regular_file(fullPath);
10371025
}
10381026
return false;
10391027
}
@@ -1044,15 +1032,9 @@ namespace RTE {
10441032
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
10451033
if (IsValidModulePath(fullPath)) {
10461034
#ifndef _WIN32
1047-
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath);
1048-
if (caseInsensitiveFullPath)
1049-
#endif
1050-
{
1051-
#ifndef _WIN32
1052-
fullPath = *caseInsensitiveFullPath;
1035+
fullPath = GetCaseInsensitiveFullPath(fullPath);
10531036
#endif
1054-
return std::filesystem::is_directory(fullPath);
1055-
}
1037+
return std::filesystem::is_directory(fullPath);
10561038
}
10571039
return false;
10581040
}
@@ -1156,16 +1138,10 @@ namespace RTE {
11561138
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
11571139
if (IsValidModulePath(fullPath)) {
11581140
#ifndef _WIN32
1159-
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath);
1160-
if (caseInsensitiveFullPath)
1161-
#endif
1162-
{
1163-
#ifndef _WIN32
1164-
fullPath = *caseInsensitiveFullPath;
1141+
fullPath = GetCaseInsensitiveFullPath(fullPath);
11651142
#endif
1166-
if (std::filesystem::is_regular_file(fullPath)) {
1167-
return std::filesystem::remove(fullPath);
1168-
}
1143+
if (std::filesystem::is_regular_file(fullPath)) {
1144+
return std::filesystem::remove(fullPath);
11691145
}
11701146
}
11711147
g_ConsoleMan.PrintString("ERROR: Failed to remove file " + path);
@@ -1178,49 +1154,15 @@ namespace RTE {
11781154
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
11791155
if (IsValidModulePath(fullPath)) {
11801156
#ifndef _WIN32
1181-
// The goal here is to return the same fullPath,
1182-
// but with the parent directories given the real filesys casing
1183-
std::filesystem::path inspectedPath = System::GetWorkingDirectory();
1184-
const std::filesystem::path relativeFilePath = std::filesystem::path(fullPath).lexically_relative(inspectedPath);
1185-
1186-
// Iterate over all path parts
1187-
for (std::filesystem::path::const_iterator relativeFilePathIterator = relativeFilePath.begin(); relativeFilePathIterator != relativeFilePath.end(); ++relativeFilePathIterator) {
1188-
bool pathPartExists = false;
1189-
1190-
// Iterate over all entries in the path part's directory,
1191-
// to check if the path part is in there case insensitively
1192-
for (const std::filesystem::path &filesystemEntryPath : std::filesystem::directory_iterator(inspectedPath)) {
1193-
if (StringsEqualCaseInsensitive(filesystemEntryPath.filename().generic_string(), relativeFilePathIterator->generic_string())) {
1194-
inspectedPath = filesystemEntryPath;
1195-
1196-
// If the path part is found, stop looking for it
1197-
pathPartExists = true;
1198-
break;
1199-
}
1200-
}
1201-
1202-
if (!pathPartExists) {
1203-
// If part of the path exists, append the rest of fullPath its parts
1204-
while (relativeFilePathIterator != relativeFilePath.end()) {
1205-
inspectedPath /= relativeFilePathIterator->generic_string();
1206-
relativeFilePathIterator++;
1207-
}
1208-
1209-
break;
1210-
}
1211-
}
1212-
1213-
fullPath = inspectedPath;
1157+
fullPath = GetCaseInsensitiveFullPath(fullPath);
12141158
#endif
1215-
{
1216-
try {
1217-
if (recursive) {
1218-
return std::filesystem::create_directories(fullPath);
1219-
} else {
1220-
return std::filesystem::create_directory(fullPath);
1221-
}
1222-
} catch (const std::filesystem::filesystem_error &e) {}
1223-
}
1159+
try {
1160+
if (recursive) {
1161+
return std::filesystem::create_directories(fullPath);
1162+
} else {
1163+
return std::filesystem::create_directory(fullPath);
1164+
}
1165+
} catch (const std::filesystem::filesystem_error &e) {}
12241166
}
12251167
g_ConsoleMan.PrintString("ERROR: Failed to remove directory " + path);
12261168
return false;
@@ -1232,22 +1174,16 @@ namespace RTE {
12321174
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
12331175
if (IsValidModulePath(fullPath)) {
12341176
#ifndef _WIN32
1235-
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath);
1236-
if (caseInsensitiveFullPath)
1237-
#endif
1238-
{
1239-
#ifndef _WIN32
1240-
fullPath = *caseInsensitiveFullPath;
1177+
fullPath = GetCaseInsensitiveFullPath(fullPath);
12411178
#endif
1242-
if (std::filesystem::is_directory(fullPath)) {
1243-
try {
1244-
if (recursive) {
1245-
return std::filesystem::remove_all(fullPath) > 0;
1246-
} else {
1247-
return std::filesystem::remove(fullPath);
1248-
}
1249-
} catch (const std::filesystem::filesystem_error &e) {}
1250-
}
1179+
if (std::filesystem::is_directory(fullPath)) {
1180+
try {
1181+
if (recursive) {
1182+
return std::filesystem::remove_all(fullPath) > 0;
1183+
} else {
1184+
return std::filesystem::remove(fullPath);
1185+
}
1186+
} catch (const std::filesystem::filesystem_error &e) {}
12511187
}
12521188
}
12531189
g_ConsoleMan.PrintString("ERROR: Failed to remove directory " + path);
@@ -1261,20 +1197,13 @@ namespace RTE {
12611197
std::string fullNewPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(newPath);
12621198
if (IsValidModulePath(fullOldPath) && IsValidModulePath(fullNewPath)) {
12631199
#ifndef _WIN32
1264-
auto caseInsensitiveFullOldPath = GetCaseInsensitiveFullPath(fullOldPath);
1265-
auto caseInsensitiveFullNewPath = GetCaseInsensitiveFullPath(fullNewPath);
1266-
if (caseInsensitiveFullOldPath && caseInsensitiveFullNewPath)
1200+
fullOldPath = GetCaseInsensitiveFullPath(fullOldPath);
1201+
fullNewPath = GetCaseInsensitiveFullPath(fullNewPath);
12671202
#endif
1268-
{
1269-
#ifndef _WIN32
1270-
fullOldPath = *caseInsensitiveFullOldPath;
1271-
fullNewPath = *caseInsensitiveFullNewPath;
1272-
#endif
1273-
try {
1274-
std::filesystem::rename(fullOldPath, fullNewPath);
1275-
return true;
1276-
} catch (const std::filesystem::filesystem_error &e) {}
1277-
}
1203+
try {
1204+
std::filesystem::rename(fullOldPath, fullNewPath);
1205+
return true;
1206+
} catch (const std::filesystem::filesystem_error &e) {}
12781207
}
12791208
g_ConsoleMan.PrintString("ERROR: Failed to rename oldPath " + oldPath + " to newPath " + newPath);
12801209
return false;

Source/Managers/LuaMan.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,8 @@ namespace RTE {
443443
/// The real path is used by the Lua file I/O handling methods to ensure full Windows compatibility.
444444
/// </summary>
445445
/// <param name="fullPath">Path to case-insensitively translate to a real path.</param>
446-
/// <returns>An optional that contains the real path, if it existed.</returns>
447-
std::optional<std::string> GetCaseInsensitiveFullPath(const std::string &fullPath);
446+
/// <returns>The real path. If the path doesn't exist, it returns the fullPath argument with all the existing parent directories correctly capitalized.</returns>
447+
std::string GetCaseInsensitiveFullPath(const std::string &fullPath);
448448

449449
/// <summary>
450450
/// Returns a vector of all the directories in path, which is relative to the working directory.
@@ -598,4 +598,4 @@ namespace RTE {
598598
LuaMan & operator=(const LuaMan &rhs) = delete;
599599
};
600600
}
601-
#endif
601+
#endif

0 commit comments

Comments
 (0)