Don't rely on subrecord order part 2

Nice side effects:
 - Subrecord name comparison now uses magic number instead of string (faster)
 - Improves the error message for unknown subrecords: will print the record in question instead of failing to read the next record with a strange error
celladd
scrawl 10 years ago
parent 75db4d6473
commit 49d912e5b6

@ -41,29 +41,6 @@ namespace ESM
} }
void AIPackageList::load(ESMReader &esm)
{
mList.clear();
while (esm.hasMoreSubs()) {
// initialize every iteration
esm.getSubName();
switch (esm.retSubName().val)
{
case AI_Wander:
case AI_Activate:
case AI_Escort:
case AI_Follow:
case AI_Travel:
case AI_CNDT:
add(esm);
break;
default:
return;
}
}
}
void AIPackageList::save(ESMWriter &esm) const void AIPackageList::save(ESMWriter &esm) const
{ {
typedef std::vector<AIPackage>::const_iterator PackageIter; typedef std::vector<AIPackage>::const_iterator PackageIter;

@ -94,9 +94,6 @@ namespace ESM
/// Add a single AIPackage, assumes subrecord name was already read /// Add a single AIPackage, assumes subrecord name was already read
void add(ESMReader &esm); void add(ESMReader &esm);
/// TODO: remove this method. The ESM format does not guarantee that all AI packages follow one another
void load(ESMReader &esm);
void save(ESMWriter &esm) const; void save(ESMWriter &esm) const;
}; };
} }

@ -8,11 +8,16 @@ namespace ESM {
void EffectList::load(ESMReader &esm) void EffectList::load(ESMReader &esm)
{ {
mList.clear(); mList.clear();
ENAMstruct s;
while (esm.isNextSub("ENAM")) { while (esm.isNextSub("ENAM")) {
add(esm);
}
}
void EffectList::add(ESMReader &esm)
{
ENAMstruct s;
esm.getHT(s, 24); esm.getHT(s, 24);
mList.push_back(s); mList.push_back(s);
}
} }
void EffectList::save(ESMWriter &esm) const void EffectList::save(ESMWriter &esm) const

@ -29,11 +29,15 @@ namespace ESM
}; };
#pragma pack(pop) #pragma pack(pop)
/// EffectList, ENAM subrecord
struct EffectList struct EffectList
{ {
std::vector<ENAMstruct> mList; std::vector<ENAMstruct> mList;
/// Load one effect, assumes subrecord name was already read
void add(ESMReader &esm);
/// Load all effects
void load(ESMReader &esm); void load(ESMReader &esm);
void save(ESMWriter &esm) const; void save(ESMWriter &esm) const;
}; };

