2022-11-22 18:55:18 +02:00
|
|
|
/*
|
2022-11-22 19:35:30 +02:00
|
|
|
* ResourceConverter.cpp, part of VCMI engine
|
2022-11-22 18:55:18 +02:00
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-11-08 23:39:56 +02:00
|
|
|
#include "StdInc.h"
|
|
|
|
|
|
|
|
#include "ResourceConverter.h"
|
|
|
|
|
|
|
|
#include "../lib/JsonNode.h"
|
|
|
|
#include "../lib/VCMIDirs.h"
|
|
|
|
#include "../lib/filesystem/Filesystem.h"
|
|
|
|
|
2022-11-19 00:03:46 +02:00
|
|
|
#include "BitmapHandler.h"
|
2022-11-14 21:32:29 +02:00
|
|
|
#include "Animation.h"
|
2022-11-08 23:39:56 +02:00
|
|
|
|
|
|
|
#include "boost/filesystem/path.hpp"
|
|
|
|
#include "boost/locale.hpp"
|
|
|
|
|
|
|
|
namespace bfs = boost::filesystem;
|
|
|
|
|
2022-11-19 00:03:46 +02:00
|
|
|
// converts all pcx files from /Images into PNG
|
2022-11-22 18:55:18 +02:00
|
|
|
void doConvertPcxToPng(bool deleteOriginals)
|
2022-11-19 00:03:46 +02:00
|
|
|
{
|
2022-11-22 18:55:18 +02:00
|
|
|
std::string filename;
|
|
|
|
|
|
|
|
bfs::path imagesPath = VCMIDirs::get().userCachePath() / "extracted" / "IMAGES";
|
2022-11-19 00:03:46 +02:00
|
|
|
bfs::directory_iterator end_iter;
|
|
|
|
|
2022-11-22 18:55:18 +02:00
|
|
|
for(bfs::directory_iterator dir_itr(imagesPath); dir_itr != end_iter; ++dir_itr)
|
2022-11-19 00:03:46 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!bfs::is_regular_file(dir_itr->status()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string filePath = dir_itr->path().string();
|
|
|
|
std::string fileStem = dir_itr->path().stem().string();
|
2022-11-22 18:55:18 +02:00
|
|
|
filename = dir_itr->path().filename().string();
|
|
|
|
std::string filenameLowerCase = boost::locale::to_lower(filename);
|
2022-11-19 00:03:46 +02:00
|
|
|
|
2022-11-22 18:55:18 +02:00
|
|
|
if(bfs::extension(filenameLowerCase) == ".pcx")
|
2022-11-19 00:03:46 +02:00
|
|
|
{
|
2022-11-22 18:55:18 +02:00
|
|
|
auto img = BitmapHandler::loadBitmap(filenameLowerCase);
|
2022-11-19 00:03:46 +02:00
|
|
|
bfs::path pngFilePath = imagesPath / (fileStem + ".png");
|
2022-11-22 18:55:18 +02:00
|
|
|
img.save(pathToQString(pngFilePath), "PNG");
|
2022-11-19 00:03:46 +02:00
|
|
|
|
2022-11-22 18:55:18 +02:00
|
|
|
if(deleteOriginals)
|
2022-11-19 00:03:46 +02:00
|
|
|
bfs::remove(filePath);
|
|
|
|
}
|
|
|
|
}
|
2022-11-22 18:55:18 +02:00
|
|
|
catch(const std::exception & ex)
|
2022-11-19 00:03:46 +02:00
|
|
|
{
|
2022-11-22 18:55:18 +02:00
|
|
|
logGlobal->info(filename + " " + ex.what() + "\n");
|
2022-11-19 00:03:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// splits a def file into individual parts and converts the output to PNG format
|
2022-11-22 18:55:18 +02:00
|
|
|
void splitDefFile(const std::string & fileName, const bfs::path & spritesPath, bool deleteOriginals)
|
2022-11-08 23:39:56 +02:00
|
|
|
{
|
2022-11-22 18:55:18 +02:00
|
|
|
if(CResourceHandler::get()->existsResource(ResourceID("SPRITES/" + fileName)))
|
2022-11-08 23:39:56 +02:00
|
|
|
{
|
2022-11-22 18:55:18 +02:00
|
|
|
std::unique_ptr<Animation> anim = make_unique<Animation>(fileName);
|
2022-11-08 23:39:56 +02:00
|
|
|
anim->preload();
|
2022-11-24 00:11:27 +02:00
|
|
|
anim->exportBitmaps(pathToQString(VCMIDirs::get().userCachePath() / "extracted"));
|
2022-11-08 23:39:56 +02:00
|
|
|
|
2022-11-20 18:48:31 +02:00
|
|
|
if(deleteOriginals)
|
2022-11-08 23:39:56 +02:00
|
|
|
bfs::remove(spritesPath / fileName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
logGlobal->error("Def File Split error! " + fileName);
|
|
|
|
}
|
|
|
|
|
2022-11-22 18:55:18 +02:00
|
|
|
// splits def files (TwCrPort, CPRSMALL, FlagPort, ITPA, ITPt, Un32 and Un44) so that faction resources are independent
|
|
|
|
// (town creature portraits, hero army creature portraits, adventure map dwellings, small town icons, big town icons,
|
|
|
|
// hero speciality small icons, hero speciality large icons)
|
2022-11-20 18:48:31 +02:00
|
|
|
void splitDefFiles(bool deleteOriginals)
|
2022-11-08 23:39:56 +02:00
|
|
|
{
|
|
|
|
bfs::path extractedPath = VCMIDirs::get().userDataPath() / "extracted";
|
2022-11-22 18:55:18 +02:00
|
|
|
bfs::path spritesPath = extractedPath / "SPRITES";
|
|
|
|
|
|
|
|
for(std::string defFilename : {"TwCrPort.def", "CPRSMALL.def", "FlagPort.def", "ITPA.def", "ITPt.def", "Un32.def", "Un44.def"})
|
|
|
|
splitDefFile(defFilename, spritesPath, deleteOriginals);
|
2022-11-08 23:39:56 +02:00
|
|
|
}
|
|
|
|
|
2022-11-22 18:55:18 +02:00
|
|
|
void convertExtractedResourceFiles(bool splitDefs, bool convertPcxToPng, bool deleteOriginals)
|
2022-11-08 23:39:56 +02:00
|
|
|
{
|
2022-11-22 18:55:18 +02:00
|
|
|
if(splitDefs)
|
2022-11-20 18:48:31 +02:00
|
|
|
splitDefFiles(deleteOriginals);
|
2022-11-08 23:39:56 +02:00
|
|
|
|
2022-11-22 18:55:18 +02:00
|
|
|
if(convertPcxToPng)
|
|
|
|
doConvertPcxToPng(deleteOriginals);
|
2022-11-19 00:03:46 +02:00
|
|
|
}
|