From e9a8eac6af7b68e9c1859c472f56d04e304e6459 Mon Sep 17 00:00:00 2001
From: Stanislav Bas <stanislav.m.bas@gmail.com>
Date: Tue, 7 Jul 2015 15:37:42 +0300
Subject: [PATCH] Add NAME and DELE handling to ESM records. Changed records
 are those where DELE is located after NAME sub-record. And DELE is the last
 sub-record.

(cherry picked from commit 926c825d0c7f0a373fb6bd22d88d0df850407f3c)

Conflicts:
	components/esm/loadstat.cpp
	components/esm/loadstat.hpp
	components/esm/util.hpp
---
 components/esm/loadacti.cpp    | 15 +++++++++++++++
 components/esm/loadacti.hpp    |  2 ++
 components/esm/loadalch.cpp    | 16 ++++++++++++++++
 components/esm/loadalch.hpp    |  2 ++
 components/esm/loadappa.cpp    | 15 +++++++++++++++
 components/esm/loadappa.hpp    |  2 ++
 components/esm/loadarmo.cpp    | 16 ++++++++++++++++
 components/esm/loadarmo.hpp    |  2 ++
 components/esm/loadbody.cpp    | 16 ++++++++++++++++
 components/esm/loadbody.hpp    |  2 ++
 components/esm/loadbook.cpp    | 15 +++++++++++++++
 components/esm/loadbook.hpp    |  2 ++
 components/esm/loadclas.cpp    | 16 ++++++++++++++++
 components/esm/loadclas.hpp    |  2 ++
 components/esm/loadclot.cpp    | 16 ++++++++++++++++
 components/esm/loadclot.hpp    |  2 ++
 components/esm/loadcont.cpp    | 16 ++++++++++++++++
 components/esm/loadcont.hpp    |  2 ++
 components/esm/loadcrea.cpp    | 15 +++++++++++++++
 components/esm/loadcrea.hpp    |  3 ++-
 components/esm/loaddoor.cpp    | 15 +++++++++++++++
 components/esm/loaddoor.hpp    |  2 ++
 components/esm/loadench.cpp    | 17 +++++++++++++++++
 components/esm/loadench.hpp    |  2 ++
 components/esm/loadfact.cpp    | 16 ++++++++++++++++
 components/esm/loadfact.hpp    |  2 ++
 components/esm/loadingr.cpp    | 16 ++++++++++++++++
 components/esm/loadingr.hpp    |  2 ++
 components/esm/loadlevlist.cpp | 15 +++++++++++++++
 components/esm/loadlevlist.hpp |  2 ++
 components/esm/loadligh.cpp    | 15 +++++++++++++++
 components/esm/loadligh.hpp    |  2 ++
 components/esm/loadlock.cpp    | 15 +++++++++++++++
 components/esm/loadlock.hpp    |  2 ++
 components/esm/loadmisc.cpp    | 15 +++++++++++++++
 components/esm/loadmisc.hpp    |  2 ++
 components/esm/loadnpc.cpp     | 15 +++++++++++++++
 components/esm/loadnpc.hpp     |  2 ++
 components/esm/loadprob.cpp    | 15 +++++++++++++++
 components/esm/loadprob.hpp    |  2 ++
 components/esm/loadrepa.cpp    | 15 +++++++++++++++
 components/esm/loadrepa.hpp    |  2 ++
 components/esm/loadsndg.cpp    | 15 +++++++++++++++
 components/esm/loadsndg.hpp    |  2 ++
 components/esm/loadsoun.cpp    | 16 ++++++++++++++++
 components/esm/loadsoun.hpp    |  2 ++
 components/esm/loadspel.cpp    | 17 +++++++++++++++++
 components/esm/loadspel.hpp    |  2 ++
 components/esm/loadstat.cpp    | 15 ++++++++++++++-
 components/esm/loadstat.hpp    |  2 +-
 components/esm/loadweap.cpp    | 16 ++++++++++++++++
 components/esm/loadweap.hpp    |  2 ++
 components/esm/util.cpp        | 20 ++++++++++++++++++++
 components/esm/util.hpp        |  9 +++++++++
 54 files changed, 483 insertions(+), 3 deletions(-)
 create mode 100644 components/esm/util.cpp

