MediaDataInstaller.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <memory>
  17. #include <unordered_map>
  18. #include "DiskImage.h"
  19. #include "data/VGearsLGPArchive.h"
  20. #include "common/BinGZipFile.h"
  21. #include "TexFile.h"
  22. #include "ExternalTools.h"
  23. #include "TaskPool.h"
  24. /**
  25. * The media data installer.
  26. */
  27. class MediaDataInstaller{
  28. public:
  29. /**
  30. * Constructor
  31. *
  32. * @param[in] disk_image Disk image to extract the data from.
  33. * @param[in] output_dir Path to the directory of the installation data.
  34. * @param[in] keep_originals True to keep original data after conversion, false to remove.
  35. */
  36. MediaDataInstaller(const DiskImage& disk_image, const std::string output_dir, const bool keep_originals);
  37. /**
  38. * Destructor.
  39. */
  40. ~MediaDataInstaller();
  41. /**
  42. * Populates the audio name maps.
  43. */
  44. void PopulateMaps();
  45. /**
  46. * Extracts all images contained in the menu LGP file and installs them.
  47. *
  48. * For composed textures, it extracts individual images. All images are saved under the
  49. * "images" directory. This also includes font maps.
  50. */
  51. void InstallSprites();
  52. /**
  53. * Builds the font images from the textures in WINDOW.BIN and saves them in the "fonts" directory.
  54. *
  55. * The font XML files that describe them are part of the V-Gears data files. The fonts are always
  56. * needed, so this doesn't depend on the images option.
  57. *
  58. * @return True if all the fonts were saved, false if WINDOW.BIN couldn't be read.
  59. */
  60. bool InstallFonts();
  61. /**
  62. * Prepares the installer for the sounds extraction.
  63. *
  64. * @return The total number of sounds to process.
  65. */
  66. int InstallSoundsInit();
  67. /**
  68. * Extracts the next sound file contained in the audio.dat file and installs it.
  69. *
  70. * @return True if there are no more sounds to extract, false otherwise.
  71. */
  72. bool InstallSounds();
  73. /**
  74. * Writes the XML file with all audio entries.
  75. */
  76. void WriteSoundIndex();
  77. /**
  78. * Prepares the installer for the sounds extraction.
  79. *
  80. * @return The total number of sounds to process.
  81. */
  82. int InstallMusicsInit();
  83. /**
  84. * Extracts the next music file contained in the midi.lgp, converts it and installs it.
  85. *
  86. * @return True if there are no more musics to extract, false otherwise.
  87. */
  88. bool InstallMusics();
  89. /**
  90. * Converts high quality musiscs to OGG.
  91. *
  92. * There are four of them.
  93. */
  94. void InstallHQMusics();
  95. /**
  96. * Waits until every audio conversion started by the install steps has finished.
  97. *
  98. * The conversions run in the background, so the other steps don't wait for them. Call it before using or
  99. * deleting their results.
  100. */
  101. void WaitForConversions();
  102. /**
  103. * Writes the XML file with all music tracks.
  104. */
  105. void WriteMusicsIndex();
  106. /**
  107. * Copies the FMV videos.
  108. */
  109. void InstallVideos();
  110. private:
  111. /**
  112. * Runs a conversion in the background.
  113. *
  114. * A failed conversion is logged as a warning.
  115. *
  116. * @param[in] description What is being converted, for the log.
  117. * @param[in] job Does the conversion. Returns false, and sets the error, if it fails.
  118. * @param[in] on_success Called, in the background thread, if the conversion succeeds.
  119. */
  120. void RunConversion(
  121. const std::string& description, std::function<bool(std::string*)> job,
  122. std::function<void()> on_success = nullptr
  123. );
  124. /**
  125. * Finds the conversion programs the first time it is called, and reports what is missing.
  126. *
  127. * @param[in] need_timidity True if TiMidity is needed too (the music).
  128. * @return True if the programs are usable.
  129. */
  130. bool ToolsReady(bool need_timidity);
  131. /**
  132. * FFmpeg and TiMidity, found by {@see ToolsReady}.
  133. */
  134. ExternalTools::Tool ffmpeg_tool_;
  135. ExternalTools::Tool timidity_tool_;
  136. bool tools_resolved_ = false;
  137. /**
  138. * Threads that run the conversions. Created when the first conversion starts.
  139. */
  140. std::unique_ptr<TaskPool> conversions_;
  141. /**
  142. * Number of sound files to extract.
  143. */
  144. static int TOTAL_SOUNDS;
  145. /**
  146. * The disk image to read the original data from.
  147. */
  148. DiskImage disk_image_;
  149. /**
  150. * The path to the directory where to save the V-Gears data.
  151. */
  152. std::string output_dir_;
  153. /**
  154. * Directory where the game data is installed (<install dir>/data/).
  155. */
  156. std::string data_dir_;
  157. /**
  158. * The menu.lgp file.
  159. */
  160. VGears::LGPArchive menu_;
  161. /**
  162. * The WINDOW.BIN file.
  163. */
  164. BinGZipFile window_;
  165. /**
  166. * Number of sounds already processed.
  167. */
  168. int processed_sounds_;
  169. /**
  170. * Map for sound files with descriptive names.
  171. */
  172. std::unordered_map<int, std::string> sound_map_;
  173. /**
  174. * Map for sound files with human readable descriptions.
  175. */
  176. std::unordered_map<int, std::string> sound_description_;
  177. /**
  178. * Sound data for each entry to write to the XML file
  179. */
  180. std::vector<std::string> sounds_;
  181. /**
  182. * Video files to copy
  183. */
  184. std::vector<std::string> videos_;
  185. /**
  186. * The midi.lgp file.
  187. */
  188. VGears::LGPArchive midi_;
  189. /**
  190. * Number of music tracks already processed.
  191. */
  192. int processed_musics_;
  193. /**
  194. * Number of video files already processed.
  195. */
  196. int processed_videos_;
  197. /**
  198. * Map for music files with descriptive names.
  199. */
  200. std::unordered_map<int, std::string> musics_map_;
  201. /**
  202. * Sound data for each entry to write to the XML file
  203. */
  204. std::vector<std::string> musics_;
  205. /**
  206. * If true, orginal files will not be deleted after conversion.
  207. */
  208. bool keep_originals_;
  209. };