BotInformations.cpp 8.8 KB

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