Skip to content

Commit

Permalink
Merge branch 'backport_saves' into 'openmw-48'
Browse files Browse the repository at this point in the history
Allow OpenMW 0.48 to parse save headers from 0.49

See merge request OpenMW/openmw!3166
  • Loading branch information
psi29a committed Jun 20, 2023
2 parents 4132d14 + d9bdd9b commit 6ea1c2b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
38 changes: 38 additions & 0 deletions components/esm3/esmreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,44 @@ void ESMReader::clearCtx()
mCtx.subName.clear();
}

std::string ESMReader::getMaybeFixedStringSize(std::size_t size)
{
if (mHeader.mFormat > 22)
{
std::uint32_t storedSize = 0;
getT(storedSize);
if (storedSize > mCtx.leftSub)
fail("String does not fit subrecord (" + std::to_string(storedSize) + " > "
+ std::to_string(mCtx.leftSub) + ")");
size = static_cast<std::size_t>(storedSize);
}

return std::string(getStringView(size));
}

std::string_view ESMReader::getStringView(std::size_t size)
{
if (mBuffer.size() <= size)
// Add some extra padding to reduce the chance of having to resize
// again later.
mBuffer.resize(3 * size);

// And make sure the string is zero terminated
mBuffer[size] = 0;

// read ESM data
char* ptr = mBuffer.data();
getExact(ptr, size);

size = strnlen(ptr, size);

// Convert to UTF8 and return
if (mEncoder != nullptr)
return mEncoder->getUtf8(std::string_view(ptr, size));

return std::string_view(ptr, size);
}

void ESMReader::resolveParentFileIndices(ReadersCache& readers)
{
mCtx.parentFileIndices.clear();
Expand Down
3 changes: 3 additions & 0 deletions components/esm3/esmreader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class ESMReader
const std::string& getName() const { return mCtx.filename; };
bool isOpen() const { return mEsm != nullptr; }

std::string getMaybeFixedStringSize(std::size_t size);
std::string_view getStringView(std::size_t size);

/*************************************************************************
*
* Opening and closing
Expand Down
4 changes: 2 additions & 2 deletions components/esm3/loadtes3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ void Header::load (ESMReader &esm)
esm.getSubHeader();
esm.getT(mData.version);
esm.getT(mData.type);
mData.author.assign( esm.getString(32) );
mData.desc.assign( esm.getString(256) );
mData.author.assign(esm.getMaybeFixedStringSize(32));
mData.desc.assign(esm.getMaybeFixedStringSize(256));
esm.getT(mData.records);
}

Expand Down
4 changes: 4 additions & 0 deletions components/esm3/savedgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ void SavedGame::load (ESMReader &esm)
esm.getHNOT (mPlayerLevel, "PLLE");

mPlayerClassId = esm.getHNOString("PLCL");
// Erase RefId type
if (esm.getFormat() >= 22 && !mPlayerClassId.empty())
mPlayerClassId = mPlayerClassId.substr(1);

mPlayerClassName = esm.getHNOString("PLCN");

mPlayerCell = esm.getHNString("PLCE");
Expand Down

0 comments on commit 6ea1c2b

Please sign in to comment.