Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #22: Try to find data files if wrong case for non-Windows platforms #33

Merged
merged 2 commits into from
Jan 24, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/openloco/envionment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ namespace openloco::environment
loco_global_array<char, 257, 0x0050B635> _path_objects;

static fs::path get_sub_path(path_id id);
#ifndef _WIN32
static fs::path find_similar_file(const fs::path& path);
#endif

static bool validate_loco_install_path(const fs::path& path)
{
Expand All @@ -31,7 +34,15 @@ namespace openloco::environment
else
{
auto g1Path = path / get_sub_path(path_id::g1);
return fs::exists(g1Path);
bool g1Exists = fs::exists(g1Path);
#ifndef _WIN32
if (!g1Exists)
{
g1Path = find_similar_file(g1Path);
g1Exists = !g1Path.empty();
}
#endif
return g1Exists;
}
}

Expand Down Expand Up @@ -101,6 +112,27 @@ namespace openloco::environment
return _path_install.get();
}

#ifndef _WIN32
/**
* Performs a case-insensitive search on the containing directory of
* the given file and returns the first match.
*/
static fs::path find_similar_file(const fs::path& path)
{
auto expectedFilename = path.filename().generic_string();
auto directory = path.parent_path();
for (auto& item : fs::directory_iterator(directory))
{
auto& p = item.path();
if (utility::iequals(p.filename().generic_string(), expectedFilename))
{
return p;
}
}
return fs::path();
}
#endif // _WIN32

// 0x004416B5
fs::path get_path(path_id id)
{
Expand All @@ -109,7 +141,15 @@ namespace openloco::environment
auto result = basePath / subPath;
if (!fs::exists(result))
{
std::cerr << "File not found: " << result << std::endl;
#ifndef _WIN32
result = find_similar_file(result);
if (result.empty())
{
#endif
std::cerr << "File not found: " << result << std::endl;
#ifndef _WIN32
}
#endif
}
return result;
}
Expand Down