1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Removed some usages of std string as resource path

This commit is contained in:
Ivan Savenko 2023-09-04 13:39:42 +03:00
parent 8dfdfffd87
commit 0f88b8969b
11 changed files with 49 additions and 48 deletions

View File

@ -59,10 +59,7 @@ AdventureMapWidget::AdventureMapWidget( std::shared_ptr<AdventureMapShortcuts> s
const JsonNode config(JsonPath::builtin("config/widgets/adventureMap.json"));
for(const auto & entry : config["options"]["imagesPlayerColored"].Vector())
{
ResourcePath resourceName(entry.String(), EResType::IMAGE);
playerColorerImages.push_back(resourceName.getName());
}
playerColorerImages.push_back(ImagePath::fromJson(entry));
build(config);
addUsedEvents(KEYBOARD);
@ -131,20 +128,20 @@ std::shared_ptr<IImage> AdventureMapWidget::loadImage(const JsonNode & name)
{
ImagePath resource = ImagePath::fromJson(name);
if(images.count(resource.getName()) == 0)
images[resource.getName()] = IImage::createFromFile(resource);
if(images.count(resource) == 0)
images[resource] = IImage::createFromFile(resource);
return images[resource.getName()];
return images[resource];
}
std::shared_ptr<CAnimation> AdventureMapWidget::loadAnimation(const JsonNode & name)
{
AnimationPath resource = AnimationPath::fromJson(name);
if(animations.count(resource.getName()) == 0)
animations[resource.getName()] = std::make_shared<CAnimation>(resource);
if(animations.count(resource) == 0)
animations[resource] = std::make_shared<CAnimation>(resource);
return animations[resource.getName()];
return animations[resource];
}
std::shared_ptr<CIntObject> AdventureMapWidget::buildInfobox(const JsonNode & input)

View File

@ -29,11 +29,11 @@ class AdventureMapWidget : public InterfaceObjectConfigurable
std::vector<Rect> subwidgetSizes;
/// list of images on which player-colored palette will be applied
std::vector<std::string> playerColorerImages;
std::vector<ImagePath> playerColorerImages;
/// list of named images shared between widgets
std::map<std::string, std::shared_ptr<IImage>> images;
std::map<std::string, std::shared_ptr<CAnimation>> animations;
std::map<ImagePath, std::shared_ptr<IImage>> images;
std::map<AnimationPath, std::shared_ptr<CAnimation>> animations;
/// Widgets that require access from adventure map
std::shared_ptr<CHeroList> heroList;

View File

@ -69,7 +69,7 @@ bool CAnimation::loadFrame(size_t frame, size_t group)
// still here? image is missing
printError(frame, group, "LoadFrame");
images[group][frame] = std::make_shared<SDLImage>("DEFAULT", EImageBlitMode::ALPHA);
images[group][frame] = std::make_shared<SDLImage>(ImagePath::builtin("DEFAULT"), EImageBlitMode::ALPHA);
}
else //load from separate file
{

View File

@ -21,7 +21,7 @@ namespace BitmapHandler
{
SDL_Surface * loadH3PCX(ui8 * data, size_t size);
SDL_Surface * loadBitmapFromDir(std::string path, std::string fname);
SDL_Surface * loadBitmapFromDir(const ImagePath & path);
}
bool isPCX(const ui8 *header)//check whether file can be PCX according to header
@ -103,28 +103,23 @@ SDL_Surface * BitmapHandler::loadH3PCX(ui8 * pcx, size_t size)
return ret;
}
SDL_Surface * BitmapHandler::loadBitmapFromDir(std::string path, std::string fname)
SDL_Surface * BitmapHandler::loadBitmapFromDir(const ImagePath & path)
{
if(!fname.size())
{
logGlobal->warn("Call to loadBitmap with void fname!");
return nullptr;
}
if (!CResourceHandler::get()->existsResource(ResourcePath(path + fname, EResType::IMAGE)))
if (!CResourceHandler::get()->existsResource(path))
{
return nullptr;
}
SDL_Surface * ret=nullptr;
auto readFile = CResourceHandler::get()->load(ResourcePath(path + fname, EResType::IMAGE))->readAll();
auto readFile = CResourceHandler::get()->load(path)->readAll();
if (isPCX(readFile.first.get()))
{//H3-style PCX
ret = loadH3PCX(readFile.first.get(), readFile.second);
if (!ret)
{
logGlobal->error("Failed to open %s as H3 PCX!", fname);
logGlobal->error("Failed to open %s as H3 PCX!", path.getOriginalName());
return nullptr;
}
}
@ -146,7 +141,7 @@ SDL_Surface * BitmapHandler::loadBitmapFromDir(std::string path, std::string fna
}
else
{
logGlobal->error("Failed to open %s via SDL_Image", fname);
logGlobal->error("Failed to open %s via SDL_Image", path.getOriginalName());
logGlobal->error("Reason: %s", IMG_GetError());
return nullptr;
}
@ -187,19 +182,29 @@ SDL_Surface * BitmapHandler::loadBitmapFromDir(std::string path, std::string fna
{
CSDL_Ext::setDefaultColorKey(ret);
}
return ret;
}
SDL_Surface * BitmapHandler::loadBitmap(std::string fname)
SDL_Surface * BitmapHandler::loadBitmap(const ImagePath & fname)
{
SDL_Surface * bitmap = nullptr;
if (!(bitmap = loadBitmapFromDir("DATA/", fname)) &&
!(bitmap = loadBitmapFromDir("SPRITES/", fname)))
if(fname.empty())
{
logGlobal->error("Error: Failed to find file %s", fname);
logGlobal->warn("Call to loadBitmap with void fname!");
return nullptr;
}
SDL_Surface * bitmap = loadBitmapFromDir(fname);
if (bitmap != nullptr)
return bitmap;
SDL_Surface * bitmapData = loadBitmapFromDir(fname.addPrefix("DATA/"));
if (bitmapData != nullptr)
return bitmapData;
SDL_Surface * bitmapSprites = loadBitmapFromDir(fname.addPrefix("SPRITES/"));
if (bitmapSprites != nullptr)
return bitmapSprites;
logGlobal->error("Error: Failed to find file %s", fname.getOriginalName());
return nullptr;
}

View File

@ -9,10 +9,12 @@
*/
#pragma once
#include "../../lib/filesystem/ResourcePath.h"
struct SDL_Surface;
namespace BitmapHandler
{
//Load file from /DATA or /SPRITES
SDL_Surface * loadBitmap(std::string fname);
SDL_Surface * loadBitmap(const ImagePath & fname);
}

View File

@ -31,7 +31,7 @@ std::shared_ptr<IImage> IImage::createFromFile( const ImagePath & path )
std::shared_ptr<IImage> IImage::createFromFile( const ImagePath & path, EImageBlitMode mode )
{
return std::shared_ptr<IImage>(new SDLImage(path.getName(), mode));
return std::shared_ptr<IImage>(new SDLImage(path, mode));
}
std::shared_ptr<IImage> IImage::createFromSurface( SDL_Surface * source )
@ -89,9 +89,7 @@ SDLImage::SDLImage(const JsonNode & conf, EImageBlitMode mode)
fullSize(0, 0),
originalPalette(nullptr)
{
std::string filename = conf["file"].String();
surf = BitmapHandler::loadBitmap(filename);
surf = BitmapHandler::loadBitmap(ImagePath::fromJson(conf["file"]));
if(surf == nullptr)
return;
@ -118,7 +116,7 @@ SDLImage::SDLImage(const JsonNode & conf, EImageBlitMode mode)
}
}
SDLImage::SDLImage(std::string filename, EImageBlitMode mode)
SDLImage::SDLImage(const ImagePath & filename, EImageBlitMode mode)
: surf(nullptr),
margins(0, 0),
fullSize(0, 0),
@ -128,7 +126,7 @@ SDLImage::SDLImage(std::string filename, EImageBlitMode mode)
if(surf == nullptr)
{
logGlobal->error("Error: failed to load image %s", filename);
logGlobal->error("Error: failed to load image %s", filename.getOriginalName());
return;
}
else

