1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-10-16 04:46:33 +00:00

Merge branch 'fix_lazy_load_skyrim' into 'master'

Fix lazy load skyrim

See merge request OpenMW/openmw!2861
This commit is contained in:
psi29a 2023-03-30 21:42:55 +00:00
commit 0c074990ce
3 changed files with 41 additions and 25 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,22 +765,28 @@ 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);
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());
bool contextValid = cell.mReaderContext.filePos != std::streampos(-1);
if (contextValid)
readerESM4.restoreContext(cell.mReaderContext); readerESM4.restoreContext(cell.mReaderContext);
bool continueRead = true;
while (ESM::RefId::formIdRefId(readerESM4.getContext().currCell) == cell.mId && readerESM4.hasMoreRecs() while (
&& continueRead) (ESM::RefId::formIdRefId(readerESM4.currCell()) == cell.mId || !contextValid) && readerESM4.hasMoreRecs())
{ {
continueRead = ESM4::ReaderUtils::readItem( if (!contextValid)
readerESM4, readerESM4.exitGroupCheck();
[&](ESM4::Reader& reader) {
auto onRecord = [&](ESM4::Reader& reader) {
auto recordType = static_cast<ESM4::RecordTypes>(reader.hdr().record.typeId); auto recordType = static_cast<ESM4::RecordTypes>(reader.hdr().record.typeId);
ESM::RecNameInts esm4RecName = static_cast<ESM::RecNameInts>(ESM::esm4Recname(recordType)); ESM::RecNameInts esm4RecName = static_cast<ESM::RecNameInts>(ESM::esm4Recname(recordType));
if (esm4RecName == ESM::RecNameInts::REC_REFR4) if (esm4RecName == ESM::RecNameInts::REC_REFR4 && contextValid)
{ {
reader.getRecordData();
ESM4::Reference ref; ESM4::Reference ref;
ref.load(reader); ref.load(reader);
invocable(ref); invocable(ref);
@ -787,12 +794,19 @@ namespace MWWorld
} }
else if (esm4RecName == ESM::RecNameInts::REC_CELL4) else if (esm4RecName == ESM::RecNameInts::REC_CELL4)
{ {
reader.getRecordData();
ESM4::Cell cellToLoad; ESM4::Cell cellToLoad;
cellToLoad.load(reader); // This is necessary to exit cellToLoad.load(reader); // This is necessary to exit or to find the correct cell
if (cellToLoad.mId == cell.mId)
contextValid = true;
return true;
} }
return false; return false;
}, };
[&](ESM4::Reader& reader) {});
if (!ESM4::ReaderUtils::readItem(readerESM4, onRecord, [&](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 == std::streampos(-1))
return mCtx;
mCtx.filePos -= mCtx.recHeaderSize; // update file position mCtx.filePos -= mCtx.recHeaderSize; // update file position
return mCtx; return mCtx;
} }

View file

@ -62,7 +62,7 @@ namespace ESM4
// case the file was re-opened. default = TES5 size, // case the file was re-opened. default = TES5 size,
// can be reduced for TES4 by setRecHeaderSize() // can be reduced for TES4 by setRecHeaderSize()
std::size_t filePos; // assume that the record header will be re-read once std::streampos filePos; // assume that the record header will be re-read once
// the context is restored // the context is restored
// for keeping track of things // for keeping track of things