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
|
|
|
|
2023-07-27 21:50:50 +02:00
|
|
|
#include <boost/filesystem/path.hpp>
|
|
|
|
#include <boost/locale.hpp>
|
2022-11-08 23:39:56 +02:00
|
|
|
|
2022-11-25 00:21:24 +02:00
|
|
|
void ResourceConverter::convertExtractedResourceFiles(ConversionOptions conversionOptions)
|
|
|
|
{
|
2023-07-28 14:51:14 +02:00
|
|
|
boost::filesystem::path spritesPath = VCMIDirs::get().userExtractedPath() / "SPRITES";
|
|
|
|
boost::filesystem::path imagesPath = VCMIDirs::get().userExtractedPath() / "IMAGES";
|
2023-01-17 01:07:57 +02:00
|
|
|
std::vector<std::string> defFiles = { "TwCrPort.def", "CPRSMALL.def", "FlagPort.def", "ITPA.def", "ITPt.def", "Un32.def", "Un44.def" };
|
|
|
|
|
|
|
|
if(conversionOptions.splitDefs)
|
2023-01-18 19:14:26 +02:00
|
|
|
splitDefFiles(defFiles, spritesPath, conversionOptions.deleteOriginals);
|
2022-11-25 00:21:24 +02:00
|
|
|
|
2023-01-17 01:07:57 +02:00
|
|
|
if(conversionOptions.convertPcxToPng)
|
|
|
|
doConvertPcxToPng(imagesPath, conversionOptions.deleteOriginals);
|
2022-11-25 00:21:24 +02:00
|
|
|
}
|
2022-11-08 23:39:56 +02:00
|
|
|
|
2023-07-28 14:51:14 +02:00
|
|
|
void ResourceConverter::doConvertPcxToPng(const boost::filesystem::path & sourceFolder, bool deleteOriginals)
|
2022-11-19 00:03:46 +02:00
|
|
|
{
|
2023-01-17 01:07:57 +02:00
|
|
|
logGlobal->info("Converting .pcx to .png from folder: %s ...\n", sourceFolder);
|
2022-11-19 00:03:46 +02:00
|
|
|
|
2023-07-28 14:51:14 +02:00
|
|
|
for(const auto & directoryEntry : boost::filesystem::directory_iterator(sourceFolder))
|
2022-11-19 00:03:46 +02:00
|
|
|
{
|
2023-01-17 01:07:57 +02:00
|
|
|
const auto filename = directoryEntry.path().filename();
|
2022-11-19 00:03:46 +02:00
|
|
|
try
|
|
|
|
{
|
2023-07-28 14:51:14 +02:00
|
|
|
if(!boost::filesystem::is_regular_file(directoryEntry))
|
2023-01-17 01:13:32 +02:00
|
|
|
continue;
|
2022-11-19 00:03:46 +02:00
|
|
|
|
2023-01-17 01:07:57 +02:00
|
|
|
std::string fileStem = directoryEntry.path().stem().string();
|
2023-01-14 15:27:11 +02:00
|
|
|
std::string filenameLowerCase = boost::algorithm::to_lower_copy(filename.string());
|
2022-11-19 00:03:46 +02:00
|
|
|
|
2023-01-14 15:27:11 +02:00
|
|
|
if(boost::algorithm::to_lower_copy(filename.extension().string()) == ".pcx")
|
2022-11-19 00:03:46 +02:00
|
|
|
{
|
2022-11-22 18:55:18 +02:00
|
|
|
auto img = BitmapHandler::loadBitmap(filenameLowerCase);
|
2023-07-28 14:51:14 +02:00
|
|
|
boost::filesystem::path pngFilePath = sourceFolder / (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)
|
2023-07-28 14:51:14 +02:00
|
|
|
boost::filesystem::remove(directoryEntry.path());
|
2022-11-19 00:03:46 +02:00
|
|
|
}
|
|
|
|
}
|
2023-01-17 01:07:57 +02:00
|
|
|
catch(const std::exception& ex)
|
2022-11-19 00:03:46 +02:00
|
|
|
{
|
2023-01-14 15:27:11 +02:00
|
|
|
logGlobal->info(filename.string() + " " + ex.what() + "\n");
|
2022-11-19 00:03:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-28 14:51:14 +02:00
|
|
|
void ResourceConverter::splitDefFile(const std::string & fileName, const boost::filesystem::path & sourceFolder, bool deleteOriginals)
|
2022-11-08 23:39:56 +02:00
|
|
|
{
|
2023-08-23 14:07:50 +02:00
|
|
|
if(CResourceHandler::get()->existsResource(ResourcePath("SPRITES/" + fileName)))
|
2022-11-08 23:39:56 +02:00
|
|
|
{
|
2022-12-07 23:36:20 +02:00
|
|
|
std::unique_ptr<Animation> anim = std::make_unique<Animation>(fileName);
|
2022-11-08 23:39:56 +02:00
|
|
|
anim->preload();
|
2023-01-17 01:07:57 +02:00
|
|
|
anim->exportBitmaps(pathToQString(sourceFolder));
|
2022-11-08 23:39:56 +02:00
|
|
|
|
2022-11-20 18:48:31 +02:00
|
|
|
if(deleteOriginals)
|
2023-07-28 14:51:14 +02:00
|
|
|
boost::filesystem::remove(sourceFolder / fileName);
|
2022-11-08 23:39:56 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
logGlobal->error("Def File Split error! " + fileName);
|
|
|
|
}
|
|
|
|
|
2023-07-28 14:51:14 +02:00
|
|
|
void ResourceConverter::splitDefFiles(const std::vector<std::string> & defFileNames, const boost::filesystem::path & sourceFolder, bool deleteOriginals)
|
2022-11-08 23:39:56 +02:00
|
|
|
{
|
2023-01-17 01:07:57 +02:00
|
|
|
logGlobal->info("Splitting Def Files from folder: %s ...\n", sourceFolder);
|
2022-11-22 18:55:18 +02:00
|
|
|
|
2023-01-18 19:14:26 +02:00
|
|
|
for(const auto & defFilename : defFileNames)
|
2023-01-17 01:07:57 +02:00
|
|
|
splitDefFile(defFilename, sourceFolder, deleteOriginals);
|
2022-12-07 23:36:20 +02:00
|
|
|
}
|