forked from teamnwah/openmw-tes3coop
Use map for texture lookup.
This commit is contained in:
parent
4921e7f5c1
commit
054e6a780e
1 changed files with 22 additions and 17 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
#include <map>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
#include <components/esm/cellid.hpp>
|
#include <components/esm/cellid.hpp>
|
||||||
|
@ -340,6 +341,15 @@ CSMWorld::LandTextureIdTable::ImportResults CSMWorld::LandTextureIdTable::import
|
||||||
{
|
{
|
||||||
ImportResults results;
|
ImportResults results;
|
||||||
|
|
||||||
|
// Map existing textures to ids
|
||||||
|
std::map<std::string, std::string> reverseLookupMap;
|
||||||
|
for (int i = 0; i < idCollection()->getSize(); ++i)
|
||||||
|
{
|
||||||
|
auto& record = static_cast<const Record<LandTexture>&>(idCollection()->getRecord(i));
|
||||||
|
if (record.isModified())
|
||||||
|
reverseLookupMap.emplace(record.get().mTexture, idCollection()->getId(i));
|
||||||
|
}
|
||||||
|
|
||||||
for (const std::string& id : ids)
|
for (const std::string& id : ids)
|
||||||
{
|
{
|
||||||
int plugin, index;
|
int plugin, index;
|
||||||
|
@ -354,8 +364,16 @@ CSMWorld::LandTextureIdTable::ImportResults CSMWorld::LandTextureIdTable::import
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try a direct mapping to the current plugin first. Otherwise iterate until one is found.
|
// Look for a pre-existing record
|
||||||
// Iteration is deterministic to avoid duplicates.
|
auto& record = static_cast<const Record<LandTexture>&>(idCollection()->getRecord(oldRow));
|
||||||
|
auto searchIt = reverseLookupMap.find(record.get().mTexture);
|
||||||
|
if (searchIt != reverseLookupMap.end())
|
||||||
|
{
|
||||||
|
results.recordMapping.push_back(std::make_pair(id, searchIt->second));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterate until an unused index or found, or the index has completely wrapped around.
|
||||||
int startIndex = index;
|
int startIndex = index;
|
||||||
do {
|
do {
|
||||||
std::string newId = LandTexture::createUniqueRecordId(0, index);
|
std::string newId = LandTexture::createUniqueRecordId(0, index);
|
||||||
|
@ -370,21 +388,8 @@ CSMWorld::LandTextureIdTable::ImportResults CSMWorld::LandTextureIdTable::import
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Id is taken, check if same handle and texture. Note that mId is the handle.
|
const size_t MaxIndex = std::numeric_limits<uint16_t>::max() - 1;
|
||||||
const LandTexture& oldLtex = dynamic_cast<const Record<LandTexture>&>(idCollection()->getRecord(oldRow)).get();
|
index = (index + 1) % MaxIndex;
|
||||||
const LandTexture& newLtex = dynamic_cast<const Record<LandTexture>&>(idCollection()->getRecord(newRow)).get();
|
|
||||||
if (oldLtex.mId == newLtex.mId && oldLtex.mTexture == newLtex.mTexture)
|
|
||||||
{
|
|
||||||
// It's a match
|
|
||||||
results.recordMapping.push_back(std::make_pair(id, newId));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Determine next index. Spread out the indices to reduce conflicts.
|
|
||||||
size_t MaxIndex = std::numeric_limits<uint16_t>::max() - 1;
|
|
||||||
size_t Prime = (1 << 13) - 1; // A mersenne prime
|
|
||||||
|
|
||||||
index = (index + Prime) % MaxIndex;
|
|
||||||
} while (index != startIndex);
|
} while (index != startIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue