Skip to content

Commit f2dfa03

Browse files
committed
Close to passing the filesystem tests on Linux
1 parent 2e25608 commit f2dfa03

File tree

2 files changed

+89
-79
lines changed

2 files changed

+89
-79
lines changed

Source/Managers/LuaMan.cpp

Lines changed: 76 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ namespace RTE {
360360

361361
// TODO
362362
// It would be nice to assign to least-saturated state, but that's a bit tricky with MO registering...
363-
/*auto itr = std::min_element(m_ScriptStates.begin(), m_ScriptStates.end(),
363+
/*auto itr = std::min_element(m_ScriptStates.begin(), m_ScriptStates.end(),
364364
[](const LuaStateWrapper& lhs, const LuaStateWrapper& rhs) { return lhs.GetRegisteredMOs().size() < rhs.GetRegisteredMOs().size(); }
365365
);
366366
@@ -375,7 +375,7 @@ namespace RTE {
375375
bool success = m_ScriptStates[ourState].GetMutex().try_lock();
376376
RTEAssert(success, "Script mutex was already locked while in a non-multithreaded environment!");
377377

378-
return &m_ScriptStates[ourState];
378+
return &m_ScriptStates[ourState];
379379
}
380380

381381
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -405,13 +405,13 @@ namespace RTE {
405405

406406
void LuaMan::ExecuteLuaScriptCallbacks() {
407407
std::vector<std::function<void()>> callbacks;
408-
408+
409409
// Move our functions into the local buffer to clear the existing callbacks and to lock for as little time as possible
410410
{
411411
std::scoped_lock lock(m_ScriptCallbacksMutex);
412412
callbacks.swap(m_ScriptCallbacks);
413413
}
414-
414+
415415
for (const std::function<void()> &callback : callbacks) {
416416
callback();
417417
}
@@ -871,7 +871,7 @@ namespace RTE {
871871

872872
bool LuaStateWrapper::TableEntryIsDefined(const std::string &tableName, const std::string &indexName) {
873873
std::lock_guard<std::recursive_mutex> lock(m_Mutex);
874-
874+
875875
// Push the table onto the stack, checking if it even exists.
876876
lua_getglobal(m_State, tableName.c_str());
877877
if (!lua_istable(m_State, -1)) {
@@ -937,6 +937,42 @@ namespace RTE {
937937
return stackDescription.str();
938938
}
939939

940+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
941+
942+
// TODO: Ask Causeless whether this is an appropriate spot for this method
943+
std::optional<std::string> LuaMan::GetCaseInsensitiveFullPath(const std::string &fullPath) {
944+
std::filesystem::path inspectedPath = System::GetWorkingDirectory();
945+
const std::filesystem::path relativeFilePath = std::filesystem::path(fullPath).lexically_relative(inspectedPath);
946+
947+
// Iterate over all path parts
948+
for (std::filesystem::path::const_iterator relativeFilePathIterator = relativeFilePath.begin(); relativeFilePathIterator != relativeFilePath.end(); ++relativeFilePathIterator) {
949+
bool pathPartExists = false;
950+
951+
// Iterate over all entries in the path part's directory,
952+
// to check if the path part is in there case insensitively
953+
for (const std::filesystem::path &filesystemEntryPath : std::filesystem::directory_iterator(inspectedPath)) {
954+
if (StringsEqualCaseInsensitive(filesystemEntryPath.filename().generic_string(), relativeFilePathIterator->generic_string())) {
955+
inspectedPath = filesystemEntryPath;
956+
957+
// If the path part is found, stop looking for it
958+
pathPartExists = true;
959+
break;
960+
}
961+
}
962+
963+
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;
970+
}
971+
}
972+
973+
return std::optional<std::string>(inspectedPath.generic_string());
974+
}
975+
940976
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
941977

942978
const std::vector<std::string> * LuaMan::DirectoryList(const std::string &path) {
@@ -945,7 +981,7 @@ namespace RTE {
945981

946982
if (IsValidModulePath(fullPath)) {
947983
#ifndef _WIN32
948-
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath)
984+
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath);
949985
if (caseInsensitiveFullPath)
950986
#endif
951987
{
@@ -968,7 +1004,7 @@ namespace RTE {
9681004

9691005
if (IsValidModulePath(fullPath)) {
9701006
#ifndef _WIN32
971-
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath)
1007+
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath);
9721008
if (caseInsensitiveFullPath)
9731009
#endif
9741010
{
@@ -989,7 +1025,7 @@ namespace RTE {
9891025
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
9901026
if (IsValidModulePath(fullPath)) {
9911027
#ifndef _WIN32
992-
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath)
1028+
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath);
9931029
if (caseInsensitiveFullPath)
9941030
#endif
9951031
{
@@ -1008,7 +1044,7 @@ namespace RTE {
10081044
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
10091045
if (IsValidModulePath(fullPath)) {
10101046
#ifndef _WIN32
1011-
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath)
1047+
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath);
10121048
if (caseInsensitiveFullPath)
10131049
#endif
10141050
{
@@ -1030,35 +1066,6 @@ namespace RTE {
10301066

10311067
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
10321068

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.generic_string());
1060-
}
1061-
10621069
int LuaMan::FileOpen(const std::string &path, const std::string &accessMode) {
10631070
if (c_FileAccessModes.find(accessMode) == c_FileAccessModes.end()) {
10641071
g_ConsoleMan.PrintString("ERROR: Cannot open file, invalid file access mode specified.");
@@ -1149,7 +1156,7 @@ namespace RTE {
11491156
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
11501157
if (IsValidModulePath(fullPath)) {
11511158
#ifndef _WIN32
1152-
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath)
1159+
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath);
11531160
if (caseInsensitiveFullPath)
11541161
#endif
11551162
{
@@ -1173,47 +1180,39 @@ namespace RTE {
11731180
#ifndef _WIN32
11741181
// The goal here is to return the same fullPath,
11751182
// but with the parent directories given the real filesys casing
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-
}
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;
11941199
}
1200+
}
11951201

1196-
if (!pathPartExists) {
1197-
// If part of the path exists, append the rest of fullPath its parts
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();
11981206
relativeFilePathIterator++;
1199-
while (relativeFilePathIterator != relativeFilePath.end()) {
1200-
inspectedPath += "/" + relativeFilePathIterator->generic_string();
1201-
relativeFilePathIterator++;
1202-
}
1203-
1204-
return inspectedPath;
12051207
}
1208+
1209+
break;
12061210
}
1211+
}
12071212

1208-
// If the entire path exists
1209-
return inspectedPath;
1210-
}();
1211-
if (caseInsensitiveFullPath)
1213+
fullPath = inspectedPath;
12121214
#endif
12131215
{
1214-
#ifndef _WIN32
1215-
fullPath = *caseInsensitiveFullPath;
1216-
#endif
12171216
try {
12181217
if (recursive) {
12191218
return std::filesystem::create_directories(fullPath);
@@ -1233,7 +1232,7 @@ namespace RTE {
12331232
std::string fullPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(path);
12341233
if (IsValidModulePath(fullPath)) {
12351234
#ifndef _WIN32
1236-
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath)
1235+
auto caseInsensitiveFullPath = GetCaseInsensitiveFullPath(fullPath);
12371236
if (caseInsensitiveFullPath)
12381237
#endif
12391238
{
@@ -1262,8 +1261,8 @@ namespace RTE {
12621261
std::string fullNewPath = System::GetWorkingDirectory() + g_PresetMan.GetFullModulePath(newPath);
12631262
if (IsValidModulePath(fullOldPath) && IsValidModulePath(fullNewPath)) {
12641263
#ifndef _WIN32
1265-
auto caseInsensitiveFullOldPath = GetCaseInsensitiveFullPath(fullOldPath)
1266-
auto caseInsensitiveFullNewPath = GetCaseInsensitiveFullPath(fullNewPath)
1264+
auto caseInsensitiveFullOldPath = GetCaseInsensitiveFullPath(fullOldPath);
1265+
auto caseInsensitiveFullNewPath = GetCaseInsensitiveFullPath(fullNewPath);
12671266
if (caseInsensitiveFullOldPath && caseInsensitiveFullNewPath)
12681267
#endif
12691268
{
@@ -1338,15 +1337,15 @@ namespace RTE {
13381337

13391338
void LuaMan::StartAsyncGarbageCollection() {
13401339
ZoneScoped;
1341-
1340+
13421341
std::vector<LuaStateWrapper*> allStates;
13431342
allStates.reserve(m_ScriptStates.size() + 1);
13441343

13451344
allStates.push_back(&m_MasterScriptState);
13461345
for (LuaStateWrapper& wrapper : m_ScriptStates) {
13471346
allStates.push_back(&wrapper);
13481347
}
1349-
1348+
13501349
m_GarbageCollectionTask = BS::multi_future<void>();
13511350
for (LuaStateWrapper* luaState : allStates) {
13521351
m_GarbageCollectionTask.push_back(

Source/Managers/LuaMan.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include "BS_thread_pool.hpp"
1010

11+
#include <optional>
12+
1113
#define g_LuaMan LuaMan::Instance()
1214

1315
struct lua_State;
@@ -82,7 +84,7 @@ namespace RTE {
8284
/// </summary>
8385
/// <returns>This LuaStateWrapper's internal lua state.</returns>
8486
lua_State* GetLuaState() { return m_State; };
85-
87+
8688
/// <summary>
8789
/// Gets m_ScriptTimings.
8890
/// </summary>
@@ -384,7 +386,7 @@ namespace RTE {
384386
/// </summary>
385387
/// <returns>A list of threaded script states.</returns>
386388
LuaStatesArray & GetThreadedScriptStates();
387-
389+
388390
/// <summary>
389391
/// Gets the current thread lua state override that new objects created will be assigned to.
390392
/// </summary>
@@ -435,6 +437,15 @@ namespace RTE {
435437
#pragma endregion
436438

437439
#pragma region File I/O Handling
440+
/// <summary>
441+
/// If a file "foo/Bar.txt" exists, and this method is passed "FOO/BAR.TXT", then this method will return "foo/Bar.txt".
442+
/// This method's purpose is to enable Linux to get the real path using a case-insensitive search.
443+
/// The real path is used by the Lua file I/O handling methods to ensure full Windows compatibility.
444+
/// </summary>
445+
/// <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);
448+
438449
/// <summary>
439450
/// Returns a vector of all the directories in path, which is relative to the working directory.
440451
/// </summary>

0 commit comments

Comments
 (0)