BotInformations.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include "BotInformations.hpp"
  2. #include "AssetManager.hpp"
  3. #include "DownloadQueue.hpp"
  4. #include "Notification.hpp"
  5. #include "Theme.hpp"
  6. #include "States/StateIdentifiers.hpp"
  7. #include "States/DialogState.hpp"
  8. #include <cpp3ds/System/I18n.hpp>
  9. #include <time.h>
  10. #ifndef EMULATION
  11. #include <3ds.h>
  12. #endif
  13. namespace FreeShop {
  14. BotInformations::BotInformations()
  15. : m_threadRefresh(&BotInformations::refresh, this)
  16. {
  17. //Texts
  18. m_textSD.setString(_("SD"));
  19. if (Theme::isTextThemed)
  20. m_textSD.setFillColor(Theme::primaryTextColor);
  21. else
  22. m_textSD.setFillColor(cpp3ds::Color::Black);
  23. m_textSD.setCharacterSize(10);
  24. m_textSD.setPosition(2.f, 31.f);
  25. m_textSD.useSystemFont();
  26. m_textSDStorage.setString(_(""));
  27. if (Theme::isTextThemed)
  28. m_textSDStorage.setFillColor(Theme::secondaryTextColor);
  29. else
  30. m_textSDStorage.setFillColor(cpp3ds::Color(130, 130, 130, 255));
  31. m_textSDStorage.setCharacterSize(10);
  32. m_textSDStorage.setPosition(2.f, 43.f);
  33. m_textSDStorage.useSystemFont();
  34. m_textNAND.setString(_("TWL NAND"));
  35. if (Theme::isTextThemed)
  36. m_textNAND.setFillColor(Theme::primaryTextColor);
  37. else
  38. m_textNAND.setFillColor(cpp3ds::Color::Black);
  39. m_textNAND.setCharacterSize(10);
  40. m_textNAND.setPosition(2.f, 59.f);
  41. m_textNAND.useSystemFont();
  42. m_textNANDStorage.setString(_(""));
  43. if (Theme::isTextThemed)
  44. m_textNANDStorage.setFillColor(Theme::secondaryTextColor);
  45. else
  46. m_textNANDStorage.setFillColor(cpp3ds::Color(130, 130, 130, 255));
  47. m_textNANDStorage.setCharacterSize(10);
  48. m_textNANDStorage.setPosition(2.f, 71.f);
  49. m_textNANDStorage.useSystemFont();
  50. //Progress bars
  51. m_progressBarNAND.setFillColor(cpp3ds::Color(0, 0, 0, 50));
  52. m_progressBarNAND.setPosition(0.f, 58.f);
  53. m_progressBarNAND.setSize(cpp3ds::Vector2f(0, 29));
  54. m_progressBarSD.setFillColor(cpp3ds::Color(0, 0, 0, 50));
  55. m_progressBarSD.setPosition(0.f, 30.f);
  56. m_progressBarSD.setSize(cpp3ds::Vector2f(0, 29));
  57. //Initializing booleans for transitions
  58. m_isProgressSDTransitioning = false;
  59. m_isProgressNANDTransitioning = false;
  60. //Progress bars' backgrounds
  61. if (Theme::isFSBGNAND9Themed)
  62. m_backgroundNAND.setTexture(&AssetManager<cpp3ds::Texture>::get(FREESHOP_DIR "/theme/images/fsbgnand.9.png"));
  63. else
  64. m_backgroundNAND.setTexture(&AssetManager<cpp3ds::Texture>::get("images/fsbgnand.9.png"));
  65. m_backgroundNAND.setSize(320, 23);
  66. m_backgroundNAND.setPosition(0.f, 58.f);
  67. if (Theme::isFSBGSD9Themed)
  68. m_backgroundSD.setTexture(&AssetManager<cpp3ds::Texture>::get(FREESHOP_DIR "/theme/images/fsbgsd.9.png"));
  69. else
  70. m_backgroundSD.setTexture(&AssetManager<cpp3ds::Texture>::get("images/fsbgsd.9.png"));
  71. m_backgroundSD.setSize(320, 23);
  72. m_backgroundSD.setPosition(0.f, 30.f);
  73. //Sleep download instruction text
  74. m_textSleepDownloads.setString(_(""));
  75. if (Theme::isTextThemed)
  76. m_textSleepDownloads.setFillColor(Theme::secondaryTextColor);
  77. else
  78. m_textSleepDownloads.setFillColor(cpp3ds::Color(0, 0, 0, 120));
  79. m_textSleepDownloads.setCharacterSize(11);
  80. m_textSleepDownloads.setPosition(2.f, 190.f);
  81. m_textSleepDownloads.useSystemFont();
  82. m_updateClock.restart();
  83. }
  84. BotInformations::~BotInformations()
  85. {
  86. }
  87. void BotInformations::draw(cpp3ds::RenderTarget &target, cpp3ds::RenderStates states) const
  88. {
  89. states.transform *= getTransform();
  90. //Firstly, draw the backgrounds for progress bars
  91. target.draw(m_backgroundNAND);
  92. target.draw(m_backgroundSD);
  93. //Secondly, draw the progress bars
  94. target.draw(m_progressBarNAND);
  95. target.draw(m_progressBarSD);
  96. //Finally, draw all the texts
  97. target.draw(m_textSD);
  98. target.draw(m_textSDStorage);
  99. target.draw(m_textNAND);
  100. target.draw(m_textNANDStorage);
  101. target.draw(m_textSleepDownloads);
  102. }
  103. void BotInformations::update(float delta)
  104. {
  105. if (m_updateClock.getElapsedTime() >= cpp3ds::seconds(2.f)) {
  106. // Reset the clock
  107. m_updateClock.restart();
  108. m_threadRefresh.wait();
  109. m_threadRefresh.launch();
  110. }
  111. m_tweenManager.update(delta);
  112. }
  113. void BotInformations::refresh()
  114. {
  115. //Update filesystems size and their progress bars
  116. #ifndef EMULATION
  117. FS_ArchiveResource resource = {0};
  118. if(R_SUCCEEDED(FSUSER_GetArchiveResource(&resource, SYSTEM_MEDIATYPE_SD))) {
  119. u64 size = (u64) resource.freeClusters * (u64) resource.clusterSize;
  120. u64 totalSize = (u64) resource.totalClusters * (u64) resource.clusterSize;
  121. u64 usedSize = totalSize - size;
  122. //m_progressBarSD.setSize(cpp3ds::Vector2f((usedSize * 320) / totalSize, 26));
  123. if (!m_isProgressSDTransitioning) {
  124. m_isProgressSDTransitioning = true;
  125. TweenEngine::Tween::to(m_progressBarSD, util3ds::TweenRectangleShape::SIZE, 0.2f)
  126. .target((usedSize * 320) / totalSize, 26.f)
  127. .setCallback(TweenEngine::TweenCallback::COMPLETE, [=](TweenEngine::BaseTween* source) {
  128. m_isProgressSDTransitioning = false;
  129. })
  130. .start(m_tweenManager);
  131. }
  132. if (usedSize > 1024 * 1024 * 1024 || totalSize > 1024 * 1024 * 1024)
  133. m_textSDStorage.setString(_("%.1f/%.1f GB", static_cast<float>(size) / 1024.f / 1024.f / 1024.f, static_cast<float>(totalSize) / 1024.f / 1024.f / 1024.f));
  134. else if (usedSize > 1024 * 1024 || totalSize > 1024 * 1024)
  135. m_textSDStorage.setString(_("%.1f/%.1f MB", static_cast<float>(size) / 1024.f / 1024.f, static_cast<float>(totalSize) / 1024.f / 1024.f));
  136. else
  137. m_textSDStorage.setString(_("%d/%d KB", size / 1024, totalSize / 1024));
  138. } else {
  139. m_textSDStorage.setString("No SD Card detected");
  140. }
  141. if(R_SUCCEEDED(FSUSER_GetArchiveResource(&resource, SYSTEM_MEDIATYPE_TWL_NAND))) {
  142. u64 size = (u64) resource.freeClusters * (u64) resource.clusterSize;
  143. u64 totalSize = (u64) resource.totalClusters * (u64) resource.clusterSize;
  144. u64 usedSize = totalSize - size;
  145. //m_progressBarNAND.setSize(cpp3ds::Vector2f((usedSize * 320) / totalSize, 26));
  146. if (!m_isProgressNANDTransitioning) {
  147. m_isProgressNANDTransitioning = true;
  148. TweenEngine::Tween::to(m_progressBarNAND, util3ds::TweenRectangleShape::SIZE, 0.2f)
  149. .target((usedSize * 320) / totalSize, 26.f)
  150. .setCallback(TweenEngine::TweenCallback::COMPLETE, [=](TweenEngine::BaseTween* source) {
  151. m_isProgressNANDTransitioning = false;
  152. })
  153. .start(m_tweenManager);
  154. }
  155. if (usedSize > 1024 * 1024 * 1024 || totalSize > 1024 * 1024 * 1024)
  156. m_textNANDStorage.setString(_("%.1f/%.1f GB", static_cast<float>(size) / 1024.f / 1024.f / 1024.f, static_cast<float>(totalSize) / 1024.f / 1024.f / 1024.f));
  157. else if (usedSize > 1024 * 1024 || totalSize > 1024 * 1024)
  158. m_textNANDStorage.setString(_("%.1f/%.1f MB", static_cast<float>(size) / 1024.f / 1024.f, static_cast<float>(totalSize) / 1024.f / 1024.f));
  159. else
  160. m_textNANDStorage.setString(_("%d/%d KB", size / 1024, totalSize / 1024));
  161. } else {
  162. m_textNANDStorage.setString("No TWL NAND detected... Wait what ?!");
  163. }
  164. #else
  165. m_textSDStorage.setString("16/32 GB");
  166. m_progressBarSD.setSize(cpp3ds::Vector2f(320, 26));
  167. m_textNANDStorage.setString("200/400 MB");
  168. m_progressBarNAND.setSize(cpp3ds::Vector2f(160, 26));
  169. #endif
  170. // Update the sleep downloads text
  171. #ifndef EMULATION
  172. // Init vars
  173. u32 pendingTitleCountSD = 0;
  174. u32 pendingTitleCountNAND = 0;
  175. u32 pendingTitleCountTotal = 0;
  176. std::vector<cpp3ds::Uint64> pendingTitleIds;
  177. // Get pending title count for SD (3ds Titles) and NAND (TWL Titles)
  178. AM_GetPendingTitleCount(&pendingTitleCountSD, MEDIATYPE_SD, AM_STATUS_MASK_INSTALLING | AM_STATUS_MASK_AWAITING_FINALIZATION);
  179. AM_GetPendingTitleCount(&pendingTitleCountNAND, MEDIATYPE_NAND, AM_STATUS_MASK_INSTALLING | AM_STATUS_MASK_AWAITING_FINALIZATION);
  180. // Get the total count
  181. pendingTitleCountTotal = pendingTitleCountSD + pendingTitleCountNAND;
  182. // Get title IDs of pending titles
  183. pendingTitleIds.resize(pendingTitleCountTotal);
  184. AM_GetPendingTitleList(nullptr, pendingTitleCountSD, MEDIATYPE_SD, AM_STATUS_MASK_INSTALLING | AM_STATUS_MASK_AWAITING_FINALIZATION, &pendingTitleIds[0]);
  185. AM_GetPendingTitleList(nullptr, pendingTitleCountNAND, MEDIATYPE_NAND, AM_STATUS_MASK_INSTALLING | AM_STATUS_MASK_AWAITING_FINALIZATION, &pendingTitleIds[pendingTitleIds.size() - pendingTitleCountNAND]);
  186. // Get pending title count for titles that are on the encTitleKey.bin file
  187. pendingTitleCountTotal = 0;
  188. for (auto& titleId : pendingTitleIds)
  189. if (TitleKeys::get(titleId))
  190. pendingTitleCountTotal++;
  191. if (pendingTitleCountTotal > 0)
  192. m_textSleepDownloads.setString(_("%i sleep download pending\nClose the software with the Start button and close\nthe lid of your console.", pendingTitleCountTotal));
  193. else
  194. m_textSleepDownloads.setString(_(""));
  195. #else
  196. m_textSleepDownloads.setString(_("%i sleep download pending\nClose the software with the Start button and close\nthe lid of your console.", rand() % 11));
  197. #endif
  198. }
  199. } // namespace FreeShop