1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-28 23:06:24 +02:00

Merge pull request #4999 from MichalZr6/frames_from_def

Fix crash on missing DEF frames for in-game assets
This commit is contained in:
Ivan Savenko 2024-12-21 15:44:25 +02:00 committed by GitHub
commit 2be528ae36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 8 deletions

View File

@ -83,13 +83,12 @@ CDefFile::CDefFile(const AnimationPath & Name):
void CDefFile::loadFrame(size_t frame, size_t group, IImageLoader &loader) const
{
std::map<size_t, std::vector <size_t> >::const_iterator it;
it = offset.find(group);
assert (it != offset.end());
assert(hasFrame(frame, group)); // hasFrame() should be called before calling loadFrame()
const ui8 * FDef = data.get()+it->second[frame];
const ui8 * FDef = data.get() + offset.at(group)[frame];
const SSpriteDef sd = *reinterpret_cast<const SSpriteDef *>(FDef);
const SSpriteDef sd = * reinterpret_cast<const SSpriteDef *>(FDef);
SSpriteDef sprite;
sprite.format = read_le_u32(&sd.format);
@ -229,6 +228,23 @@ void CDefFile::loadFrame(size_t frame, size_t group, IImageLoader &loader) const
}
}
bool CDefFile::hasFrame(size_t frame, size_t group) const
{
std::map<size_t, std::vector <size_t> >::const_iterator it;
it = offset.find(group);
if(it == offset.end())
{
return false;
}
if(frame >= it->second.size())
{
return false;
}
return true;
}
CDefFile::~CDefFile() = default;
const std::map<size_t, size_t > CDefFile::getEntries() const

View File

@ -45,6 +45,7 @@ public:
//load frame as SDL_Surface
void loadFrame(size_t frame, size_t group, IImageLoader &loader) const;
bool hasFrame(size_t frame, size_t group) const;
const std::map<size_t, size_t> getEntries() const;
};

View File

@ -239,13 +239,13 @@ std::shared_ptr<const ISharedImage> RenderHandler::loadImageImpl(const ImageLoca
std::shared_ptr<const ISharedImage> RenderHandler::loadImageFromFileUncached(const ImageLocator & locator)
{
if (locator.image)
if(locator.image)
{
// TODO: create EmptySharedImage class that will be instantiated if image does not exists or fails to load
return std::make_shared<SDLImageShared>(*locator.image, locator.preScaledFactor);
}
if (locator.defFile)
if(locator.defFile)
{
auto defFile = getAnimationFile(*locator.defFile);
int preScaledFactor = locator.preScaledFactor;
@ -258,7 +258,14 @@ std::shared_ptr<const ISharedImage> RenderHandler::loadImageFromFileUncached(con
preScaledFactor = 1;
defFile = getAnimationFile(AnimationPath::builtin(tmpPath));
}
return std::make_shared<SDLImageShared>(defFile.get(), locator.defFrame, locator.defGroup, preScaledFactor);
if(defFile->hasFrame(locator.defFrame, locator.defGroup))
return std::make_shared<SDLImageShared>(defFile.get(), locator.defFrame, locator.defGroup, preScaledFactor);
else
{
logGlobal->error("Frame %d in group %d not found in file: %s",
locator.defFrame, locator.defGroup, locator.defFile->getName().c_str());
return std::make_shared<SDLImageShared>(ImagePath::builtin("DEFAULT"), locator.preScaledFactor);
}
}
throw std::runtime_error("Invalid image locator received!");