1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-29 00:32:49 +00:00

Use ESM::decompose to handle ENAMstruct

This commit is contained in:
Evil Eye 2024-02-24 14:03:24 +01:00
parent 595e42ae43
commit ec1c6ee171
7 changed files with 56 additions and 19 deletions

View File

@ -1,6 +1,7 @@
#include <components/esm/fourcc.hpp> #include <components/esm/fourcc.hpp>
#include <components/esm3/aipackage.hpp> #include <components/esm3/aipackage.hpp>
#include <components/esm3/aisequence.hpp> #include <components/esm3/aisequence.hpp>
#include <components/esm3/effectlist.hpp>
#include <components/esm3/esmreader.hpp> #include <components/esm3/esmreader.hpp>
#include <components/esm3/esmwriter.hpp> #include <components/esm3/esmwriter.hpp>
#include <components/esm3/loadcont.hpp> #include <components/esm3/loadcont.hpp>
@ -525,6 +526,34 @@ namespace ESM
EXPECT_EQ(result.mServices, record.mServices); EXPECT_EQ(result.mServices, record.mServices);
} }
TEST_P(Esm3SaveLoadRecordTest, enamShouldNotChange)
{
EffectList record;
record.mList.emplace_back(ENAMstruct{
.mEffectID = 1,
.mSkill = 2,
.mAttribute = 3,
.mRange = 4,
.mArea = 5,
.mDuration = 6,
.mMagnMin = 7,
.mMagnMax = 8,
});
EffectList result;
saveAndLoadRecord(record, GetParam(), result);
EXPECT_EQ(result.mList.size(), record.mList.size());
EXPECT_EQ(result.mList[0].mEffectID, record.mList[0].mEffectID);
EXPECT_EQ(result.mList[0].mSkill, record.mList[0].mSkill);
EXPECT_EQ(result.mList[0].mAttribute, record.mList[0].mAttribute);
EXPECT_EQ(result.mList[0].mRange, record.mList[0].mRange);
EXPECT_EQ(result.mList[0].mArea, record.mList[0].mArea);
EXPECT_EQ(result.mList[0].mDuration, record.mList[0].mDuration);
EXPECT_EQ(result.mList[0].mMagnMin, record.mList[0].mMagnMin);
EXPECT_EQ(result.mList[0].mMagnMax, record.mList[0].mMagnMax);
}
INSTANTIATE_TEST_SUITE_P(FormatVersions, Esm3SaveLoadRecordTest, ValuesIn(getFormats())); INSTANTIATE_TEST_SUITE_P(FormatVersions, Esm3SaveLoadRecordTest, ValuesIn(getFormats()));
} }
} }

View File

@ -54,29 +54,25 @@ namespace ESM
else if (esm.retSubName() == AI_Wander) else if (esm.retSubName() == AI_Wander)
{ {
pack.mType = AI_Wander; pack.mType = AI_Wander;
esm.getSubHeader(); esm.getSubComposite(pack.mWander);
esm.getComposite(pack.mWander);
mList.push_back(pack); mList.push_back(pack);
} }
else if (esm.retSubName() == AI_Travel) else if (esm.retSubName() == AI_Travel)
{ {
pack.mType = AI_Travel; pack.mType = AI_Travel;
esm.getSubHeader(); esm.getSubComposite(pack.mTravel);
esm.getComposite(pack.mTravel);
mList.push_back(pack); mList.push_back(pack);
} }
else if (esm.retSubName() == AI_Escort || esm.retSubName() == AI_Follow) else if (esm.retSubName() == AI_Escort || esm.retSubName() == AI_Follow)
{ {
pack.mType = (esm.retSubName() == AI_Escort) ? AI_Escort : AI_Follow; pack.mType = (esm.retSubName() == AI_Escort) ? AI_Escort : AI_Follow;
esm.getSubHeader(); esm.getSubComposite(pack.mTarget);
esm.getComposite(pack.mTarget);
mList.push_back(pack); mList.push_back(pack);
} }
else if (esm.retSubName() == AI_Activate) else if (esm.retSubName() == AI_Activate)
{ {
pack.mType = AI_Activate; pack.mType = AI_Activate;
esm.getSubHeader(); esm.getSubComposite(pack.mActivate);
esm.getComposite(pack.mActivate);
mList.push_back(pack); mList.push_back(pack);
} }
} }

