2013-02-09 12:00:57 +00:00
|
|
|
#include "store.hpp"
|
2013-04-28 14:36:12 +00:00
|
|
|
#include "esmstore.hpp"
|
2013-02-09 12:00:57 +00:00
|
|
|
|
2015-01-25 00:53:20 +00:00
|
|
|
#include <components/esm/esmreader.hpp>
|
|
|
|
|
2013-02-09 12:00:57 +00:00
|
|
|
namespace MWWorld {
|
|
|
|
|
2014-06-11 13:37:05 +00:00
|
|
|
void Store<ESM::Cell>::handleMovedCellRefs(ESM::ESMReader& esm, ESM::Cell* cell)
|
2013-02-09 12:00:57 +00:00
|
|
|
{
|
2013-04-28 14:36:12 +00:00
|
|
|
//Handling MovedCellRefs, there is no way to do it inside loadcell
|
|
|
|
while (esm.isNextSub("MVRF")) {
|
|
|
|
ESM::CellRef ref;
|
|
|
|
ESM::MovedCellRef cMRef;
|
|
|
|
cell->getNextMVRF(esm, cMRef);
|
|
|
|
|
2015-01-09 00:01:55 +00:00
|
|
|
ESM::Cell *cellAlt = const_cast<ESM::Cell*>(searchOrCreate(cMRef.mTarget[0], cMRef.mTarget[1]));
|
2013-04-28 14:36:12 +00:00
|
|
|
|
|
|
|
// Get regular moved reference data. Adapted from CellStore::loadRefs. Maybe we can optimize the following
|
|
|
|
// implementation when the oher implementation works as well.
|
2014-01-06 12:53:20 +00:00
|
|
|
bool deleted = false;
|
|
|
|
cell->getNextRef(esm, ref, deleted);
|
2013-04-28 14:36:12 +00:00
|
|
|
|
|
|
|
// Add data required to make reference appear in the correct cell.
|
|
|
|
// We should not need to test for duplicates, as this part of the code is pre-cell merge.
|
|
|
|
cell->mMovedRefs.push_back(cMRef);
|
|
|
|
// But there may be duplicates here!
|
2014-07-01 19:44:16 +00:00
|
|
|
if (!deleted)
|
|
|
|
{
|
|
|
|
ESM::CellRefTracker::iterator iter = std::find(cellAlt->mLeasedRefs.begin(), cellAlt->mLeasedRefs.end(), ref.mRefNum);
|
|
|
|
if (iter == cellAlt->mLeasedRefs.end())
|
|
|
|
cellAlt->mLeasedRefs.push_back(ref);
|
|
|
|
else
|
|
|
|
*iter = ref;
|
|
|
|
}
|
2013-04-28 14:36:12 +00:00
|
|
|
}
|
2014-06-11 13:37:05 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 18:24:05 +00:00
|
|
|
void Store<ESM::Cell>::load(ESM::ESMReader &esm)
|
2014-06-11 13:37:05 +00:00
|
|
|
{
|
|
|
|
// Don't automatically assume that a new cell must be spawned. Multiple plugins write to the same cell,
|
|
|
|
// and we merge all this data into one Cell object. However, we can't simply search for the cell id,
|
|
|
|
// as many exterior cells do not have a name. Instead, we need to search by (x,y) coordinates - and they
|
|
|
|
// are not available until both cells have been loaded at least partially!
|
|
|
|
|
|
|
|
// All cells have a name record, even nameless exterior cells.
|
|
|
|
ESM::Cell cell;
|
2015-07-08 18:24:05 +00:00
|
|
|
cell.loadName(esm);
|
|
|
|
std::string idLower = Misc::StringUtils::lowerCase(cell.mName);
|
2013-04-28 14:36:12 +00:00
|
|
|
|
2014-06-11 13:37:05 +00:00
|
|
|
// Load the (x,y) coordinates of the cell, if it is an exterior cell,
|
|
|
|
// so we can find the cell we need to merge with
|
|
|
|
cell.loadData(esm);
|
2013-02-09 12:00:57 +00:00
|
|
|
|
2014-06-11 13:37:05 +00:00
|
|
|
if(cell.mData.mFlags & ESM::Cell::Interior)
|
2013-02-09 12:00:57 +00:00
|
|
|
{
|
|
|
|
// Store interior cell by name, try to merge with existing parent data.
|
|
|
|
ESM::Cell *oldcell = const_cast<ESM::Cell*>(search(idLower));
|
|
|
|
if (oldcell) {
|
2014-06-11 13:37:05 +00:00
|
|
|
// merge new cell into old cell
|
|
|
|
// push the new references on the list of references to manage (saveContext = true)
|
|
|
|
oldcell->mData = cell.mData;
|
2014-09-19 03:02:54 +00:00
|
|
|
oldcell->mName = cell.mName; // merge name just to be sure (ID will be the same, but case could have been changed)
|
2014-06-11 13:37:05 +00:00
|
|
|
oldcell->loadCell(esm, true);
|
2013-02-09 12:00:57 +00:00
|
|
|
} else
|
2014-06-11 13:37:05 +00:00
|
|
|
{
|
|
|
|
// spawn a new cell
|
|
|
|
cell.loadCell(esm, true);
|
|
|
|
|
|
|
|
mInt[idLower] = cell;
|
|
|
|
}
|
2013-02-09 12:00:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Store exterior cells by grid position, try to merge with existing parent data.
|
2014-06-11 13:37:05 +00:00
|
|
|
ESM::Cell *oldcell = const_cast<ESM::Cell*>(search(cell.getGridX(), cell.getGridY()));
|
2013-02-09 12:00:57 +00:00
|
|
|
if (oldcell) {
|
2014-06-11 13:37:05 +00:00
|
|
|
// merge new cell into old cell
|
|
|
|
oldcell->mData = cell.mData;
|
2014-09-19 03:02:54 +00:00
|
|
|
oldcell->mName = cell.mName;
|
2014-06-11 19:56:10 +00:00
|
|
|
oldcell->loadCell(esm, false);
|
2014-06-11 13:37:05 +00:00
|
|
|
|
|
|
|
// handle moved ref (MVRF) subrecords
|
|
|
|
handleMovedCellRefs (esm, &cell);
|
|
|
|
|
2014-06-11 19:56:10 +00:00
|
|
|
// push the new references on the list of references to manage
|
|
|
|
oldcell->postLoad(esm);
|
|
|
|
|
2013-02-09 12:00:57 +00:00
|
|
|
// merge lists of leased references, use newer data in case of conflict
|
2014-06-11 13:37:05 +00:00
|
|
|
for (ESM::MovedCellRefTracker::const_iterator it = cell.mMovedRefs.begin(); it != cell.mMovedRefs.end(); ++it) {
|
2013-02-09 12:00:57 +00:00
|
|
|
// remove reference from current leased ref tracker and add it to new cell
|
2013-12-17 20:19:05 +00:00
|
|
|
ESM::MovedCellRefTracker::iterator itold = std::find(oldcell->mMovedRefs.begin(), oldcell->mMovedRefs.end(), it->mRefNum);
|
2013-02-09 12:00:57 +00:00
|
|
|
if (itold != oldcell->mMovedRefs.end()) {
|
|
|
|
ESM::MovedCellRef target0 = *itold;
|
|
|
|
ESM::Cell *wipecell = const_cast<ESM::Cell*>(search(target0.mTarget[0], target0.mTarget[1]));
|
2013-12-17 20:19:05 +00:00
|
|
|
ESM::CellRefTracker::iterator it_lease = std::find(wipecell->mLeasedRefs.begin(), wipecell->mLeasedRefs.end(), it->mRefNum);
|
2013-02-09 12:00:57 +00:00
|
|
|
wipecell->mLeasedRefs.erase(it_lease);
|
|
|
|
*itold = *it;
|
|
|
|
}
|
2015-11-27 20:40:36 +00:00
|
|
|
++sharedIter;
|
|
|
|
}
|
|
|
|
mStatic.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool Store<T>::erase(const std::string &id)
|
|
|
|
{
|
|
|
|
std::string key = Misc::StringUtils::lowerCase(id);
|
|
|
|
typename Dynamic::iterator it = mDynamic.find(key);
|
|
|
|
if (it == mDynamic.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mDynamic.erase(it);
|
|
|
|
|
|
|
|
// have to reinit the whole shared part
|
|
|
|
assert(mShared.size() >= mStatic.size());
|
|
|
|
mShared.erase(mShared.begin() + mStatic.size(), mShared.end());
|
|
|
|
for (it = mDynamic.begin(); it != mDynamic.end(); ++it) {
|
|
|
|
mShared.push_back(&it->second);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
bool Store<T>::erase(const T &item)
|
|
|
|
{
|
|
|
|
return erase(item.mId);
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
void Store<T>::write (ESM::ESMWriter& writer, Loading::Listener& progress) const
|
|
|
|
{
|
|
|
|
for (typename Dynamic::const_iterator iter (mDynamic.begin()); iter!=mDynamic.end();
|
|
|
|
++iter)
|
|
|
|
{
|
|
|
|
writer.startRecord (T::sRecordId);
|
|
|
|
iter->second.save (writer);
|
|
|
|
writer.endRecord (T::sRecordId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template<typename T>
|
|
|
|
RecordId Store<T>::read(ESM::ESMReader& reader)
|
|
|
|
{
|
|
|
|
T record;
|
|
|
|
bool isDeleted = false;
|
|
|
|
|
|
|
|
record.load (reader, isDeleted);
|
|
|
|
insert (record);
|
|
|
|
|
|
|
|
return RecordId(record.mId, isDeleted);
|
|
|
|
}
|
|
|
|
|
|
|
|
// LandTexture
|
|
|
|
//=========================================================================
|
|
|
|
Store<ESM::LandTexture>::Store()
|
|
|
|
{
|
|
|
|
mStatic.push_back(LandTextureList());
|
|
|
|
LandTextureList <exl = mStatic[0];
|
|
|
|
// More than enough to hold Morrowind.esm. Extra lists for plugins will we
|
|
|
|
// added on-the-fly in a different method.
|
|
|
|
ltexl.reserve(128);
|
|
|
|
}
|
|
|
|
const ESM::LandTexture *Store<ESM::LandTexture>::search(size_t index, size_t plugin) const
|
|
|
|
{
|
|
|
|
assert(plugin < mStatic.size());
|
|
|
|
const LandTextureList <exl = mStatic[plugin];
|
|
|
|
|
|
|
|
if (index >= ltexl.size())
|
|
|
|
return NULL;
|
|
|
|
return <exl[index];
|
|
|
|
}
|
|
|
|
const ESM::LandTexture *Store<ESM::LandTexture>::find(size_t index, size_t plugin) const
|
|
|
|
{
|
|
|
|
const ESM::LandTexture *ptr = search(index, plugin);
|
|
|
|
if (ptr == 0) {
|
|
|
|
std::ostringstream msg;
|
|
|
|
msg << "Land texture with index " << index << " not found";
|
|
|
|
throw std::runtime_error(msg.str());
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
size_t Store<ESM::LandTexture>::getSize() const
|
|
|
|
{
|
|
|
|
return mStatic.size();
|
|
|
|
}
|
|
|
|
size_t Store<ESM::LandTexture>::getSize(size_t plugin) const
|
|
|
|
{
|
|
|
|
assert(plugin < mStatic.size());
|
|
|
|
return mStatic[plugin].size();
|
|
|
|
}
|
|
|
|
RecordId Store<ESM::LandTexture>::load(ESM::ESMReader &esm, size_t plugin)
|
|
|
|
{
|
|
|
|
ESM::LandTexture lt;
|
|
|
|
bool isDeleted = false;
|
|
|
|
|
|
|
|
lt.load(esm, isDeleted);
|
|
|
|
|
|
|
|
assert(plugin < mStatic.size());
|
|
|
|
|
|
|
|
LandTextureList <exl = mStatic[plugin];
|
|
|
|
if(lt.mIndex + 1 > (int)ltexl.size())
|
|
|
|
ltexl.resize(lt.mIndex+1);
|
|
|
|
|
|
|
|
// Store it
|
|
|
|
ltexl[lt.mIndex] = lt;
|
|
|
|
|
|
|
|
return RecordId(lt.mId, isDeleted);
|
|
|
|
}
|
|
|
|
RecordId Store<ESM::LandTexture>::load(ESM::ESMReader &esm)
|
|
|
|
{
|
|
|
|
return load(esm, esm.getIndex());
|
|
|
|
}
|
|
|
|
Store<ESM::LandTexture>::iterator Store<ESM::LandTexture>::begin(size_t plugin) const
|
|
|
|
{
|
|
|
|
assert(plugin < mStatic.size());
|
|
|
|
return mStatic[plugin].begin();
|
|
|
|
}
|
|
|
|
Store<ESM::LandTexture>::iterator Store<ESM::LandTexture>::end(size_t plugin) const
|
|
|
|
{
|
|
|
|
assert(plugin < mStatic.size());
|
|
|
|
return mStatic[plugin].end();
|
|
|
|
}
|
|
|
|
void Store<ESM::LandTexture>::resize(size_t num)
|
|
|
|
{
|
|
|
|
if (mStatic.size() < num)
|
|
|
|
mStatic.resize(num);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Land
|
|
|
|
//=========================================================================
|
|
|
|
Store<ESM::Land>::~Store()
|
|
|
|
{
|
|
|
|
for (std::vector<ESM::Land *>::const_iterator it =
|
|
|
|
mStatic.begin(); it != mStatic.end(); ++it)
|
|
|
|
{
|
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
size_t Store<ESM::Land>::getSize() const
|
|
|
|
{
|
|
|
|
return mStatic.size();
|
|
|
|
}
|
|
|
|
Store<ESM::Land>::iterator Store<ESM::Land>::begin() const
|
|
|
|
{
|
|
|
|
return iterator(mStatic.begin());
|
|
|
|
}
|
|
|
|
Store<ESM::Land>::iterator Store<ESM::Land>::end() const
|
|
|
|
{
|
|
|
|
return iterator(mStatic.end());
|
|
|
|
}
|
|
|
|
ESM::Land *Store<ESM::Land>::search(int x, int y) const
|
|
|
|
{
|
|
|
|
ESM::Land land;
|
|
|
|
land.mX = x, land.mY = y;
|
|
|
|
|
|
|
|
std::vector<ESM::Land *>::const_iterator it =
|
|
|
|
std::lower_bound(mStatic.begin(), mStatic.end(), &land, Compare());
|
|
|
|
|
|
|
|
if (it != mStatic.end() && (*it)->mX == x && (*it)->mY == y) {
|
|
|
|
return const_cast<ESM::Land *>(*it);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ESM::Land *Store<ESM::Land>::find(int x, int y) const
|
|
|
|
{
|
|
|
|
ESM::Land *ptr = search(x, y);
|
|
|
|
if (ptr == 0) {
|
|
|
|
std::ostringstream msg;
|
|
|
|
msg << "Land at (" << x << ", " << y << ") not found";
|
|
|
|
throw std::runtime_error(msg.str());
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
RecordId Store<ESM::Land>::load(ESM::ESMReader &esm)
|
|
|
|
{
|
|
|
|
ESM::Land *ptr = new ESM::Land();
|
|
|
|
bool isDeleted = false;
|
|
|
|
|
|
|
|
ptr->load(esm, isDeleted);
|
|
|
|
|
|
|
|
// Same area defined in multiple plugins? -> last plugin wins
|
|
|
|
// Can't use search() because we aren't sorted yet - is there any other way to speed this up?
|
|
|
|
for (std::vector<ESM::Land*>::iterator it = mStatic.begin(); it != mStatic.end(); ++it)
|
|
|
|
{
|
|
|
|
if ((*it)->mX == ptr->mX && (*it)->mY == ptr->mY)
|
|
|
|
{
|
|
|
|
delete *it;
|
|
|
|
mStatic.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mStatic.push_back(ptr);
|
|
|
|
|
|
|
|
return RecordId("", isDeleted);
|
|
|
|
}
|
|
|
|
void Store<ESM::Land>::setUp()
|
|
|
|
{
|
|
|
|
std::sort(mStatic.begin(), mStatic.end(), Compare());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Cell
|
|
|
|
//=========================================================================
|
|
|
|
|
|
|
|
const ESM::Cell *Store<ESM::Cell>::search(const ESM::Cell &cell) const
|
|
|
|
{
|
|
|
|
if (cell.isExterior()) {
|
|
|
|
return search(cell.getGridX(), cell.getGridY());
|
|
|
|
}
|
|
|
|
return search(cell.mName);
|
|
|
|
}
|
|
|
|
void Store<ESM::Cell>::handleMovedCellRefs(ESM::ESMReader& esm, ESM::Cell* cell)
|
|
|
|
{
|
|
|
|
//Handling MovedCellRefs, there is no way to do it inside loadcell
|
|
|
|
while (esm.isNextSub("MVRF")) {
|
|
|
|
ESM::CellRef ref;
|
|
|
|
ESM::MovedCellRef cMRef;
|
|
|
|
cell->getNextMVRF(esm, cMRef);
|
|
|
|
|
|
|
|
ESM::Cell *cellAlt = const_cast<ESM::Cell*>(searchOrCreate(cMRef.mTarget[0], cMRef.mTarget[1]));
|
|
|
|
|
|
|
|
// Get regular moved reference data. Adapted from CellStore::loadRefs. Maybe we can optimize the following
|
|
|
|
// implementation when the oher implementation works as well.
|
|
|
|
bool deleted = false;
|
|
|
|
cell->getNextRef(esm, ref, deleted);
|
|
|
|
|
|
|
|
// Add data required to make reference appear in the correct cell.
|
|
|
|
// We should not need to test for duplicates, as this part of the code is pre-cell merge.
|
|
|
|
cell->mMovedRefs.push_back(cMRef);
|
|
|
|
// But there may be duplicates here!
|
|
|
|
if (!deleted)
|
|
|
|
{
|
|
|
|
ESM::CellRefTracker::iterator iter = std::find(cellAlt->mLeasedRefs.begin(), cellAlt->mLeasedRefs.end(), ref.mRefNum);
|
|
|
|
if (iter == cellAlt->mLeasedRefs.end())
|
|
|
|
cellAlt->mLeasedRefs.push_back(ref);
|
2014-07-01 19:42:11 +00:00
|
|
|
else
|
|
|
|
oldcell->mMovedRefs.push_back(*it);
|
2013-02-09 12:00:57 +00:00
|
|
|
}
|
2014-06-11 13:37:05 +00:00
|
|
|
|
|
|
|
// We don't need to merge mLeasedRefs of cell / oldcell. This list is filled when another cell moves a
|
|
|
|
// reference to this cell, so the list for the new cell should be empty. The list for oldcell,
|
|
|
|
// however, could have leased refs in it and so should be kept.
|
2013-02-09 12:00:57 +00:00
|
|
|
} else
|
2014-06-11 13:37:05 +00:00
|
|
|
{
|
|
|
|
// spawn a new cell
|
2014-06-11 19:56:10 +00:00
|
|
|
cell.loadCell(esm, false);
|
2014-06-11 13:37:05 +00:00
|
|
|
|
|
|
|
// handle moved ref (MVRF) subrecords
|
|
|
|
handleMovedCellRefs (esm, &cell);
|
|
|
|
|
2014-06-11 19:56:10 +00:00
|
|
|
// push the new references on the list of references to manage
|
|
|
|
cell.postLoad(esm);
|
|
|
|
|
2014-06-11 13:37:05 +00:00
|
|
|
mExt[std::make_pair(cell.mData.mX, cell.mData.mY)] = cell;
|
|
|
|
}
|
2013-02-09 12:00:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-08 18:24:05 +00:00
|
|
|
void Store<ESM::LandTexture>::load(ESM::ESMReader &esm)
|
2015-01-25 00:53:20 +00:00
|
|
|
{
|
2015-07-08 18:24:05 +00:00
|
|
|
load(esm, esm.getIndex());
|
2015-01-25 00:53:20 +00:00
|
|
|
}
|
|
|
|
|
2013-04-28 14:36:12 +00:00
|
|
|
}
|