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