View File

@ -3,8 +3,15 @@
#include "esmreader.hpp" #include "esmreader.hpp"
#include "esmwriter.hpp" #include "esmwriter.hpp"
#include <components/misc/concepts.hpp>
namespace ESM namespace ESM
{ {
template <Misc::SameAsWithoutCvref<ENAMstruct> T>
void decompose(T&& v, const auto& f)
{
f(v.mEffectID, v.mSkill, v.mAttribute, v.mRange, v.mArea, v.mDuration, v.mMagnMin, v.mMagnMax);
}
void EffectList::load(ESMReader& esm) void EffectList::load(ESMReader& esm)
{ {
@ -18,15 +25,15 @@ namespace ESM
void EffectList::add(ESMReader& esm) void EffectList::add(ESMReader& esm)
{ {
ENAMstruct s; ENAMstruct s;
esm.getHT(s.mEffectID, s.mSkill, s.mAttribute, s.mRange, s.mArea, s.mDuration, s.mMagnMin, s.mMagnMax); esm.getSubComposite(s);
mList.push_back(s); mList.push_back(s);
} }
void EffectList::save(ESMWriter& esm) const void EffectList::save(ESMWriter& esm) const
{ {
for (std::vector<ENAMstruct>::const_iterator it = mList.begin(); it != mList.end(); ++it) for (const ENAMstruct& enam : mList)
{ {
esm.writeHNT<ENAMstruct>("ENAM", *it, 24); esm.writeNamedComposite("ENAM", enam);
} }
} }

View File

@ -9,9 +9,6 @@ namespace ESM
class ESMReader; class ESMReader;
class ESMWriter; class ESMWriter;
#pragma pack(push)
#pragma pack(1)
/** Defines a spell effect. Shared between SPEL (Spells), ALCH /** Defines a spell effect. Shared between SPEL (Spells), ALCH
(Potions) and ENCH (Item enchantments) records (Potions) and ENCH (Item enchantments) records
*/ */
@ -28,7 +25,6 @@ namespace ESM
int32_t mRange; // 0 - self, 1 - touch, 2 - target (RangeType enum) int32_t mRange; // 0 - self, 1 - touch, 2 - target (RangeType enum)
int32_t mArea, mDuration, mMagnMin, mMagnMax; int32_t mArea, mDuration, mMagnMin, mMagnMax;
}; };
#pragma pack(pop)
/// EffectList, ENAM subrecord /// EffectList, ENAM subrecord
struct EffectList struct EffectList

View File

@ -189,6 +189,17 @@ namespace ESM
decompose(value, [&](auto&... args) { (getT(args), ...); }); decompose(value, [&](auto&... args) { (getT(args), ...); });
} }
void getSubComposite(auto& value)
{
decompose(value, [&](auto&... args) {
constexpr size_t size = (0 + ... + sizeof(decltype(args)));
getSubHeader();
if (mCtx.leftSub != size)
reportSubSizeMismatch(size, mCtx.leftSub);
(getT(args), ...);
});
}
template <typename T, typename = std::enable_if_t<IsReadable<T>>> template <typename T, typename = std::enable_if_t<IsReadable<T>>>
void skipHT() void skipHT()
{ {

View File

@ -69,8 +69,7 @@ namespace ESM
mSpells.add(esm); mSpells.add(esm);
break; break;
case fourCC("AIDT"): case fourCC("AIDT"):
esm.getSubHeader(); esm.getSubComposite(mAiData);
esm.getComposite(mAiData);
break; break;
case fourCC("DODT"): case fourCC("DODT"):
case fourCC("DNAM"): case fourCC("DNAM"):

View File

@ -102,8 +102,7 @@ namespace ESM
mInventory.add(esm); mInventory.add(esm);
break; break;
case fourCC("AIDT"): case fourCC("AIDT"):
esm.getSubHeader(); esm.getSubComposite(mAiData);
esm.getComposite(mAiData);
break; break;
case fourCC("DODT"): case fourCC("DODT"):
case fourCC("DNAM"): case fourCC("DNAM"):