2018-04-08 02:27:36 +00:00
|
|
|
#include "cellnameloader.hpp"
|
|
|
|
|
2023-07-10 21:40:23 +00:00
|
|
|
#include <fstream>
|
|
|
|
|
2023-03-20 22:57:53 +00:00
|
|
|
#include <components/debug/debuglog.hpp>
|
2023-07-10 21:40:23 +00:00
|
|
|
#include <components/esm/format.hpp>
|
2022-01-22 14:58:41 +00:00
|
|
|
#include <components/esm3/loadcell.hpp>
|
2023-07-10 21:40:23 +00:00
|
|
|
#include <components/files/openfile.hpp>
|
2023-01-20 23:39:30 +00:00
|
|
|
#include <components/files/qtconversion.hpp>
|
2018-04-08 02:27:36 +00:00
|
|
|
|
2023-03-20 22:57:53 +00:00
|
|
|
QSet<QString> CellNameLoader::getCellNames(const QStringList& contentPaths)
|
2018-04-08 02:27:36 +00:00
|
|
|
{
|
|
|
|
QSet<QString> cellNames;
|
|
|
|
ESM::ESMReader esmReader;
|
|
|
|
|
|
|
|
// Loop through all content files
|
2023-03-20 22:57:53 +00:00
|
|
|
for (const QString& contentPath : contentPaths)
|
2018-04-08 02:27:36 +00:00
|
|
|
{
|
2021-10-29 18:09:47 +00:00
|
|
|
if (contentPath.endsWith(".omwscripts", Qt::CaseInsensitive))
|
|
|
|
continue;
|
2023-03-20 22:57:53 +00:00
|
|
|
try
|
2018-04-08 02:27:36 +00:00
|
|
|
{
|
2023-07-10 21:40:23 +00:00
|
|
|
std::filesystem::path filepath = Files::pathFromQString(contentPath);
|
|
|
|
auto stream = Files::openBinaryInputFileStream(filepath);
|
|
|
|
if (!stream->is_open())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const ESM::Format format = ESM::readFormat(*stream);
|
|
|
|
if (format != ESM::Format::Tes3)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
stream->seekg(0);
|
|
|
|
esmReader.open(std::move(stream), filepath);
|
2018-04-08 02:27:36 +00:00
|
|
|
|
2023-03-20 22:57:53 +00:00
|
|
|
// Loop through all records
|
|
|
|
while (esmReader.hasMoreRecs())
|
2018-04-08 02:27:36 +00:00
|
|
|
{
|
2023-03-20 22:57:53 +00:00
|
|
|
ESM::NAME recordName = esmReader.getRecName();
|
|
|
|
esmReader.getRecHeader();
|
|
|
|
|
|
|
|
if (isCellRecord(recordName))
|
2018-04-08 02:27:36 +00:00
|
|
|
{
|
2023-03-20 22:57:53 +00:00
|
|
|
QString cellName = getCellName(esmReader);
|
|
|
|
if (!cellName.isEmpty())
|
|
|
|
{
|
|
|
|
cellNames.insert(cellName);
|
|
|
|
}
|
2018-04-08 02:27:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 22:57:53 +00:00
|
|
|
// Stop loading content for this record and continue to the next
|
|
|
|
esmReader.skipRecord();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
|
|
|
Log(Debug::Error) << "Failed to get cell names from " << contentPath.toStdString() << ": " << e.what();
|
2018-04-08 02:27:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cellNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CellNameLoader::isCellRecord(ESM::NAME& recordName)
|
|
|
|
{
|
2021-10-17 00:52:22 +00:00
|
|
|
return recordName.toInt() == ESM::REC_CELL;
|
2018-04-08 02:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString CellNameLoader::getCellName(ESM::ESMReader& esmReader)
|
|
|
|
{
|
|
|
|
ESM::Cell cell;
|
|
|
|
bool isDeleted = false;
|
|
|
|
cell.loadNameAndData(esmReader, isDeleted);
|
|
|
|
|
2023-01-19 16:31:45 +00:00
|
|
|
return QString::fromStdString(cell.mName);
|
2021-01-01 15:54:45 +00:00
|
|
|
}
|