@ -8,18 +8,34 @@ namespace ESM
{ {
unsigned int Activator::sRecordId = REC_ACTI; unsigned int Activator::sRecordId = REC_ACTI;
void Activator::load(ESMReader &esm) void Activator::load(ESMReader &esm)
{ {
mModel = esm.getHNString("MODL"); while (esm.hasMoreSubs())
mName = esm.getHNOString("FNAM"); {
mScript = esm.getHNOString("SCRI"); esm.getSubName();
} uint32_t name = esm.retSubName().val;
void Activator::save(ESMWriter &esm) const switch (name)
{ {
case ESM::FourCC<'M','O','D','L'>::value:
mModel = esm.getHString();
break;
case ESM::FourCC<'F','N','A','M'>::value:
mName = esm.getHString();
break;
case ESM::FourCC<'S','C','R','I'>::value:
mScript = esm.getHString();
break;
default:
esm.fail("Unknown subrecord");
}
}
}
void Activator::save(ESMWriter &esm) const
{
esm.writeHNCString("MODL", mModel); esm.writeHNCString("MODL", mModel);
esm.writeHNOCString("FNAM", mName); esm.writeHNOCString("FNAM", mName);
esm.writeHNOCString("SCRI", mScript); esm.writeHNOCString("SCRI", mScript);
} }
void Activator::blank() void Activator::blank()
{ {

@ -8,24 +8,51 @@ namespace ESM
{ {
unsigned int Potion::sRecordId = REC_ALCH; unsigned int Potion::sRecordId = REC_ALCH;
void Potion::load(ESMReader &esm) void Potion::load(ESMReader &esm)
{ {
mModel = esm.getHNOString("MODL"); mEffects.mList.clear();
mIcon = esm.getHNOString("TEXT"); // not ITEX here for some reason bool hasData = false;
mScript = esm.getHNOString("SCRI"); while (esm.hasMoreSubs())
mName = esm.getHNOString("FNAM"); {
esm.getHNT(mData, "ALDT", 12); esm.getSubName();
mEffects.load(esm); uint32_t name = esm.retSubName().val;
} switch (name)
void Potion::save(ESMWriter &esm) const {
{ case ESM::FourCC<'M','O','D','L'>::value:
mModel = esm.getHString();
break;
case ESM::FourCC<'T','E','X','T'>::value: // not ITEX here for some reason
mIcon = esm.getHString();
break;
case ESM::FourCC<'S','C','R','I'>::value:
mScript = esm.getHString();
break;
case ESM::FourCC<'F','N','A','M'>::value:
mName = esm.getHString();
break;
case ESM::FourCC<'A','L','D','T'>::value:
esm.getHT(mData, 12);
hasData = true;
break;
case ESM::FourCC<'E','N','A','M'>::value:
mEffects.add(esm);
break;
default:
esm.fail("Unknown subrecord");
}
}
if (!hasData)
esm.fail("Missing ALDT");
}
void Potion::save(ESMWriter &esm) const
{
esm.writeHNCString("MODL", mModel); esm.writeHNCString("MODL", mModel);
esm.writeHNOCString("TEXT", mIcon); esm.writeHNOCString("TEXT", mIcon);
esm.writeHNOCString("SCRI", mScript); esm.writeHNOCString("SCRI", mScript);
esm.writeHNOCString("FNAM", mName); esm.writeHNOCString("FNAM", mName);
esm.writeHNT("ALDT", mData, 12); esm.writeHNT("ALDT", mData, 12);
mEffects.save(esm); mEffects.save(esm);
} }
void Potion::blank() void Potion::blank()
{ {

@ -10,25 +10,35 @@ namespace ESM
void Apparatus::load(ESMReader &esm) void Apparatus::load(ESMReader &esm)
{ {
// we will not treat duplicated subrecords as errors here bool hasData = false;
while (esm.hasMoreSubs()) while (esm.hasMoreSubs())
{ {
esm.getSubName(); esm.getSubName();
NAME subName = esm.retSubName(); uint32_t name = esm.retSubName().val;
switch (name)
if (subName == "MODL") {
case ESM::FourCC<'M','O','D','L'>::value:
mModel = esm.getHString(); mModel = esm.getHString();
else if (subName == "FNAM") break;
case ESM::FourCC<'F','N','A','M'>::value:
mName = esm.getHString(); mName = esm.getHString();
else if (subName == "AADT") break;
case ESM::FourCC<'A','A','D','T'>::value:
esm.getHT(mData); esm.getHT(mData);
else if (subName == "SCRI") hasData = true;
break;
case ESM::FourCC<'S','C','R','I'>::value:
mScript = esm.getHString(); mScript = esm.getHString();
else if (subName == "ITEX") break;
case ESM::FourCC<'I','T','E','X'>::value:
mIcon = esm.getHString(); mIcon = esm.getHString();
else break;
esm.fail("wrong subrecord type " + subName.toString() + " for APPA record"); default:
esm.fail("Unknown subrecord");
}
} }
if (!hasData)
esm.fail("Missing AADT");
} }
void Apparatus::save(ESMWriter &esm) const void Apparatus::save(ESMWriter &esm) const

@ -11,9 +11,30 @@ namespace ESM
void BodyPart::load(ESMReader &esm) void BodyPart::load(ESMReader &esm)
{ {
mModel = esm.getHNString("MODL"); bool hasData = false;
mRace = esm.getHNOString("FNAM"); while (esm.hasMoreSubs())
esm.getHNT(mData, "BYDT", 4); {
esm.getSubName();
uint32_t name = esm.retSubName().val;
switch (name)
{
case ESM::FourCC<'M','O','D','L'>::value:
mModel = esm.getHString();
break;
case ESM::FourCC<'F','N','A','M'>::value:
mRace = esm.getHString();
break;
case ESM::FourCC<'B','Y','D','T'>::value:
esm.getHT(mData, 4);
hasData = true;
break;
default:
esm.fail("Unknown subrecord");
}
}
if (!hasData)
esm.fail("Missing BYDT subrecord");
} }
void BodyPart::save(ESMWriter &esm) const void BodyPart::save(ESMWriter &esm) const
{ {

@ -10,11 +10,29 @@ namespace ESM
void BirthSign::load(ESMReader &esm) void BirthSign::load(ESMReader &esm)
{ {
mName = esm.getHNOString("FNAM"); mPowers.mList.clear();
mTexture = esm.getHNOString("TNAM"); while (esm.hasMoreSubs())
mDescription = esm.getHNOString("DESC"); {
esm.getSubName();
mPowers.load(esm); uint32_t name = esm.retSubName().val;
switch (name)
{
case ESM::FourCC<'F','N','A','M'>::value:
mName = esm.getHString();
break;
case ESM::FourCC<'T','N','A','M'>::value:
mTexture = esm.getHString();
break;
case ESM::FourCC<'D','E','S','C'>::value:
mDescription = esm.getHString();
break;
case ESM::FourCC<'N','P','C','S'>::value:
mPowers.add(esm);
break;
default:
esm.fail("Unknown subrecord");
}
}
} }
void BirthSign::save(ESMWriter &esm) const void BirthSign::save(ESMWriter &esm) const

@ -10,17 +10,17 @@ namespace ESM
{ {
unsigned int Class::sRecordId = REC_CLAS; unsigned int Class::sRecordId = REC_CLAS;
const Class::Specialization Class::sSpecializationIds[3] = { const Class::Specialization Class::sSpecializationIds[3] = {
Class::Combat, Class::Combat,
Class::Magic, Class::Magic,
Class::Stealth Class::Stealth
}; };
const char *Class::sGmstSpecializationIds[3] = { const char *Class::sGmstSpecializationIds[3] = {
"sSpecializationCombat", "sSpecializationCombat",
"sSpecializationMagic", "sSpecializationMagic",
"sSpecializationStealth" "sSpecializationStealth"
}; };
int& Class::CLDTstruct::getSkill (int index, bool major) int& Class::CLDTstruct::getSkill (int index, bool major)
@ -39,22 +39,40 @@ const char *Class::sGmstSpecializationIds[3] = {
return mSkills[index][major ? 1 : 0]; return mSkills[index][major ? 1 : 0];
} }
void Class::load(ESMReader &esm) void Class::load(ESMReader &esm)
{ {
mName = esm.getHNOString("FNAM"); bool hasData = false;
esm.getHNT(mData, "CLDT", 60); while (esm.hasMoreSubs())
{
esm.getSubName();
uint32_t name = esm.retSubName().val;
switch (name)
{
case ESM::FourCC<'F','N','A','M'>::value:
mName = esm.getHString();
break;
case ESM::FourCC<'C','L','D','T'>::value:
esm.getHT(mData, 60);
if (mData.mIsPlayable > 1) if (mData.mIsPlayable > 1)
esm.fail("Unknown bool value"); esm.fail("Unknown bool value");
hasData = true;
mDescription = esm.getHNOString("DESC"); break;
} case ESM::FourCC<'D','E','S','C'>::value:
void Class::save(ESMWriter &esm) const mDescription = esm.getHString();
{ break;
default:
esm.fail("Unknown subrecord");
}
}
if (!hasData)
esm.fail("Missing CLDT subrecord");
}
void Class::save(ESMWriter &esm) const
{
esm.writeHNOCString("FNAM", mName); esm.writeHNOCString("FNAM", mName);
esm.writeHNT("CLDT", mData, 60); esm.writeHNT("CLDT", mData, 60);
esm.writeHNOString("DESC", mDescription); esm.writeHNOString("DESC", mDescription);
} }
void Class::blank() void Class::blank()
{ {

@ -7,51 +7,70 @@
namespace ESM namespace ESM
{ {
void InventoryList::add(ESMReader &esm) void InventoryList::add(ESMReader &esm)
{ {
ContItem ci; ContItem ci;
esm.getHT(ci, 36); esm.getHT(ci, 36);
mList.push_back(ci); mList.push_back(ci);
}
void InventoryList::load(ESMReader &esm)
{
mList.clear();
while (esm.isNextSub("NPCO"))
{
add(esm);
} }
}
void InventoryList::save(ESMWriter &esm) const void InventoryList::save(ESMWriter &esm) const
{ {
for (std::vector<ContItem>::const_iterator it = mList.begin(); it != mList.end(); ++it) for (std::vector<ContItem>::const_iterator it = mList.begin(); it != mList.end(); ++it)
{ {
esm.writeHNT("NPCO", *it, 36); esm.writeHNT("NPCO", *it, 36);
} }
} }
unsigned int Container::sRecordId = REC_CONT; unsigned int Container::sRecordId = REC_CONT;
void Container::load(ESMReader &esm) void Container::load(ESMReader &esm)
{ {
mModel = esm.getHNString("MODL"); mInventory.mList.clear();
mName = esm.getHNOString("FNAM"); bool hasWeight = false;
esm.getHNT(mWeight, "CNDT", 4); bool hasFlags = false;
esm.getHNT(mFlags, "FLAG", 4); while (esm.hasMoreSubs())
{
esm.getSubName();
uint32_t name = esm.retSubName().val;
switch (name)
{
case ESM::FourCC<'M','O','D','L'>::value:
mModel = esm.getHString();
break;
case ESM::FourCC<'F','N','A','M'>::value:
mName = esm.getHString();
break;
case ESM::FourCC<'C','N','D','T'>::value:
esm.getHT(mWeight, 4);
hasWeight = true;
break;
case ESM::FourCC<'F','L','A','G'>::value:
esm.getHT(mFlags, 4);
if (mFlags & 0xf4) if (mFlags & 0xf4)
esm.fail("Unknown flags"); esm.fail("Unknown flags");
if (!(mFlags & 0x8)) if (!(mFlags & 0x8))
esm.fail("Flag 8 not set"); esm.fail("Flag 8 not set");
hasFlags = true;
break;
case ESM::FourCC<'S','C','R','I'>::value:
mScript = esm.getHString();
break;
case ESM::FourCC<'N','P','C','O'>::value:
mInventory.add(esm);
break;
default:
esm.fail("Unknown subrecord");
}
}
if (!hasWeight)
esm.fail("Missing CNDT subrecord");
if (!hasFlags)
esm.fail("Missing FLAG subrecord");
}
mScript = esm.getHNOString("SCRI"); void Container::save(ESMWriter &esm) const
{
mInventory.load(esm);
}
void Container::save(ESMWriter &esm) const
{
esm.writeHNCString("MODL", mModel); esm.writeHNCString("MODL", mModel);
esm.writeHNOCString("FNAM", mName); esm.writeHNOCString("FNAM", mName);
esm.writeHNT("CNDT", mWeight, 4); esm.writeHNT("CNDT", mWeight, 4);
@ -60,7 +79,7 @@ void Container::save(ESMWriter &esm) const
esm.writeHNOCString("SCRI", mScript); esm.writeHNOCString("SCRI", mScript);
mInventory.save(esm); mInventory.save(esm);
} }
void Container::blank() void Container::blank()
{ {

@ -22,6 +22,7 @@ struct ContItem
NAME32 mItem; NAME32 mItem;
}; };
/// InventoryList, NPCO subrecord
struct InventoryList struct InventoryList
{ {
std::vector<ContItem> mList; std::vector<ContItem> mList;
@ -29,8 +30,6 @@ struct InventoryList
/// Load one item, assumes subrecord name is already read /// Load one item, assumes subrecord name is already read
void add(ESMReader &esm); void add(ESMReader &esm);
/// TODO: remove this method, the ESM format doesn't guarantee that all ContItems follow one another
void load(ESMReader &esm);
void save(ESMWriter &esm) const; void save(ESMWriter &esm) const;
}; };

@ -8,38 +8,77 @@ namespace ESM {
unsigned int Creature::sRecordId = REC_CREA; unsigned int Creature::sRecordId = REC_CREA;
void Creature::load(ESMReader &esm) void Creature::load(ESMReader &esm)
{ {
mPersistent = esm.getRecordFlags() & 0x0400; mPersistent = esm.getRecordFlags() & 0x0400;
mModel = esm.getHNString("MODL"); mAiPackage.mList.clear();
mOriginal = esm.getHNOString("CNAM"); mInventory.mList.clear();
mName = esm.getHNOString("FNAM"); mSpells.mList.clear();
mScript = esm.getHNOString("SCRI");
esm.getHNT(mData, "NPDT", 96);
esm.getHNT(mFlags, "FLAG");
mScale = 1.0;
esm.getHNOT(mScale, "XSCL");
mInventory.load(esm);
mSpells.load(esm);
if (esm.isNextSub("AIDT")) mScale = 1.f;
mHasAI = false;
bool hasNpdt = false;
bool hasFlags = false;
while (esm.hasMoreSubs())
{
esm.getSubName();
uint32_t name = esm.retSubName().val;
switch (name)
{ {
case ESM::FourCC<'M','O','D','L'>::value:
mModel = esm.getHString();
break;
case ESM::FourCC<'C','N','A','M'>::value:
mOriginal = esm.getHString();
break;
case ESM::FourCC<'F','N','A','M'>::value:
mName = esm.getHString();
break;
case ESM::FourCC<'S','C','R','I'>::value:
mScript = esm.getHString();
break;
case ESM::FourCC<'N','P','D','T'>::value:
esm.getHT(mData, 96);
hasNpdt = true;
break;
case ESM::FourCC<'F','L','A','G'>::value:
esm.getHT(mFlags);
hasFlags = true;
break;
case ESM::FourCC<'X','S','C','L'>::value:
esm.getHT(mScale);
break;
case ESM::FourCC<'N','P','C','O'>::value:
mInventory.add(esm);
break;
case ESM::FourCC<'N','P','C','S'>::value:
mSpells.add(esm);
break;
case ESM::FourCC<'A','I','D','T'>::value:
esm.getHExact(&mAiData, sizeof(mAiData)); esm.getHExact(&mAiData, sizeof(mAiData));
mHasAI = true; mHasAI = true;
break;
case AI_Wander:
case AI_Activate:
case AI_Escort:
case AI_Follow:
case AI_Travel:
case AI_CNDT:
mAiPackage.add(esm);
break;
default:
esm.fail("Unknown subrecord");
}
}
if (!hasNpdt)
esm.fail("Missing NPDT subrecord");
if (!hasFlags)
esm.fail("Missing FLAG subrecord");
} }
else
mHasAI = false;
mAiPackage.load(esm);
esm.skipRecord();
}
void Creature::save(ESMWriter &esm) const void Creature::save(ESMWriter &esm) const
{ {
esm.writeHNCString("MODL", mModel); esm.writeHNCString("MODL", mModel);
esm.writeHNOCString("CNAM", mOriginal); esm.writeHNOCString("CNAM", mOriginal);
esm.writeHNOCString("FNAM", mName); esm.writeHNOCString("FNAM", mName);
@ -56,7 +95,7 @@ void Creature::save(ESMWriter &esm) const
esm.writeHNT("AIDT", mAiData, sizeof(mAiData)); esm.writeHNT("AIDT", mAiData, sizeof(mAiData));
} }
mAiPackage.save(esm); mAiPackage.save(esm);
} }
void Creature::blank() void Creature::blank()
{ {

@ -10,8 +10,28 @@ namespace ESM
void Enchantment::load(ESMReader &esm) void Enchantment::load(ESMReader &esm)
{ {
esm.getHNT(mData, "ENDT", 16); mEffects.mList.clear();
mEffects.load(esm); bool hasData = false;
while (esm.hasMoreSubs())
{
esm.getSubName();
uint32_t name = esm.retSubName().val;
switch (name)
{
case ESM::FourCC<'E','N','D','T'>::value:
esm.getHT(mData, 16);
hasData = true;
break;
case ESM::FourCC<'E','N','A','M'>::value:
mEffects.add(esm);
break;
default:
esm.fail("Unknown subrecord");
break;
}
}
if (!hasData)
esm.fail("Missing ENDT subrecord");
} }
void Enchantment::save(ESMWriter &esm) const void Enchantment::save(ESMWriter &esm) const

@ -28,27 +28,46 @@ namespace ESM
void Faction::load(ESMReader &esm) void Faction::load(ESMReader &esm)
{ {
mName = esm.getHNOString("FNAM"); mReactions.clear();
for (int i=0;i<10;++i)
// Read rank names. These are optional. mRanks[i].clear();
int i = 0;
while (esm.isNextSub("RNAM") && i < 10)
mRanks[i++] = esm.getHString();
// Main data struct
esm.getHNT(mData, "FADT", 240);
int rankCounter=0;
bool hasData = false;
while (esm.hasMoreSubs())
{
esm.getSubName();
uint32_t name = esm.retSubName().val;
switch (name)
{
case ESM::FourCC<'F','N','A','M'>::value:
mName = esm.getHString();
break;
case ESM::FourCC<'R','N','A','M'>::value:
if (rankCounter >= 10)
esm.fail("Rank out of range");
mRanks[rankCounter++] = esm.getHString();
break;
case ESM::FourCC<'F','A','D','T'>::value:
esm.getHT(mData, 240);
if (mData.mIsHidden > 1) if (mData.mIsHidden > 1)
esm.fail("Unknown flag!"); esm.fail("Unknown flag!");
hasData = true;
// Read faction response values break;
while (esm.hasMoreSubs()) case ESM::FourCC<'A','N','A','M'>::value:
{ {
std::string faction = esm.getHNString("ANAM"); std::string faction = esm.getHString();
int reaction; int reaction;
esm.getHNT(reaction, "INTV"); esm.getHNT(reaction, "INTV");
mReactions[faction] = reaction; mReactions[faction] = reaction;
break;
}
default:
esm.fail("Unknown subrecord");
}
} }
if (!hasData)
esm.fail("Missing FADT subrecord");
} }
void Faction::save(ESMWriter &esm) const void Faction::save(ESMWriter &esm) const
{ {

@ -8,13 +8,39 @@ namespace ESM
{ {
unsigned int Ingredient::sRecordId = REC_INGR; unsigned int Ingredient::sRecordId = REC_INGR;
void Ingredient::load(ESMReader &esm) void Ingredient::load(ESMReader &esm)
{ {
mModel = esm.getHNString("MODL"); bool hasData = false;
mName = esm.getHNOString("FNAM"); while (esm.hasMoreSubs())
esm.getHNT(mData, "IRDT", 56); {
mScript = esm.getHNOString("SCRI"); esm.getSubName();
mIcon = esm.getHNOString("ITEX"); uint32_t name = esm.retSubName().val;
switch (name)
{
case ESM::FourCC<'M','O','D','L'>::value:
mModel = esm.getHString();
break;
case ESM::FourCC<'F','N','A','M'>::value:
mName = esm.getHString();
break;
case ESM::FourCC<'I','R','D','T'>::value:
esm.getHT(mData, 56);
hasData = true;
break;
case ESM::FourCC<'S','C','R','I'>::value:
mScript = esm.getHString();
break;
case ESM::FourCC<'I','T','E','X'>::value:
mIcon = esm.getHString();
break;
default:
esm.fail("Unknown subrecord");
}
}
if (!hasData)
esm.fail("Missing IRDT subrecord");
// horrible hack to fix broken data in records // horrible hack to fix broken data in records
for (int i=0; i<4; ++i) for (int i=0; i<4; ++i)
{ {
@ -37,16 +63,16 @@ void Ingredient::load(ESMReader &esm)
mData.mSkills[i] = -1; mData.mSkills[i] = -1;
} }
} }
} }
void Ingredient::save(ESMWriter &esm) const void Ingredient::save(ESMWriter &esm) const
{ {
esm.writeHNCString("MODL", mModel); esm.writeHNCString("MODL", mModel);
esm.writeHNOCString("FNAM", mName); esm.writeHNOCString("FNAM", mName);
esm.writeHNT("IRDT", mData, 56); esm.writeHNT("IRDT", mData, 56);
esm.writeHNOCString("SCRI", mScript); esm.writeHNOCString("SCRI", mScript);
esm.writeHNOCString("ITEX", mIcon); esm.writeHNOCString("ITEX", mIcon);
} }
void Ingredient::blank() void Ingredient::blank()
{ {

@ -7,8 +7,8 @@
namespace ESM namespace ESM
{ {
void LevelledListBase::load(ESMReader &esm) void LevelledListBase::load(ESMReader &esm)
{ {
esm.getHNT(mFlags, "DATA"); esm.getHNT(mFlags, "DATA");
esm.getHNT(mChanceNone, "NNAM"); esm.getHNT(mChanceNone, "NNAM");
@ -20,6 +20,7 @@ void LevelledListBase::load(ESMReader &esm)
} }
else else
{ {
// Original engine ignores rest of the record, even if there are items following
esm.skipRecord(); esm.skipRecord();
return; return;
} }
@ -37,9 +38,9 @@ void LevelledListBase::load(ESMReader &esm)
li.mId = esm.getHNString(mRecName); li.mId = esm.getHNString(mRecName);
esm.getHNT(li.mLevel, "INTV"); esm.getHNT(li.mLevel, "INTV");
} }
} }
void LevelledListBase::save(ESMWriter &esm) const void LevelledListBase::save(ESMWriter &esm) const
{ {
esm.writeHNT("DATA", mFlags); esm.writeHNT("DATA", mFlags);
esm.writeHNT("NNAM", mChanceNone); esm.writeHNT("NNAM", mChanceNone);
esm.writeHNT<int>("INDX", mList.size()); esm.writeHNT<int>("INDX", mList.size());
@ -49,7 +50,7 @@ void LevelledListBase::save(ESMWriter &esm) const
esm.writeHNCString(mRecName, it->mId); esm.writeHNCString(mRecName, it->mId);
esm.writeHNT("INTV", it->mLevel); esm.writeHNT("INTV", it->mLevel);
} }
} }
void LevelledListBase::blank() void LevelledListBase::blank()
{ {

@ -8,25 +8,50 @@ namespace ESM
{ {
unsigned int Light::sRecordId = REC_LIGH; unsigned int Light::sRecordId = REC_LIGH;
void Light::load(ESMReader &esm) void Light::load(ESMReader &esm)
{ {
mModel = esm.getHNOString("MODL"); bool hasData = false;
mName = esm.getHNOString("FNAM"); while (esm.hasMoreSubs())
mIcon = esm.getHNOString("ITEX"); {
assert(sizeof(mData) == 24); esm.getSubName();
esm.getHNT(mData, "LHDT", 24); uint32_t name = esm.retSubName().val;
mScript = esm.getHNOString("SCRI"); switch (name)
mSound = esm.getHNOString("SNAM"); {
} case ESM::FourCC<'M','O','D','L'>::value:
void Light::save(ESMWriter &esm) const mModel = esm.getHString();
{ break;
case ESM::FourCC<'F','N','A','M'>::value:
mName = esm.getHString();
break;
case ESM::FourCC<'I','T','E','X'>::value:
mIcon = esm.getHString();
break;
case ESM::FourCC<'L','H','D','T'>::value:
esm.getHT(mData, 24);
hasData = true;
break;
case ESM::FourCC<'S','C','R','I'>::value:
mScript = esm.getHString();
break;
case ESM::FourCC<'S','N','A','M'>::value:
mSound = esm.getHString();
break;
default:
esm.fail("Unknown subrecord");
}
}
if (!hasData)
esm.fail("Missing LHDT subrecord");
}
void Light::save(ESMWriter &esm) const
{
esm.writeHNCString("MODL", mModel); esm.writeHNCString("MODL", mModel);
esm.writeHNOCString("FNAM", mName); esm.writeHNOCString("FNAM", mName);
esm.writeHNOCString("ITEX", mIcon); esm.writeHNOCString("ITEX", mIcon);
esm.writeHNT("LHDT", mData, 24); esm.writeHNT("LHDT", mData, 24);
esm.writeHNOCString("SCRI", mScript); esm.writeHNOCString("SCRI", mScript);
esm.writeHNOCString("SNAM", mSound); esm.writeHNOCString("SNAM", mSound);
} }
void Light::blank() void Light::blank()
{ {

@ -8,26 +8,48 @@ namespace ESM
{ {
unsigned int Lockpick::sRecordId = REC_LOCK; unsigned int Lockpick::sRecordId = REC_LOCK;
void Lockpick::load(ESMReader &esm) void Lockpick::load(ESMReader &esm)
{ {
mModel = esm.getHNString("MODL"); bool hasData = true;
mName = esm.getHNOString("FNAM"); while (esm.hasMoreSubs())
{
esm.getHNT(mData, "LKDT", 16); esm.getSubName();
uint32_t name = esm.retSubName().val;
mScript = esm.getHNOString("SCRI"); switch (name)
mIcon = esm.getHNOString("ITEX"); {
} case ESM::FourCC<'M','O','D','L'>::value:
mModel = esm.getHString();
break;
case ESM::FourCC<'F','N','A','M'>::value:
mName = esm.getHString();
break;
case ESM::FourCC<'L','K','D','T'>::value:
esm.getHT(mData, 16);
hasData = true;
break;
case ESM::FourCC<'S','C','R','I'>::value:
mScript = esm.getHString();
break;
case ESM::FourCC<'I','T','E','X'>::value:
mIcon = esm.getHString();
break;
default:
esm.fail("Unknown subrecord");
}
}
if (!hasData)
esm.fail("Missing LKDT subrecord");
}
void Lockpick::save(ESMWriter &esm) const void Lockpick::save(ESMWriter &esm) const
{ {
esm.writeHNCString("MODL", mModel); esm.writeHNCString("MODL", mModel);
esm.writeHNOCString("FNAM", mName); esm.writeHNOCString("FNAM", mName);
esm.writeHNT("LKDT", mData, 16); esm.writeHNT("LKDT", mData, 16);
esm.writeHNOString("SCRI", mScript); esm.writeHNOString("SCRI", mScript);
esm.writeHNOCString("ITEX", mIcon); esm.writeHNOCString("ITEX", mIcon);
} }
void Lockpick::blank() void Lockpick::blank()
{ {

@ -246,7 +246,7 @@ void MagicEffect::load(ESMReader &esm)
mDescription = esm.getHString(); mDescription = esm.getHString();
break; break;
default: default:
esm.fail("Unknown subrecord " + esm.retSubName().toString()); esm.fail("Unknown subrecord");
} }
} }
} }

@ -57,9 +57,9 @@ struct MagicEffect
// Glow color for enchanted items with this effect // Glow color for enchanted items with this effect
int mRed, mGreen, mBlue; int mRed, mGreen, mBlue;
float mUnknown1; float mUnknown1; // Called "Size X" in CS
float mSpeed; // Speed of fired projectile float mSpeed; // Speed of fired projectile
float mUnknown2; float mUnknown2; // Called "Size Cap" in CS
}; // 36 bytes }; // 36 bytes
static const std::map<short,std::string> sNames; static const std::map<short,std::string> sNames;

@ -99,7 +99,7 @@ namespace ESM
mAiPackage.add(esm); mAiPackage.add(esm);
break; break;
default: default:
esm.fail("Unknown subrecord " + esm.retSubName().toString()); esm.fail("Unknown subrecord");
} }
} }
if (!hasNpdt) if (!hasNpdt)

@ -8,26 +8,48 @@ namespace ESM
{ {
unsigned int Probe::sRecordId = REC_PROB; unsigned int Probe::sRecordId = REC_PROB;
void Probe::load(ESMReader &esm) void Probe::load(ESMReader &esm)
{ {
mModel = esm.getHNString("MODL"); bool hasData = true;
mName = esm.getHNOString("FNAM"); while (esm.hasMoreSubs())
{
esm.getHNT(mData, "PBDT", 16); esm.getSubName();
uint32_t name = esm.retSubName().val;
mScript = esm.getHNOString("SCRI"); switch (name)
mIcon = esm.getHNOString("ITEX"); {
} case ESM::FourCC<'M','O','D','L'>::value:
mModel = esm.getHString();
break;
case ESM::FourCC<'F','N','A','M'>::value:
mName = esm.getHString();
break;
case ESM::FourCC<'P','B','D','T'>::value:
esm.getHT(mData, 16);
hasData = true;
break;
case ESM::FourCC<'S','C','R','I'>::value:
mScript = esm.getHString();
break;
case ESM::FourCC<'I','T','E','X'>::value:
mIcon = esm.getHString();
break;
default:
esm.fail("Unknown subrecord");
}
}
if (!hasData)
esm.fail("Missing PBDT subrecord");
}
void Probe::save(ESMWriter &esm) const void Probe::save(ESMWriter &esm) const
{ {
esm.writeHNCString("MODL", mModel); esm.writeHNCString("MODL", mModel);
esm.writeHNOCString("FNAM", mName); esm.writeHNOCString("FNAM", mName);
esm.writeHNT("PBDT", mData, 16); esm.writeHNT("PBDT", mData, 16);
esm.writeHNOString("SCRI", mScript); esm.writeHNOString("SCRI", mScript);
esm.writeHNOCString("ITEX", mIcon); esm.writeHNOCString("ITEX", mIcon);
} }
void Probe::blank() void Probe::blank()
{ {

@ -43,7 +43,7 @@ void Race::load(ESMReader &esm)
mPowers.add(esm); mPowers.add(esm);
break; break;
default: default:
esm.fail("Unknown subrecord " + esm.retSubName().toString()); esm.fail("Unknown subrecord");
} }
} }
if (!hasData) if (!hasData)

@ -10,13 +10,35 @@ namespace ESM
void Repair::load(ESMReader &esm) void Repair::load(ESMReader &esm)
{ {
mModel = esm.getHNString("MODL"); bool hasData = true;
mName = esm.getHNOString("FNAM"); while (esm.hasMoreSubs())
{
esm.getHNT(mData, "RIDT", 16); esm.getSubName();
uint32_t name = esm.retSubName().val;
mScript = esm.getHNOString("SCRI"); switch (name)
mIcon = esm.getHNOString("ITEX"); {
case ESM::FourCC<'M','O','D','L'>::value:
mModel = esm.getHString();
break;
case ESM::FourCC<'F','N','A','M'>::value:
mName = esm.getHString();
break;
case ESM::FourCC<'R','I','D','T'>::value:
esm.getHT(mData, 16);
hasData = true;
break;
case ESM::FourCC<'S','C','R','I'>::value:
mScript = esm.getHString();
break;
case ESM::FourCC<'I','T','E','X'>::value:
mIcon = esm.getHString();
break;
default:
esm.fail("Unknown subrecord");
}
}
if (!hasData)
esm.fail("Missing RIDT subrecord");
} }
void Repair::save(ESMWriter &esm) const void Repair::save(ESMWriter &esm) const

@ -9,17 +9,7 @@ namespace ESM
unsigned int Script::sRecordId = REC_SCPT; unsigned int Script::sRecordId = REC_SCPT;
void Script::load(ESMReader &esm) void Script::loadSCVR(ESMReader &esm)
{
SCHD data;
esm.getHNT(data, "SCHD", 52);
mData = data.mData;
mId = data.mName.toString();
mVarNames.clear();
// List of local variables
if (esm.isNextSub("SCVR"))
{ {
int s = mData.mStringTableSize; int s = mData.mStringTableSize;
@ -66,29 +56,41 @@ void Script::load(ESMReader &esm)
} }
} }
// Script mData void Script::load(ESMReader &esm)
if (esm.isNextSub("SCDT")) {
SCHD data;
esm.getHNT(data, "SCHD", 52);
mData = data.mData;
mId = data.mName.toString();
mVarNames.clear();
while (esm.hasMoreSubs())
{ {
esm.getSubName();
uint32_t name = esm.retSubName().val;
switch (name)
{
case ESM::FourCC<'S','C','V','R'>::value:
// list of local variables
loadSCVR(esm);
break;
case ESM::FourCC<'S','C','D','T'>::value:
// compiled script
mScriptData.resize(mData.mScriptDataSize); mScriptData.resize(mData.mScriptDataSize);
esm.getHExact(&mScriptData[0], mScriptData.size()); esm.getHExact(&mScriptData[0], mScriptData.size());
break;
case ESM::FourCC<'S','C','T','X'>::value:
mScriptText = esm.getHString();
break;
default:
esm.fail("Unknown subrecord");
} }
// Script text
mScriptText = esm.getHNOString("SCTX");
// NOTE: A minor hack/workaround...
//
// MAO_Containers.esp from Morrowind Acoustic Overhaul has SCVR records
// at the end (see Bug #1849). Since OpenMW does not use SCVR subrecords
// for variable names just skip these as a quick fix. An alternative
// solution would be to decode and validate SCVR subrecords even if they
// appear here.
if (esm.isNextSub("SCVR")) {
esm.skipHSub();
} }
} }
void Script::save(ESMWriter &esm) const
{ void Script::save(ESMWriter &esm) const
{
std::string varNameString; std::string varNameString;
if (!mVarNames.empty()) if (!mVarNames.empty())
for (std::vector<std::string>::const_iterator it = mVarNames.begin(); it != mVarNames.end(); ++it) for (std::vector<std::string>::const_iterator it = mVarNames.begin(); it != mVarNames.end(); ++it)
@ -117,7 +119,7 @@ void Script::save(ESMWriter &esm) const
esm.endRecord("SCDT"); esm.endRecord("SCDT");
esm.writeHNOString("SCTX", mScriptText); esm.writeHNOString("SCTX", mScriptText);
} }
void Script::blank() void Script::blank()
{ {

@ -53,6 +53,9 @@ public:
void blank(); void blank();
///< Set record to default state (does not touch the ID/index). ///< Set record to default state (does not touch the ID/index).
private:
void loadSCVR(ESMReader &esm);
}; };
} }
#endif #endif

@ -129,23 +129,47 @@ namespace ESM
unsigned int Skill::sRecordId = REC_SKIL; unsigned int Skill::sRecordId = REC_SKIL;
void Skill::load(ESMReader &esm) void Skill::load(ESMReader &esm)
{ {
esm.getHNT(mIndex, "INDX"); bool hasIndex = false;
esm.getHNT(mData, "SKDT", 24); bool hasData = false;
mDescription = esm.getHNOString("DESC"); while (esm.hasMoreSubs())
{
esm.getSubName();
uint32_t name = esm.retSubName().val;
switch (name)
{
case ESM::FourCC<'I','N','D','X'>::value:
esm.getHT(mIndex);
hasIndex = true;
break;
case ESM::FourCC<'S','K','D','T'>::value:
esm.getHT(mData, 24);
hasData = true;
break;
case ESM::FourCC<'D','E','S','C'>::value:
mDescription = esm.getHString();
break;
default:
esm.fail("Unknown subrecord");
}
}
if (!hasIndex)
esm.fail("Missing INDX");
if (!hasData)
esm.fail("Missing SKDT");
// create an ID from the index and the name (only used in the editor and likely to change in the // create an ID from the index and the name (only used in the editor and likely to change in the
// future) // future)
mId = indexToId (mIndex); mId = indexToId (mIndex);
} }
void Skill::save(ESMWriter &esm) const void Skill::save(ESMWriter &esm) const
{ {
esm.writeHNT("INDX", mIndex); esm.writeHNT("INDX", mIndex);
esm.writeHNT("SKDT", mData, 24); esm.writeHNT("SKDT", mData, 24);
esm.writeHNOString("DESC", mDescription); esm.writeHNOString("DESC", mDescription);
} }
void Skill::blank() void Skill::blank()
{ {

@ -8,19 +8,38 @@ namespace ESM
{ {
unsigned int SoundGenerator::sRecordId = REC_SNDG; unsigned int SoundGenerator::sRecordId = REC_SNDG;
void SoundGenerator::load(ESMReader &esm) void SoundGenerator::load(ESMReader &esm)
{ {
esm.getHNT(mType, "DATA", 4); bool hasData = false;
while (esm.hasMoreSubs())
mCreature = esm.getHNOString("CNAM"); {
mSound = esm.getHNOString("SNAM"); esm.getSubName();
} uint32_t name = esm.retSubName().val;
void SoundGenerator::save(ESMWriter &esm) const switch (name)
{ {
case ESM::FourCC<'D','A','T','A'>::value:
esm.getHT(mType, 4);
hasData = true;
break;
case ESM::FourCC<'C','N','A','M'>::value:
mCreature = esm.getHString();
break;
case ESM::FourCC<'S','N','A','M'>::value:
mSound = esm.getHString();
break;
default:
esm.fail("Unknown subrecord");
}
}
if (!hasData)
esm.fail("Missing DATA");
}
void SoundGenerator::save(ESMWriter &esm) const
{
esm.writeHNT("DATA", mType, 4); esm.writeHNT("DATA", mType, 4);
esm.writeHNOCString("CNAM", mCreature); esm.writeHNOCString("CNAM", mCreature);
esm.writeHNOCString("SNAM", mSound); esm.writeHNOCString("SNAM", mSound);
} }
void SoundGenerator::blank() void SoundGenerator::blank()
{ {

@ -8,22 +8,35 @@ namespace ESM
{ {
unsigned int Sound::sRecordId = REC_SOUN; unsigned int Sound::sRecordId = REC_SOUN;
void Sound::load(ESMReader &esm) void Sound::load(ESMReader &esm)
{ {
mSound = esm.getHNOString("FNAM"); bool hasData = false;
esm.getHNT(mData, "DATA", 3); while (esm.hasMoreSubs())
/* {
cout << "vol=" << (int)data.volume esm.getSubName();
<< " min=" << (int)data.minRange uint32_t name = esm.retSubName().val;
<< " max=" << (int)data.maxRange switch (name)
<< endl; {
*/ case ESM::FourCC<'F','N','A','M'>::value:
} mSound = esm.getHString();
void Sound::save(ESMWriter &esm) const break;
{ case ESM::FourCC<'D','A','T','A'>::value:
esm.getHT(mData, 3);
hasData = true;
break;
default:
esm.fail("Unknown subrecord");
}
}
if (!hasData)
esm.fail("Missing DATA");
}
void Sound::save(ESMWriter &esm) const
{
esm.writeHNOCString("FNAM", mSound); esm.writeHNOCString("FNAM", mSound);
esm.writeHNT("DATA", mData, 3); esm.writeHNT("DATA", mData, 3);
} }
void Sound::blank() void Sound::blank()
{ {

@ -8,8 +8,8 @@ namespace ESM
{ {
unsigned int Spell::sRecordId = REC_SPEL; unsigned int Spell::sRecordId = REC_SPEL;
void Spell::load(ESMReader &esm) void Spell::load(ESMReader &esm)
{ {
bool hasData = false; bool hasData = false;
while (esm.hasMoreSubs()) while (esm.hasMoreSubs())
{ {
@ -34,14 +34,14 @@ void Spell::load(ESMReader &esm)
} }
if (!hasData) if (!hasData)
esm.fail("Missing SPDT subrecord"); esm.fail("Missing SPDT subrecord");
} }
void Spell::save(ESMWriter &esm) const void Spell::save(ESMWriter &esm) const
{ {
esm.writeHNOCString("FNAM", mName); esm.writeHNOCString("FNAM", mName);
esm.writeHNT("SPDT", mData, 12); esm.writeHNT("SPDT", mData, 12);
mEffects.save(esm); mEffects.save(esm);
} }
void Spell::blank() void Spell::blank()
{ {

@ -8,15 +8,37 @@ namespace ESM
{ {
unsigned int StartScript::sRecordId = REC_SSCR; unsigned int StartScript::sRecordId = REC_SSCR;
void StartScript::load(ESMReader &esm) void StartScript::load(ESMReader &esm)
{ {
mData = esm.getHNString("DATA"); bool hasData = false;
mScript = esm.getHNString("NAME"); bool hasName = false;
} while (esm.hasMoreSubs())
void StartScript::save(ESMWriter &esm) const {
{ esm.getSubName();
uint32_t name = esm.retSubName().val;
switch (name)
{
case ESM::FourCC<'D','A','T','A'>::value:
mData = esm.getHString();
hasData = true;
break;
case ESM::FourCC<'N','A','M','E'>::value:
mScript = esm.getHString();
hasName = true;
break;
default:
esm.fail("Unknown subrecord");
}
}
if (!hasData)
esm.fail("Missing DATA");
if (!hasName)
esm.fail("Missing NAME");
}
void StartScript::save(ESMWriter &esm) const
{
esm.writeHNString("DATA", mData); esm.writeHNString("DATA", mData);
esm.writeHNString("NAME", mScript); esm.writeHNString("NAME", mScript);
} }
} }

@ -8,15 +8,16 @@ namespace ESM
{ {
unsigned int Static::sRecordId = REC_STAT; unsigned int Static::sRecordId = REC_STAT;
void Static::load(ESMReader &esm) void Static::load(ESMReader &esm)
{ {
mPersistent = esm.getRecordFlags() & 0x0400; mPersistent = esm.getRecordFlags() & 0x0400;
mModel = esm.getHNString("MODL"); mModel = esm.getHNString("MODL");
} }
void Static::save(ESMWriter &esm) const void Static::save(ESMWriter &esm) const
{ {
esm.writeHNCString("MODL", mModel); esm.writeHNCString("MODL", mModel);
} }
void Static::blank() void Static::blank()
{ {

@ -50,8 +50,8 @@ namespace ESM
NAME32 mPlayerName; NAME32 mPlayerName;
}; };
GMDT mGameData; // Used in .ess savegames only GMDT mGameData; // Used in .ess savegames only
std::vector<unsigned char> mSCRD; // Used in .ess savegames only, screenshot? std::vector<unsigned char> mSCRD; // Used in .ess savegames only, unknown
std::vector<unsigned char> mSCRS; // Used in .ess savegames only, screenshot? std::vector<unsigned char> mSCRS; // Used in .ess savegames only, screenshot
Data mData; Data mData;
int mFormat; int mFormat;

@ -5,14 +5,6 @@
namespace ESM { namespace ESM {
void SpellList::load(ESMReader &esm)
{
mList.clear();
while (esm.isNextSub("NPCS")) {
add(esm);
}
}
void SpellList::add(ESMReader &esm) void SpellList::add(ESMReader &esm)
{ {
mList.push_back(esm.getHString()); mList.push_back(esm.getHString());

@ -11,6 +11,7 @@ namespace ESM
/** A list of references to spells and spell effects. This is shared /** A list of references to spells and spell effects. This is shared
between the records BSGN, NPC and RACE. between the records BSGN, NPC and RACE.
NPCS subrecord.
*/ */
struct SpellList struct SpellList
{ {
@ -22,9 +23,6 @@ namespace ESM
/// Load one spell, assumes the subrecord name was already read /// Load one spell, assumes the subrecord name was already read
void add(ESMReader &esm); void add(ESMReader &esm);
/// Load all spells
/// TODO: remove this method, the ESM format doesn't guarantee that all spell subrecords follow one another
void load(ESMReader &esm);
void save(ESMWriter &esm) const; void save(ESMWriter &esm) const;
}; };
} }

Loading…
Cancel
Save