play_time_manager.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 Service::Account::ProfileManager& manager) {
  29. const auto filename = GetCurrentUserPlayTimePath(manager);
  30. if (!filename.has_value()) {
  31. LOG_ERROR(Frontend, "Failed to get current user path");
  32. return false;
  33. }
  34. out_play_time_db.clear();
  35. if (Common::FS::Exists(filename.value())) {
  36. Common::FS::IOFile file{filename.value(), Common::FS::FileAccessMode::Read,
  37. Common::FS::FileType::BinaryFile};
  38. if (!file.IsOpen()) {
  39. LOG_ERROR(Frontend, "Failed to open play time file: {}",
  40. Common::FS::PathToUTF8String(filename.value()));
  41. return false;
  42. }
  43. const size_t num_elements = file.GetSize() / sizeof(PlayTimeElement);
  44. std::vector<PlayTimeElement> elements(num_elements);
  45. if (file.ReadSpan<PlayTimeElement>(elements) != num_elements) {
  46. return false;
  47. }
  48. for (const auto& [program_id, play_time] : elements) {
  49. if (program_id != 0) {
  50. out_play_time_db[program_id] = play_time;
  51. }
  52. }
  53. }
  54. return true;
  55. }
  56. [[nodiscard]] bool WritePlayTimeFile(const PlayTimeDatabase& play_time_db,
  57. const Service::Account::ProfileManager& manager) {
  58. const auto filename = GetCurrentUserPlayTimePath(manager);
  59. if (!filename.has_value()) {
  60. LOG_ERROR(Frontend, "Failed to get current user path");
  61. return false;
  62. }
  63. Common::FS::IOFile file{filename.value(), Common::FS::FileAccessMode::Write,
  64. Common::FS::FileType::BinaryFile};
  65. if (!file.IsOpen()) {
  66. LOG_ERROR(Frontend, "Failed to open play time file: {}",
  67. Common::FS::PathToUTF8String(filename.value()));
  68. return false;
  69. }
  70. std::vector<PlayTimeElement> elements;
  71. elements.reserve(play_time_db.size());
  72. for (auto& [program_id, play_time] : play_time_db) {
  73. if (program_id != 0) {
  74. elements.push_back(PlayTimeElement{program_id, play_time});
  75. }
  76. }
  77. return file.WriteSpan<PlayTimeElement>(elements) == elements.size();
  78. }
  79. } // namespace
  80. PlayTimeManager::PlayTimeManager(Service::Account::ProfileManager& profile_manager)
  81. : manager{profile_manager} {
  82. if (!ReadPlayTimeFile(database, manager)) {
  83. LOG_ERROR(Frontend, "Failed to read play time database! Resetting to default.");
  84. }
  85. }
  86. PlayTimeManager::~PlayTimeManager() {
  87. Save();
  88. }
  89. void PlayTimeManager::SetProgramId(u64 program_id) {
  90. running_program_id = program_id;
  91. }
  92. void PlayTimeManager::Start() {
  93. play_time_thread = std::jthread([&](std::stop_token stop_token) { AutoTimestamp(stop_token); });
  94. }
  95. void PlayTimeManager::Stop() {
  96. play_time_thread = {};
  97. }
  98. void PlayTimeManager::AutoTimestamp(std::stop_token stop_token) {
  99. Common::SetCurrentThreadName("PlayTimeReport");
  100. using namespace std::literals::chrono_literals;
  101. using std::chrono::seconds;
  102. using std::chrono::steady_clock;
  103. auto timestamp = steady_clock::now();
  104. const auto GetDuration = [&]() -> u64 {
  105. const auto last_timestamp = std::exchange(timestamp, steady_clock::now());
  106. const auto duration = std::chrono::duration_cast<seconds>(timestamp - last_timestamp);
  107. return static_cast<u64>(duration.count());
  108. };
  109. while (!stop_token.stop_requested()) {
  110. Common::StoppableTimedWait(stop_token, 30s);
  111. database[running_program_id] += GetDuration();
  112. Save();
  113. }
  114. }
  115. void PlayTimeManager::Save() {
  116. if (!WritePlayTimeFile(database, manager)) {
  117. LOG_ERROR(Frontend, "Failed to update play time database!");
  118. }
  119. }
  120. u64 PlayTimeManager::GetPlayTime(u64 program_id) const {
  121. auto it = database.find(program_id);
  122. if (it != database.end()) {
  123. return it->second;
  124. } else {
  125. return 0;
  126. }
  127. }
  128. void PlayTimeManager::ResetProgramPlayTime(u64 program_id) {
  129. database.erase(program_id);
  130. Save();
  131. }
  132. QString ReadablePlayTime(qulonglong time_seconds) {
  133. if (time_seconds == 0) {
  134. return {};
  135. }
  136. const auto time_minutes = std::max(static_cast<double>(time_seconds) / 60, 1.0);
  137. const auto time_hours = static_cast<double>(time_seconds) / 3600;
  138. const bool is_minutes = time_minutes < 60;
  139. const char* unit = is_minutes ? "m" : "h";
  140. const auto value = is_minutes ? time_minutes : time_hours;
  141. return QStringLiteral("%L1 %2")
  142. .arg(value, 0, 'f', !is_minutes && time_seconds % 60 != 0)
  143. .arg(QString::fromUtf8(unit));
  144. }
  145. } // namespace PlayTime