diff --git a/components/esm/loadacti.cpp b/components/esm/loadacti.cpp
index b5adce5509..295d35f7e9 100644
--- a/components/esm/loadacti.cpp
+++ b/components/esm/loadacti.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -10,6 +11,12 @@ namespace ESM
 
     void Activator::load(ESMReader &esm)
     {
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         while (esm.hasMoreSubs())
         {
             esm.getSubName();
@@ -32,6 +39,13 @@ namespace ESM
     }
     void Activator::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNCString("MODL", mModel);
         esm.writeHNOCString("FNAM", mName);
         esm.writeHNOCString("SCRI", mScript);
@@ -42,5 +56,6 @@ namespace ESM
         mName.clear();
         mScript.clear();
         mModel.clear();
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadacti.hpp b/components/esm/loadacti.hpp
index d9a55023bc..406512a225 100644
--- a/components/esm/loadacti.hpp
+++ b/components/esm/loadacti.hpp
@@ -17,6 +17,8 @@ struct Activator
 
     std::string mId, mName, mScript, mModel;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadalch.cpp b/components/esm/loadalch.cpp
index 18db512c0c..12078a5f7e 100644
--- a/components/esm/loadalch.cpp
+++ b/components/esm/loadalch.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -11,6 +12,13 @@ namespace ESM
     void Potion::load(ESMReader &esm)
     {
         mEffects.mList.clear();
+
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         bool hasData = false;
         while (esm.hasMoreSubs())
         {
@@ -46,6 +54,13 @@ namespace ESM
     }
     void Potion::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNCString("MODL", mModel);
         esm.writeHNOCString("TEXT", mIcon);
         esm.writeHNOCString("SCRI", mScript);
@@ -64,5 +79,6 @@ namespace ESM
         mIcon.clear();
         mScript.clear();
         mEffects.mList.clear();
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadalch.hpp b/components/esm/loadalch.hpp
index b90a7c448d..3d1e6dee71 100644
--- a/components/esm/loadalch.hpp
+++ b/components/esm/loadalch.hpp
@@ -33,6 +33,8 @@ struct Potion
     std::string mId, mName, mModel, mIcon, mScript;
     EffectList mEffects;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadappa.cpp b/components/esm/loadappa.cpp
index f2c82aacfb..9107e5a8eb 100644
--- a/components/esm/loadappa.cpp
+++ b/components/esm/loadappa.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -10,6 +11,12 @@ namespace ESM
 
 void Apparatus::load(ESMReader &esm)
 {
+    mId = esm.getHNString("NAME");
+    if (mIsDeleted = readDeleSubRecord(esm))
+    {
+        return;
+    }
+
     bool hasData = false;
     while (esm.hasMoreSubs())
     {
@@ -43,6 +50,13 @@ void Apparatus::load(ESMReader &esm)
 
 void Apparatus::save(ESMWriter &esm) const
 {
+    esm.writeHNCString("NAME", mId);
+    if (mIsDeleted)
+    {
+        writeDeleSubRecord(esm);
+        return;
+    }
+
     esm.writeHNCString("MODL", mModel);
     esm.writeHNCString("FNAM", mName);
     esm.writeHNT("AADT", mData, 16);
@@ -60,5 +74,6 @@ void Apparatus::save(ESMWriter &esm) const
         mIcon.clear();
         mScript.clear();
         mName.clear();
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadappa.hpp b/components/esm/loadappa.hpp
index f18b046486..62d8561a1d 100644
--- a/components/esm/loadappa.hpp
+++ b/components/esm/loadappa.hpp
@@ -38,6 +38,8 @@ struct Apparatus
     AADTstruct mData;
     std::string mId, mModel, mIcon, mScript, mName;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadarmo.cpp b/components/esm/loadarmo.cpp
index 066551d6fe..626893d00f 100644
--- a/components/esm/loadarmo.cpp
+++ b/components/esm/loadarmo.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -41,6 +42,13 @@ namespace ESM
     void Armor::load(ESMReader &esm)
     {
         mParts.mParts.clear();
+
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         bool hasData = false;
         while (esm.hasMoreSubs())
         {
@@ -80,6 +88,13 @@ namespace ESM
 
     void Armor::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNCString("MODL", mModel);
         esm.writeHNOCString("FNAM", mName);
         esm.writeHNOCString("SCRI", mScript);
@@ -103,5 +118,6 @@ namespace ESM
         mIcon.clear();
         mScript.clear();
         mEnchant.clear();
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadarmo.hpp b/components/esm/loadarmo.hpp
index 54416fd318..eb911254fd 100644
--- a/components/esm/loadarmo.hpp
+++ b/components/esm/loadarmo.hpp
@@ -96,6 +96,8 @@ struct Armor
 
     std::string mId, mName, mModel, mIcon, mScript, mEnchant;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadbody.cpp b/components/esm/loadbody.cpp
index ed24ded57b..644eb3b502 100644
--- a/components/esm/loadbody.cpp
+++ b/components/esm/loadbody.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -11,6 +12,12 @@ namespace ESM
 
 void BodyPart::load(ESMReader &esm)
 {
+    mId = esm.getHNString("NAME");
+    if (mIsDeleted = readDeleSubRecord(esm))
+    {
+        return;
+    }
+
     bool hasData = false;
     while (esm.hasMoreSubs())
     {
@@ -38,6 +45,13 @@ void BodyPart::load(ESMReader &esm)
 }
 void BodyPart::save(ESMWriter &esm) const
 {
+    esm.writeHNCString("NAME", mId);
+    if (mIsDeleted)
+    {
+        writeDeleSubRecord(esm);
+        return;
+    }
+
     esm.writeHNCString("MODL", mModel);
     esm.writeHNOCString("FNAM", mRace);
     esm.writeHNT("BYDT", mData, 4);
@@ -52,5 +66,7 @@ void BodyPart::save(ESMWriter &esm) const
 
         mModel.clear();
         mRace.clear();
+
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadbody.hpp b/components/esm/loadbody.hpp
index c48c313056..8e1e81f827 100644
--- a/components/esm/loadbody.hpp
+++ b/components/esm/loadbody.hpp
@@ -60,6 +60,8 @@ struct BodyPart
     BYDTstruct mData;
     std::string mId, mModel, mRace;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadbook.cpp b/components/esm/loadbook.cpp
index 47f52fc31a..aec361873d 100644
--- a/components/esm/loadbook.cpp
+++ b/components/esm/loadbook.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -10,6 +11,12 @@ namespace ESM
 
     void Book::load(ESMReader &esm)
     {
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         bool hasData = false;
         while (esm.hasMoreSubs())
         {
@@ -48,6 +55,13 @@ namespace ESM
     }
     void Book::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNCString("MODL", mModel);
         esm.writeHNOCString("FNAM", mName);
         esm.writeHNT("BKDT", mData, 20);
@@ -70,5 +84,6 @@ namespace ESM
         mScript.clear();
         mEnchant.clear();
         mText.clear();
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadbook.hpp b/components/esm/loadbook.hpp
index 6211b3e457..2f374c4b2e 100644
--- a/components/esm/loadbook.hpp
+++ b/components/esm/loadbook.hpp
@@ -28,6 +28,8 @@ struct Book
     std::string mName, mModel, mIcon, mScript, mEnchant, mText;
     std::string mId;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadclas.cpp b/components/esm/loadclas.cpp
index 66acaea721..df47a1a337 100644
--- a/components/esm/loadclas.cpp
+++ b/components/esm/loadclas.cpp
@@ -5,6 +5,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -41,6 +42,12 @@ namespace ESM
 
     void Class::load(ESMReader &esm)
     {
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         bool hasData = false;
         while (esm.hasMoreSubs())
         {
@@ -69,6 +76,13 @@ namespace ESM
     }
     void Class::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNOCString("FNAM", mName);
         esm.writeHNT("CLDT", mData, 60);
         esm.writeHNOString("DESC", mDescription);
@@ -87,5 +101,7 @@ namespace ESM
         for (int i=0; i<5; ++i)
             for (int i2=0; i2<2; ++i2)
                 mData.mSkills[i][i2] = 0;
+
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadclas.hpp b/components/esm/loadclas.hpp
index 972b48e889..b619137345 100644
--- a/components/esm/loadclas.hpp
+++ b/components/esm/loadclas.hpp
@@ -73,6 +73,8 @@ struct Class
     std::string mId, mName, mDescription;
     CLDTstruct mData;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadclot.cpp b/components/esm/loadclot.cpp
index 5f49b5e709..1c73c4f21f 100644
--- a/components/esm/loadclot.cpp
+++ b/components/esm/loadclot.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -11,6 +12,13 @@ namespace ESM
     void Clothing::load(ESMReader &esm)
     {
         mParts.mParts.clear();
+
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         bool hasData = false;
         while (esm.hasMoreSubs())
         {
@@ -50,6 +58,13 @@ namespace ESM
 
     void Clothing::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNCString("MODL", mModel);
         esm.writeHNOCString("FNAM", mName);
         esm.writeHNT("CTDT", mData, 12);
@@ -74,5 +89,6 @@ namespace ESM
         mIcon.clear();
         mEnchant.clear();
         mScript.clear();
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadclot.hpp b/components/esm/loadclot.hpp
index 6945f224a4..8b4a8b3bb2 100644
--- a/components/esm/loadclot.hpp
+++ b/components/esm/loadclot.hpp
@@ -48,6 +48,8 @@ struct Clothing
 
     std::string mId, mName, mModel, mIcon, mEnchant, mScript;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadcont.cpp b/components/esm/loadcont.cpp
index 3481189c37..f54fe66e25 100644
--- a/components/esm/loadcont.cpp
+++ b/components/esm/loadcont.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -27,6 +28,13 @@ namespace ESM
     void Container::load(ESMReader &esm)
     {
         mInventory.mList.clear();
+
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         bool hasWeight = false;
         bool hasFlags = false;
         while (esm.hasMoreSubs())
@@ -71,6 +79,13 @@ namespace ESM
 
     void Container::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNCString("MODL", mModel);
         esm.writeHNOCString("FNAM", mName);
         esm.writeHNT("CNDT", mWeight, 4);
@@ -89,5 +104,6 @@ namespace ESM
         mWeight = 0;
         mFlags = 0x8; // set default flag value
         mInventory.mList.clear();
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadcont.hpp b/components/esm/loadcont.hpp
index ab587f9353..51166b8d47 100644
--- a/components/esm/loadcont.hpp
+++ b/components/esm/loadcont.hpp
@@ -52,6 +52,8 @@ struct Container
     int mFlags;
     InventoryList mInventory;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadcrea.cpp b/components/esm/loadcrea.cpp
index fb235e6b3d..f1590f19ba 100644
--- a/components/esm/loadcrea.cpp
+++ b/components/esm/loadcrea.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM {
 
@@ -17,6 +18,12 @@ namespace ESM {
         mSpells.mList.clear();
         mTransport.mList.clear();
 
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         mScale = 1.f;
         mHasAI = false;
         bool hasNpdt = false;
@@ -84,6 +91,13 @@ namespace ESM {
 
     void Creature::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNCString("MODL", mModel);
         esm.writeHNOCString("CNAM", mOriginal);
         esm.writeHNOCString("FNAM", mName);
@@ -132,6 +146,7 @@ namespace ESM {
         mAiData.mServices = 0;
         mAiPackage.mList.clear();
         mTransport.mList.clear();
+        mIsDeleted = false;
     }
 
     const std::vector<Transport::Dest>& Creature::getTransport() const
diff --git a/components/esm/loadcrea.hpp b/components/esm/loadcrea.hpp
index 47e5954a5f..fb43f6272d 100644
--- a/components/esm/loadcrea.hpp
+++ b/components/esm/loadcrea.hpp
@@ -91,12 +91,13 @@ struct Creature
     InventoryList mInventory;
     SpellList mSpells;
 
-
     bool mHasAI;
     AIData mAiData;
     AIPackageList mAiPackage;
     Transport mTransport;
 
+    bool mIsDeleted;
+
     const std::vector<Transport::Dest>& getTransport() const;
 
     void load(ESMReader &esm);
diff --git a/components/esm/loaddoor.cpp b/components/esm/loaddoor.cpp
index f446eed611..6ee43fd1ac 100644
--- a/components/esm/loaddoor.cpp
+++ b/components/esm/loaddoor.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -10,6 +11,12 @@ namespace ESM
 
     void Door::load(ESMReader &esm)
     {
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         while (esm.hasMoreSubs())
         {
             esm.getSubName();
@@ -39,6 +46,13 @@ namespace ESM
 
     void Door::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNCString("MODL", mModel);
         esm.writeHNOCString("FNAM", mName);
         esm.writeHNOCString("SCRI", mScript);
@@ -53,5 +67,6 @@ namespace ESM
         mScript.clear();
         mOpenSound.clear();
         mCloseSound.clear();
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loaddoor.hpp b/components/esm/loaddoor.hpp
index 3073f4e9de..b0326a744a 100644
--- a/components/esm/loaddoor.hpp
+++ b/components/esm/loaddoor.hpp
@@ -17,6 +17,8 @@ struct Door
 
     std::string mId, mName, mModel, mScript, mOpenSound, mCloseSound;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadench.cpp b/components/esm/loadench.cpp
index 54690d9a0b..88d9e0f2e4 100644
--- a/components/esm/loadench.cpp
+++ b/components/esm/loadench.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -11,6 +12,13 @@ namespace ESM
 void Enchantment::load(ESMReader &esm)
 {
     mEffects.mList.clear();
+
+    mId = esm.getHNString("NAME");
+    if (mIsDeleted = readDeleSubRecord(esm))
+    {
+        return;
+    }
+
     bool hasData = false;
     while (esm.hasMoreSubs())
     {
@@ -36,6 +44,13 @@ void Enchantment::load(ESMReader &esm)
 
 void Enchantment::save(ESMWriter &esm) const
 {
+    esm.writeHNCString("NAME", mId);
+    if (mIsDeleted)
+    {
+        writeDeleSubRecord(esm);
+        return;
+    }
+
     esm.writeHNT("ENDT", mData, 16);
     mEffects.save(esm);
 }
@@ -48,5 +63,7 @@ void Enchantment::save(ESMWriter &esm) const
         mData.mAutocalc = 0;
 
         mEffects.mList.clear();
+
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadench.hpp b/components/esm/loadench.hpp
index cfcdd4edce..c9b19e31b3 100644
--- a/components/esm/loadench.hpp
+++ b/components/esm/loadench.hpp
@@ -42,6 +42,8 @@ struct Enchantment
     ENDTstruct mData;
     EffectList mEffects;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadfact.cpp b/components/esm/loadfact.cpp
index 006ca0ce00..9149c2a30e 100644
--- a/components/esm/loadfact.cpp
+++ b/components/esm/loadfact.cpp
@@ -5,6 +5,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -32,6 +33,12 @@ void Faction::load(ESMReader &esm)
     for (int i=0;i<10;++i)
         mRanks[i].clear();
 
+    mId = esm.getHNString("NAME");
+    if (mIsDeleted = readDeleSubRecord(esm))
+    {
+        return;
+    }
+
     int rankCounter=0;
     bool hasData = false;
     while (esm.hasMoreSubs())
@@ -71,6 +78,13 @@ void Faction::load(ESMReader &esm)
 }
 void Faction::save(ESMWriter &esm) const
 {
+    esm.writeHNCString("NAME", mId);
+    if (mIsDeleted)
+    {
+        writeDeleSubRecord(esm);
+        return;
+    }
+
     esm.writeHNOCString("FNAM", mName);
 
     for (int i = 0; i < 10; i++)
@@ -109,5 +123,7 @@ void Faction::save(ESMWriter &esm) const
             mData.mSkills[i] = 0;
 
         mReactions.clear();
+
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadfact.hpp b/components/esm/loadfact.hpp
index 8645e23fd8..fec13b1ca0 100644
--- a/components/esm/loadfact.hpp
+++ b/components/esm/loadfact.hpp
@@ -62,6 +62,8 @@ struct Faction
     // Name of faction ranks (may be empty for NPC factions)
     std::string mRanks[10];
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadingr.cpp b/components/esm/loadingr.cpp
index 7e0cc3168d..8bf0278f7a 100644
--- a/components/esm/loadingr.cpp
+++ b/components/esm/loadingr.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -10,6 +11,12 @@ namespace ESM
 
     void Ingredient::load(ESMReader &esm)
     {
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         bool hasData = false;
         while (esm.hasMoreSubs())
         {
@@ -67,6 +74,13 @@ namespace ESM
 
     void Ingredient::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNCString("MODL", mModel);
         esm.writeHNOCString("FNAM", mName);
         esm.writeHNT("IRDT", mData, 56);
@@ -89,5 +103,7 @@ namespace ESM
         mModel.clear();
         mIcon.clear();
         mScript.clear();
+
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadingr.hpp b/components/esm/loadingr.hpp
index 5846a97809..69a6501800 100644
--- a/components/esm/loadingr.hpp
+++ b/components/esm/loadingr.hpp
@@ -31,6 +31,8 @@ struct Ingredient
     IRDTstruct mData;
     std::string mId, mName, mModel, mIcon, mScript;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadlevlist.cpp b/components/esm/loadlevlist.cpp
index ca5c5d74d8..c8cb110a6e 100644
--- a/components/esm/loadlevlist.cpp
+++ b/components/esm/loadlevlist.cpp
@@ -3,12 +3,19 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
 
     void LevelledListBase::load(ESMReader &esm)
     {
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         esm.getHNT(mFlags, "DATA");
         esm.getHNT(mChanceNone, "NNAM");
 
@@ -42,6 +49,13 @@ namespace ESM
     }
     void LevelledListBase::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNT("DATA", mFlags);
         esm.writeHNT("NNAM", mChanceNone);
         esm.writeHNT<int>("INDX", mList.size());
@@ -58,6 +72,7 @@ namespace ESM
         mFlags = 0;
         mChanceNone = 0;
         mList.clear();
+        mIsDeleted = false;
     }
 
     unsigned int CreatureLevList::sRecordId = REC_LEVC;
diff --git a/components/esm/loadlevlist.hpp b/components/esm/loadlevlist.hpp
index dc6fcda5ec..4165275b21 100644
--- a/components/esm/loadlevlist.hpp
+++ b/components/esm/loadlevlist.hpp
@@ -36,6 +36,8 @@ struct LevelledListBase
 
     std::vector<LevelItem> mList;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadligh.cpp b/components/esm/loadligh.cpp
index 26d70d964a..bd91e096ce 100644
--- a/components/esm/loadligh.cpp
+++ b/components/esm/loadligh.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -10,6 +11,12 @@ namespace ESM
 
     void Light::load(ESMReader &esm)
     {
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         bool hasData = false;
         while (esm.hasMoreSubs())
         {
@@ -45,6 +52,13 @@ namespace ESM
     }
     void Light::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNCString("MODL", mModel);
         esm.writeHNOCString("FNAM", mName);
         esm.writeHNOCString("ITEX", mIcon);
@@ -66,5 +80,6 @@ namespace ESM
         mModel.clear();
         mIcon.clear();
         mName.clear();
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadligh.hpp b/components/esm/loadligh.hpp
index ed8c36665c..76274e6f4e 100644
--- a/components/esm/loadligh.hpp
+++ b/components/esm/loadligh.hpp
@@ -47,6 +47,8 @@ struct Light
 
     std::string mSound, mScript, mModel, mIcon, mName, mId;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadlock.cpp b/components/esm/loadlock.cpp
index 2747a6f787..c1e866b585 100644
--- a/components/esm/loadlock.cpp
+++ b/components/esm/loadlock.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -10,6 +11,12 @@ namespace ESM
 
     void Lockpick::load(ESMReader &esm)
     {
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         bool hasData = true;
         while (esm.hasMoreSubs())
         {
@@ -43,6 +50,13 @@ namespace ESM
 
     void Lockpick::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNCString("MODL", mModel);
         esm.writeHNOCString("FNAM", mName);
 
@@ -61,5 +75,6 @@ namespace ESM
         mModel.clear();
         mIcon.clear();
         mScript.clear();
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadlock.hpp b/components/esm/loadlock.hpp
index 0d678cd64c..af18773ed8 100644
--- a/components/esm/loadlock.hpp
+++ b/components/esm/loadlock.hpp
@@ -27,6 +27,8 @@ struct Lockpick
     Data mData;
     std::string mId, mName, mModel, mIcon, mScript;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadmisc.cpp b/components/esm/loadmisc.cpp
index 81c094f2bc..11d2f2a058 100644
--- a/components/esm/loadmisc.cpp
+++ b/components/esm/loadmisc.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -10,6 +11,12 @@ namespace ESM
 
     void Miscellaneous::load(ESMReader &esm)
     {
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         bool hasData = false;
         while (esm.hasMoreSubs())
         {
@@ -41,6 +48,13 @@ namespace ESM
 
     void Miscellaneous::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNCString("MODL", mModel);
         esm.writeHNOCString("FNAM", mName);
         esm.writeHNT("MCDT", mData, 12);
@@ -57,5 +71,6 @@ namespace ESM
         mModel.clear();
         mIcon.clear();
         mScript.clear();
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadmisc.hpp b/components/esm/loadmisc.hpp
index 6e0b4e01b8..5d5e9f66f7 100644
--- a/components/esm/loadmisc.hpp
+++ b/components/esm/loadmisc.hpp
@@ -32,6 +32,8 @@ struct Miscellaneous
 
     std::string mId, mName, mModel, mIcon, mScript;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadnpc.cpp b/components/esm/loadnpc.cpp
index 67a437176c..96957f1e23 100644
--- a/components/esm/loadnpc.cpp
+++ b/components/esm/loadnpc.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -17,6 +18,12 @@ namespace ESM
         mTransport.mList.clear();
         mAiPackage.mList.clear();
 
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         bool hasNpdt = false;
         bool hasFlags = false;
         mHasAI = false;
@@ -103,6 +110,13 @@ namespace ESM
     }
     void NPC::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNOCString("MODL", mModel);
         esm.writeHNOCString("FNAM", mName);
         esm.writeHNCString("RNAM", mRace);
@@ -183,6 +197,7 @@ namespace ESM
         mScript.clear();
         mHair.clear();
         mHead.clear();
+        mIsDeleted = false;
     }
 
     int NPC::getFactionRank() const
diff --git a/components/esm/loadnpc.hpp b/components/esm/loadnpc.hpp
index 9bda9560e1..7b75cb178a 100644
--- a/components/esm/loadnpc.hpp
+++ b/components/esm/loadnpc.hpp
@@ -130,6 +130,8 @@ struct NPC
     // body parts
     std::string mHair, mHead;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadprob.cpp b/components/esm/loadprob.cpp
index c5f80c5844..12f7ad00a6 100644
--- a/components/esm/loadprob.cpp
+++ b/components/esm/loadprob.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -10,6 +11,12 @@ namespace ESM
 
     void Probe::load(ESMReader &esm)
     {
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         bool hasData = true;
         while (esm.hasMoreSubs())
         {
@@ -43,6 +50,13 @@ namespace ESM
 
     void Probe::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNCString("MODL", mModel);
         esm.writeHNOCString("FNAM", mName);
 
@@ -61,5 +75,6 @@ namespace ESM
         mModel.clear();
         mIcon.clear();
         mScript.clear();
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadprob.hpp b/components/esm/loadprob.hpp
index c737757aa3..9daab6b1ba 100644
--- a/components/esm/loadprob.hpp
+++ b/components/esm/loadprob.hpp
@@ -27,6 +27,8 @@ struct Probe
     Data mData;
     std::string mId, mName, mModel, mIcon, mScript;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadrepa.cpp b/components/esm/loadrepa.cpp
index f90f9e39dc..608536f02b 100644
--- a/components/esm/loadrepa.cpp
+++ b/components/esm/loadrepa.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -10,6 +11,12 @@ namespace ESM
 
 void Repair::load(ESMReader &esm)
 {
+    mId = esm.getHNString("NAME");
+    if (mIsDeleted = readDeleSubRecord(esm))
+    {
+        return;
+    }
+
     bool hasData = true;
     while (esm.hasMoreSubs())
     {
@@ -43,6 +50,13 @@ void Repair::load(ESMReader &esm)
 
 void Repair::save(ESMWriter &esm) const
 {
+    esm.writeHNCString("NAME", mId);
+    if (mIsDeleted)
+    {
+        writeDeleSubRecord(esm);
+        return;
+    }
+
     esm.writeHNCString("MODL", mModel);
     esm.writeHNOCString("FNAM", mName);
 
@@ -61,5 +75,6 @@ void Repair::save(ESMWriter &esm) const
         mModel.clear();
         mIcon.clear();
         mScript.clear();
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadrepa.hpp b/components/esm/loadrepa.hpp
index e765bc93a5..a660574be0 100644
--- a/components/esm/loadrepa.hpp
+++ b/components/esm/loadrepa.hpp
@@ -27,6 +27,8 @@ struct Repair
     Data mData;
     std::string mId, mName, mModel, mIcon, mScript;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadsndg.cpp b/components/esm/loadsndg.cpp
index 5ee6f5245c..f92c4eee83 100644
--- a/components/esm/loadsndg.cpp
+++ b/components/esm/loadsndg.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -10,6 +11,12 @@ namespace ESM
 
     void SoundGenerator::load(ESMReader &esm)
     {
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         bool hasData = false;
         while (esm.hasMoreSubs())
         {
@@ -36,6 +43,13 @@ namespace ESM
     }
     void SoundGenerator::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNT("DATA", mType, 4);
         esm.writeHNOCString("CNAM", mCreature);
         esm.writeHNOCString("SNAM", mSound);
@@ -46,5 +60,6 @@ namespace ESM
         mType = LeftFoot;
         mCreature.clear();
         mSound.clear();
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadsndg.hpp b/components/esm/loadsndg.hpp
index 056958f0a8..e486976f51 100644
--- a/components/esm/loadsndg.hpp
+++ b/components/esm/loadsndg.hpp
@@ -36,6 +36,8 @@ struct SoundGenerator
 
     std::string mId, mCreature, mSound;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadsoun.cpp b/components/esm/loadsoun.cpp
index 690c1b4484..19e8a5d804 100644
--- a/components/esm/loadsoun.cpp
+++ b/components/esm/loadsoun.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -10,6 +11,12 @@ namespace ESM
 
     void Sound::load(ESMReader &esm)
     {
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         bool hasData = false;
         while (esm.hasMoreSubs())
         {
@@ -34,6 +41,13 @@ namespace ESM
 
     void Sound::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNOCString("FNAM", mSound);
         esm.writeHNT("DATA", mData, 3);
     }
@@ -45,5 +59,7 @@ namespace ESM
         mData.mVolume = 128;
         mData.mMinRange = 0;
         mData.mMaxRange = 255;
+        
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadsoun.hpp b/components/esm/loadsoun.hpp
index ff2202ca7d..95c89b29d3 100644
--- a/components/esm/loadsoun.hpp
+++ b/components/esm/loadsoun.hpp
@@ -23,6 +23,8 @@ struct Sound
     SOUNstruct mData;
     std::string mId, mSound;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadspel.cpp b/components/esm/loadspel.cpp
index 96c048e0a9..cac06b2001 100644
--- a/components/esm/loadspel.cpp
+++ b/components/esm/loadspel.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -11,6 +12,13 @@ namespace ESM
     void Spell::load(ESMReader &esm)
     {
         mEffects.mList.clear();
+
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         bool hasData = false;
         while (esm.hasMoreSubs())
         {
@@ -39,6 +47,13 @@ namespace ESM
 
     void Spell::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNOCString("FNAM", mName);
         esm.writeHNT("SPDT", mData, 12);
         mEffects.save(esm);
@@ -53,5 +68,7 @@ namespace ESM
         mName.clear();
 
         mEffects.mList.clear();
+
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadspel.hpp b/components/esm/loadspel.hpp
index 491da1d179..2aba131add 100644
--- a/components/esm/loadspel.hpp
+++ b/components/esm/loadspel.hpp
@@ -45,6 +45,8 @@ struct Spell
     std::string mId, mName;
     EffectList mEffects;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/loadstat.cpp b/components/esm/loadstat.cpp
index ed90b04751..a49caba897 100644
--- a/components/esm/loadstat.cpp
+++ b/components/esm/loadstat.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -10,17 +11,29 @@ namespace ESM
 
     void Static::load(ESMReader &esm)
     {
-        mPersistent = (esm.getRecordFlags() & 0x0400) != 0;
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
 
         mModel = esm.getHNString("MODL");
     }
     void Static::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNCString("MODL", mModel);
     }
 
     void Static::blank()
     {
         mModel.clear();
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadstat.hpp b/components/esm/loadstat.hpp
index 21a9e66e88..c4306ad8fd 100644
--- a/components/esm/loadstat.hpp
+++ b/components/esm/loadstat.hpp
@@ -28,7 +28,7 @@ struct Static
 
   std::string mId, mModel;
 
-  bool mPersistent;
+  bool mIsDeleted;
 
   void load(ESMReader &esm);
   void save(ESMWriter &esm) const;
diff --git a/components/esm/loadweap.cpp b/components/esm/loadweap.cpp
index 981a5815a2..66d65d0f51 100644
--- a/components/esm/loadweap.cpp
+++ b/components/esm/loadweap.cpp
@@ -3,6 +3,7 @@
 #include "esmreader.hpp"
 #include "esmwriter.hpp"
 #include "defs.hpp"
+#include "util.hpp"
 
 namespace ESM
 {
@@ -10,6 +11,12 @@ namespace ESM
 
     void Weapon::load(ESMReader &esm)
     {
+        mId = esm.getHNString("NAME");
+        if (mIsDeleted = readDeleSubRecord(esm))
+        {
+            return;
+        }
+
         bool hasData = false;
         while (esm.hasMoreSubs())
         {
@@ -45,6 +52,13 @@ namespace ESM
     }
     void Weapon::save(ESMWriter &esm) const
     {
+        esm.writeHNCString("NAME", mId);
+        if (mIsDeleted)
+        {
+            writeDeleSubRecord(esm);
+            return;
+        }
+
         esm.writeHNCString("MODL", mModel);
         esm.writeHNOCString("FNAM", mName);
         esm.writeHNT("WPDT", mData, 32);
@@ -72,5 +86,7 @@ namespace ESM
         mIcon.clear();
         mEnchant.clear();
         mScript.clear();
+
+        mIsDeleted = false;
     }
 }
diff --git a/components/esm/loadweap.hpp b/components/esm/loadweap.hpp
index f66e9f3a68..30dfbc92af 100644
--- a/components/esm/loadweap.hpp
+++ b/components/esm/loadweap.hpp
@@ -69,6 +69,8 @@ struct Weapon
 
     std::string mId, mName, mModel, mIcon, mEnchant, mScript;
 
+    bool mIsDeleted;
+
     void load(ESMReader &esm);
     void save(ESMWriter &esm) const;
 
diff --git a/components/esm/util.cpp b/components/esm/util.cpp
new file mode 100644
index 0000000000..c20bd4d518
--- /dev/null
+++ b/components/esm/util.cpp
@@ -0,0 +1,20 @@
+#include "util.hpp"
+
+namespace ESM
+{
+    bool readDeleSubRecord(ESMReader &esm)
+    {
+        if (esm.isNextSub("DELE"))
+        {
+            esm.getSubName();
+            esm.skipHSub();
+            return true;
+        }
+        return false;
+    }
+
+    void writeDeleSubRecord(ESMWriter &esm)
+    {
+        esm.writeHNString("DELE", "");
+    }
+}
diff --git a/components/esm/util.hpp b/components/esm/util.hpp
index bb7f3cf7cd..61cb138923 100644
--- a/components/esm/util.hpp
+++ b/components/esm/util.hpp
@@ -1,9 +1,15 @@
 #ifndef OPENMW_ESM_UTIL_H
 #define OPENMW_ESM_UTIL_H
 
+#include <string>
+
 #include <OgreVector3.h>
 #include <OgreQuaternion.h>
 
+#include "esmreader.hpp"
+#include "esmwriter.hpp"
+#include "loadbsgn.hpp"
+
 namespace ESM
 {
 
@@ -46,6 +52,9 @@ struct Vector3
     }
 };
 
+bool readDeleSubRecord(ESMReader &esm);
+void writeDeleSubRecord(ESMWriter &esm);
+
 }
 
 #endif