ResourceConverter.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "SDL.h"
  7. #include "./gui/CAnimation.h"
  8. #include "CBitmapHandler.h"
  9. #include "boost/filesystem/path.hpp"
  10. #include "boost/locale.hpp"
  11. namespace bfs = boost::filesystem;
  12. bool split_def_files = 1;
  13. bool convert_pcx_to_bmp = 1; // converts Images from .pcx to bmp. Can be used when you have .pcx converted already
  14. bool delete_source_files = 1; // delete source files or leave a copy in place.
  15. // converts all pcx files into bmp (H3 saves images as .pcx)
  16. void convertPcxToBmp()
  17. {
  18. bfs::path extractedPath = VCMIDirs::get().userDataPath() / "extracted";
  19. bfs::path imagesPath = extractedPath / "Images";
  20. bfs::directory_iterator end_iter;
  21. for ( bfs::directory_iterator dir_itr(imagesPath); dir_itr != end_iter; ++dir_itr )
  22. {
  23. try
  24. {
  25. if ( bfs::is_regular_file( dir_itr->status() ) )
  26. {
  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. SDL_Surface *bitmap;
  32. bitmap = BitmapHandler::loadBitmap(filename);
  33. if(delete_source_files)
  34. bfs::remove(imagesPath / filename);
  35. bfs::path outFilePath = imagesPath / filename;
  36. outFilePath.replace_extension(".bmp");
  37. SDL_SaveBMP(bitmap, outFilePath.string().c_str());
  38. }
  39. }
  40. else
  41. {
  42. logGlobal->info(dir_itr->path().filename().string() + " [other]\n");
  43. }
  44. }
  45. catch ( const std::exception & ex )
  46. {
  47. logGlobal->info(dir_itr->path().filename().string() + " " + ex.what() + "\n");
  48. }
  49. }
  50. }
  51. // splits a def file into individual parts
  52. void splitDefFile(std::string fileName, bfs::path spritesPath)
  53. {
  54. if (CResourceHandler::get()->existsResource(ResourceID("SPRITES/" + fileName)))
  55. {
  56. std::string URI = fileName;
  57. std::unique_ptr<CAnimation> anim = make_unique<CAnimation>(URI);
  58. anim->preload();
  59. anim->exportBitmaps(VCMIDirs::get().userCachePath() / "extracted", true);
  60. if(delete_source_files)
  61. bfs::remove(spritesPath / fileName);
  62. }
  63. else
  64. logGlobal->error("Def File Split error! " + fileName);
  65. }
  66. // split def files, this way faction resources are independent
  67. void splitDefFiles()
  68. {
  69. bfs::path extractedPath = VCMIDirs::get().userDataPath() / "extracted";
  70. bfs::path spritesPath = extractedPath / "Sprites";
  71. splitDefFile("TwCrPort.def", spritesPath); // split town creature portraits
  72. splitDefFile("CPRSMALL.def", spritesPath); // split hero army creature portraits
  73. splitDefFile("FlagPort.def", spritesPath); // adventure map dwellings
  74. splitDefFile("ITPA.def", spritesPath); // small town icons
  75. splitDefFile("ITPt.def", spritesPath); // big town icons
  76. splitDefFile("Un32.def", spritesPath); // big town icons
  77. splitDefFile("Un44.def", spritesPath); // big town icons
  78. }
  79. // Splits def files that are shared between factions and converts pcx to bmp
  80. void ConvertOriginalResourceFiles()
  81. {
  82. if (split_def_files)
  83. splitDefFiles();
  84. if (convert_pcx_to_bmp)
  85. convertPcxToBmp();
  86. }