VolumeGC.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // Copyright 2008 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include <cstddef>
  5. #include <map>
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "Common/ColorUtil.h"
  10. #include "Common/CommonTypes.h"
  11. #include "Common/StringUtil.h"
  12. #include "Common/Logging/Log.h"
  13. #include "DiscIO/Blob.h"
  14. #include "DiscIO/FileMonitor.h"
  15. #include "DiscIO/Filesystem.h"
  16. #include "DiscIO/Volume.h"
  17. #include "DiscIO/VolumeGC.h"
  18. namespace DiscIO
  19. {
  20. CVolumeGC::CVolumeGC(IBlobReader* _pReader)
  21. : m_pReader(_pReader)
  22. {}
  23. CVolumeGC::~CVolumeGC()
  24. {
  25. }
  26. bool CVolumeGC::Read(u64 _Offset, u64 _Length, u8* _pBuffer, bool decrypt) const
  27. {
  28. if (decrypt)
  29. PanicAlertT("Tried to decrypt data from a non-Wii volume");
  30. if (m_pReader == nullptr)
  31. return false;
  32. FileMon::FindFilename(_Offset);
  33. return m_pReader->Read(_Offset, _Length, _pBuffer);
  34. }
  35. std::string CVolumeGC::GetUniqueID() const
  36. {
  37. static const std::string NO_UID("NO_UID");
  38. if (m_pReader == nullptr)
  39. return NO_UID;
  40. char ID[7];
  41. if (!Read(0, sizeof(ID), reinterpret_cast<u8*>(ID)))
  42. {
  43. PanicAlertT("Failed to read unique ID from disc image");
  44. return NO_UID;
  45. }
  46. ID[6] = '\0';
  47. return ID;
  48. }
  49. IVolume::ECountry CVolumeGC::GetCountry() const
  50. {
  51. if (!m_pReader)
  52. return COUNTRY_UNKNOWN;
  53. u8 country_code;
  54. m_pReader->Read(3, 1, &country_code);
  55. return CountrySwitch(country_code);
  56. }
  57. std::string CVolumeGC::GetMakerID() const
  58. {
  59. if (m_pReader == nullptr)
  60. return std::string();
  61. char makerID[3];
  62. if (!Read(0x4, 0x2, (u8*)&makerID))
  63. return std::string();
  64. makerID[2] = '\0';
  65. return makerID;
  66. }
  67. u16 CVolumeGC::GetRevision() const
  68. {
  69. if (!m_pReader)
  70. return 0;
  71. u8 revision;
  72. if (!Read(7, 1, &revision))
  73. return 0;
  74. return revision;
  75. }
  76. std::string CVolumeGC::GetInternalName() const
  77. {
  78. char name[0x60];
  79. if (m_pReader != nullptr && Read(0x20, 0x60, (u8*)name))
  80. return DecodeString(name);
  81. else
  82. return "";
  83. }
  84. std::map<IVolume::ELanguage, std::string> CVolumeGC::GetNames(bool prefer_long) const
  85. {
  86. return ReadMultiLanguageStrings(false, prefer_long);
  87. }
  88. std::map<IVolume::ELanguage, std::string> CVolumeGC::GetDescriptions() const
  89. {
  90. return ReadMultiLanguageStrings(true);
  91. }
  92. std::string CVolumeGC::GetCompany() const
  93. {
  94. if (!LoadBannerFile())
  95. return "";
  96. auto const pBanner = (GCBanner*)m_banner_file.data();
  97. std::string company = DecodeString(pBanner->comment[0].longMaker);
  98. if (company.empty())
  99. company = DecodeString(pBanner->comment[0].shortMaker);
  100. return company;
  101. }
  102. std::vector<u32> CVolumeGC::GetBanner(int* width, int* height) const
  103. {
  104. if (!LoadBannerFile())
  105. {
  106. *width = 0;
  107. *height = 0;
  108. return std::vector<u32>();
  109. }
  110. std::vector<u32> image_buffer(GC_BANNER_WIDTH * GC_BANNER_HEIGHT);
  111. auto const pBanner = (GCBanner*)m_banner_file.data();
  112. ColorUtil::decode5A3image(image_buffer.data(), pBanner->image, GC_BANNER_WIDTH, GC_BANNER_HEIGHT);
  113. *width = GC_BANNER_WIDTH;
  114. *height = GC_BANNER_HEIGHT;
  115. return image_buffer;
  116. }
  117. u64 CVolumeGC::GetFSTSize() const
  118. {
  119. if (m_pReader == nullptr)
  120. return 0;
  121. u32 size;
  122. if (!Read(0x428, 0x4, (u8*)&size))
  123. return 0;
  124. return Common::swap32(size);
  125. }
  126. std::string CVolumeGC::GetApploaderDate() const
  127. {
  128. if (m_pReader == nullptr)
  129. return std::string();
  130. char date[16];
  131. if (!Read(0x2440, 0x10, (u8*)&date))
  132. return std::string();
  133. // Should be 0 already, but just in case
  134. date[10] = '\0';
  135. return date;
  136. }
  137. u64 CVolumeGC::GetSize() const
  138. {
  139. if (m_pReader)
  140. return m_pReader->GetDataSize();
  141. else
  142. return 0;
  143. }
  144. u64 CVolumeGC::GetRawSize() const
  145. {
  146. if (m_pReader)
  147. return m_pReader->GetRawSize();
  148. else
  149. return 0;
  150. }
  151. u8 CVolumeGC::GetDiscNumber() const
  152. {
  153. u8 disc_number;
  154. Read(6, 1, &disc_number);
  155. return disc_number;
  156. }
  157. IVolume::EPlatform CVolumeGC::GetVolumeType() const
  158. {
  159. return GAMECUBE_DISC;
  160. }
  161. // Returns true if the loaded banner file is valid,
  162. // regardless of whether it was loaded by the current call
  163. bool CVolumeGC::LoadBannerFile() const
  164. {
  165. // The methods ReadMultiLanguageStrings, GetCompany and GetBanner
  166. // need to access the opening.bnr file. These methods are
  167. // usually called one after another. The file is cached in
  168. // RAM to avoid reading it from the disc several times, but
  169. // if none of these methods are called, the file is never loaded.
  170. // If opening.bnr has been loaded already, return immediately
  171. if (m_banner_file_type != BANNER_NOT_LOADED)
  172. return m_banner_file_type != BANNER_INVALID;
  173. std::unique_ptr<IFileSystem> file_system(CreateFileSystem(this));
  174. size_t file_size = (size_t)file_system->GetFileSize("opening.bnr");
  175. if (file_size == BNR1_SIZE || file_size == BNR2_SIZE)
  176. {
  177. m_banner_file.resize(file_size);
  178. file_system->ReadFile("opening.bnr", m_banner_file.data(), m_banner_file.size());
  179. u32 bannerSignature = *(u32*)m_banner_file.data();
  180. if (file_size == BNR1_SIZE && bannerSignature == 0x31524e42) // "BNR1"
  181. {
  182. m_banner_file_type = BANNER_BNR1;
  183. }
  184. else if (file_size == BNR2_SIZE && bannerSignature == 0x32524e42) // "BNR2"
  185. {
  186. m_banner_file_type = BANNER_BNR2;
  187. }
  188. else
  189. {
  190. m_banner_file_type = BANNER_INVALID;
  191. WARN_LOG(DISCIO, "Invalid opening.bnr. Type: %0x Size: %0lx", bannerSignature, (unsigned long)file_size);
  192. }
  193. }
  194. else
  195. {
  196. m_banner_file_type = BANNER_INVALID;
  197. WARN_LOG(DISCIO, "Invalid opening.bnr. Size: %0lx", (unsigned long)file_size);
  198. }
  199. return m_banner_file_type != BANNER_INVALID;
  200. }
  201. std::map<IVolume::ELanguage, std::string> CVolumeGC::ReadMultiLanguageStrings(bool description, bool prefer_long) const
  202. {
  203. std::map<ELanguage, std::string> strings;
  204. if (!LoadBannerFile())
  205. return strings;
  206. u32 number_of_languages = 0;
  207. ELanguage start_language;
  208. bool is_japanese = GetCountry() == ECountry::COUNTRY_JAPAN;
  209. switch (m_banner_file_type)
  210. {
  211. case BANNER_BNR1: // NTSC
  212. number_of_languages = 1;
  213. start_language = is_japanese ? ELanguage::LANGUAGE_JAPANESE : ELanguage::LANGUAGE_ENGLISH;
  214. break;
  215. case BANNER_BNR2: // PAL
  216. number_of_languages = 6;
  217. start_language = ELanguage::LANGUAGE_ENGLISH;
  218. break;
  219. // Shouldn't happen
  220. case BANNER_INVALID:
  221. case BANNER_NOT_LOADED:
  222. break;
  223. }
  224. auto const banner = reinterpret_cast<const GCBanner*>(m_banner_file.data());
  225. for (u32 i = 0; i < number_of_languages; ++i)
  226. {
  227. GCBannerComment comment = banner->comment[i];
  228. std::string string;
  229. if (description)
  230. {
  231. string = DecodeString(comment.comment);
  232. }
  233. else // Title
  234. {
  235. if (prefer_long)
  236. string = DecodeString(comment.longTitle);
  237. if (string.empty())
  238. string = DecodeString(comment.shortTitle);
  239. }
  240. if (!string.empty())
  241. strings[(ELanguage)(start_language + i)] = string;
  242. }
  243. return strings;
  244. }
  245. } // namespace