1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 03:59:56 +00:00

Initialize FileSystemArchive index in constructor

It should be initialize for each created archive anyway. There is no good reason
to have additional complexity for lazy initialization. And it helps to catch
problems with specific directory when it's added to the VFS not when all are
added and index is built.
This commit is contained in:
elsid 2024-01-30 01:19:42 +01:00
parent 0db730825d
commit d147d1d250
No known key found for this signature in database
GPG key ID: 4DE04C198CBA7625
2 changed files with 25 additions and 38 deletions

View file

@ -12,48 +12,36 @@ namespace VFS
{
FileSystemArchive::FileSystemArchive(const std::filesystem::path& path)
: mBuiltIndex(false)
, mPath(path)
: mPath(path)
{
const auto str = mPath.u8string();
std::size_t prefix = str.size();
if (prefix > 0 && str[prefix - 1] != '\\' && str[prefix - 1] != '/')
++prefix;
for (const auto& i : std::filesystem::recursive_directory_iterator(mPath))
{
if (std::filesystem::is_directory(i))
continue;
const std::filesystem::path& filePath = i.path();
const std::string proper = Files::pathToUnicodeString(filePath);
VFS::Path::Normalized searchable(std::string_view{ proper }.substr(prefix));
FileSystemArchiveFile file(filePath);
const auto inserted = mIndex.emplace(std::move(searchable), std::move(file));
if (!inserted.second)
Log(Debug::Warning)
<< "Found duplicate file for '" << proper
<< "', please check your file system for two files with the same name in different cases.";
}
}
void FileSystemArchive::listResources(FileMap& out)
{
if (!mBuiltIndex)
{
const auto str = mPath.u8string();
size_t prefix = str.size();
if (!mPath.empty() && str[prefix - 1] != '\\' && str[prefix - 1] != '/')
++prefix;
for (const auto& i : std::filesystem::recursive_directory_iterator(mPath))
{
if (std::filesystem::is_directory(i))
continue;
const auto& path = i.path();
const std::string proper = Files::pathToUnicodeString(path);
FileSystemArchiveFile file(path);
VFS::Path::Normalized searchable(std::string_view{ proper }.substr(prefix));
const auto inserted = mIndex.emplace(std::move(searchable), std::move(file));
if (!inserted.second)
Log(Debug::Warning)
<< "Warning: found duplicate file for '" << proper
<< "', please check your file system for two files with the same name in different cases.";
else
out[inserted.first->first] = &inserted.first->second;
}
mBuiltIndex = true;
}
else
{
for (auto& [k, v] : mIndex)
out[k] = &v;
}
for (auto& [k, v] : mIndex)
out[k] = &v;
}
bool FileSystemArchive::contains(Path::NormalizedView file) const

View file

@ -36,7 +36,6 @@ namespace VFS
private:
std::map<VFS::Path::Normalized, FileSystemArchiveFile, std::less<>> mIndex;
bool mBuiltIndex;
std::filesystem::path mPath;
};