diff --git a/runtime/host.h b/runtime/host.h index 1cf399a3..21d0534f 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -318,7 +318,8 @@ int probe_mods(void); // mods_probe.c void discord_init(void); // discord_rpc.c: the Rich Presence thread, game runs only void playtime_init(void); // playtime.c: the play time thread, game runs only void playtime_save(void); // playtime.c: writes /playtime.txt; at exit -int playtime_get(uint64_t *total_s, uint64_t *session_s); // playtime.c: all runs and this one; 0 while not counted +int playtime_get(uint64_t *total_s, uint64_t *session_s); // playtime.c: all runs and this one; 0 without a profile +int probe_playtime(void); // playtime.c: the file, the ticks and the save int probe_discord(void); // discord_rpc.c int probe_loader(void), probe_kernel(void), probe_ipc(void), probe_services(void), probe_fs(void); int probe_gfx_abi(void); // gfx_abi_probe.c diff --git a/runtime/main.c b/runtime/main.c index 3cf47de3..49d227a0 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -7,6 +7,7 @@ // --probe-loader load and relocate the modules, check them against the measured 1.2.1 values // --probe-kernel, --probe-ipc, --probe-services, --probe-fs host probes // --probe-discord the Rich Presence IPC exchange; skips without a Discord client; no game files +// --probe-playtime the play time file, its ticks and saves (playtime.c); needs no game files // --probe-dpi the process DPI awareness (per-monitor v2) and the primary monitor's size; no game files // --probe-gfx-abi NVN object layouts against nvn.h's reserved sizes; needs no game files // --probe-nvn, --probe-nvn-tex, --probe-window, --probe-shader, --probe-vkmem, --probe-vkexec graphics probes @@ -463,7 +464,7 @@ static int probe_crash(int null) { static int usage(const char *argv0) { fprintf(stderr, "usage: %s [--exefs DIR] [--romfs DIR] [--profile DIR] [--selftest | --probe-loader | " - "--probe-cfg | --probe-dpi | --probe-mods | --probe-kernel | --probe-ipc | --probe-services | --probe-fs | --probe-gfx-abi | " + "--probe-cfg | --probe-dpi | --probe-mods | --probe-playtime | --probe-kernel | --probe-ipc | --probe-services | --probe-fs | --probe-gfx-abi | " "--probe-nvn | --probe-nvn-tex | --probe-window | --probe-shader | --probe-vkmem | " "--probe-vkexec | --probe-astc | --probe-movie | --probe-crash-call | --probe-crash-null | --probe-discord | " "--prepare-shaders | --warm-shaders | --boot |--seconds S [--shot PNG]]\n", argv0); @@ -567,6 +568,7 @@ int main(int argc, char **argv) { #endif if (!strcmp(mode, "--probe-mods")) return probe_mods(); if (!strcmp(mode, "--probe-discord")) return probe_discord(); + if (!strcmp(mode, "--probe-playtime")) return probe_playtime(); if (!strcmp(mode, "--probe-kernel")) return probe_kernel(); if (!strcmp(mode, "--probe-ipc")) return probe_ipc(); if (!strcmp(mode, "--probe-services")) return probe_services(); diff --git a/runtime/playtime.c b/runtime/playtime.c index 3d11d043..5b53700f 100644 --- a/runtime/playtime.c +++ b/runtime/playtime.c @@ -12,7 +12,7 @@ // install is updated or moved. It is plain text, "total_seconds ", written atomically (plat_file_write_atomic) // every SAVE_EVERY_MS while it grows and once more when the game exits (playtime_save from boot.c, svc.c and // gfx_present.c, next to prof_write); a crash loses at most SAVE_EVERY_MS. A file that does not read as that is kept -// as playtime.txt.bad and the count starts again from zero. +// as playtime.txt.bad and the count starts again from zero. A scripted run (--seconds) reads the total and adds nothing. #include #include #include @@ -33,36 +33,47 @@ static uint64_t total_ms, session_ms, saved_ms; static int loaded; // 1 once the file was read (or found missing), -1 when there is no profile static char path[1100]; -// Holding lock. -static void load(void) { +// The seconds of a playtime.txt: "total_seconds ", a line end or nothing after it, a UTF-8 byte order mark before it +// (an editor may add one). 1 when the file reads so and n is at most TOTAL_CAP_S. +static int parse(const uint8_t *b, uint64_t n, uint64_t *seconds) { + if (n >= 3 && !memcmp(b, "\xEF\xBB\xBF", 3)) b += 3, n -= 3; + char text[64] = {0}; + memcpy(text, b, n < sizeof text - 1 ? n : sizeof text - 1); + unsigned long long s = 0; + char tail = 0; + if (sscanf(text, "total_seconds %llu%c", &s, &tail) < 1 || (tail && tail != '\n' && tail != '\r') || + s > TOTAL_CAP_S) + return 0; + *seconds = s; + return 1; +} + +// Holding lock. dir: the profile directory. +static void load(const char *dir) { if (loaded) return; - const char *dir = rt_config()->profile; if (!dir || !*dir || snprintf(path, sizeof path, "%s/playtime.txt", dir) >= (int)sizeof path) { loaded = -1; rt_log("playtime: no profile directory; play time is not counted"); return; } loaded = 1; - uint64_t n = 0; + uint64_t n = 0, s = 0; uint8_t *b = plat_file_read(path, &n); if (!b) { rt_log("playtime: no %s yet; counting from zero", path); return; } - char text[64] = {0}; - memcpy(text, b, n < sizeof text - 1 ? n : sizeof text - 1); - free(b); - unsigned long long s = 0; - char tail = 0; - if (sscanf(text, "total_seconds %llu%c", &s, &tail) >= 1 && (!tail || tail == '\n' || tail == '\r') && - s <= TOTAL_CAP_S) { - total_ms = saved_ms = (uint64_t)s * 1000; - rt_log("playtime: %llu h %02llu min played before this run (%s)", s / 3600, s / 60 % 60, path); + if (parse(b, n, &s)) { + free(b); + total_ms = saved_ms = s * 1000; + rt_log("playtime: %llu h %02llu min played before this run (%s)", (unsigned long long)(s / 3600), + (unsigned long long)(s / 60 % 60), path); return; } char bad[1110]; snprintf(bad, sizeof bad, "%s.bad", path); - int kept = plat_file_write_atomic(bad, text, strlen(text)); + int kept = plat_file_write_atomic(bad, b, (size_t)n); // the whole file, for the player to recover the number + free(b); rt_log("playtime: %s does not read as \"total_seconds \"; %s, counting from zero", path, kept ? "kept as playtime.txt.bad" : "could not keep a copy"); } @@ -91,31 +102,113 @@ int playtime_get(uint64_t *total_s, uint64_t *session_s) { return ok; } +// Holding lock: the time since the last tick, d ms, when the game was played meanwhile; the file every SAVE_EVERY_MS. +static void tick_locked(uint64_t now, uint64_t d, int playing, uint64_t *last_save) { + if (d > TICK_CAP_MS) d = TICK_CAP_MS; + if (playing) total_ms += d, session_ms += d; + if (now - *last_save >= SAVE_EVERY_MS) save_locked(), *last_save = now; +} + static void playtime_main(void *arg) { (void)arg; plat_thread_priority(PLAT_PRIO_BELOW_NORMAL); - plat_mutex_lock(&lock); - load(); - int on = loaded == 1; - plat_mutex_unlock(&lock); - if (!on) return; uint64_t last = plat_now_ms(), last_save = last, frames = gfx_stats().frames; for (;;) { plat_sleep_ms(TICK_MS); - uint64_t now = plat_now_ms(), f = gfx_stats().frames, d = now - last; + uint64_t now = plat_now_ms(), f = gfx_stats().frames; int playing = f != frames && gfx_window_focused(); - last = now, frames = f; - if (d > TICK_CAP_MS) d = TICK_CAP_MS; plat_mutex_lock(&lock); - if (playing) total_ms += d, session_ms += d; - if (now - last_save >= SAVE_EVERY_MS) save_locked(), last_save = now; + tick_locked(now, now - last, playing, &last_save); plat_mutex_unlock(&lock); + last = now, frames = f; } } +// The file is read at once, so the Game tab shows the total from the first frame. A scripted run (--seconds: benches, +// menu shots, input scripts) shows it but adds nothing: those runs are not play. void playtime_init(void) { static int32_t started; if (plat_xchg32(&started, 1)) return; + plat_mutex_lock(&lock); + load(rt_config()->profile); + int on = loaded == 1; + plat_mutex_unlock(&lock); + if (!on) return; + if (rt_config()->seconds > 0) { + rt_log("playtime: a scripted run (--seconds); play time is shown, not counted"); + return; + } if (!plat_thread_spawn(playtime_main, NULL, "playtime", 0)) rt_log("playtime: cannot start its thread; play time is not counted"); } + +// --probe-playtime: the file's parsing, a missing, a valid and a damaged file, the periodic save and the tick's cap, in +// a directory next to totk.exe. Needs no game files. +static int fails; +#define CHECK(c, what) do { if (!(c)) { fails++; fprintf(stderr, "FAIL playtime: %s\n", what); } } while (0) + +static void reset(void) { total_ms = session_ms = saved_ms = 0, loaded = 0, path[0] = 0; } + +static int file_is(const char *p, const char *want) { + uint64_t n = 0; + uint8_t *b = plat_file_read(p, &n); + int ok = b && n == strlen(want) && !memcmp(b, want, n); + free(b); + return ok; +} + +int probe_playtime(void) { + uint64_t s = 0; + CHECK(parse((const uint8_t *)"total_seconds 42\n", 17, &s) && s == 42, "a line with LF"); + CHECK(parse((const uint8_t *)"total_seconds 42\r\n", 18, &s) && s == 42, "a line with CRLF"); + CHECK(parse((const uint8_t *)"total_seconds 42", 16, &s) && s == 42, "no line end"); + CHECK(parse((const uint8_t *)"\xEF\xBB\xBFtotal_seconds 7\n", 19, &s) && s == 7, "a byte order mark"); + CHECK(!parse((const uint8_t *)"total_seconds 42x", 17, &s), "a character after the number"); + CHECK(!parse((const uint8_t *)"total_seconds -1\n", 17, &s), "a negative number"); + CHECK(!parse((const uint8_t *)"total_seconds 3153600001\n", 25, &s), "over a hundred years"); + CHECK(!parse((const uint8_t *)"seconds 42\n", 11, &s), "another key"); + CHECK(!parse((const uint8_t *)"", 0, &s), "an empty file"); + + char dir[1024]; + if (!plat_exe_path(dir, sizeof dir)) return 1; + char *cut = plat_last_sep(dir); + if (!cut) return 1; + strcpy(cut + 1, "playtime-probe"); + plat_file_mkdir_p(dir); + char file[1100], bad[1110]; + snprintf(file, sizeof file, "%s/playtime.txt", dir); + snprintf(bad, sizeof bad, "%s.bad", file); + plat_file_delete(file); + plat_file_delete(bad); + + uint64_t total, session, last_save = 0; + reset(); + load(dir); + CHECK(playtime_get(&total, &session) && total == 0, "a missing file counts from zero"); + tick_locked(1000, 1000, 1, &last_save); + tick_locked(2000, 1000, 0, &last_save); + CHECK(plat_file_size(file) < 0, "nothing is written before SAVE_EVERY_MS"); + tick_locked(2000 + 3600000, 3600000, 1, &last_save); // a machine that slept: one tick adds TICK_CAP_MS, and saves + playtime_get(&total, &session); + CHECK(total == 3 && session == 3, "played ticks count, an idle one does not, a long one is capped"); + CHECK(file_is(file, "total_seconds 3\n"), "the save after SAVE_EVERY_MS writes the total"); + + reset(); + load(dir); + playtime_get(&total, &session); + CHECK(total == 3 && session == 0, "the next run reads the total back"); + + static const char damaged[] = "total_seconds twelve\n"; + plat_file_write_atomic(file, damaged, sizeof damaged - 1); + reset(); + load(dir); + playtime_get(&total, &session); + CHECK(total == 0, "a damaged file counts from zero"); + CHECK(file_is(bad, damaged), "a damaged file is kept whole as playtime.txt.bad"); + + plat_file_delete(file); + plat_file_delete(bad); + reset(); + fprintf(stderr, "playtime: %s (%d failures)\n", fails ? "FAILED" : "ok", fails); + return fails ? 1 : 0; +}