play_time_manager.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // SPDX-FileCopyrightText: 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "common/alignment.h"
  4. #include "common/fs/file.h"
  5. #include "common/fs/fs.h"
  6. #include "common/fs/path_util.h"
  7. #include "common/logging/log.h"
  8. #include "common/settings.h"
  9. #include "common/thread.h"
  10. #include "core/hle/service/acc/profile_manager.h"
  11. #include "yuzu/play_time_manager.h"
  12. namespace PlayTime {
  13. namespace {
  14. struct PlayTimeElement {
  15. ProgramId program_id;
  16. PlayTime play_time;
  17. };
  18. std::optional<std::filesystem::path> GetCurrentUserPlayTimePath() {
  19. const Service::Account::ProfileManager manager;
  20. const auto uuid = manager.GetUser(static_cast<s32>(Settings::values.current_user));
  21. if (!uuid.has_value()) {
  22. return std::nullopt;
  23. }
  24. return Common::FS::GetYuzuPath(Common::FS::YuzuPath::PlayTimeDir) /
  25. uuid->RawString().append(".bin");
  26. }
  27. [[nodiscard]] bool ReadPlayTimeFile(PlayTimeDatabase& out_play_time_db) {
  28. const auto filename = GetCurrentUserPlayTimePath();
  29. if (!filename.has_value()) {
  30. LOG_ERROR(Frontend, "Failed to get current user path");
  31. return false;
  32. }
  33. out_play_time_db.clear();
  34. if (Common::FS::Exists(filename.value())) {
  35. Common::FS::IOFile file{filename.value(), Common::FS::FileAccessMode::Read,
  36. Common::FS::FileType::BinaryFile};
  37. if (!file.IsOpen()) {
  38. LOG_ERROR(Frontend, "Failed to open play time file: {}",
  39. Common::FS::PathToUTF8String(filename.value()));
  40. return false;
  41. }
  42. const size_t num_elements = file.GetSize() / sizeof(PlayTimeElement);
  43. std::vector<PlayTimeElement> elements(num_elements);
  44. if (file.ReadSpan<PlayTimeElement>(elements) != num_elements) {
  45. return false;
  46. }
  47. for (const auto& [program_id, play_time] : elements) {
  48. if (program_id != 0) {
  49. out_play_time_db[program_id] = play_time;
  50. }
  51. }
  52. }
  53. return true;
  54. }
  55. [[nodiscard]] bool WritePlayTimeFile(const PlayTimeDatabase& play_time_db) {
  56. const auto filename = GetCurrentUserPlayTimePath();
  57. if (!filename.has_value()) {
  58. LOG_ERROR(Frontend, "Failed to get current user path");
  59. return false;
  60. }
  61. Common::FS::IOFile file{filename.value(), Common::FS::FileAccessMode::Write,
  62. Common::FS::FileType::BinaryFile};
  63. if (!file.IsOpen()) {
  64. LOG_ERROR(Frontend, "Failed to open play time file: {}",
  65. Common::FS::PathToUTF8String(filename.value()));
  66. return false;
  67. }
  68. std::vector<PlayTimeElement> elements;
  69. elements.reserve(play_time_db.size());
  70. for (auto& [program_id, play_time] : play_time_db) {
  71. if (program_id != 0) {
  72. elements.push_back(PlayTimeElement{program_id, play_time});
  73. }
  74. }
  75. return file.WriteSpan<PlayTimeElement>(elements) == elements.size();
  76. }
  77. } // namespace
  78. PlayTimeManager::PlayTimeManager() {
  79. if (!ReadPlayTimeFile(database)) {
  80. LOG_ERROR(Frontend, "Failed to read play time database! Resetting to default.");
  81. }
  82. }
  83. PlayTimeManager::~PlayTimeManager() {
  84. Save();
  85. }
  86. void PlayTimeManager::SetProgramId(u64 program_id) {
  87. running_program_id = program_id;
  88. }
  89. void PlayTimeManager::Start() {
  90. play_time_thread = std::jthread([&](std::stop_token stop_token) { AutoTimestamp(stop_token); });
  91. }
  92. void PlayTimeManager::Stop() {
  93. play_time_thread = {};
  94. }
  95. void PlayTimeManager::AutoTimestamp(std::stop_token stop_token) {
  96. Common::SetCurrentThreadName("PlayTimeReport");
  97. using namespace std::literals::chrono_literals;
  98. using std::chrono::seconds;
  99. using std::chrono::steady_clock;
  100. auto timestamp = steady_clock::now();
  101. const auto GetDuration = [&]() -> u64 {
  102. const auto last_timestamp = std::exchange(timestamp, steady_clock::now());
  103. const auto duration = std::chrono::duration_cast<seconds>(timestamp - last_timestamp);
  104. return static_cast<u64>(duration.count());
  105. };
  106. while (!stop_token.stop_requested()) {
  107. Common::StoppableTimedWait(stop_token, 30s);
  108. database[running_program_id] += GetDuration();
  109. Save();
  110. }
  111. }
  112. void PlayTimeManager::Save() {
  113. if (!WritePlayTimeFile(database)) {
  114. LOG_ERROR(Frontend, "Failed to update play time database!");
  115. }
  116. }
  117. u64 PlayTimeManager::GetPlayTime(u64 program_id) const {
  118. auto it = database.find(program_id);
  119. if (it != database.end()) {
  120. return it->second;
  121. } else {
  122. return 0;
  123. }
  124. }
  125. void PlayTimeManager::ResetProgramPlayTime(u64 program_id) {
  126. database.erase(program_id);
  127. Save();
  128. }
  129. QString ReadablePlayTime(qulonglong time_seconds) {
  130. if (time_seconds == 0) {
  131. return {};
  132. }
  133. const auto time_minutes = std::max(static_cast<double>(time_seconds) / 60, 1.0);
  134. const auto time_hours = static_cast<double>(time_seconds) / 3600;
  135. const bool is_minutes = time_minutes < 60;
  136. const char* unit = is_minutes ? "m" : "h";
  137. const auto value = is_minutes ? time_minutes : time_hours;
  138. return QStringLiteral("%L1 %2")
  139. .arg(value, 0, 'f', !is_minutes && time_seconds % 60 != 0)
  140. .arg(QString::fromUtf8(unit));
  141. }
  142. } // namespace PlayTime