From 79b3855c5bd758ffbce281c49b9b14abada36ede Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Sun, 30 Jul 2023 11:37:32 +0200 Subject: [PATCH] Move TimeStamp to ESM3 and remove sized reads --- apps/openmw/mwworld/timestamp.cpp | 2 +- components/CMakeLists.txt | 2 +- components/esm/defs.hpp | 6 ------ components/esm3/activespells.cpp | 2 +- components/esm3/activespells.hpp | 13 +++++++------ components/esm3/cellstate.cpp | 3 ++- components/esm3/cellstate.hpp | 3 ++- components/esm3/creaturestats.cpp | 26 ++++++++++++++------------ components/esm3/creaturestats.hpp | 11 ++++++----- components/esm3/esmreader.hpp | 11 +++++++++++ components/esm3/spellstate.cpp | 11 ++++------- components/esm3/spellstate.hpp | 11 ++++++----- components/esm3/timestamp.cpp | 11 +++++++++++ components/esm3/timestamp.hpp | 21 +++++++++++++++++++++ 14 files changed, 87 insertions(+), 46 deletions(-) create mode 100644 components/esm3/timestamp.cpp create mode 100644 components/esm3/timestamp.hpp diff --git a/apps/openmw/mwworld/timestamp.cpp b/apps/openmw/mwworld/timestamp.cpp index c21d90f285..ccf3ec2434 100644 --- a/apps/openmw/mwworld/timestamp.cpp +++ b/apps/openmw/mwworld/timestamp.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include #include "duration.hpp" diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index 2f0025a2c7..a174301e07 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -142,7 +142,7 @@ add_component_dir (esm3 inventorystate containerstate npcstate creaturestate dialoguestate statstate npcstats creaturestats weatherstate quickkeys fogstate spellstate activespells creaturelevliststate doorstate projectilestate debugprofile aisequence magiceffects custommarkerstate stolenitems transport animationstate controlsstate mappings readerscache - infoorder + infoorder timestamp ) add_component_dir (esm3terrain diff --git a/components/esm/defs.hpp b/components/esm/defs.hpp index dc590534d0..617873d8e1 100644 --- a/components/esm/defs.hpp +++ b/components/esm/defs.hpp @@ -15,12 +15,6 @@ namespace ESM { - struct TimeStamp - { - float mHour; - int mDay; - }; - struct EpochTimeStamp { float mGameHour; diff --git a/components/esm3/activespells.cpp b/components/esm3/activespells.cpp index bef9299da1..27d260fe72 100644 --- a/components/esm3/activespells.cpp +++ b/components/esm3/activespells.cpp @@ -68,7 +68,7 @@ namespace ESM if (esm.isNextSub("WORS")) { esm.getHT(params.mWorsenings); - esm.getHNTSized<8>(params.mNextWorsening, "TIME"); + params.mNextWorsening.load(esm); } else params.mWorsenings = -1; diff --git a/components/esm3/activespells.hpp b/components/esm3/activespells.hpp index 5e04144d6f..531a39b4c7 100644 --- a/components/esm3/activespells.hpp +++ b/components/esm3/activespells.hpp @@ -4,6 +4,7 @@ #include "cellref.hpp" #include "components/esm/defs.hpp" #include "components/esm/refid.hpp" +#include "timestamp.hpp" #include #include @@ -27,15 +28,15 @@ namespace ESM Flag_Ignore_SpellAbsorption = 1 << 4 }; - int mEffectId; + int32_t mEffectId; float mMagnitude; float mMinMagnitude; float mMaxMagnitude; - int mArg; // skill or attribute + int32_t mArg; // skill or attribute float mDuration; float mTimeLeft; - int mEffectIndex; - int mFlags; + int32_t mEffectIndex; + int32_t mFlags; }; // format 0, saved games only @@ -55,10 +56,10 @@ namespace ESM RefId mId; std::vector mEffects; std::string mDisplayName; - int mCasterActorId; + int32_t mCasterActorId; RefNum mItem; EffectType mType; - int mWorsenings; + int32_t mWorsenings; TimeStamp mNextWorsening; }; diff --git a/components/esm3/cellstate.cpp b/components/esm3/cellstate.cpp index 1c5514480a..1d4ad79337 100644 --- a/components/esm3/cellstate.cpp +++ b/components/esm3/cellstate.cpp @@ -16,7 +16,8 @@ namespace ESM mLastRespawn.mDay = 0; mLastRespawn.mHour = 0; - esm.getHNOTSized<8>(mLastRespawn, "RESP"); + if (esm.peekNextSub("RESP")) + mLastRespawn.load(esm, "RESP"); } void CellState::save(ESMWriter& esm) const diff --git a/components/esm3/cellstate.hpp b/components/esm3/cellstate.hpp index 708afb13f8..747de33110 100644 --- a/components/esm3/cellstate.hpp +++ b/components/esm3/cellstate.hpp @@ -3,6 +3,7 @@ #include "components/esm/defs.hpp" #include "components/esm/refid.hpp" +#include "timestamp.hpp" namespace ESM { @@ -18,7 +19,7 @@ namespace ESM bool mIsInterior; float mWaterLevel; - int mHasFogOfWar; // Do we have fog of war state (0 or 1)? (see fogstate.hpp) + int32_t mHasFogOfWar; // Do we have fog of war state (0 or 1)? (see fogstate.hpp) TimeStamp mLastRespawn; diff --git a/components/esm3/creaturestats.cpp b/components/esm3/creaturestats.cpp index 6460cada21..d8fb0d6969 100644 --- a/components/esm3/creaturestats.cpp +++ b/components/esm3/creaturestats.cpp @@ -21,9 +21,10 @@ namespace ESM mTradeTime.mDay = 0; mTradeTime.mHour = 0; - esm.getHNOTSized<8>(mTradeTime, "TIME"); + if (esm.peekNextSub("TIME")) + mTradeTime.load(esm); - int flags = 0; + int32_t flags = 0; mDead = false; mDeathAnimationFinished = false; mDied = false; @@ -108,7 +109,8 @@ namespace ESM mTimeOfDeath.mDay = 0; mTimeOfDeath.mHour = 0; - esm.getHNOTSized<8>(mTimeOfDeath, "DTIM"); + if (esm.peekNextSub("DTIM")) + mTimeOfDeath.load(esm, "DTIM"); mSpells.load(esm); mActiveSpells.load(esm); @@ -119,12 +121,12 @@ namespace ESM { while (esm.isNextSub("SUMM")) { - int magicEffect; + int32_t magicEffect; esm.getHT(magicEffect); ESM::RefId source = esm.getHNORefId("SOUR"); - int effectIndex = -1; + int32_t effectIndex = -1; esm.getHNOT(effectIndex, "EIND"); - int actorId; + int32_t actorId; esm.getHNT(actorId, "ACID"); mSummonedCreatureMap[SummonKey(magicEffect, source, effectIndex)] = actorId; mSummonedCreatures.emplace(magicEffect, actorId); @@ -134,9 +136,9 @@ namespace ESM { while (esm.isNextSub("SUMM")) { - int magicEffect; + int32_t magicEffect; esm.getHT(magicEffect); - int actorId; + int32_t actorId; esm.getHNT(actorId, "ACID"); mSummonedCreatures.emplace(magicEffect, actorId); } @@ -144,7 +146,7 @@ namespace ESM while (esm.isNextSub("GRAV")) { - int actorId; + int32_t actorId; esm.getHT(actorId); mSummonGraveyard.push_back(actorId); } @@ -164,7 +166,7 @@ namespace ESM CorprusStats stats; esm.getHNT(stats.mWorsenings, "WORS"); - esm.getHNTSized<8>(stats.mNextWorsening, "TIME"); + stats.mNextWorsening.load(esm); mCorprusSpells[id] = stats; } @@ -191,7 +193,7 @@ namespace ESM if (mTradeTime.mDay != 0 || mTradeTime.mHour != 0) esm.writeHNT("TIME", mTradeTime); - int flags = 0; + int32_t flags = 0; if (mDead) flags |= Dead; if (mDeathAnimationFinished) @@ -260,7 +262,7 @@ namespace ESM esm.writeHNT("ACID", actorId); } - for (int key : mSummonGraveyard) + for (int32_t key : mSummonGraveyard) { esm.writeHNT("GRAV", key); } diff --git a/components/esm3/creaturestats.hpp b/components/esm3/creaturestats.hpp index d9df6200af..63ba49cdaa 100644 --- a/components/esm3/creaturestats.hpp +++ b/components/esm3/creaturestats.hpp @@ -16,6 +16,7 @@ #include "components/esm/refid.hpp" #include "magiceffects.hpp" #include "spellstate.hpp" +#include "timestamp.hpp" namespace ESM { @@ -27,7 +28,7 @@ namespace ESM { struct CorprusStats { - std::array mWorsenings; + std::array mWorsenings; TimeStamp mNextWorsening; }; @@ -78,15 +79,15 @@ namespace ESM bool mKnockdownOverOneFrame; bool mHitRecovery; bool mBlock; - unsigned int mMovementFlags; + uint32_t mMovementFlags; float mFallHeight; ESM::RefId mLastHitObject; ESM::RefId mLastHitAttemptObject; bool mRecalcDynamicStats; - int mDrawState; - signed char mDeathAnimation; + int32_t mDrawState; + int8_t mDeathAnimation; TimeStamp mTimeOfDeath; - int mLevel; + int32_t mLevel; bool mMissingACDT; std::map mCorprusSpells; diff --git a/components/esm3/esmreader.hpp b/components/esm3/esmreader.hpp index bab11a9fb1..d84eae0e5a 100644 --- a/components/esm3/esmreader.hpp +++ b/components/esm3/esmreader.hpp @@ -128,6 +128,17 @@ namespace ESM getHNTSized(x, name); } + template + void getHNT(NAME name, Args&... args) + { + constexpr size_t size = (0 + ... + sizeof(Args)); + getSubNameIs(name); + getSubHeader(); + if (mCtx.leftSub != size) + reportSubSizeMismatch(size, mCtx.leftSub); + (getT(args), ...); + } + // Optional version of getHNT template >> void getHNOT(X& x, NAME name) diff --git a/components/esm3/spellstate.cpp b/components/esm3/spellstate.cpp index 4b54bf31c9..41591f56b7 100644 --- a/components/esm3/spellstate.cpp +++ b/components/esm3/spellstate.cpp @@ -17,7 +17,7 @@ namespace ESM SpellParams state; while (esm.isNextSub("INDX")) { - int index; + int32_t index; esm.getHT(index); float magnitude; @@ -28,7 +28,7 @@ namespace ESM while (esm.isNextSub("PURG")) { - int index; + int32_t index; esm.getHT(index); state.mPurgedEffects.insert(index); } @@ -79,7 +79,7 @@ namespace ESM CorprusStats stats; esm.getHNT(stats.mWorsenings, "WORS"); - esm.getHNTSized<8>(stats.mNextWorsening, "TIME"); + stats.mNextWorsening.load(esm); mCorprusSpells[id] = stats; } @@ -87,10 +87,7 @@ namespace ESM while (esm.isNextSub("USED")) { ESM::RefId id = esm.getRefId(); - TimeStamp time; - esm.getHNTSized<8>(time, "TIME"); - - mUsedPowers[id] = time; + mUsedPowers[id].load(esm); } mSelectedSpell = esm.getHNORefId("SLCT"); diff --git a/components/esm3/spellstate.hpp b/components/esm3/spellstate.hpp index 1b273b92fa..9ddf80193e 100644 --- a/components/esm3/spellstate.hpp +++ b/components/esm3/spellstate.hpp @@ -8,6 +8,7 @@ #include "components/esm/defs.hpp" #include "components/esm/refid.hpp" +#include "timestamp.hpp" namespace ESM { @@ -20,21 +21,21 @@ namespace ESM { struct CorprusStats { - int mWorsenings; + int32_t mWorsenings; TimeStamp mNextWorsening; }; struct PermanentSpellEffectInfo { - int mId; - int mArg; + int32_t mId; + int32_t mArg; float mMagnitude; }; struct SpellParams { - std::map mEffectRands; // - std::set mPurgedEffects; // indices of purged effects + std::map mEffectRands; // + std::set mPurgedEffects; // indices of purged effects }; std::vector mSpells; diff --git a/components/esm3/timestamp.cpp b/components/esm3/timestamp.cpp new file mode 100644 index 0000000000..b89c498e42 --- /dev/null +++ b/components/esm3/timestamp.cpp @@ -0,0 +1,11 @@ +#include "timestamp.hpp" + +#include "esmreader.hpp" + +namespace ESM +{ + void TimeStamp::load(ESMReader& esm, NAME name) + { + esm.getHNT(name, mHour, mDay); + } +} diff --git a/components/esm3/timestamp.hpp b/components/esm3/timestamp.hpp new file mode 100644 index 0000000000..3f3ce8a344 --- /dev/null +++ b/components/esm3/timestamp.hpp @@ -0,0 +1,21 @@ +#ifndef OPENMW_COMPONENTS_ESM3_TIMESTAMP_H +#define OPENMW_COMPONENTS_ESM3_TIMESTAMP_H + +#include + +#include + +namespace ESM +{ + class ESMReader; + + struct TimeStamp + { + float mHour; + int32_t mDay; + + void load(ESMReader& esm, NAME name = "TIME"); + }; +} + +#endif