From 2be97c5947776009b8b6a58c65a43267ee674dc3 Mon Sep 17 00:00:00 2001 From: Milenko <39630670+MrMilenko@users.noreply.github.com> Date: Wed, 18 Nov 2026 23:11:59 -0500 Subject: [PATCH] Time played, and back to the dashboard when a game closes Launches from the dashboard are watched now. A game started as a program counts while its process lives; a Steam game counts from when Steam reports it running until it stops. When it closes, the time is added to the game and the dashboard comes back (Settings > Games > Back to UIX When a Game Closes). A launcher that hands off and quits within 20 seconds isn't timed and doesn't pull the dashboard over the game. The guide shows Time Played on each game, the dashboard idles while a game is up and it's in the background, and finished child processes get reaped instead of piling up. --- theseus/desktop/desktop_settings.cpp | 3 +- theseus/desktop/guide.cpp | 11 ++ theseus/desktop/host.h | 14 ++- theseus/desktop/host_posix_app.cpp | 24 ++++- theseus/desktop/host_win_app.cpp | 32 +++++- theseus/desktop/launch.cpp | 156 +++++++++++++++++++++++++-- theseus/desktop/launch.h | 5 + theseus/desktop/launchers/steam.cpp | 36 +++++++ theseus/desktop/launchers/steam.h | 4 + theseus/desktop/sdl_main.cpp | 3 + theseus/desktop/virtual_games.h | 27 ++++- 11 files changed, 294 insertions(+), 21 deletions(-) diff --git a/theseus/desktop/desktop_settings.cpp b/theseus/desktop/desktop_settings.cpp index 9af10ab..eb5efca 100644 --- a/theseus/desktop/desktop_settings.cpp +++ b/theseus/desktop/desktop_settings.cpp @@ -17,7 +17,7 @@ extern char s_steamPath[512]; extern char s_retroarchPath[512]; extern char s_esdePath[512]; extern bool g_showRetroArchTab, g_showSteamTab, g_showEsdeTab; -extern bool g_retroarchFullscreen; +extern bool g_retroarchFullscreen, g_returnAfterGame; extern int g_audioBufferFrames; extern int g_startupMode; extern bool g_bUseOnScreenKeyboard, g_bShowBootAnimation; @@ -116,6 +116,7 @@ static const SettingDef kDefs[] = { S_PATH("EsdePath", "Desktop", "Games", "ES-DE Folder", s_esdePath, 0), S_BOOL("ShowEsdeTab", "Desktop", "Games", "ES-DE Import Tab", g_showEsdeTab, "1", 0), S_BOOL("RetroArchFullscreen", "Desktop", "Games", "RetroArch Fullscreen", g_retroarchFullscreen, "1", 0), + S_BOOL("ReturnAfterGame", "Desktop", "Games", "Back to UIX When a Game Closes", g_returnAfterGame, "1", 0), { "AudioBufferFrames", "Desktop", "Audio", "Audio Buffer", SK_INT, &g_audioBufferFrames, 0, "1024", SF_RESTART | SF_HIDDEN, NOENUM, 0, 0, 256, NULL, NULL }, S_BOOL("ShowRetroArchTab", "Desktop", "Games", "RetroArch Tab", g_showRetroArchTab, "1", 0), diff --git a/theseus/desktop/guide.cpp b/theseus/desktop/guide.cpp index 3119c95..1c2a0f5 100644 --- a/theseus/desktop/guide.cpp +++ b/theseus/desktop/guide.cpp @@ -397,6 +397,15 @@ static void GameListRows(int list, std::vector& out) }); } +static std::string PlayTime(long long s) +{ + char buf[32]; + if (s <= 0) return "None yet"; + if (s < 3600) snprintf(buf, sizeof(buf), "%lld min", s < 60 ? 1 : s / 60); + else snprintf(buf, sizeof(buf), "%lld h %02lld min", s / 3600, (s % 3600) / 60); + return buf; +} + static std::string PlayedOn(long long t) { if (!t) return "Never"; @@ -543,6 +552,8 @@ static void Build() Add("Hidden", vg->hidden ? "Yes" : "No", "Hidden games stay out of the launcher until you bring them back.", GA_HIDE); Add("Last Played", PlayedOn(vg->lastPlayed).c_str(), NULL); + Add("Time Played", PlayTime(vg->playTime).c_str(), + "Counted when the game closes. Games another launcher starts for us can't be timed."); std::string where = vg->group[0] ? std::string(vg->category) + "/" + vg->group : std::string(vg->category); Add("Category", where.c_str(), NULL); break; diff --git a/theseus/desktop/host.h b/theseus/desktop/host.h index 83316d5..e3f4039 100644 --- a/theseus/desktop/host.h +++ b/theseus/desktop/host.h @@ -75,17 +75,23 @@ void Host_InstallCrashHandler(const char* userDataDir, const char* shippedDir); // What bgfx needs to draw into the SDL window. ndt is the display // connection (X11/Wayland), NULL elsewhere. bool Host_NativeWindow(SDL_Window* window, void** nwh, void** ndt, bool* wayland); -// Fire and forget: a URL goes to the OS handler, anything else runs as a -// shell command from workdir (may be NULL). err gets a line on failure. +// A URL goes to the OS handler, anything else runs as a shell command from +// workdir (may be NULL). err gets a line on failure. When proc isn't NULL it +// gets the child to watch, or NULL when there's nothing to watch (a URL on +// Windows); hand every one back to Host_ProcFree. +struct HostProc; bool Host_Spawn(const char* spec, bool isUrl, bool isSteamUrl, const char* workdir, - char* err, size_t errSize); + char* err, size_t errSize, HostProc** proc); +// True once the child has exited. Never blocks. +bool Host_ProcDone(HostProc* proc); +void Host_ProcFree(HostProc* proc); // Native "open file" dialog. False on cancel or when there isn't one. bool Host_PickFile(const char* title, char* out, size_t outSize); // Full path of the running executable. bool Host_ExePath(char* out, size_t outSize); // Show a folder in the OS file manager. Doesn't wait. void Host_OpenFolder(const char* dir); -// A string value under HKEY_CURRENT_USER. Always false off Windows. +// A value under HKEY_CURRENT_USER as text (numbers in decimal). Always false off Windows. bool Host_RegistryString(const char* subKey, const char* value, char* out, size_t outSize); // Audio CD in the first optical drive: track count and per-track lengths // in seconds. -1 when there's no readable disc. diff --git a/theseus/desktop/host_posix_app.cpp b/theseus/desktop/host_posix_app.cpp index 44f844e..a8cdbbe 100644 --- a/theseus/desktop/host_posix_app.cpp +++ b/theseus/desktop/host_posix_app.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #ifdef __APPLE__ #include @@ -493,8 +494,11 @@ static void ExecLaunch(const char* spec, bool isUrl, bool isSteamUrl) { } } +struct HostProc { pid_t pid; bool done; }; + bool Host_Spawn(const char* spec, bool isUrl, bool isSteamUrl, const char* workdir, - char* err, size_t errSize) { + char* err, size_t errSize, HostProc** proc) { + if (proc) *proc = NULL; pid_t pid = fork(); if (pid == 0) { if (workdir) (void)chdir(workdir); @@ -506,10 +510,26 @@ bool Host_Spawn(const char* spec, bool isUrl, bool isSteamUrl, const char* workd snprintf(err, errSize, "fork() failed: %s", strerror(errno)); return false; } - // Parent: don't wait. Fire-and-forget. + if (proc) { + *proc = new HostProc; + (*proc)->pid = pid; + (*proc)->done = false; + } return true; } +bool Host_ProcDone(HostProc* proc) { + if (!proc || proc->done) return true; + int status; + pid_t r = waitpid(proc->pid, &status, WNOHANG); + if (r == proc->pid || (r < 0 && errno == ECHILD)) proc->done = true; + return proc->done; +} + +void Host_ProcFree(HostProc* proc) { + delete proc; +} + bool Host_PickFile(const char* title, char* out, size_t outSize) { char cmd[512]; #if defined(__APPLE__) diff --git a/theseus/desktop/host_win_app.cpp b/theseus/desktop/host_win_app.cpp index e37c191..e9ad014 100644 --- a/theseus/desktop/host_win_app.cpp +++ b/theseus/desktop/host_win_app.cpp @@ -414,9 +414,12 @@ bool Host_NativeWindow(SDL_Window* window, void** nwh, void** ndt, bool* wayland // URLs go through ShellExecute. Raw commands run through cmd /C; no /S, so // cmd preserves outer quotes for the 2-quote/exists case and strips // first/last otherwise. +struct HostProc { HANDLE h; }; + bool Host_Spawn(const char* spec, bool isUrl, bool isSteamUrl, const char* workdir, - char* err, size_t errSize) { + char* err, size_t errSize, HostProc** proc) { (void)isSteamUrl; + if (proc) *proc = NULL; if (isUrl) { HINSTANCE rc = ShellExecuteA(NULL, "open", spec, NULL, workdir, SW_SHOWNORMAL); if ((INT_PTR)rc <= 32) { @@ -452,11 +455,26 @@ bool Host_Spawn(const char* spec, bool isUrl, bool isSteamUrl, const char* workd snprintf(err, errSize, "CreateProcess failed (error %lu): %s", e, spec); return false; } - CloseHandle(pi.hProcess); CloseHandle(pi.hThread); + if (proc) { + *proc = new HostProc; + (*proc)->h = pi.hProcess; + } else { + CloseHandle(pi.hProcess); + } return true; } +bool Host_ProcDone(HostProc* proc) { + return !proc || WaitForSingleObject(proc->h, 0) != WAIT_TIMEOUT; +} + +void Host_ProcFree(HostProc* proc) { + if (!proc) return; + CloseHandle(proc->h); + delete proc; +} + bool Host_PickFile(const char* title, char* out, size_t outSize) { OPENFILENAMEA ofn = {}; char path[MAX_PATH] = {}; @@ -484,9 +502,15 @@ bool Host_RegistryString(const char* subKey, const char* value, char* out, size_ if (RegOpenKeyExA(HKEY_CURRENT_USER, subKey, 0, KEY_READ, &hKey) != ERROR_SUCCESS) return false; DWORD size = (DWORD)outSize; DWORD type = 0; - bool ok = RegQueryValueExA(hKey, value, NULL, &type, (BYTE*)out, &size) == ERROR_SUCCESS && - (type == REG_SZ || type == REG_EXPAND_SZ); + bool ok = RegQueryValueExA(hKey, value, NULL, &type, (BYTE*)out, &size) == ERROR_SUCCESS; RegCloseKey(hKey); + if (ok && type == REG_DWORD && size == sizeof(DWORD)) { + DWORD n; + memcpy(&n, out, sizeof(n)); + snprintf(out, outSize, "%lu", (unsigned long)n); + return true; + } + ok = ok && (type == REG_SZ || type == REG_EXPAND_SZ); if (ok) out[outSize - 1] = '\0'; return ok; } diff --git a/theseus/desktop/launch.cpp b/theseus/desktop/launch.cpp index 019e29d..5a49320 100644 --- a/theseus/desktop/launch.cpp +++ b/theseus/desktop/launch.cpp @@ -17,6 +17,9 @@ #include "stb_image.h" #include "path_template.h" #include "launchers/launcher.h" +#include "launchers/steam.h" + +#include extern "C" void DashAudio_MuteAll(void); extern SDL_Window* g_pSDLWindow; @@ -133,7 +136,7 @@ static bool DeriveWorkDir(const char* cmd, char* out, size_t outSize) // e.g. Title Maker Test Launch) and SpawnLaunchSpec (overlay-driven game // launch) call this. Returns true if the OS handed us back a process / // shell-exec handle; the parent doesn't wait on the child either way. -static bool Launch_DoSpawn(const char* expanded) +static bool Launch_DoSpawn(const char* expanded, HostProc** proc) { if (!expanded || !*expanded) return false; @@ -147,7 +150,7 @@ static bool Launch_DoSpawn(const char* expanded) bool haveWorkdir = DeriveWorkDir(spec, workdir, sizeof(workdir)); if (!Host_Spawn(spec, IsUrl(spec), IsSteamUrl(spec), haveWorkdir ? workdir : NULL, - g_launchLastResult, sizeof(g_launchLastResult))) { + g_launchLastResult, sizeof(g_launchLastResult), proc)) { fprintf(stderr, "[launch] %s\n", g_launchLastResult); return false; } @@ -162,7 +165,7 @@ static bool Launch_DoSpawn(const char* expanded) // through this so the trace, error reporting, and command-form rules // live in one place. static bool Launch_ExpandAndSpawn(const char* spec, - char* finalOut, size_t finalSize) + char* finalOut, size_t finalSize, HostProc** proc) { if (!spec || !spec[0]) return false; char expanded[2048]; @@ -173,13 +176,151 @@ static bool Launch_ExpandAndSpawn(const char* spec, fprintf(stderr, "[launch] var: %s\n", expanded); if (strcmp(expanded, finalOut) != 0) fprintf(stderr, "[launch] cmd: %s\n", finalOut); - return Launch_DoSpawn(finalOut); + return Launch_DoSpawn(finalOut, proc); +} + +// ============================================================================ +// The game that's running: its play time, and the trip back to the dashboard +// when it closes. +// ============================================================================ + +bool g_returnAfterGame = true; // desktop.ini ReturnAfterGame + +// A launcher that starts the real game and quits this fast was only a +// doorway: the game is still up, so there's nothing to time or come back to. +static const double kHandOff = 20.0; +// How long a steam:// launch gets to show up as running. Updates, shader +// caches and a cold Steam client all count against it. +static const double kSteamStartWait = 300.0; + +enum { SESSION_NONE, SESSION_PROC, SESSION_STEAM_WAIT, SESSION_STEAM }; +static struct { + int kind; + HostProc* proc; + int appid; + double start; + char launch[1024]; // the games.ini Launch line, for the play time +} s_session; + +// Children nobody watches, reaped as they finish. +static std::vector s_strays; + +static double SessionNow() +{ + return (double)SDL_GetPerformanceCounter() / (double)SDL_GetPerformanceFrequency(); +} + +static void Stray(HostProc* proc) +{ + if (proc) s_strays.push_back(proc); +} + +// The app id in steam://rungameid/220 or steam://run/220. Shortcuts Steam +// made for non-Steam games have 64-bit ids it never reports, so no. +static int SteamAppId(const char* url) +{ + const char* p = strrchr(url, '/'); + if (!p || !p[1]) return 0; + long long id = 0; + for (p++; *p >= '0' && *p <= '9'; p++) { + id = id * 10 + (*p - '0'); + if (id > 0x7fffffff) return 0; + } + return *p == 0 ? (int)id : 0; +} + +static void SessionDrop() +{ + Stray(s_session.proc); + s_session.proc = NULL; + s_session.kind = SESSION_NONE; +} + +static void SessionBegin(const char* launch, const char* final, HostProc* proc) +{ + SessionDrop(); + strncpy(s_session.launch, launch, sizeof(s_session.launch) - 1); + s_session.launch[sizeof(s_session.launch) - 1] = 0; + s_session.start = SessionNow(); + if (IsSteamUrl(final)) { + // What we spawned only hands the URL to Steam, or is Steam itself. + Stray(proc); + s_session.appid = SteamAppId(final); + s_session.kind = s_session.appid ? SESSION_STEAM_WAIT : SESSION_NONE; + } else if (proc) { + s_session.proc = proc; + s_session.kind = SESSION_PROC; + } + if (s_session.kind == SESSION_STEAM_WAIT) + fprintf(stderr, "[launch] watching Steam app %d\n", s_session.appid); + else if (s_session.kind == SESSION_PROC) + fprintf(stderr, "[launch] watching the game\n"); +} + +static void SessionEnd() +{ + double played = SessionNow() - s_session.start; + SessionDrop(); + if (played < kHandOff) { + fprintf(stderr, "[launch] closed after %.0fs, taken as a hand-off\n", played); + return; + } + fprintf(stderr, "[launch] game closed after %.0fs\n", played); + VGames_AddPlayTime(s_session.launch, (long long)played); + if (g_returnAfterGame && g_pSDLWindow) { + // Windows won't let a background app come forward without this. + SDL_SetHint(SDL_HINT_FORCE_RAISEWINDOW, "1"); + SDL_RestoreWindow(g_pSDLWindow); + SDL_RaiseWindow(g_pSDLWindow); + } +} + +static void SessionTick() +{ + static double s_next = 0; + double now = SessionNow(); + if (now < s_next) return; + s_next = now + 1.0; + + for (size_t i = 0; i < s_strays.size();) { + if (Host_ProcDone(s_strays[i])) { + Host_ProcFree(s_strays[i]); + s_strays.erase(s_strays.begin() + i); + } else { + i++; + } + } + + switch (s_session.kind) { + case SESSION_PROC: + if (Host_ProcDone(s_session.proc)) SessionEnd(); + break; + case SESSION_STEAM_WAIT: + if (Steam_RunningAppId() == s_session.appid) { + s_session.kind = SESSION_STEAM; + s_session.start = now; + } else if (now - s_session.start > kSteamStartWait) { + fprintf(stderr, "[launch] Steam app %d never showed up as running\n", s_session.appid); + SessionDrop(); + } + break; + case SESSION_STEAM: + if (Steam_RunningAppId() != s_session.appid) SessionEnd(); + break; + } +} + +bool Launch_GameRunning() +{ + return s_session.kind != SESSION_NONE; } void DesktopLaunch(const char* spec) { char finalCmd[2048]; - Launch_ExpandAndSpawn(spec, finalCmd, sizeof(finalCmd)); + HostProc* proc = NULL; + Launch_ExpandAndSpawn(spec, finalCmd, sizeof(finalCmd), &proc); + Stray(proc); } // Spawn called by the launch overlay tick once the fade-in completes. @@ -188,8 +329,10 @@ void DesktopLaunch(const char* spec) static void SpawnLaunchSpec(const char* spec) { char finalCmd[2048]; - if (!Launch_ExpandAndSpawn(spec, finalCmd, sizeof(finalCmd))) return; + HostProc* proc = NULL; + if (!Launch_ExpandAndSpawn(spec, finalCmd, sizeof(finalCmd), &proc)) return; VGames_MarkPlayed(spec); + SessionBegin(spec, finalCmd, proc); extern SDL_Window* g_pSDLWindow; if (g_pSDLWindow) SDL_MinimizeWindow(g_pSDLWindow); @@ -281,6 +424,7 @@ unsigned long long LaunchOverlay_LogoGLTex(int* outW, int* outH) void LaunchOverlay_Tick() { + SessionTick(); if (!s_overlayActive) return; double t = LaunchOverlay_Now() - s_overlayStart; diff --git a/theseus/desktop/launch.h b/theseus/desktop/launch.h index 33e364d..e4daf34 100644 --- a/theseus/desktop/launch.h +++ b/theseus/desktop/launch.h @@ -46,6 +46,11 @@ void DesktopLaunchTitle(const char* devicePath); // through stderr to see why a spec didn't run. extern char g_launchLastResult[256]; +// A game started from the dashboard is still running. Steam games count from +// when Steam reports them, other launches while their process lives. +bool Launch_GameRunning(); +extern bool g_returnAfterGame; // bring the dashboard back when it closes + // Launch overlay (desktop-only fade transition emulating theLaunchGameLevel). // DesktopLaunchGame queues the spawn and arms the overlay; the SDL render loop // ticks every frame to advance the fade and fires the actual spawn + minimize diff --git a/theseus/desktop/launchers/steam.cpp b/theseus/desktop/launchers/steam.cpp index f6ab78b..90fed85 100644 --- a/theseus/desktop/launchers/steam.cpp +++ b/theseus/desktop/launchers/steam.cpp @@ -216,3 +216,39 @@ int Steam_DiscoverLibraries(const char* userOverride, return n; } + +// Steam keeps RunningAppID in the registry on Windows and in registry.vdf +// everywhere else. Native, Flatpak and macOS homes are all tried. +int Steam_RunningAppId() +{ + char val[64]; + if (Host_RegistryString("Software\\Valve\\Steam", "RunningAppID", val, sizeof(val))) + return atoi(val); + + const char* home = getenv("HOME"); + if (!home) return -1; + static const char* kFiles[] = { + "/.steam/registry.vdf", + "/.var/app/com.valvesoftware.Steam/.steam/registry.vdf", + "/Library/Application Support/Steam/registry.vdf", + }; + for (size_t i = 0; i < sizeof(kFiles) / sizeof(kFiles[0]); i++) { + char path[1024]; + snprintf(path, sizeof(path), "%s%s", home, kFiles[i]); + FILE* f = fopen(path, "r"); + if (!f) continue; + char line[512]; + int id = -1; + while (fgets(line, sizeof(line), f)) { + // "RunningAppID" "220" + const char* k = strstr(line, "\"RunningAppID\""); + if (!k) continue; + const char* v = strchr(k + 14, '"'); + if (v) id = atoi(v + 1); + break; + } + fclose(f); + if (id >= 0) return id; + } + return -1; +} diff --git a/theseus/desktop/launchers/steam.h b/theseus/desktop/launchers/steam.h index 9de8305..166ebbc 100644 --- a/theseus/desktop/launchers/steam.h +++ b/theseus/desktop/launchers/steam.h @@ -36,3 +36,7 @@ int Steam_DiscoverInstallRoots(const char* userOverride, // Returns the count written into `outLibraries`, capped at maxLibs. int Steam_DiscoverLibraries(const char* userOverride, char outLibraries[][512], int maxLibs); + +// The app Steam says is running: 0 for none, -1 when Steam's state can't be +// read (not installed, or not started yet this session). +int Steam_RunningAppId(); diff --git a/theseus/desktop/sdl_main.cpp b/theseus/desktop/sdl_main.cpp index ab23459..98959a4 100644 --- a/theseus/desktop/sdl_main.cpp +++ b/theseus/desktop/sdl_main.cpp @@ -1775,6 +1775,9 @@ int main(int argc, char* argv[]) { if (IDirect3DDevice8::s_preSwapCB) IDirect3DDevice8::s_preSwapCB(); Frame_PhaseEnd(FP_IMGUI); + // A game we started is up and we're behind it: stay out of its way. + if (!g_windowFocused && Launch_GameRunning()) SDL_Delay(100); + // FPS cap: sleep until budget elapsed. 0 = unlimited. if (g_fpsCap > 0) { Uint64 budget = perfFreq / (Uint64)g_fpsCap; diff --git a/theseus/desktop/virtual_games.h b/theseus/desktop/virtual_games.h index ea90ca3..642e9dd 100644 --- a/theseus/desktop/virtual_games.h +++ b/theseus/desktop/virtual_games.h @@ -43,6 +43,7 @@ struct VirtualGame { bool favorite; bool hidden; long long lastPlayed; // unix time of the last launch that started, 0 for never + long long playTime; // seconds, counted only when the launcher saw the game close // Another source has the same game and wins (VGames_FindDuplicates). bool duplicate; }; @@ -124,6 +125,8 @@ inline void VGames_Load() { cur->hidden = atoi(val) != 0; else if (strcmp(key, "LastPlayed") == 0) cur->lastPlayed = atoll(val); + else if (strcmp(key, "PlayTime") == 0) + cur->playTime = atoll(val); } fclose(fp); g_vgames.generation++; @@ -192,6 +195,7 @@ inline void VGames_Save() { if (g.favorite) fprintf(fp, "Favorite=1\n"); if (g.hidden) fprintf(fp, "Hidden=1\n"); if (g.lastPlayed) fprintf(fp, "LastPlayed=%lld\n", g.lastPlayed); + if (g.playTime) fprintf(fp, "PlayTime=%lld\n", g.playTime); fprintf(fp, "\n"); } save.Commit(); @@ -389,7 +393,7 @@ inline int VGames_MakeShortcutContent(int idx, char* buf, int bufSize) { // Favorites, hidden and last played for live rows (ES-DE, xCloud), which // never reach games.ini. Keyed by title id, which both keep stable. -struct VGameMarks { bool favorite, hidden; long long lastPlayed; }; +struct VGameMarks { bool favorite, hidden; long long lastPlayed, playTime; }; #define VGAMES_LIVE_INI AppPath("Configs/games_live.ini") inline std::map& VGames_LiveMarks() { @@ -407,7 +411,7 @@ inline std::map& VGames_LiveMarks() { char* end = strrchr(line, ']'); if (!end) { cur = NULL; continue; } *end = 0; - VGameMarks blank = { false, false, 0 }; + VGameMarks blank = { false, false, 0, 0 }; cur = &(s_marks[line + 1] = blank); continue; } @@ -417,6 +421,7 @@ inline std::map& VGames_LiveMarks() { if (strcmp(line, "Favorite") == 0) cur->favorite = atoi(eq + 1) != 0; else if (strcmp(line, "Hidden") == 0) cur->hidden = atoi(eq + 1) != 0; else if (strcmp(line, "LastPlayed") == 0) cur->lastPlayed = atoll(eq + 1); + else if (strcmp(line, "PlayTime") == 0) cur->playTime = atoll(eq + 1); } fclose(fp); return s_marks; @@ -429,11 +434,12 @@ inline void VGames_SaveLiveMarks() { std::map& m = VGames_LiveMarks(); for (std::map::iterator it = m.begin(); it != m.end(); ++it) { const VGameMarks& k = it->second; - if (!k.favorite && !k.hidden && !k.lastPlayed) continue; + if (!k.favorite && !k.hidden && !k.lastPlayed && !k.playTime) continue; fprintf(save.f, "[%s]\n", it->first.c_str()); if (k.favorite) fprintf(save.f, "Favorite=1\n"); if (k.hidden) fprintf(save.f, "Hidden=1\n"); if (k.lastPlayed) fprintf(save.f, "LastPlayed=%lld\n", k.lastPlayed); + if (k.playTime) fprintf(save.f, "PlayTime=%lld\n", k.playTime); fprintf(save.f, "\n"); } save.Commit(); @@ -449,6 +455,7 @@ inline void VGames_ApplyLiveMarks(int idx) { g.favorite = it->second.favorite; g.hidden = it->second.hidden; g.lastPlayed = it->second.lastPlayed; + g.playTime = it->second.playTime; } // Writes the row's marks wherever that row lives. @@ -457,7 +464,7 @@ inline void VGames_StoreMarks(int idx) { VirtualGame& g = g_vgames.games[idx]; g_vgames.generation++; if (!VGames_IsLive(g)) { VGames_Save(); return; } - VGameMarks k = { g.favorite, g.hidden, g.lastPlayed }; + VGameMarks k = { g.favorite, g.hidden, g.lastPlayed, g.playTime }; VGames_LiveMarks()[g.titleID] = k; VGames_SaveLiveMarks(); } @@ -488,6 +495,18 @@ inline void VGames_MarkPlayed(const char* launch) { } } +// The launcher calls this when it saw the game close. +inline void VGames_AddPlayTime(const char* launch, long long seconds) { + if (!launch || !*launch || seconds <= 0) return; + VGames_Load(); + for (int i = 0; i < g_vgames.count; i++) { + VirtualGame& g = g_vgames.games[i]; + if (!g.valid || strcmp(g.launch, launch) != 0) continue; + g.playTime += seconds; + VGames_StoreMarks(i); + } +} + // The two lists the launcher builds from marks instead of a category. #define VGAMES_FAVORITES "Favorites" #define VGAMES_RECENT "Recently Played"