From 965bc20babc9c2b39764d1bc750b91053292341a Mon Sep 17 00:00:00 2001
From: Evil Eye <malusluminis@hotmail.com>
Date: Wed, 22 May 2024 22:41:23 +0200
Subject: [PATCH] Set mPluginIndex to -1 for textures defined in this plugin

---
 apps/opencs/model/world/idcollection.hpp          | 7 +++++++
 apps/opencs/model/world/idtable.cpp               | 2 +-
 apps/opencs/view/render/terraintexturemode.cpp    | 6 +++---
 apps/opencs/view/widget/scenetooltexturebrush.cpp | 4 ++--
 apps/opencs/view/world/landtexturecreator.cpp     | 2 +-
 5 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/apps/opencs/model/world/idcollection.hpp b/apps/opencs/model/world/idcollection.hpp
index 67b886c3f8..c15193fe46 100644
--- a/apps/opencs/model/world/idcollection.hpp
+++ b/apps/opencs/model/world/idcollection.hpp
@@ -73,6 +73,13 @@ namespace CSMWorld
         bool isDeleted = false;
 
         loadRecord(record, reader, isDeleted);
+        if constexpr (std::is_same_v<ESXRecordT, LandTexture>)
+        {
+            // This doesn't really matter since the value never gets saved, but it makes the index uniqueness check more
+            // sensible
+            if (!base)
+                record.mPluginIndex = -1;
+        }
 
         ESM::RefId id = getRecordId(record);
         int index = this->searchId(id);
diff --git a/apps/opencs/model/world/idtable.cpp b/apps/opencs/model/world/idtable.cpp
index 8409402dd3..baa453fc8b 100644
--- a/apps/opencs/model/world/idtable.cpp
+++ b/apps/opencs/model/world/idtable.cpp
@@ -410,7 +410,7 @@ CSMWorld::LandTextureIdTable::ImportResults CSMWorld::LandTextureIdTable::import
         int startIndex = index;
         do
         {
-            std::string newId = LandTexture::createUniqueRecordId(0, index);
+            std::string newId = LandTexture::createUniqueRecordId(-1, index);
             const ESM::RefId newRefId = ESM::RefId::stringRefId(newId);
             int newRow = idCollection()->searchId(newRefId);
 
diff --git a/apps/opencs/view/render/terraintexturemode.cpp b/apps/opencs/view/render/terraintexturemode.cpp
index d89261aa1e..554e312527 100644
--- a/apps/opencs/view/render/terraintexturemode.cpp
+++ b/apps/opencs/view/render/terraintexturemode.cpp
@@ -680,13 +680,13 @@ void CSVRender::TerrainTextureMode::createTexture(const std::string& textureFile
         const size_t maxCounter = std::numeric_limits<uint16_t>::max() - 1;
         try
         {
-            newId = CSMWorld::LandTexture::createUniqueRecordId(0, counter);
-            if (ltexTable.getRecord(newId).isDeleted() == 0)
+            newId = CSMWorld::LandTexture::createUniqueRecordId(-1, counter);
+            if (!ltexTable.getRecord(newId).isDeleted())
                 counter = (counter + 1) % maxCounter;
         }
         catch (const std::exception&)
         {
-            newId = CSMWorld::LandTexture::createUniqueRecordId(0, counter);
+            newId = CSMWorld::LandTexture::createUniqueRecordId(-1, counter);
             freeIndexFound = true;
         }
     } while (freeIndexFound == false);
diff --git a/apps/opencs/view/widget/scenetooltexturebrush.cpp b/apps/opencs/view/widget/scenetooltexturebrush.cpp
index e2cb241cb3..dcdd2f0412 100644
--- a/apps/opencs/view/widget/scenetooltexturebrush.cpp
+++ b/apps/opencs/view/widget/scenetooltexturebrush.cpp
@@ -167,7 +167,7 @@ void CSVWidget::TextureBrushWindow::setBrushTexture(std::string brushTexture)
     int pluginInDragged = 0;
     CSMWorld::LandTexture::parseUniqueRecordId(brushTexture, pluginInDragged, index);
     const ESM::RefId brushTextureRefId = ESM::RefId::stringRefId(brushTexture);
-    std::string newBrushTextureId = CSMWorld::LandTexture::createUniqueRecordId(0, index);
+    std::string newBrushTextureId = CSMWorld::LandTexture::createUniqueRecordId(-1, index);
     ESM::RefId newBrushTextureRefId = ESM::RefId::stringRefId(newBrushTextureId);
     int rowInBase = landtexturesCollection.searchId(brushTextureRefId);
     int rowInNew = landtexturesCollection.searchId(newBrushTextureRefId);
@@ -184,7 +184,7 @@ void CSVWidget::TextureBrushWindow::setBrushTexture(std::string brushTexture)
             const int maxCounter = std::numeric_limits<uint16_t>::max() - 1;
             do
             {
-                newBrushTextureId = CSMWorld::LandTexture::createUniqueRecordId(0, counter);
+                newBrushTextureId = CSMWorld::LandTexture::createUniqueRecordId(-1, counter);
                 newBrushTextureRefId = ESM::RefId::stringRefId(newBrushTextureId);
                 if (landtexturesCollection.searchId(brushTextureRefId) != -1
                     && landtexturesCollection.getRecord(brushTextureRefId).isDeleted() == 0
diff --git a/apps/opencs/view/world/landtexturecreator.cpp b/apps/opencs/view/world/landtexturecreator.cpp
index a46e5b6dbe..1d2af5005f 100644
--- a/apps/opencs/view/world/landtexturecreator.cpp
+++ b/apps/opencs/view/world/landtexturecreator.cpp
@@ -91,7 +91,7 @@ namespace CSVWorld
 
     std::string LandTextureCreator::getId() const
     {
-        return CSMWorld::LandTexture::createUniqueRecordId(0, mIndexBox->value());
+        return CSMWorld::LandTexture::createUniqueRecordId(-1, mIndexBox->value());
     }
 
     void LandTextureCreator::nameChanged(const QString& value)