From a3d48fd482ba463790baf27b29a3593ccd4becf2 Mon Sep 17 00:00:00 2001
From: Stanislav Bas <stanislav.m.bas@gmail.com>
Date: Sun, 19 Jul 2015 16:07:56 +0300
Subject: [PATCH] Rework ESS importer code. Remove explicit NAME handling for
 ESM records

(cherry picked from commit 6b21da7f8e6b50de71047248c36d4ee9ec51751e)
---
 apps/essimporter/converter.cpp |  6 ++----
 apps/essimporter/converter.hpp | 36 +++++++++++++---------------------
 apps/essimporter/importer.cpp  |  2 +-
 3 files changed, 17 insertions(+), 27 deletions(-)

diff --git a/apps/essimporter/converter.cpp b/apps/essimporter/converter.cpp
index 91d290f331..5c4c0e62ed 100644
--- a/apps/essimporter/converter.cpp
+++ b/apps/essimporter/converter.cpp
@@ -134,8 +134,6 @@ namespace ESSImport
     void ConvertCell::read(ESM::ESMReader &esm)
     {
         ESM::Cell cell;
-        std::string id = esm.getHNString("NAME");
-        cell.mName = id;
         cell.load(esm, false);
 
         // I wonder what 0x40 does?
@@ -145,7 +143,7 @@ namespace ESSImport
         }
 
         // note if the player is in a nameless exterior cell, we will assign the cellId later based on player position
-        if (id == mContext->mPlayerCellName)
+        if (cell.mName == mContext->mPlayerCellName)
         {
             mContext->mPlayer.mCellId = cell.getCellId();
         }
@@ -253,7 +251,7 @@ namespace ESSImport
         if (cell.isExterior())
             mExtCells[std::make_pair(cell.mData.mX, cell.mData.mY)] = newcell;
         else
-            mIntCells[id] = newcell;
+            mIntCells[cell.mName] = newcell;
     }
 
     void ConvertCell::writeCell(const Cell &cell, ESM::ESMWriter& esm)
diff --git a/apps/essimporter/converter.hpp b/apps/essimporter/converter.hpp
index 5711e6754b..80d5899ec5 100644
--- a/apps/essimporter/converter.hpp
+++ b/apps/essimporter/converter.hpp
@@ -75,10 +75,9 @@ public:
 
     virtual void read(ESM::ESMReader& esm)
     {
-        std::string id = esm.getHNString("NAME");
         T record;
         record.load(esm);
-        mRecords[id] = record;
+        mRecords[record.mId] = record;
     }
 
     virtual void write(ESM::ESMWriter& esm)
@@ -86,7 +85,6 @@ public:
         for (typename std::map<std::string, T>::const_iterator it = mRecords.begin(); it != mRecords.end(); ++it)
         {
             esm.startRecord(T::sRecordId);
-            esm.writeHNString("NAME", it->first);
             it->second.save(esm);
             esm.endRecord(T::sRecordId);
         }
@@ -102,14 +100,13 @@ public:
     virtual void read(ESM::ESMReader &esm)
     {
         ESM::NPC npc;
-        std::string id = esm.getHNString("NAME");
         npc.load(esm);
-        if (id != "player")
+        if (npc.mId != "player")
         {
             // Handles changes to the NPC struct, but since there is no index here
             // it will apply to ALL instances of the class. seems to be the reason for the
             // "feature" in MW where changing AI settings of one guard will change it for all guards of that refID.
-            mContext->mNpcs[Misc::StringUtils::lowerCase(id)] = npc;
+            mContext->mNpcs[Misc::StringUtils::lowerCase(npc.mId)] = npc;
         }
         else
         {
@@ -139,9 +136,8 @@ public:
     {
         // See comment in ConvertNPC
         ESM::Creature creature;
-        std::string id = esm.getHNString("NAME");
         creature.load(esm);
-        mContext->mCreatures[Misc::StringUtils::lowerCase(id)] = creature;
+        mContext->mCreatures[Misc::StringUtils::lowerCase(creature.mId)] = creature;
     }
 };
 
@@ -154,18 +150,17 @@ class ConvertGlobal : public DefaultConverter<ESM::Global>
 public:
     virtual void read(ESM::ESMReader &esm)
     {
-        std::string id = esm.getHNString("NAME");
         ESM::Global global;
         global.load(esm);
-        if (Misc::StringUtils::ciEqual(id, "gamehour"))
+        if (Misc::StringUtils::ciEqual(global.mId, "gamehour"))
             mContext->mHour = global.mValue.getFloat();
-        if (Misc::StringUtils::ciEqual(id, "day"))
+        if (Misc::StringUtils::ciEqual(global.mId, "day"))
             mContext->mDay = global.mValue.getInteger();
-        if (Misc::StringUtils::ciEqual(id, "month"))
+        if (Misc::StringUtils::ciEqual(global.mId, "month"))
             mContext->mMonth = global.mValue.getInteger();
-        if (Misc::StringUtils::ciEqual(id, "year"))
+        if (Misc::StringUtils::ciEqual(global.mId, "year"))
             mContext->mYear = global.mValue.getInteger();
-        mRecords[id] = global;
+        mRecords[global.mId] = global;
     }
 };
 
@@ -174,14 +169,13 @@ class ConvertClass : public DefaultConverter<ESM::Class>
 public:
     virtual void read(ESM::ESMReader &esm)
     {
-        std::string id = esm.getHNString("NAME");
         ESM::Class class_;
         class_.load(esm);
 
-        if (id == "NEWCLASSID_CHARGEN")
+        if (class_.mId == "NEWCLASSID_CHARGEN")
             mContext->mCustomPlayerClassName = class_.mName;
 
-        mRecords[id] = class_;
+        mRecords[class_.mId] = class_;
     }
 };
 
@@ -190,13 +184,12 @@ class ConvertBook : public DefaultConverter<ESM::Book>
 public:
     virtual void read(ESM::ESMReader &esm)
     {
-        std::string id = esm.getHNString("NAME");
         ESM::Book book;
         book.load(esm);
         if (book.mData.mSkillID == -1)
-            mContext->mPlayer.mObject.mNpcStats.mUsedIds.push_back(Misc::StringUtils::lowerCase(id));
+            mContext->mPlayer.mObject.mNpcStats.mUsedIds.push_back(Misc::StringUtils::lowerCase(book.mId));
 
-        mRecords[id] = book;
+        mRecords[book.mId] = book;
     }
 };
 
@@ -368,11 +361,10 @@ class ConvertFACT : public Converter
 public:
     virtual void read(ESM::ESMReader& esm)
     {
-        std::string id = esm.getHNString("NAME");
         ESM::Faction faction;
         faction.load(esm);
+        std::string id = Misc::StringUtils::toLower(faction.mId);
 
-        Misc::StringUtils::toLower(id);
         for (std::map<std::string, int>::const_iterator it = faction.mReactions.begin(); it != faction.mReactions.end(); ++it)
         {
             std::string faction2 = Misc::StringUtils::lowerCase(it->first);
diff --git a/apps/essimporter/importer.cpp b/apps/essimporter/importer.cpp
index b9468ed870..57c4ba4c36 100644
--- a/apps/essimporter/importer.cpp
+++ b/apps/essimporter/importer.cpp
@@ -348,7 +348,7 @@ namespace ESSImport
         }
 
         writer.startRecord(ESM::REC_NPC_);
-        writer.writeHNString("NAME", "player");
+        context.mPlayerBase.mId = "player";
         context.mPlayerBase.save(writer);
         writer.endRecord(ESM::REC_NPC_);