Merge DialInfo objects by subrecord instead of overwriting the object

Fixes #1360
deque
scrawl 11 years ago
parent d26f756c53
commit 16ac6e7aac

@ -68,13 +68,12 @@ void ESMStore::load(ESM::ESMReader &esm, Loading::Listener* listener)
if (it == mStores.end()) { if (it == mStores.end()) {
if (n.val == ESM::REC_INFO) { if (n.val == ESM::REC_INFO) {
std::string id = esm.getHNOString("INAM"); if (dialogue)
if (dialogue) { {
ESM::DialInfo info; dialogue->readInfo(esm, esm.getIndex() != 0);
info.mId = id; }
info.load(esm); else
dialogue->addInfo(info, esm.getIndex() != 0); {
} else {
std::cerr << "error: info record without dialog" << std::endl; std::cerr << "error: info record without dialog" << std::endl;
esm.skipRecord(); esm.skipRecord();
} }

@ -40,21 +40,21 @@ void Dialogue::save(ESMWriter &esm) const
} }
} }
void Dialogue::blank() void Dialogue::blank()
{ {
mInfo.clear(); mInfo.clear();
} }
void Dialogue::addInfo(const ESM::DialInfo& info, bool merge) void Dialogue::readInfo(ESMReader &esm, bool merge)
{ {
if (!merge || mInfo.empty() || info.mNext.empty()) const std::string& id = esm.getHNOString("INAM");
{
mLookup[info.mId] = mInfo.insert(mInfo.end(), info); if (!merge || mInfo.empty())
return;
}
if (info.mPrev.empty())
{ {
mLookup[info.mId] = mInfo.insert(mInfo.begin(), info); ESM::DialInfo info;
info.mId = id;
info.load(esm);
mLookup[id] = mInfo.insert(mInfo.end(), info);
return; return;
} }
@ -62,11 +62,30 @@ void Dialogue::addInfo(const ESM::DialInfo& info, bool merge)
std::map<std::string, ESM::Dialogue::InfoContainer::iterator>::iterator lookup; std::map<std::string, ESM::Dialogue::InfoContainer::iterator>::iterator lookup;
lookup = mLookup.find(info.mId); lookup = mLookup.find(id);
if (lookup != mLookup.end()) if (lookup != mLookup.end())
{ {
it = lookup->second; it = lookup->second;
*it = info;
// Merge with existing record. Only the subrecords that are present in
// the new record will be overwritten.
it->load(esm);
return;
}
// New record
ESM::DialInfo info;
info.mId = id;
info.load(esm);
if (info.mNext.empty())
{
mLookup[id] = mInfo.insert(mInfo.end(), info);
return;
}
if (info.mPrev.empty())
{
mLookup[id] = mInfo.insert(mInfo.begin(), info);
return; return;
} }
@ -75,7 +94,7 @@ void Dialogue::addInfo(const ESM::DialInfo& info, bool merge)
{ {
it = lookup->second; it = lookup->second;
mLookup[info.mId] = mInfo.insert(++it, info); mLookup[id] = mInfo.insert(++it, info);
return; return;
} }
@ -84,11 +103,11 @@ void Dialogue::addInfo(const ESM::DialInfo& info, bool merge)
{ {
it = lookup->second; it = lookup->second;
mLookup[info.mId] = mInfo.insert(it, info); mLookup[id] = mInfo.insert(it, info);
return; return;
} }
std::cerr << "Failed to insert info " << info.mId << std::endl; std::cerr << "Failed to insert info " << id << std::endl;
} }
} }

@ -47,8 +47,9 @@ struct Dialogue
void load(ESMReader &esm); void load(ESMReader &esm);
void save(ESMWriter &esm) const; void save(ESMWriter &esm) const;
/// Read the next info record
/// @param merge Merge with existing list, or just push each record to the end of the list? /// @param merge Merge with existing list, or just push each record to the end of the list?
void addInfo (const ESM::DialInfo& info, bool merge); void readInfo (ESM::ESMReader& esm, bool merge);
void blank(); void blank();
///< Set record to default state (does not touch the ID and does not change the type). ///< Set record to default state (does not touch the ID and does not change the type).

Loading…
Cancel
Save