AppList.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. #include <cpp3ds/System/FileSystem.hpp>
  2. #include <cpp3ds/System/FileInputStream.hpp>
  3. #include <functional>
  4. #include <iostream>
  5. #include <rapidjson/document.h>
  6. #include <rapidjson/filereadstream.h>
  7. #include <TweenEngine/Tween.h>
  8. #include <fstream>
  9. #include <cpp3ds/System/Clock.hpp>
  10. #include <cpp3ds/System/Sleep.hpp>
  11. #include "AppList.hpp"
  12. #include "Util.hpp"
  13. #include "TitleKeys.hpp"
  14. #include "AssetManager.hpp"
  15. #include "Theme.hpp"
  16. #include "States/BrowseState.hpp"
  17. #include "LoadInformations.hpp"
  18. namespace FreeShop {
  19. AppList::AppList(std::string jsonFilename)
  20. : m_sortType(Name)
  21. , m_selectedIndex(-1)
  22. , m_collapsed(false)
  23. , m_filterRegions(0)
  24. , m_filterLanguages(0)
  25. , m_sortAscending(true)
  26. , m_targetPosX(0.f)
  27. , m_indexDelta(0)
  28. , m_startKeyRepeat(false)
  29. , m_processedFirstKey(false)
  30. {
  31. m_jsonFilename = jsonFilename;
  32. if (Theme::isSoundBlipThemed)
  33. m_soundBlip.setBuffer(AssetManager<cpp3ds::SoundBuffer>::get(FREESHOP_DIR "/theme/sounds/blip.ogg"));
  34. else
  35. m_soundBlip.setBuffer(AssetManager<cpp3ds::SoundBuffer>::get("sounds/blip.ogg"));
  36. }
  37. AppList::~AppList()
  38. {
  39. for (auto& texture : m_iconTextures)
  40. delete texture;
  41. }
  42. void AppList::refresh()
  43. {
  44. m_appItems.clear();
  45. m_guiAppItems.clear();
  46. #ifdef EMULATION
  47. bool isNew3DS = true;
  48. #else
  49. bool isNew3DS = false;
  50. APT_CheckNew3DS(&isNew3DS);
  51. #endif
  52. cpp3ds::Clock clock;
  53. cpp3ds::FileInputStream file;
  54. int gameCount = calculateGameCount();
  55. int actualGame = 0;
  56. if (file.open(m_jsonFilename))
  57. {
  58. // Read file to string
  59. int size = file.getSize();
  60. std::string json;
  61. json.resize(size);
  62. file.read(&json[0], size);
  63. // Parse json string
  64. rapidjson::Document doc;
  65. doc.Parse(json.c_str());
  66. int i = 0;
  67. for (rapidjson::Value::ConstMemberIterator iter = doc.MemberBegin(); iter != doc.MemberEnd(); ++iter)
  68. {
  69. std::string id = iter->name.GetString();
  70. cpp3ds::Uint64 titleId = strtoull(id.c_str(), 0, 16);
  71. if (!isNew3DS && ((titleId >> 24) & 0xF) == 0xF)
  72. continue;
  73. if (TitleKeys::get(titleId))
  74. {
  75. auto appItem = std::make_shared<AppItem>();
  76. std::unique_ptr<GUI::AppItem> guiAppItem(new GUI::AppItem());
  77. appItem->loadFromJSON(iter->name.GetString(), iter->value);
  78. guiAppItem->setAppItem(appItem);
  79. // Move offscreen to avoid everything being drawn at once and crashing
  80. guiAppItem->setPosition(500.f, 100.f);
  81. m_appItems.emplace_back(std::move(appItem));
  82. m_guiAppItems.emplace_back(std::move(guiAppItem));
  83. // Increment the game count
  84. actualGame++;
  85. // Update percentage on the loading screen
  86. LoadInformations::getInstance().updateLoadingPercentage(actualGame * 100 / gameCount);
  87. }
  88. }
  89. }
  90. if (m_selectedIndex < 0 && getVisibleCount() > 0)
  91. m_selectedIndex = 0;
  92. setSelectedIndex(m_selectedIndex);
  93. }
  94. bool AppList::processEvent(const cpp3ds::Event &event)
  95. {
  96. if (event.type == cpp3ds::Event::KeyPressed)
  97. {
  98. m_processedFirstKey = false;
  99. if (event.key.code & cpp3ds::Keyboard::Up) {
  100. setIndexDelta(-1);
  101. } else if (event.key.code & cpp3ds::Keyboard::Down) {
  102. setIndexDelta(1);
  103. } else if (event.key.code & cpp3ds::Keyboard::Left) {
  104. setIndexDelta(-4);
  105. } else if (event.key.code & cpp3ds::Keyboard::Right) {
  106. setIndexDelta(4);
  107. } else if (event.key.code & cpp3ds::Keyboard::L) {
  108. setIndexDelta(-8);
  109. } else if (event.key.code & cpp3ds::Keyboard::R) {
  110. setIndexDelta(8);
  111. } else if (event.key.code & cpp3ds::Keyboard::ZL) {
  112. setSelectedIndex(0);
  113. } else if (event.key.code & cpp3ds::Keyboard::ZR) {
  114. setSelectedIndex(getVisibleCount());
  115. } else
  116. m_processedFirstKey = true;
  117. }
  118. else if (event.type == cpp3ds::Event::KeyReleased)
  119. {
  120. if (event.key.code & (cpp3ds::Keyboard::Up | cpp3ds::Keyboard::Down | cpp3ds::Keyboard::Left | cpp3ds::Keyboard::Right | cpp3ds::Keyboard::L | cpp3ds::Keyboard::R))
  121. {
  122. if (cpp3ds::Keyboard::isKeyDown(cpp3ds::Keyboard::Up)
  123. || cpp3ds::Keyboard::isKeyDown(cpp3ds::Keyboard::Down)
  124. || cpp3ds::Keyboard::isKeyDown(cpp3ds::Keyboard::Left)
  125. || cpp3ds::Keyboard::isKeyDown(cpp3ds::Keyboard::Right)
  126. || cpp3ds::Keyboard::isKeyDown(cpp3ds::Keyboard::L)
  127. || cpp3ds::Keyboard::isKeyDown(cpp3ds::Keyboard::R))
  128. return false;
  129. setIndexDelta(0);
  130. m_startKeyRepeat = false;
  131. m_processedFirstKey = false;
  132. }
  133. }
  134. return false;
  135. }
  136. void AppList::update(float delta)
  137. {
  138. if (m_indexDelta != 0)
  139. {
  140. if (m_startKeyRepeat)
  141. {
  142. if (m_clockKeyRepeat.getElapsedTime() > cpp3ds::milliseconds(60))
  143. processKeyRepeat();
  144. }
  145. else if (!m_processedFirstKey)
  146. {
  147. m_processedFirstKey = true;
  148. processKeyRepeat();
  149. }
  150. else if (m_clockKeyRepeat.getElapsedTime() > cpp3ds::milliseconds(300))
  151. m_startKeyRepeat = true;
  152. }
  153. m_tweenManager.update(delta);
  154. }
  155. void AppList::processKeyRepeat()
  156. {
  157. int index = getSelectedIndex();
  158. // Don't keep changing index on top/bottom boundaries
  159. if ((m_indexDelta != 1 || index % 4 != 3) && (m_indexDelta != -1 || index % 4 != 0))
  160. {
  161. int newIndex = index + m_indexDelta;
  162. if (newIndex < 0)
  163. newIndex = 0;
  164. if (newIndex >= getVisibleCount())
  165. newIndex = getVisibleCount() - 1;
  166. if (newIndex != index)
  167. m_soundBlip.play();
  168. setSelectedIndex(newIndex);
  169. m_clockKeyRepeat.restart();
  170. }
  171. }
  172. void AppList::setSortType(AppList::SortType sortType, bool ascending)
  173. {
  174. m_sortType = sortType;
  175. m_sortAscending = ascending;
  176. sort();
  177. }
  178. void AppList::sort()
  179. {
  180. setSelectedIndex(-1);
  181. m_tweenManager.killAll();
  182. std::sort(m_guiAppItems.begin(), m_guiAppItems.end(), [&](const std::unique_ptr<GUI::AppItem>& a, const std::unique_ptr<GUI::AppItem>& b)
  183. {
  184. if (a->isFilteredOut() != b->isFilteredOut())
  185. return !a->isFilteredOut();
  186. if (a->getMatchScore() != b->getMatchScore())
  187. {
  188. return a->getMatchScore() > b->getMatchScore();
  189. }
  190. else
  191. {
  192. if (m_sortAscending)
  193. {
  194. switch(m_sortType) {
  195. case Name: return a->getAppItem()->getNormalizedTitle() < b->getAppItem()->getNormalizedTitle();
  196. case Size: return a->getAppItem()->getFilesize() < b->getAppItem()->getFilesize();
  197. case VoteScore: return a->getAppItem()->getVoteScore() < b->getAppItem()->getVoteScore();
  198. case VoteCount: return a->getAppItem()->getVoteCount() < b->getAppItem()->getVoteCount();
  199. case ReleaseDate: return a->getAppItem()->getReleaseDate() < b->getAppItem()->getReleaseDate();
  200. }
  201. }
  202. else
  203. {
  204. switch(m_sortType) {
  205. case Name: return a->getAppItem()->getNormalizedTitle() > b->getAppItem()->getNormalizedTitle();
  206. case Size: return a->getAppItem()->getFilesize() > b->getAppItem()->getFilesize();
  207. case VoteScore: return a->getAppItem()->getVoteScore() > b->getAppItem()->getVoteScore();
  208. case VoteCount: return a->getAppItem()->getVoteCount() > b->getAppItem()->getVoteCount();
  209. case ReleaseDate: return a->getAppItem()->getReleaseDate() > b->getAppItem()->getReleaseDate();
  210. }
  211. }
  212. }
  213. });
  214. if (!m_guiAppItems.empty())
  215. setSelectedIndex(0);
  216. reposition();
  217. }
  218. void AppList::filter()
  219. {
  220. if (m_selectedIndex >= 0)
  221. m_guiAppItems[m_selectedIndex]->deselect();
  222. // Region filter
  223. // Also resets the filter state when no region filter is set.
  224. if (m_filterRegions)
  225. {
  226. for (const auto& appItemGUI : m_guiAppItems)
  227. appItemGUI->setFilteredOut(!(appItemGUI->getAppItem()->getRegions() & m_filterRegions));
  228. }
  229. else
  230. for (const auto& appItemGUI : m_guiAppItems)
  231. appItemGUI->setFilteredOut(false);
  232. // Language filter
  233. if (m_filterLanguages != 0)
  234. {
  235. for (const auto& appItemGUI : m_guiAppItems)
  236. if (appItemGUI->isVisible())
  237. if (!(appItemGUI->getAppItem()->getLanguages() & m_filterLanguages))
  238. appItemGUI->setFilteredOut(true);
  239. }
  240. // Genre filter
  241. if (!m_filterGenres.empty())
  242. {
  243. for (const auto& appItemGUI : m_guiAppItems)
  244. if (appItemGUI->isVisible())
  245. {
  246. for (const auto& appGenre : appItemGUI->getAppItem()->getGenres())
  247. for (const auto& filterGenre : m_filterGenres)
  248. if (appGenre == filterGenre)
  249. goto matchedGenre;
  250. appItemGUI->setFilteredOut(true);
  251. matchedGenre:;
  252. }
  253. }
  254. // Platform filter
  255. if (!m_filterPlatforms.empty())
  256. {
  257. for (const auto& appItemGUI : m_guiAppItems)
  258. if (appItemGUI->isVisible())
  259. {
  260. for (const auto& filterPlatform : m_filterPlatforms)
  261. if (appItemGUI->getAppItem()->getPlatform() == filterPlatform)
  262. goto matchedPlatform;
  263. appItemGUI->setFilteredOut(true);
  264. matchedPlatform:;
  265. }
  266. }
  267. // Features filter
  268. if (!m_filterFeatures.empty())
  269. {
  270. for (const auto& appItemGUI : m_guiAppItems)
  271. if (appItemGUI->isVisible())
  272. {
  273. for (const auto& appFeature : appItemGUI->getAppItem()->getFeatures())
  274. for (const auto& filterFeature : m_filterFeatures)
  275. if (appFeature == filterFeature)
  276. goto matchedFeature;
  277. appItemGUI->setFilteredOut(true);
  278. matchedFeature:;
  279. }
  280. }
  281. // Publisher filter
  282. if (!m_filterPublishers.empty())
  283. {
  284. for (const auto& appItemGUI : m_guiAppItems)
  285. if (appItemGUI->isVisible())
  286. {
  287. int appPublisher = appItemGUI->getAppItem()->getPublisher();
  288. for (const auto& filterPublisher : m_filterPublishers)
  289. if (appPublisher == filterPublisher)
  290. goto matchedPublisher;
  291. appItemGUI->setFilteredOut(true);
  292. matchedPublisher:;
  293. }
  294. }
  295. sort();
  296. setPosition(0.f, 0.f);
  297. }
  298. void AppList::reposition()
  299. {
  300. bool segmentFound = false;
  301. float destY = 4.f;
  302. int i = 0;
  303. float newX = (m_selectedIndex ? : 0) / 4 * (m_collapsed ? 59.f : 200.f);
  304. for (auto& app : m_guiAppItems)
  305. {
  306. if (!app->isVisible() || app->isFilteredOut())
  307. continue;
  308. float destX = 4.f + (i/4) * (m_collapsed ? 59.f : 200.f);
  309. float itemPosX = app->getPosition().x + getPosition().x;
  310. if ((destX-newX < -200.f || destX-newX > 400.f) && (itemPosX < -200 || itemPosX > 400.f))
  311. {
  312. if (segmentFound == m_collapsed)
  313. TweenEngine::Tween::set(*app, GUI::AppItem::POSITION_XY)
  314. .target(destX, destY)
  315. .delay(0.3f)
  316. .start(m_tweenManager);
  317. else
  318. app->setPosition(destX, destY);
  319. }
  320. else
  321. {
  322. segmentFound = true;
  323. TweenEngine::Tween::to(*app, GUI::AppItem::POSITION_XY, 0.3f)
  324. .target(destX, destY)
  325. .start(m_tweenManager);
  326. }
  327. if (++i % 4 == 0)
  328. destY = 4.f;
  329. else
  330. destY += 59.f;
  331. }
  332. }
  333. void AppList::setSelectedIndex(int index)
  334. {
  335. if (getVisibleCount() == 0)
  336. return;
  337. if (index >= 0)
  338. {
  339. if (index >= getVisibleCount())
  340. index = getVisibleCount() - 1;
  341. float extra = 1.0f; //std::abs(m_appList.getSelectedIndex() - index) == 8.f ? 2.f : 1.f;
  342. float pos = -200.f * extra * (index / 4);
  343. if (pos > m_targetPosX)
  344. m_targetPosX = pos;
  345. else if (pos <= m_targetPosX - 400.f)
  346. m_targetPosX = pos + 200.f * extra;
  347. TweenEngine::Tween::to(*this, AppList::POSITION_X, 0.3f)
  348. .target(m_targetPosX)
  349. .start(m_tweenManager);
  350. }
  351. if (m_selectedIndex >= 0)
  352. m_guiAppItems[m_selectedIndex]->deselect();
  353. m_selectedIndex = index;
  354. if (m_selectedIndex >= 0)
  355. m_guiAppItems[m_selectedIndex]->select();
  356. }
  357. int AppList::getSelectedIndex() const
  358. {
  359. return m_selectedIndex;
  360. }
  361. GUI::AppItem *AppList::getSelected()
  362. {
  363. if (m_selectedIndex < 0 || m_selectedIndex > m_guiAppItems.size()-1)
  364. return nullptr;
  365. return m_guiAppItems[m_selectedIndex].get();
  366. }
  367. void AppList::draw(cpp3ds::RenderTarget &target, cpp3ds::RenderStates states) const
  368. {
  369. states.transform *= getTransform();
  370. for (auto& app : m_guiAppItems)
  371. {
  372. if (app->isVisible() && !app->isFilteredOut())
  373. target.draw(*app, states);
  374. }
  375. }
  376. size_t AppList::getCount() const
  377. {
  378. return m_guiAppItems.size();
  379. }
  380. size_t AppList::getVisibleCount() const
  381. {
  382. size_t count = 0;
  383. for (auto& item : m_guiAppItems)
  384. if (item->isVisible() && !item->isFilteredOut())
  385. count++;
  386. else break;
  387. return count;
  388. }
  389. void AppList::setCollapsed(bool collapsed)
  390. {
  391. if (m_collapsed == collapsed)
  392. return;
  393. m_tweenManager.killAll();
  394. float newX = m_selectedIndex / 4 * (collapsed ? 59.f : 200.f);
  395. if (collapsed)
  396. {
  397. for (auto &app : m_guiAppItems)
  398. {
  399. float appPosX = app->getPosition().x + getPosition().x;
  400. if (appPosX >= 0.f && appPosX < 400.f)
  401. TweenEngine::Tween::to(*app, GUI::AppItem::INFO_ALPHA, 0.3f)
  402. .target(0)
  403. .setCallback(TweenEngine::TweenCallback::COMPLETE, [&](TweenEngine::BaseTween *source) {
  404. app->setInfoVisible(false);
  405. })
  406. .start(m_tweenManager);
  407. else {
  408. app->setInfoVisible(false);
  409. TweenEngine::Tween::set(*app, GUI::AppItem::INFO_ALPHA)
  410. .target(0)
  411. .start(m_tweenManager);
  412. }
  413. }
  414. TweenEngine::Tween::to(*this, AppList::POSITION_X, 0.3f)
  415. .target(-newX)
  416. .delay(0.3f)
  417. .setCallback(TweenEngine::TweenCallback::START, [=](TweenEngine::BaseTween *source) {
  418. m_collapsed = collapsed;
  419. reposition();
  420. })
  421. .start(m_tweenManager);
  422. }
  423. else
  424. {
  425. m_collapsed = collapsed;
  426. reposition();
  427. TweenEngine::Tween::to(*this, AppList::POSITION_X, 0.3f)
  428. .target(-newX)
  429. .setCallback(TweenEngine::TweenCallback::COMPLETE, [this, newX](TweenEngine::BaseTween *source) {
  430. for (auto &app : m_guiAppItems) {
  431. float appPosX = app->getPosition().x - newX;
  432. app->setInfoVisible(true);
  433. if (appPosX >= 0.f && appPosX < 400.f)
  434. TweenEngine::Tween::to(*app, GUI::AppItem::INFO_ALPHA, 0.3f)
  435. .target(255.f)
  436. .start(m_tweenManager);
  437. else
  438. TweenEngine::Tween::set(*app, GUI::AppItem::INFO_ALPHA)
  439. .target(255.f)
  440. .start(m_tweenManager);
  441. }
  442. GUI::AppItem *item = getSelected();
  443. if (item)
  444. TweenEngine::Tween::to(*item, GUI::AppItem::BACKGROUND_ALPHA, 0.3f)
  445. .target(255.f)
  446. .setCallback(TweenEngine::TweenCallback::COMPLETE, [this](TweenEngine::BaseTween *source) {
  447. setSelectedIndex(m_selectedIndex);
  448. })
  449. .delay(0.3f)
  450. .start(m_tweenManager);
  451. })
  452. .start(m_tweenManager);
  453. }
  454. }
  455. bool AppList::isCollapsed() const
  456. {
  457. return m_collapsed;
  458. }
  459. void AppList::filterBySearch(const std::string &searchTerm, std::vector<util3ds::RichText> &textMatches)
  460. {
  461. if (searchTerm == m_currentSearchTerm)
  462. return;
  463. m_currentSearchTerm = searchTerm;
  464. m_tweenManager.killAll();
  465. for (auto& item : m_guiAppItems)
  466. {
  467. item->setMatchTerm(searchTerm);
  468. item->setVisible(item->getMatchScore() > -99);
  469. }
  470. if (m_selectedIndex >= 0)
  471. m_guiAppItems[m_selectedIndex]->deselect();
  472. sort();
  473. int i = 0;
  474. for (auto& textMatch : textMatches)
  475. {
  476. textMatch.clear();
  477. if (searchTerm.empty())
  478. continue;
  479. if (i < getVisibleCount())
  480. {
  481. auto item = m_guiAppItems[i].get();
  482. if (item->getMatchScore() > -99)
  483. {
  484. bool matching = false;
  485. const char *str = item->getAppItem()->getNormalizedTitle().c_str();
  486. const char *pattern = searchTerm.c_str();
  487. const char *strLastPos = str;
  488. auto title = item->getAppItem()->getTitle().toUtf8();
  489. auto titleCurPos = title.begin();
  490. auto titleLastPos = title.begin();
  491. textMatch << cpp3ds::Color(150,150,150);
  492. while (*str != '\0') {
  493. if (tolower(*pattern) == tolower(*str)) {
  494. if (!matching) {
  495. matching = true;
  496. if (str != strLastPos) {
  497. textMatch << cpp3ds::String::fromUtf8(titleLastPos, titleCurPos);
  498. titleLastPos = titleCurPos;
  499. strLastPos = str;
  500. }
  501. textMatch << cpp3ds::Color::Black;
  502. }
  503. ++pattern;
  504. } else {
  505. if (matching) {
  506. matching = false;
  507. if (str != strLastPos) {
  508. textMatch << cpp3ds::String::fromUtf8(titleLastPos, titleCurPos);
  509. titleLastPos = titleCurPos;
  510. strLastPos = str;
  511. }
  512. textMatch << cpp3ds::Color(150,150,150);
  513. }
  514. }
  515. ++str;
  516. titleCurPos = cpp3ds::Utf8::next(titleCurPos, title.end());
  517. }
  518. if (str != strLastPos)
  519. textMatch << cpp3ds::String::fromUtf8(titleLastPos, title.end());
  520. }
  521. i++;
  522. }
  523. }
  524. if (m_guiAppItems.size() > 0 && textMatches.size() > 0)
  525. setSelectedIndex(0);
  526. }
  527. std::vector<std::unique_ptr<GUI::AppItem>> &AppList::getList()
  528. {
  529. return m_guiAppItems;
  530. }
  531. AppList &AppList::getInstance()
  532. {
  533. static AppList list(FREESHOP_DIR "/cache/data.json");
  534. return list;
  535. }
  536. void AppList::setFilterGenres(const std::vector<int> &genres)
  537. {
  538. m_filterGenres = genres;
  539. filter();
  540. }
  541. void AppList::setFilterPlatforms(const std::vector<int> &platforms)
  542. {
  543. m_filterPlatforms = platforms;
  544. filter();
  545. }
  546. void AppList::setFilterRegions(int regions)
  547. {
  548. m_filterRegions = regions;
  549. filter();
  550. }
  551. void AppList::setFilterLanguages(int languages)
  552. {
  553. m_filterLanguages = languages;
  554. filter();
  555. }
  556. void AppList::setFilterFeatures(const std::vector<int> &features)
  557. {
  558. m_filterFeatures = features;
  559. filter();
  560. }
  561. void AppList::setFilterPublishers(const std::vector<int> &publishers)
  562. {
  563. m_filterPublishers = publishers;
  564. filter();
  565. }
  566. void AppList::setIndexDelta(int indexDelta)
  567. {
  568. m_indexDelta = indexDelta;
  569. }
  570. int AppList::calculateGameCount()
  571. {
  572. // Stripped down refresh()
  573. #ifdef EMULATION
  574. bool isNew3DS = true;
  575. #else
  576. bool isNew3DS = false;
  577. APT_CheckNew3DS(&isNew3DS);
  578. #endif
  579. cpp3ds::Clock clock;
  580. cpp3ds::FileInputStream file;
  581. int gameCount = 0;
  582. if (file.open(m_jsonFilename))
  583. {
  584. // Read file to string
  585. int size = file.getSize();
  586. std::string json;
  587. json.resize(size);
  588. file.read(&json[0], size);
  589. // Parse json string
  590. rapidjson::Document doc;
  591. doc.Parse(json.c_str());
  592. int i = 0;
  593. for (rapidjson::Value::ConstMemberIterator iter = doc.MemberBegin(); iter != doc.MemberEnd(); ++iter)
  594. {
  595. std::string id = iter->name.GetString();
  596. cpp3ds::Uint64 titleId = strtoull(id.c_str(), 0, 16);
  597. if (!isNew3DS && ((titleId >> 24) & 0xF) == 0xF)
  598. continue;
  599. if (TitleKeys::get(titleId))
  600. {
  601. gameCount++;
  602. }
  603. }
  604. }
  605. return gameCount;
  606. }
  607. } // namespace FreeShop