ResourceConverter.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "StdInc.h"
  2. #include "ResourceConverter.h"
  3. #include "../lib/JsonNode.h"
  4. #include "../lib/VCMIDirs.h"
  5. #include "../lib/filesystem/Filesystem.h"
  6. #include "BitmapHandler.h"
  7. #include "Animation.h"
  8. #include "boost/filesystem/path.hpp"
  9. #include "boost/locale.hpp"
  10. namespace bfs = boost::filesystem;
  11. bool split_def_files = false; // splits TwCrPort, CPRSMALL, FlagPort, ITPA, ITPt, Un32 and Un44 into individual PNG's
  12. bool convert_pcx_to_png = false; // converts single Images (found in Images folder) from .pcx to png.
  13. bool delete_source_files = false; // delete source files after splitting / conversion.
  14. // converts all pcx files from /Images into PNG
  15. void convertPcxToPng()
  16. {
  17. bfs::path imagesPath = VCMIDirs::get().userCachePath() / "extracted" / "Images";
  18. bfs::directory_iterator end_iter;
  19. for ( bfs::directory_iterator dir_itr(imagesPath); dir_itr != end_iter; ++dir_itr )
  20. {
  21. try
  22. {
  23. if (!bfs::is_regular_file(dir_itr->status()))
  24. return;
  25. std::string filePath = dir_itr->path().string();
  26. std::string fileStem = dir_itr->path().stem().string();
  27. std::string filename = dir_itr->path().filename().string();
  28. filename = boost::locale::to_lower(filename);
  29. if(filename.find(".pcx") != std::string::npos)
  30. {
  31. auto img = BitmapHandler::loadBitmap(filename);
  32. bfs::path pngFilePath = imagesPath / (fileStem + ".png");
  33. img.save(QStringFromPath(pngFilePath), "PNG");
  34. if (delete_source_files)
  35. bfs::remove(filePath);
  36. }
  37. }
  38. catch ( const std::exception & ex )
  39. {
  40. logGlobal->info(dir_itr->path().filename().string() + " " + ex.what() + "\n");
  41. }
  42. }
  43. }
  44. // splits a def file into individual parts and converts the output to PNG format
  45. void splitDefFile(std::string fileName, bfs::path spritesPath)
  46. {
  47. if (CResourceHandler::get()->existsResource(ResourceID("SPRITES/" + fileName)))
  48. {
  49. std::string URI = fileName;
  50. std::unique_ptr<Animation> anim = make_unique<Animation>(URI);
  51. anim->preload();
  52. anim->exportBitmaps(VCMIDirs::get().userCachePath() / "extracted", true);
  53. if(delete_source_files)
  54. bfs::remove(spritesPath / fileName);
  55. }
  56. else
  57. logGlobal->error("Def File Split error! " + fileName);
  58. }
  59. // splits def files (TwCrPort, CPRSMALL, FlagPort, ITPA, ITPt, Un32 and Un44), this way faction resources are independent
  60. void splitDefFiles()
  61. {
  62. bfs::path extractedPath = VCMIDirs::get().userDataPath() / "extracted";
  63. bfs::path spritesPath = extractedPath / "Sprites";
  64. splitDefFile("TwCrPort.def", spritesPath); // split town creature portraits
  65. splitDefFile("CPRSMALL.def", spritesPath); // split hero army creature portraits
  66. splitDefFile("FlagPort.def", spritesPath); // adventure map dwellings
  67. splitDefFile("ITPA.def", spritesPath); // small town icons
  68. splitDefFile("ITPt.def", spritesPath); // big town icons
  69. splitDefFile("Un32.def", spritesPath); // big town icons
  70. splitDefFile("Un44.def", spritesPath); // big town icons
  71. }
  72. // Splits def files that are shared between factions and converts pcx to bmp
  73. void ConvertOriginalResourceFiles()
  74. {
  75. if (split_def_files)
  76. splitDefFiles();
  77. if (convert_pcx_to_png)
  78. convertPcxToPng();
  79. }