mirror of
https://github.com/vcmi/vcmi.git
synced 2025-03-03 14:52:11 +02:00
Refactored resource Convertor to be more easy on the eyes
Added a little more documentation to functions. Not using anymore dyrectory_iterator but directory_entry. Private functions made a little more generic, extracting some parametrers to higher levels. Export Bitmaps is no longer tied to SPRITES folder.
This commit is contained in:
parent
3c403bcda3
commit
8f433d345a
@ -694,7 +694,7 @@ void Animation::exportBitmaps(const QDir & path) const
|
||||
return;
|
||||
}
|
||||
|
||||
QString actualPath = path.absolutePath() + "/SPRITES/" + QString::fromStdString(name);
|
||||
QString actualPath = path.absolutePath() + "/" + QString::fromStdString(name);
|
||||
QDir().mkdir(actualPath);
|
||||
|
||||
size_t counter = 0;
|
||||
|
@ -24,64 +24,69 @@
|
||||
|
||||
void ResourceConverter::convertExtractedResourceFiles(ConversionOptions conversionOptions)
|
||||
{
|
||||
if (conversionOptions.splitDefs)
|
||||
splitDefFiles(conversionOptions.deleteOriginals);
|
||||
bfs::path spritesPath = VCMIDirs::get().userExtractedPath() / "SPRITES";
|
||||
bfs::path imagesPath = VCMIDirs::get().userExtractedPath() / "IMAGES";
|
||||
std::vector<std::string> defFiles = { "TwCrPort.def", "CPRSMALL.def", "FlagPort.def", "ITPA.def", "ITPt.def", "Un32.def", "Un44.def" };
|
||||
|
||||
if (conversionOptions.convertPcxToPng)
|
||||
doConvertPcxToPng(conversionOptions.deleteOriginals);
|
||||
if(conversionOptions.splitDefs)
|
||||
splitDefFiles(spritesPath, defFiles, conversionOptions.deleteOriginals);
|
||||
|
||||
if(conversionOptions.convertPcxToPng)
|
||||
doConvertPcxToPng(imagesPath, conversionOptions.deleteOriginals);
|
||||
}
|
||||
|
||||
void ResourceConverter::doConvertPcxToPng(bool deleteOriginals)
|
||||
void ResourceConverter::doConvertPcxToPng(const bfs::path & sourceFolder, bool deleteOriginals)
|
||||
{
|
||||
bfs::path imagesPath = VCMIDirs::get().userExtractedPath() / "IMAGES";
|
||||
bfs::directory_iterator end_iter;
|
||||
logGlobal->info("Converting .pcx to .png from folder: %s ...\n", sourceFolder);
|
||||
|
||||
for(bfs::directory_iterator dir_itr(imagesPath); dir_itr != end_iter; ++dir_itr)
|
||||
for(bfs::directory_entry & directoryEntry : bfs::directory_iterator(sourceFolder))
|
||||
{
|
||||
const auto filename = dir_itr->path().filename();
|
||||
const auto filename = directoryEntry.path().filename();
|
||||
try
|
||||
{
|
||||
if (!bfs::is_regular_file(dir_itr->status()))
|
||||
if(!bfs::is_regular_file(directoryEntry))
|
||||
return;
|
||||
|
||||
std::string filePath = directoryEntry.path().string();
|
||||
std::string fileStem = directoryEntry.path().stem().string();
|
||||
std::string filenameLowerCase = boost::algorithm::to_lower_copy(filename.string());
|
||||
|
||||
if(boost::algorithm::to_lower_copy(filename.extension().string()) == ".pcx")
|
||||
{
|
||||
auto img = BitmapHandler::loadBitmap(filenameLowerCase);
|
||||
bfs::path pngFilePath = imagesPath / (dir_itr->path().stem().string() + ".png");
|
||||
bfs::path pngFilePath = sourceFolder / (fileStem + ".png");
|
||||
img.save(pathToQString(pngFilePath), "PNG");
|
||||
|
||||
if(deleteOriginals)
|
||||
bfs::remove(dir_itr->path());
|
||||
bfs::remove(directoryEntry.path());
|
||||
}
|
||||
}
|
||||
catch(const std::exception & ex)
|
||||
catch(const std::exception& ex)
|
||||
{
|
||||
logGlobal->info(filename.string() + " " + ex.what() + "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ResourceConverter::splitDefFile(const std::string & fileName, const bfs::path & spritesPath, bool deleteOriginals)
|
||||
void ResourceConverter::splitDefFile(const std::string & fileName, const bfs::path & sourceFolder, bool deleteOriginals)
|
||||
{
|
||||
if(CResourceHandler::get()->existsResource(ResourceID("SPRITES/" + fileName)))
|
||||
{
|
||||
std::unique_ptr<Animation> anim = std::make_unique<Animation>(fileName);
|
||||
anim->preload();
|
||||
anim->exportBitmaps(pathToQString(VCMIDirs::get().userExtractedPath()));
|
||||
anim->exportBitmaps(pathToQString(sourceFolder));
|
||||
|
||||
if(deleteOriginals)
|
||||
bfs::remove(spritesPath / fileName);
|
||||
bfs::remove(sourceFolder / fileName);
|
||||
}
|
||||
else
|
||||
logGlobal->error("Def File Split error! " + fileName);
|
||||
}
|
||||
|
||||
void ResourceConverter::splitDefFiles(bool deleteOriginals)
|
||||
void ResourceConverter::splitDefFiles(const bfs::path & sourceFolder, std::vector<std::string> defFileNames, bool deleteOriginals)
|
||||
{
|
||||
bfs::path spritesPath = VCMIDirs::get().userExtractedPath() / "SPRITES";
|
||||
logGlobal->info("Splitting Def Files from folder: %s ...\n", sourceFolder);
|
||||
|
||||
for(std::string defFilename : {"TwCrPort.def", "CPRSMALL.def", "FlagPort.def", "ITPA.def", "ITPt.def", "Un32.def", "Un44.def"})
|
||||
splitDefFile(defFilename, spritesPath, deleteOriginals);
|
||||
for(std::string defFilename : defFileNames)
|
||||
splitDefFile(defFilename, sourceFolder, deleteOriginals);
|
||||
}
|
||||
|
@ -24,26 +24,34 @@ struct ExtractionOptions
|
||||
{
|
||||
bool extractArchives = false; // if set, original H3 archives will be extracted into a separate folder
|
||||
ConversionOptions conversionOptions;
|
||||
bool moveExtractedArchivesToSoDMod = false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Class functionality to be used after extracting original H3 resources and before moving those resources to SoD Mod
|
||||
* Splits def files containing individual images, so that faction resources are independent. (TwCrPort, CPRSMALL, FlagPort, ITPA, ITPt, Un32 and Un44)
|
||||
* (town creature portraits, hero army creature portraits, adventure map dwellings, small town icons, big town icons,
|
||||
* hero speciality small icons, hero speciality large icons)
|
||||
* Converts all PCX images to PNG
|
||||
*/
|
||||
class ResourceConverter
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
// Splits def files that are shared between factions and converts pcx to bmp depending on Extraction Options
|
||||
// Splits def files that are shared between factions and converts pcx to PNG depending on Extraction Options
|
||||
static void convertExtractedResourceFiles(ConversionOptions conversionOptions);
|
||||
|
||||
private:
|
||||
|
||||
// converts all pcx files from /Images into PNG
|
||||
static void doConvertPcxToPng(bool deleteOriginals);
|
||||
/** Converts all .pcx from extractedFolder/Images into .png */
|
||||
static void doConvertPcxToPng(const bfs::path & sourceFolder, bool deleteOriginals);
|
||||
|
||||
// splits a def file into individual parts and converts the output to PNG format
|
||||
static void splitDefFile(const std::string& fileName, const bfs::path& spritesPath, bool deleteOriginals);
|
||||
/** splits a .def file into individual images and converts the output to PNG format */
|
||||
static void splitDefFile(const std::string & fileName, const bfs::path & sourceFolder, bool deleteOriginals);
|
||||
|
||||
// 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)
|
||||
static void splitDefFiles(bool deleteOriginals);
|
||||
};
|
||||
/** Splits the given .def files into individual images.
|
||||
* For each .def file, the resulting images will be output in the same folder, in a subfolder (named just like the .def file)
|
||||
*/
|
||||
static void splitDefFiles(const bfs::path & sourceFolder, std::vector<std::string> defFileNames, bool deleteOriginals);
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user