From 6c8b856dea65570c3f335204bfd8e25e5049c1a9 Mon Sep 17 00:00:00 2001 From: tallbl0nde <40382856+tallbl0nde@users.noreply.github.com> Date: Fri, 9 Jul 2021 22:12:58 +0930 Subject: [PATCH] apply adjustments throughout app --- Application/include/Config.hpp | 12 ++++++ .../include/ui/screen/AdjustPlaytime.hpp | 2 +- Application/include/utils/Time.hpp | 5 ++- Application/romfs/lang | 2 +- Application/source/Application.ImportJob.cpp | 10 ++--- Application/source/Config.cpp | 43 +++++++++++++++++++ .../source/ui/screen/AdjustPlaytime.cpp | 22 +++++++--- Application/source/ui/screen/AllActivity.cpp | 23 ++++++++-- Application/source/ui/screen/Details.cpp | 17 +++++++- Application/source/utils/Time.cpp | 6 ++- 10 files changed, 120 insertions(+), 22 deletions(-) diff --git a/Application/include/Config.hpp b/Application/include/Config.hpp index 1245cdd..783f621 100644 --- a/Application/include/Config.hpp +++ b/Application/include/Config.hpp @@ -1,9 +1,16 @@ #ifndef CONFIG_HPP #define CONFIG_HPP +#include #include "SimpleIniParser.hpp" #include "Types.hpp" +struct AdjustmentValue { + uint64_t titleID; + AccountUid userID; + int value; +}; + namespace Main { // Reads/writes and stores config of application class Config { @@ -11,6 +18,9 @@ namespace Main { // Ini object to read/write to/from file simpleIniParser::Ini * ini; + // Vector of adjustments for each title ID + std::vector adjustments; + // Vector of hidden title IDs std::vector hidden; @@ -35,6 +45,7 @@ namespace Main { void writeConfig(); // Getters + setters for all settings' + std::vector adjustmentValues(); std::vector hiddenTitles(); bool gGraph(); bool gIs24H(); @@ -45,6 +56,7 @@ namespace Main { ViewPeriod lView(); bool tImage(); + bool setAdjustmentValues(const std::vector &); bool setHiddenTitles(const std::vector &); void setGGraph(bool); void setGIs24H(bool); diff --git a/Application/include/ui/screen/AdjustPlaytime.hpp b/Application/include/ui/screen/AdjustPlaytime.hpp index 90a832f..c2332a3 100644 --- a/Application/include/ui/screen/AdjustPlaytime.hpp +++ b/Application/include/ui/screen/AdjustPlaytime.hpp @@ -39,7 +39,7 @@ namespace Screen { void setupPlaytimePicker(const std::string &, size_t, CustomElm::ListAdjust *); // Vector of title IDs and their adjustment value - std::vector> adjustments; + std::vector adjustments; public: // Constructs the screen diff --git a/Application/include/utils/Time.hpp b/Application/include/utils/Time.hpp index 847fd21..81f881b 100644 --- a/Application/include/utils/Time.hpp +++ b/Application/include/utils/Time.hpp @@ -27,6 +27,9 @@ namespace Utils::Time { // Get time struct for given value struct tm getTm(time_t); + // Converts a POSIX timestamp to Pdm format + uint32_t posixTimestampToPdm(uint64_t); + // Return number of days in tm's month int tmGetDaysInMonth(struct tm); @@ -56,4 +59,4 @@ namespace Utils::Time { std::string getShortMonthString(int); }; -#endif \ No newline at end of file +#endif diff --git a/Application/romfs/lang b/Application/romfs/lang index 6570752..bdcd55f 160000 --- a/Application/romfs/lang +++ b/Application/romfs/lang @@ -1 +1 @@ -Subproject commit 6570752f3645ed6dd358881a10b3b815c5634717 +Subproject commit bdcd55fa955f46524623f64a7e9540b0ef538747 diff --git a/Application/source/Application.ImportJob.cpp b/Application/source/Application.ImportJob.cpp index de340be..86e616e 100644 --- a/Application/source/Application.ImportJob.cpp +++ b/Application/source/Application.ImportJob.cpp @@ -4,11 +4,7 @@ #include #include "nlohmann/json.hpp" #include "utils/Utils.hpp" - -// Converts a POSIX timestamp to Pdm format -static inline uint32_t posixTimestampToPdm(uint64_t timestamp) { - return static_cast((timestamp - 946598400)/60); -} +#include "utils/Time.hpp" namespace Main { Application::ImportJob::ImportJob(Application * app, std::atomic & percent) : Aether::ThreadPool::Job(), percent(percent) { @@ -136,8 +132,8 @@ namespace Main { nlohmann::json s = title["summary"]; if (s["firstPlayed"] != nullptr && s["lastPlayed"] != nullptr && s["playtime"] != nullptr && s["launches"] != nullptr) { tJson["summary"] = nlohmann::json(); - tJson["summary"]["firstPlayed"] = posixTimestampToPdm(s["firstPlayed"].get()); - tJson["summary"]["lastPlayed"] = posixTimestampToPdm(s["lastPlayed"].get()); + tJson["summary"]["firstPlayed"] = Utils::Time::posixTimestampToPdm(s["firstPlayed"].get()); + tJson["summary"]["lastPlayed"] = Utils::Time::posixTimestampToPdm(s["lastPlayed"].get()); tJson["summary"]["playtime"] = s["playtime"]; tJson["summary"]["launches"] = s["launches"]; } diff --git a/Application/source/Config.cpp b/Application/source/Config.cpp index 0c9f722..c3340b5 100644 --- a/Application/source/Config.cpp +++ b/Application/source/Config.cpp @@ -127,6 +127,32 @@ namespace Main { while (file >> line) { this->hidden.push_back(Utils::stringToU64(line)); } + + // Read in adjustment values + if (!std::filesystem::exists("/config/NX-Activity-Log/adjustments.conf")) { + std::ofstream file("/config/NX-Activity-Log/adjustments.conf"); + file.close(); + } + + std::ifstream file2("/config/NX-Activity-Log/adjustments.conf"); + std::string token; + while (file2 >> line) { + // Break apart line + std::stringstream ss(line); + std::vector tokens; + while (std::getline(ss, token, ':')) { + tokens.push_back(token); + } + + if (tokens.size() != 3 || tokens[0].length() != 16 || tokens[1].length() != 32) { + continue; + } + + AccountUid uid; + uid.uid[0] = Utils::stringToU64(tokens[1].substr(0, 16)); + uid.uid[1] = Utils::stringToU64(tokens[1].substr(16, 16)); + this->adjustments.push_back(AdjustmentValue{Utils::stringToU64(tokens[0]), uid, std::stoi(tokens[2])}); + } } void Config::writeConfig() { @@ -230,6 +256,10 @@ namespace Main { ini->writeToFile("/config/NX-Activity-Log/config.ini"); } + std::vector Config::adjustmentValues() { + return this->adjustments; + } + std::vector Config::hiddenTitles() { return this->hidden; } @@ -266,6 +296,19 @@ namespace Main { return this->tImage_; } + bool Config::setAdjustmentValues(const std::vector & values) { + this->adjustments = values; + + std::ofstream file("/config/NX-Activity-Log/adjustments.conf"); + for (AdjustmentValue value : values) { + file << Utils::formatHexString(value.titleID) << ":"; + file << Utils::formatHexString(value.userID.uid[0]) << Utils::formatHexString(value.userID.uid[1]) << ":"; + file << value.value << std::endl; + } + + return true; + } + bool Config::setHiddenTitles(const std::vector & titles) { this->hidden = titles; diff --git a/Application/source/ui/screen/AdjustPlaytime.cpp b/Application/source/ui/screen/AdjustPlaytime.cpp index b669db4..3de85e9 100644 --- a/Application/source/ui/screen/AdjustPlaytime.cpp +++ b/Application/source/ui/screen/AdjustPlaytime.cpp @@ -23,13 +23,14 @@ namespace Screen { Aether::ControlBar * c = new Aether::ControlBar(); c->addControl(Aether::Button::A, "common.buttonHint.ok"_lang); c->addControl(Aether::Button::B, "common.buttonHint.back"_lang); + c->addControl(Aether::Button::X, "adjustPlaytime.save"_lang); c->setDisabledColour(this->app->theme()->text()); c->setEnabledColour(this->app->theme()->text()); this->addElement(c); // Save on X this->onButtonPress(Aether::Button::X, [this]() { - // TODO: save + this->app->config()->setAdjustmentValues(this->adjustments); this->app->popScreen(); }); // Quit without saving on B @@ -58,8 +59,8 @@ namespace Screen { void AdjustPlaytime::setupPlaytimePicker(const std::string & title, size_t idx, CustomElm::ListAdjust * l) { delete this->picker; - this->picker = new CustomOvl::PlaytimePicker(title, this->adjustments[idx].second, [this, idx, l](int val) { - this->adjustments[idx].second = val; + this->picker = new CustomOvl::PlaytimePicker(title, this->adjustments[idx].value, [this, idx, l](int val) { + this->adjustments[idx].value = val; l->setAdjustedTime(this->getValueString(val)); }); this->picker->setBackLabel("common.buttonHint.back"_lang); @@ -98,7 +99,7 @@ namespace Screen { return (lhs->name() < rhs->name()); }); - this->adjustments.clear(); + this->adjustments = this->app->config()->adjustmentValues(); std::vector hidden = this->app->config()->hiddenTitles(); for (NX::Title * title : titles) { // Skip over hidden games @@ -106,11 +107,18 @@ namespace Screen { continue; } - this->adjustments.push_back(std::make_pair(title->titleID(), 0)); - size_t idx = this->adjustments.size()-1; + // Find adjustment value or create if one doesn't exist + std::vector::iterator it = std::find_if(this->adjustments.begin(), this->adjustments.end(), [this, title](AdjustmentValue val) { + return (val.titleID == title->titleID() && val.userID == this->app->activeUser()->ID()); + }); + if (it == this->adjustments.end()) { + this->adjustments.push_back(AdjustmentValue{title->titleID(), this->app->activeUser()->ID(), 0}); + it = (this->adjustments.end() - 1); + } + size_t idx = std::distance(this->adjustments.begin(), it); NX::PlayStatistics * stats = this->app->playdata()->getStatisticsForUser(title->titleID(), this->app->activeUser()->ID()); - CustomElm::ListAdjust * l = new CustomElm::ListAdjust(title->name(), Utils::playtimeToPlayedForString(stats->playtime), this->getValueString(this->adjustments[idx].second)); + CustomElm::ListAdjust * l = new CustomElm::ListAdjust(title->name(), Utils::playtimeToPlayedForString(stats->playtime), this->getValueString(this->adjustments[idx].value)); delete stats; l->setImage(title->imgPtr(), title->imgSize()); diff --git a/Application/source/ui/screen/AllActivity.cpp b/Application/source/ui/screen/AllActivity.cpp index 6b64a29..cfd418f 100644 --- a/Application/source/ui/screen/AllActivity.cpp +++ b/Application/source/ui/screen/AllActivity.cpp @@ -2,6 +2,7 @@ #include "Application.hpp" #include "utils/Lang.hpp" #include "utils/Utils.hpp" +#include "utils/Time.hpp" namespace Screen { AllActivity::AllActivity(Main::Application * a) { @@ -117,6 +118,7 @@ namespace Screen { this->list->setScrollBarColour(this->app->theme()->mutedLine()); // Populate list + count total time + std::vector adjustments = this->app->config()->adjustmentValues(); std::vector t = this->app->titleVector(); std::vector hidden = this->app->config()->hiddenTitles(); unsigned int totalSecs = 0; @@ -126,12 +128,27 @@ namespace Screen { continue; } + // Get statistics and append adjustment if needed NX::PlayStatistics * ps = this->app->playdata()->getStatisticsForUser(t[i]->titleID(), this->app->activeUser()->ID()); + std::vector::iterator it = std::find_if(adjustments.begin(), adjustments.end(), [this, t, i](AdjustmentValue val) { + return (val.titleID == t[i]->titleID() && val.userID == this->app->activeUser()->ID()); + }); + if (it != adjustments.end()) { + ps->playtime += (*it).value; + } + + // Skip unplayed titles totalSecs += ps->playtime; if (ps->launches == 0) { - // Skip unplayed titles - delete ps; - continue; + // Add in dummy data if not launched before (due to adjustment) + ps->firstPlayed = Utils::Time::posixTimestampToPdm(Utils::Time::getTimeT(Utils::Time::getTmForCurrentTime())); + ps->lastPlayed = ps->firstPlayed; + ps->launches = 1; + + if (ps->playtime == 0) { + delete ps; + continue; + } } // "Convert" PlayStatistics to SortInfo diff --git a/Application/source/ui/screen/Details.cpp b/Application/source/ui/screen/Details.cpp index 98dee1f..593a489 100644 --- a/Application/source/ui/screen/Details.cpp +++ b/Application/source/ui/screen/Details.cpp @@ -674,8 +674,23 @@ namespace Screen { this->updateActivity(); this->setFocussed(this->list); - // Add side stats + // Get statistics and append adjustment if needed + std::vector adjustments = this->app->config()->adjustmentValues(); NX::PlayStatistics * ps = this->app->playdata()->getStatisticsForUser(this->app->activeTitle()->titleID(), this->app->activeUser()->ID()); + std::vector::iterator it = std::find_if(adjustments.begin(), adjustments.end(), [this](AdjustmentValue val) { + return (val.titleID == this->app->activeTitle()->titleID() && val.userID == this->app->activeUser()->ID()); + }); + if (it != adjustments.end()) { + ps->playtime += (*it).value; + } + + if (ps->launches == 0) { + // Add in dummy data if not launched before (due to adjustment) + ps->firstPlayed = Utils::Time::posixTimestampToPdm(Utils::Time::getTimeT(Utils::Time::getTmForCurrentTime())); + ps->lastPlayed = ps->firstPlayed; + ps->launches = 1; + } + this->playtime = new Aether::Text(1070, 220, Utils::playtimeToString(ps->playtime), 20); this->playtime->setColour(this->app->theme()->accent()); this->playtime->setX(this->playtime->x() - this->playtime->w()/2); diff --git a/Application/source/utils/Time.cpp b/Application/source/utils/Time.cpp index 1d91884..1921a5f 100644 --- a/Application/source/utils/Time.cpp +++ b/Application/source/utils/Time.cpp @@ -101,6 +101,10 @@ namespace Utils::Time { return *(std::localtime(&t)); } + uint32_t posixTimestampToPdm(uint64_t timestamp) { + return static_cast((timestamp - 946598400)/60); + } + int tmGetDaysInMonth(struct tm t) { switch (t.tm_mon) { // Except February alone... @@ -289,4 +293,4 @@ namespace Utils::Time { break; } } -}; \ No newline at end of file +};