1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-20 20:53:53 +00:00
openmw/apps/opencs/model/world/collection.hpp

597 lines
18 KiB
C++
Raw Normal View History

#ifndef CSM_WOLRD_COLLECTION_H
#define CSM_WOLRD_COLLECTION_H
#include <algorithm>
#include <cctype>
#include <functional>
2022-09-22 18:26:05 +00:00
#include <map>
#include <memory>
2022-09-22 18:26:05 +00:00
#include <stdexcept>
#include <string>
#include <string_view>
#include <unordered_set>
2022-09-22 18:26:05 +00:00
#include <vector>
#include <QVariant>
#include <components/misc/strings/lower.hpp>
#include "collectionbase.hpp"
2022-09-22 18:26:05 +00:00
#include "columnbase.hpp"
#include "info.hpp"
2017-09-04 05:06:58 +00:00
#include "land.hpp"
#include "landtexture.hpp"
2022-10-19 17:02:00 +00:00
#include "record.hpp"
#include "ref.hpp"
namespace CSMWorld
{
inline std::pair<std::string_view, std::string_view> parseInfoRefId(const ESM::RefId& infoId)
{
const auto separator = infoId.getRefIdString().find('#');
if (separator == std::string::npos)
throw std::runtime_error("Invalid info id: " + infoId.getRefIdString());
const std::string_view view(infoId.getRefIdString());
return { view.substr(0, separator), view.substr(separator + 1) };
}
template <typename T>
void setRecordId(const decltype(T::mId)& id, T& record)
{
record.mId = id;
}
inline void setRecordId(const ESM::RefId& id, Info& record)
{
record.mId = id;
const auto [topicId, originalId] = parseInfoRefId(id);
record.mTopicId = ESM::RefId::stringRefId(topicId);
record.mOriginalId = ESM::RefId::stringRefId(originalId);
}
template <typename T>
auto getRecordId(const T& record)
{
return record.mId;
}
inline void setRecordId(const ESM::RefId& id, Land& record)
2017-09-04 05:06:58 +00:00
{
int x = 0;
int y = 0;
2017-09-04 05:06:58 +00:00
Land::parseUniqueRecordId(id.getRefIdString(), x, y);
2017-09-04 05:06:58 +00:00
record.mX = x;
record.mY = y;
}
inline ESM::RefId getRecordId(const Land& record)
{
return ESM::RefId::stringRefId(Land::createUniqueRecordId(record.mX, record.mY));
}
inline void setRecordId(const ESM::RefId& id, LandTexture& record)
{
int plugin = 0;
int index = 0;
LandTexture::parseUniqueRecordId(id.getRefIdString(), plugin, index);
record.mPluginIndex = plugin;
record.mIndex = index;
}
inline ESM::RefId getRecordId(const LandTexture& record)
{
return ESM::RefId::stringRefId(LandTexture::createUniqueRecordId(record.mPluginIndex, record.mIndex));
}
/// \brief Single-type record collection
template <typename ESXRecordT>
class Collection : public CollectionBase
{
2022-09-22 18:26:05 +00:00
public:
typedef ESXRecordT ESXRecord;
2022-09-22 18:26:05 +00:00
private:
std::vector<std::unique_ptr<Record<ESXRecordT>>> mRecords;
std::map<ESM::RefId, int> mIndex;
2022-09-22 18:26:05 +00:00
std::vector<Column<ESXRecordT>*> mColumns;
2014-01-27 18:40:05 +00:00
2022-09-22 18:26:05 +00:00
// not implemented
Collection(const Collection&);
Collection& operator=(const Collection&);
2013-11-08 10:52:30 +00:00
2022-09-22 18:26:05 +00:00
protected:
const std::vector<std::unique_ptr<Record<ESXRecordT>>>& getRecords() const;
void reorderRowsImp(const std::vector<int>& indexOrder);
2022-09-22 18:26:05 +00:00
bool reorderRowsImp(int baseIndex, const std::vector<int>& newOrder);
///< Reorder the rows [baseIndex, baseIndex+newOrder.size()) according to the indices
/// given in \a newOrder (baseIndex+newOrder[0] specifies the new index of row baseIndex).
///
/// \return Success?
int cloneRecordImp(const ESM::RefId& origin, const ESM::RefId& dest, UniversalId::Type type);
2022-09-22 18:26:05 +00:00
///< Returns the index of the clone.
2017-09-04 05:06:58 +00:00
int touchRecordImp(const ESM::RefId& id);
2022-09-22 18:26:05 +00:00
///< Returns the index of the record on success, -1 on failure.
2017-09-04 05:06:58 +00:00
2022-09-22 18:26:05 +00:00
public:
Collection();
~Collection() override;
2022-09-22 18:26:05 +00:00
void add(const ESXRecordT& record);
///< Add a new record (modified)
2022-09-22 18:26:05 +00:00
int getSize() const override;
ESM::RefId getId(int index) const override;
2014-06-09 08:35:39 +00:00
int getIndex(const ESM::RefId& id) const override;
2022-09-22 18:26:05 +00:00
int getColumns() const override;
2022-09-22 18:26:05 +00:00
QVariant getData(int index, int column) const override;
2022-09-22 18:26:05 +00:00
void setData(int index, int column, const QVariant& data) override;
2022-09-22 18:26:05 +00:00
const ColumnBase& getColumn(int column) const override;
void merge();
2022-09-22 18:26:05 +00:00
///< Merge modified into base.
void purge();
2022-09-22 18:26:05 +00:00
///< Remove records that are flagged as erased.
2022-09-22 18:26:05 +00:00
void removeRows(int index, int count) override;
void appendBlankRecord(const ESM::RefId& id, UniversalId::Type type = UniversalId::Type_None) override;
2022-09-22 18:26:05 +00:00
///< \param type Will be ignored, unless the collection supports multiple record types
2022-09-22 18:26:05 +00:00
void cloneRecord(
const ESM::RefId& origin, const ESM::RefId& destination, const UniversalId::Type type) override;
2014-01-27 18:40:05 +00:00
bool touchRecord(const ESM::RefId& id) override;
2022-09-22 18:26:05 +00:00
///< Change the state of a record from base to modified, if it is not already.
/// \return True if the record was changed.
2014-01-27 18:40:05 +00:00
int searchId(const ESM::RefId& id) const override;
////< Search record with \a id.
/// \return index of record (if found) or -1 (not found)
2022-09-22 18:26:05 +00:00
void replace(int index, std::unique_ptr<RecordBase> record) override;
///< If the record type does not match, an exception is thrown.
///
/// \attention \a record must not change the ID.
2022-09-22 18:26:05 +00:00
void appendRecord(std::unique_ptr<RecordBase> record, UniversalId::Type type = UniversalId::Type_None) override;
///< If the record type does not match, an exception is thrown.
///< \param type Will be ignored, unless the collection supports multiple record types
const Record<ESXRecordT>& getRecord(const ESM::RefId& id) const override;
2022-09-22 18:26:05 +00:00
const Record<ESXRecordT>& getRecord(int index) const override;
int getAppendIndex(const ESM::RefId& id, UniversalId::Type type = UniversalId::Type_None) const override;
2022-09-22 18:26:05 +00:00
///< \param type Will be ignored, unless the collection supports multiple record types
std::vector<ESM::RefId> getIds(bool listDeleted = true) const override;
2022-09-22 18:26:05 +00:00
///< Return a sorted collection of all IDs
///
/// \param listDeleted include deleted record in the list
2022-09-22 18:26:05 +00:00
virtual void insertRecord(
std::unique_ptr<RecordBase> record, int index, UniversalId::Type type = UniversalId::Type_None);
///< Insert record before index.
///
/// If the record type does not match, an exception is thrown.
///
/// If the index is invalid either generally (by being out of range) or for the particular
/// record, an exception is thrown.
2022-09-22 18:26:05 +00:00
bool reorderRows(int baseIndex, const std::vector<int>& newOrder) override;
///< Reorder the rows [baseIndex, baseIndex+newOrder.size()) according to the indices
/// given in \a newOrder (baseIndex+newOrder[0] specifies the new index of row baseIndex).
///
/// \return Success?
2022-09-22 18:26:05 +00:00
void addColumn(Column<ESXRecordT>* column);
2022-09-22 18:26:05 +00:00
void setRecord(int index, std::unique_ptr<Record<ESXRecordT>> record);
///< \attention This function must not change the ID.
2022-09-22 18:26:05 +00:00
NestableColumn* getNestableColumn(int column) const;
};
2014-01-27 18:40:05 +00:00
template <typename ESXRecordT>
const std::vector<std::unique_ptr<Record<ESXRecordT>>>& Collection<ESXRecordT>::getRecords() const
{
return mRecords;
}
template <typename ESXRecordT>
void Collection<ESXRecordT>::reorderRowsImp(const std::vector<int>& indexOrder)
{
assert(indexOrder.size() == mRecords.size());
assert(std::unordered_set(indexOrder.begin(), indexOrder.end()).size() == indexOrder.size());
std::vector<std::unique_ptr<Record<ESXRecordT>>> orderedRecords;
for (const int index : indexOrder)
{
mIndex.at(mRecords[index]->get().mId) = static_cast<int>(orderedRecords.size());
orderedRecords.push_back(std::move(mRecords[index]));
}
mRecords = std::move(orderedRecords);
}
template <typename ESXRecordT>
bool Collection<ESXRecordT>::reorderRowsImp(int baseIndex, const std::vector<int>& newOrder)
{
if (!newOrder.empty())
{
2022-09-22 18:26:05 +00:00
int size = static_cast<int>(newOrder.size());
// check that all indices are present
2022-09-22 18:26:05 +00:00
std::vector<int> test(newOrder);
std::sort(test.begin(), test.end());
if (*test.begin() != 0 || *--test.end() != size - 1)
return false;
// reorder records
2022-09-22 18:26:05 +00:00
std::vector<std::unique_ptr<Record<ESXRecordT>>> buffer(size);
2022-09-22 18:26:05 +00:00
for (int i = 0; i < size; ++i)
2013-11-14 11:21:10 +00:00
{
2022-09-22 18:26:05 +00:00
buffer[newOrder[i]] = std::move(mRecords[baseIndex + i]);
2022-07-12 08:56:53 +00:00
if (buffer[newOrder[i]])
2022-09-22 18:26:05 +00:00
buffer[newOrder[i]]->setModified(buffer[newOrder[i]]->get());
2013-11-14 11:21:10 +00:00
}
2022-09-22 18:26:05 +00:00
std::move(buffer.begin(), buffer.end(), mRecords.begin() + baseIndex);
// adjust index
for (auto& [id, index] : mIndex)
if (index >= baseIndex && index < baseIndex + size)
index = newOrder.at(index - baseIndex) + baseIndex;
}
return true;
}
template <typename ESXRecordT>
int Collection<ESXRecordT>::cloneRecordImp(
const ESM::RefId& origin, const ESM::RefId& destination, UniversalId::Type type)
{
2022-05-29 11:25:17 +00:00
auto copy = std::make_unique<Record<ESXRecordT>>();
copy->mModified = getRecord(origin).get();
2022-05-29 11:25:17 +00:00
copy->mState = RecordBase::State_ModifiedOnly;
setRecordId(destination, copy->get());
2014-01-27 18:40:05 +00:00
2022-05-29 11:25:17 +00:00
if (type == UniversalId::Type_Reference)
{
2022-09-22 18:26:05 +00:00
CSMWorld::CellRef* ptr = (CSMWorld::CellRef*)&copy->mModified;
ptr->mRefNum.mIndex = 0;
}
const int index = getAppendIndex(destination, type);
insertRecord(std::move(copy), getAppendIndex(destination, type));
2017-09-04 05:06:58 +00:00
return index;
}
2014-01-27 18:40:05 +00:00
template <typename ESXRecordT>
int Collection<ESXRecordT>::touchRecordImp(const ESM::RefId& id)
{
const int index = getIndex(id);
Record<ESXRecordT>& record = *mRecords.at(index);
if (record.isDeleted())
{
throw std::runtime_error("attempt to touch deleted record");
}
2017-09-04 05:06:58 +00:00
if (!record.isModified())
{
record.setModified(record.get());
2017-09-04 05:06:58 +00:00
return index;
}
return -1;
}
template <typename ESXRecordT>
void Collection<ESXRecordT>::cloneRecord(
const ESM::RefId& origin, const ESM::RefId& destination, const UniversalId::Type type)
2017-09-04 05:06:58 +00:00
{
cloneRecordImp(origin, destination, type);
2017-09-04 05:06:58 +00:00
}
2022-09-22 18:26:05 +00:00
template <>
inline void Collection<Land>::cloneRecord(
const ESM::RefId& origin, const ESM::RefId& destination, const UniversalId::Type type)
2017-09-04 05:06:58 +00:00
{
const int index = cloneRecordImp(origin, destination, type);
mRecords.at(index)->get().setPlugin(0);
2017-09-04 05:06:58 +00:00
}
template <typename ESXRecordT>
bool Collection<ESXRecordT>::touchRecord(const ESM::RefId& id)
2017-09-04 05:06:58 +00:00
{
return touchRecordImp(id) != -1;
2017-09-04 05:06:58 +00:00
}
2022-09-22 18:26:05 +00:00
template <>
inline bool Collection<Land>::touchRecord(const ESM::RefId& id)
2017-09-04 05:06:58 +00:00
{
const int index = touchRecordImp(id);
2017-09-04 05:06:58 +00:00
if (index >= 0)
{
mRecords.at(index)->get().setPlugin(0);
return true;
}
2017-09-04 05:06:58 +00:00
return false;
}
template <typename ESXRecordT>
Collection<ESXRecordT>::Collection()
2022-09-22 18:26:05 +00:00
{
}
template <typename ESXRecordT>
Collection<ESXRecordT>::~Collection()
{
2022-09-22 18:26:05 +00:00
for (typename std::vector<Column<ESXRecordT>*>::iterator iter(mColumns.begin()); iter != mColumns.end(); ++iter)
delete *iter;
}
template <typename ESXRecordT>
void Collection<ESXRecordT>::add(const ESXRecordT& record)
{
const ESM::RefId id = getRecordId(record);
auto iter = mIndex.find(id);
2022-09-22 18:26:05 +00:00
if (iter == mIndex.end())
{
2022-05-29 11:25:17 +00:00
auto record2 = std::make_unique<Record<ESXRecordT>>();
record2->mState = Record<ESXRecordT>::State_ModifiedOnly;
record2->mModified = record;
2022-09-22 18:26:05 +00:00
insertRecord(std::move(record2), getAppendIndex(id));
}
else
{
2022-09-22 18:26:05 +00:00
mRecords[iter->second]->setModified(record);
}
}
template <typename ESXRecordT>
int Collection<ESXRecordT>::getSize() const
{
return mRecords.size();
}
2014-07-17 11:03:53 +00:00
template <typename ESXRecordT>
ESM::RefId Collection<ESXRecordT>::getId(int index) const
{
return getRecordId(mRecords.at(index)->get());
}
template <typename ESXRecordT>
int Collection<ESXRecordT>::getIndex(const ESM::RefId& id) const
{
2022-09-22 18:26:05 +00:00
int index = searchId(id);
2022-09-22 18:26:05 +00:00
if (index == -1)
throw std::runtime_error("invalid ID: " + id.getRefIdString());
return index;
}
template <typename ESXRecordT>
int Collection<ESXRecordT>::getColumns() const
{
return mColumns.size();
}
template <typename ESXRecordT>
QVariant Collection<ESXRecordT>::getData(int index, int column) const
{
2022-09-22 18:26:05 +00:00
return mColumns.at(column)->get(*mRecords.at(index));
}
template <typename ESXRecordT>
void Collection<ESXRecordT>::setData(int index, int column, const QVariant& data)
{
2022-09-22 18:26:05 +00:00
return mColumns.at(column)->set(*mRecords.at(index), data);
}
template <typename ESXRecordT>
const ColumnBase& Collection<ESXRecordT>::getColumn(int column) const
{
2022-09-22 18:26:05 +00:00
return *mColumns.at(column);
}
template <typename ESXRecordT>
NestableColumn* Collection<ESXRecordT>::getNestableColumn(int column) const
{
if (column < 0 || column >= static_cast<int>(mColumns.size()))
throw std::runtime_error("column index out of range");
2022-09-22 18:26:05 +00:00
return mColumns.at(column);
}
template <typename ESXRecordT>
void Collection<ESXRecordT>::addColumn(Column<ESXRecordT>* column)
{
2022-09-22 18:26:05 +00:00
mColumns.push_back(column);
}
template <typename ESXRecordT>
void Collection<ESXRecordT>::merge()
{
2022-09-22 18:26:05 +00:00
for (typename std::vector<std::unique_ptr<Record<ESXRecordT>>>::iterator iter(mRecords.begin());
iter != mRecords.end(); ++iter)
(*iter)->merge();
purge();
}
template <typename ESXRecordT>
void Collection<ESXRecordT>::purge()
{
int i = 0;
2022-09-22 18:26:05 +00:00
while (i < static_cast<int>(mRecords.size()))
{
if (mRecords[i]->isErased())
2022-09-22 18:26:05 +00:00
removeRows(i, 1);
else
++i;
}
}
template <typename ESXRecordT>
void Collection<ESXRecordT>::removeRows(int index, int count)
{
2022-09-22 18:26:05 +00:00
mRecords.erase(mRecords.begin() + index, mRecords.begin() + index + count);
auto iter = mIndex.begin();
2022-09-22 18:26:05 +00:00
while (iter != mIndex.end())
{
2022-09-22 18:26:05 +00:00
if (iter->second >= index)
{
2022-09-22 18:26:05 +00:00
if (iter->second >= index + count)
{
iter->second -= count;
2013-08-26 10:25:19 +00:00
++iter;
}
else
{
iter = mIndex.erase(iter);
}
}
2013-08-26 10:25:19 +00:00
else
++iter;
}
}
template <typename ESXRecordT>
void Collection<ESXRecordT>::appendBlankRecord(const ESM::RefId& id, UniversalId::Type type)
{
ESXRecordT record;
setRecordId(id, record);
record.blank();
2022-05-29 11:25:17 +00:00
auto record2 = std::make_unique<Record<ESXRecordT>>();
record2->mState = Record<ESXRecordT>::State_ModifiedOnly;
record2->mModified = record;
2022-09-22 18:26:05 +00:00
insertRecord(std::move(record2), getAppendIndex(id, type), type);
}
template <typename ESXRecordT>
int Collection<ESXRecordT>::searchId(const ESM::RefId& id) const
{
const auto iter = mIndex.find(id);
2022-09-22 18:26:05 +00:00
if (iter == mIndex.end())
return -1;
return iter->second;
}
template <typename ESXRecordT>
void Collection<ESXRecordT>::replace(int index, std::unique_ptr<RecordBase> record)
{
2022-09-22 18:26:05 +00:00
std::unique_ptr<Record<ESXRecordT>> tmp(static_cast<Record<ESXRecordT>*>(record.release()));
mRecords.at(index) = std::move(tmp);
}
template <typename ESXRecordT>
void Collection<ESXRecordT>::appendRecord(std::unique_ptr<RecordBase> record, UniversalId::Type type)
{
int index = getAppendIndex(getRecordId(static_cast<Record<ESXRecordT>*>(record.get())->get()), type);
2022-09-22 18:26:05 +00:00
insertRecord(std::move(record), index, type);
}
template <typename ESXRecordT>
int Collection<ESXRecordT>::getAppendIndex(const ESM::RefId& id, UniversalId::Type type) const
{
2022-09-22 18:26:05 +00:00
return static_cast<int>(mRecords.size());
}
template <typename ESXRecordT>
std::vector<ESM::RefId> Collection<ESXRecordT>::getIds(bool listDeleted) const
{
std::vector<ESM::RefId> ids;
for (auto iter = mIndex.begin(); iter != mIndex.end(); ++iter)
{
if (listDeleted || !mRecords[iter->second]->isDeleted())
ids.push_back(getRecordId(mRecords[iter->second]->get()));
}
return ids;
}
template <typename ESXRecordT>
const Record<ESXRecordT>& Collection<ESXRecordT>::getRecord(const ESM::RefId& id) const
{
2022-09-22 18:26:05 +00:00
int index = getIndex(id);
return *mRecords.at(index);
}
template <typename ESXRecordT>
const Record<ESXRecordT>& Collection<ESXRecordT>::getRecord(int index) const
{
2022-09-22 18:26:05 +00:00
return *mRecords.at(index);
}
template <typename ESXRecordT>
void Collection<ESXRecordT>::insertRecord(std::unique_ptr<RecordBase> record, int index, UniversalId::Type type)
{
int size = static_cast<int>(mRecords.size());
if (index < 0 || index > size)
2022-09-22 18:26:05 +00:00
throw std::runtime_error("index out of range");
2022-09-22 18:26:05 +00:00
std::unique_ptr<Record<ESXRecordT>> record2(static_cast<Record<ESXRecordT>*>(record.release()));
ESM::RefId id = getRecordId(record2->get());
if (index == size)
2022-09-22 18:26:05 +00:00
mRecords.push_back(std::move(record2));
else
2022-09-22 18:26:05 +00:00
mRecords.insert(mRecords.begin() + index, std::move(record2));
2022-09-22 18:26:05 +00:00
if (index < size - 1)
{
for (auto& [key, value] : mIndex)
{
if (value >= index)
++value;
}
}
mIndex.insert(std::make_pair(id, index));
}
template <typename ESXRecordT>
void Collection<ESXRecordT>::setRecord(int index, std::unique_ptr<Record<ESXRecordT>> record)
{
if (getRecordId(mRecords.at(index)->get()) != getRecordId(record->get()))
2022-09-22 18:26:05 +00:00
throw std::runtime_error("attempt to change the ID of a record");
2022-09-22 18:26:05 +00:00
mRecords.at(index) = std::move(record);
}
template <typename ESXRecordT>
bool Collection<ESXRecordT>::reorderRows(int baseIndex, const std::vector<int>& newOrder)
{
return false;
}
}
#endif