AppItem.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include <cpp3ds/System/Err.hpp>
  2. #include <iostream>
  3. #include <cpp3ds/System/I18n.hpp>
  4. #include "AssetManager.hpp"
  5. #include "AppItem.hpp"
  6. #include "Util.hpp"
  7. #include "TitleKeys.hpp"
  8. #include "DownloadQueue.hpp"
  9. #include <sstream>
  10. namespace {
  11. std::vector<std::unique_ptr<cpp3ds::Texture>> textures;
  12. }
  13. namespace FreeShop
  14. {
  15. AppItem::AppItem()
  16. : m_installed(false)
  17. , m_regions(0)
  18. , m_languages(0)
  19. {
  20. //
  21. }
  22. const cpp3ds::String &AppItem::getTitle() const
  23. {
  24. return m_title;
  25. }
  26. void AppItem::loadFromJSON(const char* titleId, const rapidjson::Value &json)
  27. {
  28. m_genres.clear();
  29. m_features.clear();
  30. m_seed.clear();
  31. m_languages = 0;
  32. m_titleIdStr = titleId;
  33. m_titleId = strtoull(titleId, 0, 16);
  34. m_updates = TitleKeys::getRelated(m_titleId, TitleKeys::Update);
  35. m_demos = TitleKeys::getRelated(m_titleId, TitleKeys::Demo);
  36. m_dlc = TitleKeys::getRelated(m_titleId, TitleKeys::DLC);
  37. const char *title = json[0].GetString();
  38. m_title = cpp3ds::String::fromUtf8(title, title + json[0].GetStringLength());
  39. m_normalizedTitle = json[1].GetString();
  40. m_contentId = json[2].GetString();
  41. m_regions = json[3].GetInt();
  42. m_uriRegion = json[4].GetString();
  43. m_filesize = json[5].GetUint64();
  44. int iconIndex = json[6].GetInt();
  45. if (iconIndex >= 0)
  46. setIconIndex(iconIndex);
  47. // Crypto seed
  48. std::string seed = json[7].GetString();
  49. if (!seed.empty())
  50. {
  51. m_seed.clear();
  52. for (int i = 0; i < 16; ++i)
  53. {
  54. std::string byteStr = seed.substr(i*2, 2);
  55. char byte = strtol(byteStr.c_str(), NULL, 16);
  56. m_seed.push_back(byte);
  57. }
  58. }
  59. // Genres
  60. auto genres = json[8].GetArray();
  61. for (int i = 0; i < genres.Size(); ++i)
  62. m_genres.push_back(genres[i].GetInt());
  63. // Languages
  64. auto languages = json[9].GetArray();
  65. for (int i = 0; i < languages.Size(); ++i)
  66. {
  67. std::string lang(languages[i].GetString());
  68. if ("ja" == lang) m_languages |= Japanese;
  69. else if ("en" == lang) m_languages |= English;
  70. else if ("es" == lang) m_languages |= Spanish;
  71. else if ("fr" == lang) m_languages |= French;
  72. else if ("de" == lang) m_languages |= German;
  73. else if ("it" == lang) m_languages |= Italian;
  74. else if ("nl" == lang) m_languages |= Dutch;
  75. else if ("pt" == lang) m_languages |= Portuguese;
  76. else if ("ru" == lang) m_languages |= Russian;
  77. }
  78. // Features
  79. auto features = json[10].GetArray();
  80. for (int i = 0; i < features.Size(); ++i)
  81. m_features.push_back(features[i].GetInt());
  82. if (!json[11].IsNull())
  83. m_voteScore = json[11].GetFloat();
  84. m_voteCount = json[12].GetInt();
  85. m_releaseDate = json[13].GetInt();
  86. m_productCode = json[14].GetString();
  87. m_platform = json[15].GetInt();
  88. m_publisher = json[16].GetInt();
  89. }
  90. void AppItem::setIconIndex(size_t iconIndex)
  91. {
  92. size_t textureIndex = iconIndex / 441;
  93. iconIndex %= 441;
  94. // Load texture from file if not done already
  95. if (textures.size() < textureIndex + 1)
  96. {
  97. for (int i = textures.size(); i <= textureIndex; ++i)
  98. {
  99. std::unique_ptr<cpp3ds::Texture> texture(new cpp3ds::Texture());
  100. #ifdef EMULATION
  101. texture->loadFromFile(_(FREESHOP_DIR "/cache/images/icons%d.jpg", i));
  102. #else
  103. texture->loadFromPreprocessedFile(_(FREESHOP_DIR "/cache/images/icons%d.bin", i), 1024, 1024, GPU_ETC1);
  104. #endif
  105. texture->setSmooth(true);
  106. textures.push_back(std::move(texture));
  107. }
  108. }
  109. m_iconIndex = iconIndex;
  110. m_iconTexture = textures[textureIndex].get();
  111. int x = (iconIndex / 21) * 48;
  112. int y = (iconIndex % 21) * 48;
  113. m_iconRect = cpp3ds::IntRect(x, y, 48, 48);
  114. }
  115. bool AppItem::isCached() const
  116. {
  117. return pathExists(getJsonFilename().c_str());
  118. }
  119. const std::string AppItem::getJsonFilename() const
  120. {
  121. std::string filename = FREESHOP_DIR "/tmp/" + getTitleIdStr() + "/data.json";
  122. return filename;
  123. }
  124. void AppItem::setInstalled(bool installed)
  125. {
  126. m_installed = installed;
  127. }
  128. bool AppItem::isInstalled() const
  129. {
  130. return m_installed;
  131. }
  132. cpp3ds::Uint64 AppItem::getFilesize() const
  133. {
  134. return m_filesize;
  135. }
  136. const std::string &AppItem::getNormalizedTitle() const
  137. {
  138. return m_normalizedTitle;
  139. }
  140. const std::string &AppItem::getTitleIdStr() const
  141. {
  142. return m_titleIdStr;
  143. }
  144. cpp3ds::Uint64 AppItem::getTitleId() const
  145. {
  146. return m_titleId;
  147. }
  148. const std::string &AppItem::getContentId() const
  149. {
  150. return m_contentId;
  151. }
  152. const std::string &AppItem::getUriRegion() const
  153. {
  154. return m_uriRegion;
  155. }
  156. int AppItem::getRegions() const
  157. {
  158. return m_regions;
  159. }
  160. int AppItem::getLanguages() const
  161. {
  162. return m_languages;
  163. }
  164. const std::vector<char> &AppItem::getSeed() const
  165. {
  166. return m_seed;
  167. }
  168. const std::vector<int> &AppItem::getGenres() const
  169. {
  170. return m_genres;
  171. }
  172. int AppItem::getPlatform() const
  173. {
  174. return m_platform;
  175. }
  176. int AppItem::getPublisher() const
  177. {
  178. return m_publisher;
  179. }
  180. int AppItem::getIconIndex() const
  181. {
  182. return m_iconIndex;
  183. }
  184. const cpp3ds::Texture *AppItem::getIcon(cpp3ds::IntRect &outRect) const
  185. {
  186. outRect = m_iconRect;
  187. return m_iconTexture;
  188. }
  189. void AppItem::queueForInstall()
  190. {
  191. DownloadQueue::getInstance().addDownload(shared_from_this());
  192. for (auto &id : m_updates)
  193. DownloadQueue::getInstance().addDownload(shared_from_this(), id);
  194. for (auto &id : m_dlc)
  195. DownloadQueue::getInstance().addDownload(shared_from_this(), id);
  196. }
  197. const std::vector<cpp3ds::Uint64> &AppItem::getUpdates() const
  198. {
  199. return m_updates;
  200. }
  201. const std::vector<cpp3ds::Uint64> &AppItem::getDemos() const
  202. {
  203. return m_demos;
  204. }
  205. const std::vector<cpp3ds::Uint64> &AppItem::getDLC() const
  206. {
  207. return m_dlc;
  208. }
  209. float AppItem::getVoteScore() const
  210. {
  211. return m_voteScore;
  212. }
  213. int AppItem::getVoteCount() const
  214. {
  215. return m_voteCount;
  216. }
  217. time_t AppItem::getReleaseDate() const
  218. {
  219. return m_releaseDate;
  220. }
  221. const std::string &AppItem::getProductCode() const
  222. {
  223. return m_productCode;
  224. }
  225. } // namespace FreeShop