mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-27 16:54:52 +01:00
Improve FileIndex (#17773)
This commit is contained in:
@@ -41,9 +41,9 @@ private:
|
||||
DirectoryStats const Stats;
|
||||
std::vector<std::string> const Files;
|
||||
|
||||
ScanResult(DirectoryStats stats, std::vector<std::string> files)
|
||||
ScanResult(DirectoryStats stats, std::vector<std::string>&& files) noexcept
|
||||
: Stats(stats)
|
||||
, Files(files)
|
||||
, Files(std::move(files))
|
||||
{
|
||||
}
|
||||
};
|
||||
@@ -82,14 +82,14 @@ public:
|
||||
* @param paths A list of search directories.
|
||||
*/
|
||||
FileIndex(
|
||||
std::string name, uint32_t magicNumber, uint8_t version, std::string indexPath, std::string pattern,
|
||||
std::vector<std::string> paths)
|
||||
: _name(name)
|
||||
std::string&& name, uint32_t magicNumber, uint8_t version, std::string&& indexPath, std::string&& pattern,
|
||||
std::vector<std::string>&& paths) noexcept
|
||||
: _name(std::move(name))
|
||||
, _magicNumber(magicNumber)
|
||||
, _version(version)
|
||||
, _indexPath(indexPath)
|
||||
, _pattern(pattern)
|
||||
, SearchPaths(paths)
|
||||
, _indexPath(std::move(indexPath))
|
||||
, _pattern(std::move(pattern))
|
||||
, SearchPaths(std::move(paths))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -127,14 +127,13 @@ public:
|
||||
protected:
|
||||
/**
|
||||
* Loads the given file and creates the item representing the data to store in the index.
|
||||
* TODO Use std::optional when C++17 is available.
|
||||
*/
|
||||
virtual std::tuple<bool, TItem> Create(int32_t language, const std::string& path) const abstract;
|
||||
virtual std::optional<TItem> Create(int32_t language, const std::string& path) const abstract;
|
||||
|
||||
/**
|
||||
* Serialises/DeSerialises an index item to/from the given stream.
|
||||
*/
|
||||
virtual void Serialise(DataSerialiser& ds, TItem& item) const abstract;
|
||||
virtual void Serialise(DataSerialiser& ds, const TItem& item) const abstract;
|
||||
|
||||
private:
|
||||
ScanResult Scan() const
|
||||
@@ -163,7 +162,7 @@ private:
|
||||
files.push_back(std::move(path));
|
||||
}
|
||||
}
|
||||
return ScanResult(stats, files);
|
||||
return ScanResult(stats, std::move(files));
|
||||
}
|
||||
|
||||
void BuildRange(
|
||||
@@ -181,13 +180,12 @@ private:
|
||||
log_verbose("FileIndex:Indexing '%s'", filePath.c_str());
|
||||
}
|
||||
|
||||
auto item = Create(language, filePath);
|
||||
if (std::get<0>(item))
|
||||
if (auto item = Create(language, filePath); item.has_value())
|
||||
{
|
||||
items.push_back(std::get<1>(item));
|
||||
items.push_back(std::move(item.value()));
|
||||
}
|
||||
|
||||
processed++;
|
||||
++processed;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,9 +222,9 @@ private:
|
||||
|
||||
auto& items = containers.emplace_back();
|
||||
|
||||
jobPool.AddTask(std::bind(
|
||||
&FileIndex<TItem>::BuildRange, this, language, std::cref(scanResult), rangeStart, rangeStart + stepSize,
|
||||
std::ref(items), std::ref(processed), std::ref(printLock)));
|
||||
jobPool.AddTask([&, rangeStart, stepSize]() {
|
||||
BuildRange(language, scanResult, rangeStart, rangeStart + stepSize, items, processed, printLock);
|
||||
});
|
||||
|
||||
reportProgress();
|
||||
}
|
||||
@@ -292,7 +290,7 @@ private:
|
||||
return std::make_tuple(loadedItems, std::move(items));
|
||||
}
|
||||
|
||||
void WriteIndexFile(int32_t language, const DirectoryStats& stats, std::vector<TItem>& items) const
|
||||
void WriteIndexFile(int32_t language, const DirectoryStats& stats, const std::vector<TItem>& items) const
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -312,7 +310,7 @@ private:
|
||||
|
||||
DataSerialiser ds(true, fs);
|
||||
// Write items
|
||||
for (auto& item : items)
|
||||
for (const auto& item : items)
|
||||
{
|
||||
Serialise(ds, item);
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ public:
|
||||
}
|
||||
|
||||
public:
|
||||
std::tuple<bool, ObjectRepositoryItem> Create([[maybe_unused]] int32_t language, const std::string& path) const override
|
||||
std::optional<ObjectRepositoryItem> Create([[maybe_unused]] int32_t language, const std::string& path) const override
|
||||
{
|
||||
std::unique_ptr<Object> object;
|
||||
auto extension = Path::GetExtension(path);
|
||||
@@ -113,6 +113,7 @@ public:
|
||||
{
|
||||
object = ObjectFactory::CreateObjectFromLegacyFile(_objectRepository, path.c_str(), false);
|
||||
}
|
||||
|
||||
if (object != nullptr)
|
||||
{
|
||||
ObjectRepositoryItem item = {};
|
||||
@@ -125,13 +126,13 @@ public:
|
||||
item.Authors = object->GetAuthors();
|
||||
item.Sources = object->GetSourceGames();
|
||||
object->SetRepositoryItem(&item);
|
||||
return std::make_tuple(true, item);
|
||||
return item;
|
||||
}
|
||||
return std::make_tuple(false, ObjectRepositoryItem());
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
protected:
|
||||
void Serialise(DataSerialiser& ds, ObjectRepositoryItem& item) const override
|
||||
void Serialise(DataSerialiser& ds, const ObjectRepositoryItem& item) const override
|
||||
{
|
||||
ds << item.Type;
|
||||
ds << item.Generation;
|
||||
@@ -443,11 +444,9 @@ private:
|
||||
void ScanObject(const std::string& path)
|
||||
{
|
||||
auto language = LocalisationService_GetCurrentLanguage();
|
||||
auto result = _fileIndex.Create(language, path);
|
||||
if (std::get<0>(result))
|
||||
if (auto result = _fileIndex.Create(language, path); result.has_value())
|
||||
{
|
||||
auto ori = std::get<1>(result);
|
||||
AddItem(ori);
|
||||
AddItem(result.value());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ public:
|
||||
}
|
||||
|
||||
public:
|
||||
std::tuple<bool, TrackRepositoryItem> Create(int32_t, const std::string& path) const override
|
||||
std::optional<TrackRepositoryItem> Create(int32_t, const std::string& path) const override
|
||||
{
|
||||
auto td6 = TrackDesignImport(path.c_str());
|
||||
if (td6 != nullptr)
|
||||
@@ -88,14 +88,14 @@ public:
|
||||
{
|
||||
item.Flags |= TRIF_READ_ONLY;
|
||||
}
|
||||
return std::make_tuple(true, item);
|
||||
return item;
|
||||
}
|
||||
|
||||
return std::make_tuple(true, TrackRepositoryItem());
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
protected:
|
||||
void Serialise(DataSerialiser& ds, TrackRepositoryItem& item) const override
|
||||
void Serialise(DataSerialiser& ds, const TrackRepositoryItem& item) const override
|
||||
{
|
||||
ds << item.Name;
|
||||
ds << item.Path;
|
||||
@@ -266,10 +266,9 @@ public:
|
||||
if (File::Copy(path, newPath, false))
|
||||
{
|
||||
auto language = LocalisationService_GetCurrentLanguage();
|
||||
auto td = _fileIndex.Create(language, newPath);
|
||||
if (std::get<0>(td))
|
||||
if (auto td = _fileIndex.Create(language, newPath); td.has_value())
|
||||
{
|
||||
_items.push_back(std::move(std::get<1>(td)));
|
||||
_items.push_back(std::move(td.value()));
|
||||
SortItems();
|
||||
result = path;
|
||||
}
|
||||
|
||||
@@ -142,19 +142,19 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
std::tuple<bool, scenario_index_entry> Create(int32_t, const std::string& path) const override
|
||||
std::optional<scenario_index_entry> Create(int32_t, const std::string& path) const override
|
||||
{
|
||||
scenario_index_entry entry;
|
||||
auto timestamp = File::GetLastModified(path);
|
||||
if (GetScenarioInfo(path, timestamp, &entry))
|
||||
{
|
||||
return std::make_tuple(true, entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
return std::make_tuple(true, scenario_index_entry());
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void Serialise(DataSerialiser& ds, scenario_index_entry& item) const override
|
||||
void Serialise(DataSerialiser& ds, const scenario_index_entry& item) const override
|
||||
{
|
||||
ds << item.path;
|
||||
ds << item.timestamp;
|
||||
|
||||
Reference in New Issue
Block a user