1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-06-27 20:11:34 +00:00

Initial changes to detect when context isn't usable

This commit is contained in:
florent.teppe 2023-03-26 11:34:21 +02:00
parent ba1f91661f
commit 34dd24b261
2 changed files with 41 additions and 24 deletions

View file

@ -6,6 +6,7 @@
#include <components/debug/debuglog.hpp> #include <components/debug/debuglog.hpp>
#include <components/esm/format.hpp>
#include <components/esm3/cellid.hpp> #include <components/esm3/cellid.hpp>
#include <components/esm3/cellref.hpp> #include <components/esm3/cellref.hpp>
#include <components/esm3/cellstate.hpp> #include <components/esm3/cellstate.hpp>
@ -764,35 +765,49 @@ namespace MWWorld
static void visitCell4References(const ESM4::Cell& cell, ESM::ReadersCache& readers, ReferenceInvocable&& invocable) static void visitCell4References(const ESM4::Cell& cell, ESM::ReadersCache& readers, ReferenceInvocable&& invocable)
{ {
auto stream = Files::openBinaryInputFileStream(cell.mReaderContext.filename); auto stream = Files::openBinaryInputFileStream(cell.mReaderContext.filename);
const ESM::Format format = ESM::readFormat(*stream);
assert(format == ESM::Tes4);
stream->seekg(0);
ESM4::Reader readerESM4( ESM4::Reader readerESM4(
std::move(stream), cell.mReaderContext.filename, MWBase::Environment::get().getResourceSystem()->getVFS()); std::move(stream), cell.mReaderContext.filename, MWBase::Environment::get().getResourceSystem()->getVFS());
readerESM4.setEncoder(readers.getStatelessEncoder()); readerESM4.setEncoder(readers.getStatelessEncoder());
readerESM4.restoreContext(cell.mReaderContext); bool contextValid = cell.mReaderContext.filePos != -1;
bool continueRead = true; if (contextValid)
while (ESM::RefId::formIdRefId(readerESM4.getContext().currCell) == cell.mId && readerESM4.hasMoreRecs() readerESM4.restoreContext(cell.mReaderContext);
&& continueRead)
while ((ESM::RefId::formIdRefId(readerESM4.getContext().currCell) == cell.mId || !contextValid)
&& readerESM4.hasMoreRecs())
{ {
continueRead = ESM4::ReaderUtils::readItem( readerESM4.exitGroupCheck();
readerESM4, if (!ESM4::ReaderUtils::readItem(
[&](ESM4::Reader& reader) { readerESM4,
auto recordType = static_cast<ESM4::RecordTypes>(reader.hdr().record.typeId); [&](ESM4::Reader& reader) {
ESM::RecNameInts esm4RecName = static_cast<ESM::RecNameInts>(ESM::esm4Recname(recordType)); auto recordType = static_cast<ESM4::RecordTypes>(reader.hdr().record.typeId);
if (esm4RecName == ESM::RecNameInts::REC_REFR4) ESM::RecNameInts esm4RecName = static_cast<ESM::RecNameInts>(ESM::esm4Recname(recordType));
{ if (esm4RecName == ESM::RecNameInts::REC_REFR4 && contextValid)
ESM4::Reference ref; {
ref.load(reader); ESM4::Reference ref;
invocable(ref); ref.load(reader);
return true; invocable(ref);
} return true;
else if (esm4RecName == ESM::RecNameInts::REC_CELL4) }
{ else if (esm4RecName == ESM::RecNameInts::REC_CELL4)
ESM4::Cell cellToLoad; {
cellToLoad.load(reader); // This is necessary to exit ESM4::Cell cellToLoad;
} cellToLoad.load(reader); // This is necessary to exit or to find the correct cell
return false; if (cellToLoad.mId == cell.mId)
}, contextValid = true;
[&](ESM4::Reader& reader) {}); return true;
}
return false;
},
[&](ESM4::Reader& reader) {}))
{
break;
}
} }
} }

View file

@ -134,6 +134,8 @@ namespace ESM4
ReaderContext Reader::getContext() ReaderContext Reader::getContext()
{ {
mCtx.filePos = mStream->tellg(); mCtx.filePos = mStream->tellg();
if (mCtx.filePos == -1)
return mCtx;
mCtx.filePos -= mCtx.recHeaderSize; // update file position mCtx.filePos -= mCtx.recHeaderSize; // update file position
return mCtx; return mCtx;
} }