From 810ba6190921572d3ac01cfb5d82bbe4b7fb171d Mon Sep 17 00:00:00 2001 From: cc9cii Date: Sat, 27 Sep 2014 17:46:48 +1000 Subject: [PATCH] While loading overwrite records with the same id. Should resolve bug #1750. --- apps/opencs/model/world/collection.hpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/apps/opencs/model/world/collection.hpp b/apps/opencs/model/world/collection.hpp index 1fb3e1f1d..387b33cf3 100644 --- a/apps/opencs/model/world/collection.hpp +++ b/apps/opencs/model/world/collection.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include @@ -431,18 +432,27 @@ namespace CSMWorld const Record& record2 = dynamic_cast&> (record); - mRecords.insert (mRecords.begin()+index, record2); + std::pair::iterator, bool> insertResult = + mIndex.insert (std::make_pair (Misc::StringUtils::lowerCase (IdAccessorT().getId (record2.get())), + index)); + if(!insertResult.second) // duplicate index found, replace the current record + { + std::cerr << "Duplicate record found, using new: " + IdAccessorT().getId(record2.get()) << std::endl; + replace(insertResult.first->second, record2); + return; + } + + // else update the index except for the record to be inserted if (index (mRecords.size())-1) { - for (std::map::iterator iter (mIndex.begin()); iter!=mIndex.end(); - ++iter) - if (iter->second>=index) + std::string id = IdAccessorT().getId(record2.get()); + for (std::map::iterator iter (mIndex.begin()); iter!=mIndex.end(); ++iter) + if (iter->second > index || (iter->second == index && iter->first != id)) ++(iter->second); } - mIndex.insert (std::make_pair (Misc::StringUtils::lowerCase (IdAccessorT().getId ( - record2.get())), index)); + mRecords.insert (mRecords.begin()+index, record2); } template