1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-21 19:19:26 +02:00

fix_crash_loading_frames_from_def

This commit is contained in:
MichalZr6 2024-11-27 23:07:45 +01:00
parent 132500b292
commit 5fc61df717
6 changed files with 61 additions and 12 deletions

View File

@ -38,7 +38,8 @@ enum class DefType : uint32_t
CDefFile::CDefFile(const AnimationPath & Name):
data(nullptr),
palette(nullptr)
palette(nullptr),
fileName(Name.getName())
{
data = CResourceHandler::get()->load(Name)->readAll().first;
@ -83,13 +84,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() + offset.at(group)[frame];
const ui8 * FDef = data.get()+it->second[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);
@ -227,6 +227,23 @@ void CDefFile::loadFrame(size_t frame, size_t group, IImageLoader &loader) const
logGlobal->error("Error: unsupported format of def file: %d", sprite.format);
break;
}
}
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;

View File

@ -43,8 +43,11 @@ public:
CDefFile(const AnimationPath & Name);
~CDefFile();
std::string fileName;
//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,7 +239,7 @@ 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);
@ -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<SDLEmptyImageShared>();
}
}
throw std::runtime_error("Invalid image locator received!");

View File

@ -99,6 +99,9 @@ SDLImageShared::SDLImageShared(const CDefFile * data, size_t frame, size_t group
SDLImageLoader loader(this);
data->loadFrame(frame, group, loader);
if(surf == nullptr)
return;
savePalette();
}

View File

@ -11,6 +11,7 @@
#include "../render/IImage.h"
#include "../../lib/Point.h"
#include <SDL_surface.h>
VCMI_LIB_NAMESPACE_BEGIN
class JsonNode;
@ -24,7 +25,7 @@ struct SDL_Palette;
/*
* Wrapper around SDL_Surface
*/
class SDLImageShared final : public ISharedImage, public std::enable_shared_from_this<SDLImageShared>, boost::noncopyable
class SDLImageShared : public ISharedImage, public std::enable_shared_from_this<SDLImageShared>, boost::noncopyable
{
//Surface without empty borders
SDL_Surface * surf;
@ -66,6 +67,20 @@ public:
friend class SDLImageLoader;
};
class SDLEmptyImageShared : public SDLImageShared
{
public:
SDLEmptyImageShared() : SDLImageShared(createDefaultSurface().get()) {}
private:
std::shared_ptr<SDL_Surface> createDefaultSurface()
{
auto surface = SDL_CreateRGBSurface(0, 32, 32, 32, 0, 0, 0, 0);
SDL_FillRect(surface, nullptr, SDL_MapRGB(surface->format, 255, 0, 0));
return std::shared_ptr<SDL_Surface>(surface, SDL_FreeSurface);
}
};
class SDLImageBase : public IImage, boost::noncopyable
{
protected:

View File

@ -38,6 +38,7 @@ void SDLImageLoader::init(Point SpriteSize, Point Margins, Point FullSize, SDL_C
SDL_FreePalette(p);
SDL_LockSurface(image->surf);
lineStart = position = (ui8*)image->surf->pixels;
}
@ -67,8 +68,11 @@ inline void SDLImageLoader::endLine()
SDLImageLoader::~SDLImageLoader()
{
SDL_UnlockSurface(image->surf);
SDL_SetColorKey(image->surf, SDL_TRUE, 0);
//TODO: RLE if compressed and bpp>1
if(image->surf)
{
SDL_UnlockSurface(image->surf);
SDL_SetColorKey(image->surf, SDL_TRUE, 0);
//TODO: RLE if compressed and bpp>1
}
}