From 62f47acf6bfda7524525ec1612fb21188763f109 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Sun, 20 Aug 2023 11:34:05 +0200 Subject: [PATCH] Replace more explicitly sized reads and variable width integers --- apps/openmw/mwscript/globalscripts.cpp | 4 ++-- components/esm3/effectlist.cpp | 2 +- components/esm3/effectlist.hpp | 7 ++++--- components/esm3/esmreader.hpp | 4 ++-- components/esm3/fogstate.cpp | 5 +++-- components/esm3/fogstate.hpp | 3 ++- components/esm3/globalmap.cpp | 2 +- components/esm3/globalmap.hpp | 3 ++- components/esm3/globalscript.cpp | 9 ++++++--- components/esm3/globalscript.hpp | 2 +- components/esm3/journalentry.hpp | 10 ++++++---- components/esm3/loadacti.hpp | 4 +++- components/esm3/loadalch.cpp | 2 +- components/esm3/loadalch.hpp | 7 ++++--- components/esm3/loadappa.cpp | 2 +- components/esm3/loadappa.hpp | 7 ++++--- components/esm3/loadarmo.cpp | 2 +- components/esm3/loadarmo.hpp | 7 ++++--- components/esm3/loadbody.cpp | 2 +- components/esm3/loadbody.hpp | 3 ++- components/esm3/loadbook.cpp | 2 +- components/esm3/loadbook.hpp | 6 ++++-- components/esm3/loadbsgn.hpp | 3 ++- components/esm3/loadclas.cpp | 7 ++++--- components/esm3/loadclas.hpp | 17 +++++++++-------- components/esm3/loadclot.cpp | 2 +- components/esm3/loadclot.hpp | 9 +++++---- components/esm3/loaddoor.hpp | 3 ++- components/esm3/loadench.cpp | 2 +- components/esm3/loadench.hpp | 11 ++++++----- 30 files changed, 86 insertions(+), 63 deletions(-) diff --git a/apps/openmw/mwscript/globalscripts.cpp b/apps/openmw/mwscript/globalscripts.cpp index ef4c3171ce..c20e2fe255 100644 --- a/apps/openmw/mwscript/globalscripts.cpp +++ b/apps/openmw/mwscript/globalscripts.cpp @@ -232,7 +232,7 @@ namespace MWScript desc->mLocals.write(script.mLocals, id); - script.mRunning = desc->mRunning ? 1 : 0; + script.mRunning = desc->mRunning; writer.startRecord(ESM::REC_GSCR); script.save(writer); @@ -276,7 +276,7 @@ namespace MWScript return true; } - iter->second->mRunning = script.mRunning != 0; + iter->second->mRunning = script.mRunning; iter->second->mLocals.read(script.mLocals, script.mId); return true; diff --git a/components/esm3/effectlist.cpp b/components/esm3/effectlist.cpp index bdde270965..701552b312 100644 --- a/components/esm3/effectlist.cpp +++ b/components/esm3/effectlist.cpp @@ -18,7 +18,7 @@ namespace ESM void EffectList::add(ESMReader& esm) { ENAMstruct s; - esm.getHTSized<24>(s); + esm.getHT(s.mEffectID, s.mSkill, s.mAttribute, s.mRange, s.mArea, s.mDuration, s.mMagnMin, s.mMagnMax); mList.push_back(s); } diff --git a/components/esm3/effectlist.hpp b/components/esm3/effectlist.hpp index 5887435d35..8f2cb959d6 100644 --- a/components/esm3/effectlist.hpp +++ b/components/esm3/effectlist.hpp @@ -1,6 +1,7 @@ #ifndef OPENMW_ESM_EFFECTLIST_H #define OPENMW_ESM_EFFECTLIST_H +#include #include namespace ESM @@ -17,15 +18,15 @@ namespace ESM struct ENAMstruct { // Magical effect, hard-coded ID - short mEffectID; + int16_t mEffectID; // Which skills/attributes are affected (for restore/drain spells // etc.) signed char mSkill, mAttribute; // -1 if N/A // Other spell parameters - int mRange; // 0 - self, 1 - touch, 2 - target (RangeType enum) - int mArea, mDuration, mMagnMin, mMagnMax; + int32_t mRange; // 0 - self, 1 - touch, 2 - target (RangeType enum) + int32_t mArea, mDuration, mMagnMin, mMagnMax; }; #pragma pack(pop) diff --git a/components/esm3/esmreader.hpp b/components/esm3/esmreader.hpp index 5ccd526dd4..63db1634fc 100644 --- a/components/esm3/esmreader.hpp +++ b/components/esm3/esmreader.hpp @@ -362,7 +362,7 @@ namespace ESM void setEncoder(ToUTF8::Utf8Encoder* encoder) { mEncoder = encoder; } /// Get record flags of last record - unsigned int getRecordFlags() { return mRecordFlags; } + uint32_t getRecordFlags() { return mRecordFlags; } size_t getFileSize() const { return mFileSize; } @@ -380,7 +380,7 @@ namespace ESM ESM_Context mCtx; - unsigned int mRecordFlags; + uint32_t mRecordFlags; // Special file signifier (see SpecialFile enum above) diff --git a/components/esm3/fogstate.cpp b/components/esm3/fogstate.cpp index fda36dc113..3ee4600c90 100644 --- a/components/esm3/fogstate.cpp +++ b/components/esm3/fogstate.cpp @@ -58,7 +58,8 @@ namespace ESM void FogState::load(ESMReader& esm) { - esm.getHNOTSized<16>(mBounds, "BOUN"); + if (esm.isNextSub("BOUN")) + esm.getHT(mBounds.mMinX, mBounds.mMinY, mBounds.mMaxX, mBounds.mMaxY); esm.getHNOT(mNorthMarkerAngle, "ANGL"); const FormatVersion dataFormat = esm.getFormatVersion(); while (esm.isNextSub("FTEX")) @@ -69,7 +70,7 @@ namespace ESM esm.getT(tex.mX); esm.getT(tex.mY); - const std::size_t imageSize = esm.getSubSize() - sizeof(int) * 2; + const std::size_t imageSize = esm.getSubSize() - sizeof(int32_t) * 2; tex.mImageData.resize(imageSize); esm.getExact(tex.mImageData.data(), imageSize); diff --git a/components/esm3/fogstate.hpp b/components/esm3/fogstate.hpp index a95218ea58..7348f3cb01 100644 --- a/components/esm3/fogstate.hpp +++ b/components/esm3/fogstate.hpp @@ -1,6 +1,7 @@ #ifndef OPENMW_ESM_FOGSTATE_H #define OPENMW_ESM_FOGSTATE_H +#include #include namespace ESM @@ -10,7 +11,7 @@ namespace ESM struct FogTexture { - int mX, mY; // Only used for interior cells + int32_t mX, mY; // Only used for interior cells std::vector mImageData; }; diff --git a/components/esm3/globalmap.cpp b/components/esm3/globalmap.cpp index 6ed6976147..519111d3df 100644 --- a/components/esm3/globalmap.cpp +++ b/components/esm3/globalmap.cpp @@ -8,7 +8,7 @@ namespace ESM void GlobalMap::load(ESMReader& esm) { - esm.getHNTSized<16>(mBounds, "BNDS"); + esm.getHNT("BNDS", mBounds.mMinX, mBounds.mMaxX, mBounds.mMinY, mBounds.mMaxY); esm.getSubNameIs("DATA"); esm.getSubHeader(); diff --git a/components/esm3/globalmap.hpp b/components/esm3/globalmap.hpp index 8aaf37e5e0..c4125c64b0 100644 --- a/components/esm3/globalmap.hpp +++ b/components/esm3/globalmap.hpp @@ -1,6 +1,7 @@ #ifndef OPENMW_COMPONENTS_ESM_GLOBALMAP_H #define OPENMW_COMPONENTS_ESM_GLOBALMAP_H +#include #include #include @@ -21,7 +22,7 @@ namespace ESM // The minimum and maximum cell coordinates struct Bounds { - int mMinX, mMaxX, mMinY, mMaxY; + int32_t mMinX, mMaxX, mMinY, mMaxY; }; Bounds mBounds; diff --git a/components/esm3/globalscript.cpp b/components/esm3/globalscript.cpp index 935bf35857..ca33428abd 100644 --- a/components/esm3/globalscript.cpp +++ b/components/esm3/globalscript.cpp @@ -3,6 +3,8 @@ #include "esmreader.hpp" #include "esmwriter.hpp" +#include + namespace ESM { @@ -12,8 +14,9 @@ namespace ESM mLocals.load(esm); - mRunning = 0; - esm.getHNOT(mRunning, "RUN_"); + int32_t running = 0; + esm.getHNOT(running, "RUN_"); + mRunning = running != 0; mTargetRef = RefNum{}; mTargetId = esm.getHNORefId("TARG"); @@ -33,7 +36,7 @@ namespace ESM mLocals.save(esm); if (mRunning) - esm.writeHNT("RUN_", mRunning); + esm.writeHNT("RUN_", int32_t{ 1 }); if (!mTargetId.empty()) { diff --git a/components/esm3/globalscript.hpp b/components/esm3/globalscript.hpp index deea622ea4..e2d0abdab6 100644 --- a/components/esm3/globalscript.hpp +++ b/components/esm3/globalscript.hpp @@ -16,7 +16,7 @@ namespace ESM { RefId mId; /// \note must be lowercase Locals mLocals; - int mRunning; + bool mRunning; RefId mTargetId; // for targeted scripts RefNum mTargetRef; diff --git a/components/esm3/journalentry.hpp b/components/esm3/journalentry.hpp index 770d8128c7..111754b39d 100644 --- a/components/esm3/journalentry.hpp +++ b/components/esm3/journalentry.hpp @@ -2,6 +2,8 @@ #define OPENMW_ESM_JOURNALENTRY_H #include + +#include #include namespace ESM @@ -20,15 +22,15 @@ namespace ESM Type_Quest = 2 }; - int mType; + int32_t mType; ESM::RefId mTopic; ESM::RefId mInfo; std::string mText; std::string mActorName; // Could also be Actor ID to allow switching of localisation, but since mText is // plaintext anyway... - int mDay; // time stamp - int mMonth; - int mDayOfMonth; + int32_t mDay; // time stamp + int32_t mMonth; + int32_t mDayOfMonth; void load(ESMReader& esm); void save(ESMWriter& esm) const; diff --git a/components/esm3/loadacti.hpp b/components/esm3/loadacti.hpp index be5b282934..9a2f98943e 100644 --- a/components/esm3/loadacti.hpp +++ b/components/esm3/loadacti.hpp @@ -3,6 +3,8 @@ #include "components/esm/defs.hpp" #include "components/esm/refid.hpp" + +#include #include namespace ESM @@ -18,7 +20,7 @@ namespace ESM /// Return a string descriptor for this record type. Currently used for debugging / error logs only. static std::string_view getRecordType() { return "Activator"; } - unsigned int mRecordFlags; + uint32_t mRecordFlags; RefId mId, mScript; std::string mName, mModel; diff --git a/components/esm3/loadalch.cpp b/components/esm3/loadalch.cpp index 77ab53ef3a..2b01dd9b09 100644 --- a/components/esm3/loadalch.cpp +++ b/components/esm3/loadalch.cpp @@ -36,7 +36,7 @@ namespace ESM mName = esm.getHString(); break; case fourCC("ALDT"): - esm.getHTSized<12>(mData); + esm.getHT(mData.mWeight, mData.mValue, mData.mAutoCalc); hasData = true; break; case fourCC("ENAM"): diff --git a/components/esm3/loadalch.hpp b/components/esm3/loadalch.hpp index d1defa204c..ddecd7e3c7 100644 --- a/components/esm3/loadalch.hpp +++ b/components/esm3/loadalch.hpp @@ -1,6 +1,7 @@ #ifndef OPENMW_ESM_ALCH_H #define OPENMW_ESM_ALCH_H +#include #include #include "components/esm/defs.hpp" @@ -27,12 +28,12 @@ namespace ESM struct ALDTstruct { float mWeight; - int mValue; - int mAutoCalc; + int32_t mValue; + int32_t mAutoCalc; }; ALDTstruct mData; - unsigned int mRecordFlags; + uint32_t mRecordFlags; RefId mId, mScript; std::string mName, mModel, mIcon; EffectList mEffects; diff --git a/components/esm3/loadappa.cpp b/components/esm3/loadappa.cpp index 793a51614b..ecc00222b8 100644 --- a/components/esm3/loadappa.cpp +++ b/components/esm3/loadappa.cpp @@ -28,7 +28,7 @@ namespace ESM mName = esm.getHString(); break; case fourCC("AADT"): - esm.getHTSized<16>(mData); + esm.getHT(mData.mType, mData.mQuality, mData.mWeight, mData.mValue); hasData = true; break; case fourCC("SCRI"): diff --git a/components/esm3/loadappa.hpp b/components/esm3/loadappa.hpp index d19fc0f1a0..e1f4655ffe 100644 --- a/components/esm3/loadappa.hpp +++ b/components/esm3/loadappa.hpp @@ -1,6 +1,7 @@ #ifndef OPENMW_ESM_APPA_H #define OPENMW_ESM_APPA_H +#include #include #include "components/esm/defs.hpp" @@ -33,14 +34,14 @@ namespace ESM struct AADTstruct { - int mType; + int32_t mType; float mQuality; float mWeight; - int mValue; + int32_t mValue; }; AADTstruct mData; - unsigned int mRecordFlags; + uint32_t mRecordFlags; RefId mId, mScript; std::string mName, mModel, mIcon; diff --git a/components/esm3/loadarmo.cpp b/components/esm3/loadarmo.cpp index fb934f9856..1832014173 100644 --- a/components/esm3/loadarmo.cpp +++ b/components/esm3/loadarmo.cpp @@ -59,7 +59,7 @@ namespace ESM mName = esm.getHString(); break; case fourCC("AODT"): - esm.getHTSized<24>(mData); + esm.getHT(mData.mType, mData.mWeight, mData.mValue, mData.mHealth, mData.mEnchant, mData.mArmor); hasData = true; break; case fourCC("SCRI"): diff --git a/components/esm3/loadarmo.hpp b/components/esm3/loadarmo.hpp index 40b931bd61..5a45269e52 100644 --- a/components/esm3/loadarmo.hpp +++ b/components/esm3/loadarmo.hpp @@ -1,6 +1,7 @@ #ifndef OPENMW_ESM_ARMO_H #define OPENMW_ESM_ARMO_H +#include #include #include @@ -90,15 +91,15 @@ namespace ESM struct AODTstruct { - int mType; + int32_t mType; float mWeight; - int mValue, mHealth, mEnchant, mArmor; + int32_t mValue, mHealth, mEnchant, mArmor; }; AODTstruct mData; PartReferenceList mParts; - unsigned int mRecordFlags; + uint32_t mRecordFlags; RefId mId, mScript, mEnchant; std::string mName, mModel, mIcon; diff --git a/components/esm3/loadbody.cpp b/components/esm3/loadbody.cpp index 8468fc5c90..066e5ec949 100644 --- a/components/esm3/loadbody.cpp +++ b/components/esm3/loadbody.cpp @@ -28,7 +28,7 @@ namespace ESM mRace = esm.getRefId(); break; case fourCC("BYDT"): - esm.getHTSized<4>(mData); + esm.getHT(mData.mPart, mData.mVampire, mData.mFlags, mData.mType); hasData = true; break; case SREC_DELE: diff --git a/components/esm3/loadbody.hpp b/components/esm3/loadbody.hpp index 0fadf190df..d839e894ce 100644 --- a/components/esm3/loadbody.hpp +++ b/components/esm3/loadbody.hpp @@ -4,6 +4,7 @@ #include "components/esm/defs.hpp" #include "components/esm/refid.hpp" +#include #include namespace ESM @@ -62,7 +63,7 @@ namespace ESM }; BYDTstruct mData; - unsigned int mRecordFlags; + uint32_t mRecordFlags; RefId mId, mRace; std::string mModel; diff --git a/components/esm3/loadbook.cpp b/components/esm3/loadbook.cpp index cc0e7d198c..8083c59828 100644 --- a/components/esm3/loadbook.cpp +++ b/components/esm3/loadbook.cpp @@ -28,7 +28,7 @@ namespace ESM mName = esm.getHString(); break; case fourCC("BKDT"): - esm.getHTSized<20>(mData); + esm.getHT(mData.mWeight, mData.mValue, mData.mIsScroll, mData.mSkillId, mData.mEnchant); hasData = true; break; case fourCC("SCRI"): diff --git a/components/esm3/loadbook.hpp b/components/esm3/loadbook.hpp index d2dbfea3ca..1c226e833c 100644 --- a/components/esm3/loadbook.hpp +++ b/components/esm3/loadbook.hpp @@ -3,6 +3,8 @@ #include "components/esm/defs.hpp" #include "components/esm/refid.hpp" + +#include #include namespace ESM @@ -24,12 +26,12 @@ namespace ESM struct BKDTstruct { float mWeight; - int mValue, mIsScroll, mSkillId, mEnchant; + int32_t mValue, mIsScroll, mSkillId, mEnchant; }; BKDTstruct mData; std::string mName, mModel, mIcon, mText; - unsigned int mRecordFlags; + uint32_t mRecordFlags; RefId mId; RefId mScript, mEnchant; diff --git a/components/esm3/loadbsgn.hpp b/components/esm3/loadbsgn.hpp index 8fbe20c774..9106060d59 100644 --- a/components/esm3/loadbsgn.hpp +++ b/components/esm3/loadbsgn.hpp @@ -1,6 +1,7 @@ #ifndef OPENMW_ESM_BSGN_H #define OPENMW_ESM_BSGN_H +#include #include #include "components/esm/defs.hpp" @@ -20,7 +21,7 @@ namespace ESM /// Return a string descriptor for this record type. Currently used for debugging / error logs only. static std::string_view getRecordType() { return "BirthSign"; } - unsigned int mRecordFlags; + uint32_t mRecordFlags; RefId mId; std::string mName, mDescription, mTexture; diff --git a/components/esm3/loadclas.cpp b/components/esm3/loadclas.cpp index 9569c1a61d..0c58f1d45a 100644 --- a/components/esm3/loadclas.cpp +++ b/components/esm3/loadclas.cpp @@ -11,12 +11,12 @@ namespace ESM const std::string_view Class::sGmstSpecializationIds[3] = { "sSpecializationCombat", "sSpecializationMagic", "sSpecializationStealth" }; - int& Class::CLDTstruct::getSkill(int index, bool major) + int32_t& Class::CLDTstruct::getSkill(int index, bool major) { return mSkills.at(index)[major ? 1 : 0]; } - int Class::CLDTstruct::getSkill(int index, bool major) const + int32_t Class::CLDTstruct::getSkill(int index, bool major) const { return mSkills.at(index)[major ? 1 : 0]; } @@ -41,7 +41,8 @@ namespace ESM mName = esm.getHString(); break; case fourCC("CLDT"): - esm.getHTSized<60>(mData); + esm.getHT( + mData.mAttribute, mData.mSpecialization, mData.mSkills, mData.mIsPlayable, mData.mServices); if (mData.mIsPlayable > 1) esm.fail("Unknown bool value"); hasData = true; diff --git a/components/esm3/loadclas.hpp b/components/esm3/loadclas.hpp index 4f5e9a41fa..47b8b73b7e 100644 --- a/components/esm3/loadclas.hpp +++ b/components/esm3/loadclas.hpp @@ -2,6 +2,7 @@ #define OPENMW_ESM_CLAS_H #include +#include #include #include "components/esm/defs.hpp" @@ -34,20 +35,20 @@ namespace ESM struct CLDTstruct { - std::array mAttribute; // Attributes that get class bonus - int mSpecialization; // 0 = Combat, 1 = Magic, 2 = Stealth - std::array, 5> mSkills; // Minor and major skills. - int mIsPlayable; // 0x0001 - Playable class - int mServices; + std::array mAttribute; // Attributes that get class bonus + int32_t mSpecialization; // 0 = Combat, 1 = Magic, 2 = Stealth + std::array, 5> mSkills; // Minor and major skills. + int32_t mIsPlayable; // 0x0001 - Playable class + int32_t mServices; - int& getSkill(int index, bool major); + int32_t& getSkill(int index, bool major); ///< Throws an exception for invalid values of \a index. - int getSkill(int index, bool major) const; + int32_t getSkill(int index, bool major) const; ///< Throws an exception for invalid values of \a index. }; // 60 bytes - unsigned int mRecordFlags; + uint32_t mRecordFlags; std::string mName, mDescription; RefId mId; CLDTstruct mData; diff --git a/components/esm3/loadclot.cpp b/components/esm3/loadclot.cpp index 14c881c84f..7d60c82197 100644 --- a/components/esm3/loadclot.cpp +++ b/components/esm3/loadclot.cpp @@ -30,7 +30,7 @@ namespace ESM mName = esm.getHString(); break; case fourCC("CTDT"): - esm.getHTSized<12>(mData); + esm.getHT(mData.mType, mData.mWeight, mData.mValue, mData.mEnchant); hasData = true; break; case fourCC("SCRI"): diff --git a/components/esm3/loadclot.hpp b/components/esm3/loadclot.hpp index 44ebc37575..5e441d0a9c 100644 --- a/components/esm3/loadclot.hpp +++ b/components/esm3/loadclot.hpp @@ -1,6 +1,7 @@ #ifndef OPENMW_ESM_CLOT_H #define OPENMW_ESM_CLOT_H +#include #include #include "components/esm/defs.hpp" @@ -40,16 +41,16 @@ namespace ESM struct CTDTstruct { - int mType; + int32_t mType; float mWeight; - unsigned short mValue; - unsigned short mEnchant; + uint16_t mValue; + uint16_t mEnchant; }; CTDTstruct mData; PartReferenceList mParts; - unsigned int mRecordFlags; + uint32_t mRecordFlags; RefId mId, mEnchant, mScript; std::string mModel, mIcon, mName; diff --git a/components/esm3/loaddoor.hpp b/components/esm3/loaddoor.hpp index dc5c8bc035..0a2feb01a9 100644 --- a/components/esm3/loaddoor.hpp +++ b/components/esm3/loaddoor.hpp @@ -1,6 +1,7 @@ #ifndef OPENMW_ESM_DOOR_H #define OPENMW_ESM_DOOR_H +#include #include #include "components/esm/defs.hpp" @@ -19,7 +20,7 @@ namespace ESM /// Return a string descriptor for this record type. Currently used for debugging / error logs only. static std::string_view getRecordType() { return "Door"; } - unsigned int mRecordFlags; + uint32_t mRecordFlags; RefId mId, mScript, mOpenSound, mCloseSound; std::string mName, mModel; diff --git a/components/esm3/loadench.cpp b/components/esm3/loadench.cpp index b72850daca..1d19b690f0 100644 --- a/components/esm3/loadench.cpp +++ b/components/esm3/loadench.cpp @@ -23,7 +23,7 @@ namespace ESM hasName = true; break; case fourCC("ENDT"): - esm.getHTSized<16>(mData); + esm.getHT(mData.mType, mData.mCost, mData.mCharge, mData.mFlags); hasData = true; break; case fourCC("ENAM"): diff --git a/components/esm3/loadench.hpp b/components/esm3/loadench.hpp index 4103f91ceb..6c6ff0c477 100644 --- a/components/esm3/loadench.hpp +++ b/components/esm3/loadench.hpp @@ -1,6 +1,7 @@ #ifndef OPENMW_ESM_ENCH_H #define OPENMW_ESM_ENCH_H +#include #include #include "components/esm/defs.hpp" @@ -39,13 +40,13 @@ namespace ESM struct ENDTstruct { - int mType; - int mCost; - int mCharge; - int mFlags; + int32_t mType; + int32_t mCost; + int32_t mCharge; + int32_t mFlags; }; - unsigned int mRecordFlags; + uint32_t mRecordFlags; RefId mId; ENDTstruct mData; EffectList mEffects;