View File

@ -43,7 +43,7 @@ public:
//Load image from def file
SDLImage(CDefFile *data, size_t frame, size_t group=0);
//Load from bitmap file
SDLImage(std::string filename, EImageBlitMode blitMode);
SDLImage(const ImagePath & filename, EImageBlitMode blitMode);
SDLImage(const JsonNode & conf, EImageBlitMode blitMode);
//Create using existing surface, extraRef will increase refcount on SDL_Surface

View File

@ -618,7 +618,7 @@ static const std::string QUICK_EXCHANGE_BG = QUICK_EXCHANGE_MOD_PREFIX + "/TRADE
static bool isQuickExchangeLayoutAvailable()
{
return CResourceHandler::get()->existsResource(ResourcePath(std::string("SPRITES/") + QUICK_EXCHANGE_BG, EResType::IMAGE));
return CResourceHandler::get()->existsResource(ImagePath::builtin("SPRITES/" + QUICK_EXCHANGE_BG));
}
CExchangeController::CExchangeController(CExchangeWindow * view, ObjectInstanceID hero1, ObjectInstanceID hero2)

View File

@ -56,7 +56,7 @@ void MapIdentifiersH3M::loadMapping(const JsonNode & mapping)
std::string h3mName = boost::to_lower_copy(entryTemplate.second.String());
std::string vcmiName = boost::to_lower_copy(entryTemplate.first);
if (!CResourceHandler::get()->existsResource(ResourcePath( "SPRITES/" + vcmiName, EResType::ANIMATION)))
if (!CResourceHandler::get()->existsResource(AnimationPath::builtin("SPRITES/" + vcmiName)))
logMod->warn("Template animation file %s was not found!", vcmiName);
mappingObjectTemplate[h3mName] = vcmiName;

View File

@ -290,9 +290,8 @@ static ui32 calculateModChecksum(const std::string & modName, ISimpleResourceLoa
// third - add all detected text files from this mod into checksum
auto files = filesystem->getFilteredFiles([](const ResourcePath & resID)
{
return resID.getType() == EResType::TEXT &&
( boost::starts_with(resID.getName(), "DATA") ||
boost::starts_with(resID.getName(), "CONFIG"));
return (resID.getType() == EResType::TEXT || resID.getType() == EResType::JSON) &&
( boost::starts_with(resID.getName(), "DATA") || boost::starts_with(resID.getName(), "CONFIG"));
});
for (const ResourcePath & file : files)

View File

@ -169,7 +169,7 @@ DefFile::DefFile(std::string Name):
qRgba(0, 0, 0, 128), // 50% - shadow body below selection
qRgba(0, 0, 0, 64) // 75% - shadow border below selection
};
data = animationCache.getCachedFile(ResourcePath(std::string("SPRITES/") + Name, EResType::ANIMATION));
data = animationCache.getCachedFile(AnimationPath::builtin("SPRITES/" + Name));
palette = std::make_unique<QVector<QRgb>>(256);
int it = 0;
@ -656,7 +656,7 @@ Animation::Animation(std::string Name):
name.erase(dotPos);
std::transform(name.begin(), name.end(), name.begin(), toupper);
ResourcePath resource(std::string("SPRITES/") + name, EResType::ANIMATION);
auto resource = AnimationPath::builtin("SPRITES/" + name);
if(CResourceHandler::get()->existsResource(resource))
defFile = std::make_shared<DefFile>(name);