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-20 18:48:31 +02:00
|
|
|
void ConvertPcxToPng(bool deleteOriginals)
|
2022-11-19 00:03:46 +02:00
|
|
|
{
|
|
|
|
bfs::path imagesPath = VCMIDirs::get().userCachePath() / "extracted" / "Images";
|
|
|
|
bfs::directory_iterator end_iter;
|
|
|
|
|
|
|
|
for ( bfs::directory_iterator dir_itr(imagesPath); dir_itr != end_iter; ++dir_itr )
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
std::string filename = dir_itr->path().filename().string();
|
|
|
|
filename = boost::locale::to_lower(filename);
|
|
|
|
|
|
|
|
if(filename.find(".pcx") != std::string::npos)
|
|
|
|
{
|
|
|
|
auto img = BitmapHandler::loadBitmap(filename);
|
|
|
|
bfs::path pngFilePath = imagesPath / (fileStem + ".png");
|
|
|
|
img.save(QStringFromPath(pngFilePath), "PNG");
|
|
|
|
|
2022-11-20 18:48:31 +02:00
|
|
|
if (deleteOriginals)
|
2022-11-19 00:03:46 +02:00
|
|
|
bfs::remove(filePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch ( const std::exception & ex )
|
|
|
|
{
|
|
|
|
logGlobal->info(dir_itr->path().filename().string() + " " + ex.what() + "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// splits a def file into individual parts and converts the output to PNG format
|
2022-11-20 18:48:31 +02:00
|
|
|
void SplitDefFile(std::string fileName, bfs::path spritesPath, bool deleteOriginals)
|
2022-11-08 23:39:56 +02:00
|
|
|
{
|
|
|
|
if (CResourceHandler::get()->existsResource(ResourceID("SPRITES/" + fileName)))
|
|
|
|
{
|
|
|
|
std::string URI = fileName;
|
2022-11-14 21:32:29 +02:00
|
|
|
std::unique_ptr<Animation> anim = make_unique<Animation>(URI);
|
2022-11-08 23:39:56 +02:00
|
|
|
anim->preload();
|
|
|
|
anim->exportBitmaps(VCMIDirs::get().userCachePath() / "extracted", true);
|
|
|
|
|
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-19 00:03:46 +02:00
|
|
|
// splits def files (TwCrPort, CPRSMALL, FlagPort, ITPA, ITPt, Un32 and Un44), this way faction resources are independent
|
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";
|
|
|
|
bfs::path spritesPath = extractedPath / "Sprites";
|
|
|
|
|
2022-11-20 18:48:31 +02:00
|
|
|
SplitDefFile("TwCrPort.def", spritesPath, deleteOriginals); // split town creature portraits
|
|
|
|
SplitDefFile("CPRSMALL.def", spritesPath, deleteOriginals); // split hero army creature portraits
|
|
|
|
SplitDefFile("FlagPort.def", spritesPath, deleteOriginals); // adventure map dwellings
|
|
|
|
SplitDefFile("ITPA.def", spritesPath, deleteOriginals); // small town icons
|
|
|
|
SplitDefFile("ITPt.def", spritesPath, deleteOriginals); // big town icons
|
|
|
|
SplitDefFile("Un32.def", spritesPath, deleteOriginals); // big town icons
|
|
|
|
SplitDefFile("Un44.def", spritesPath, deleteOriginals); // big town icons
|
2022-11-08 23:39:56 +02:00
|
|
|
}
|
|
|
|
|
2022-11-20 18:48:31 +02:00
|
|
|
void ConvertExtractedResourceFiles(bool splitDefs, bool convertPcxToPng, bool deleteOriginals)
|
2022-11-08 23:39:56 +02:00
|
|
|
{
|
2022-11-20 18:48:31 +02:00
|
|
|
if (splitDefs)
|
|
|
|
splitDefFiles(deleteOriginals);
|
2022-11-08 23:39:56 +02:00
|
|
|
|
2022-11-20 18:48:31 +02:00
|
|
|
if (convertPcxToPng)
|
|
|
|
ConvertPcxToPng(deleteOriginals);
|
2022-11-19 00:03:46 +02:00
|
|
|
}
|