/* (Triforce) Achievements, as a console's or a store's: things done in the game that are marked as earned, once, with a notice on the screen and a sound when one is, and a page of the options that lists them. host/achievements.h has what an achievement is, the list every game has, the form of a player's own list and of the file that keeps the earned ones; this file has what is the game folder's and the screen's. - On by default (11-22, the owner's word); HOS_ACHIEVEMENTS=0 (triforce_env.txt) turns them off. While off nothing is read, of the game or of the disk. - The lists are read at the game's first present (hos_achievements_frame, host/nvn_native.c): the built-in one, then achievements.txt in the game folder when there is one. Once the renderer answers they are named to the menu (HOS_OPT_ACH_TEXT, _ADD, _COUNT: gpu/vk/achievement_rows.h), which lists them on the System tab's Achievements page. - host/hos_main.c asks once a second at Link's update, on the game's thread, while a save is loaded, and hands over how the save data is read (hos_achievements_check). - An achievement earned is written to triforce_achievements.txt in the game folder at once (the file whole, under another name first), said in the log, and shown: the menu's notice (HOS_OPT_ACH_EARNED) with the system menu's decide sound (HOS_MENU_SOUND_SYS_DECIDE, at the Menu sounds row's volume; silent where that row is Off. Not the item fanfare: most are earned as the game plays that itself). One at a time: a save that earns several at once shows them one after the other. - The present's thread and the game's both come here: what they share is under one lock, held for a list's naming or a check and no longer. - Nothing of the game's is written here, and nothing is changed in the game: the save data is only read. */ #include "hos.h" #include "hos_paths.h" #include #include #include #include #include "../gpu/vk/vk_api.h" #include "achievements.h" #include "menu_sound.h" int rx_option(int id, int otherwise); void rx_option_set(int id, int value); #define EARNED_FILE "triforce_achievements.txt" #define OWN_FILE "achievements.txt" enum { OWN_MOST = 1 << 18, SHOWN_MS = 4500 /* (a notice stays 4 s: gpu/vk/vk_menu.cpp) */ }; static AchList list; static int ready; /* the lists were read */ static int named; /* and named to the menu: it shows them, and a notice for one earned */ static int waiting[ACH_MAX], nwaiting; /* earned, not yet shown */ static int64_t next_shown; static volatile char busy; /* the lock: the present's thread names the list, the game's checks it */ static void lock(void) { while (__atomic_test_and_set(&busy, __ATOMIC_ACQUIRE)) {} } static void unlock(void) { __atomic_clear(&busy, __ATOMIC_RELEASE); } static const char *last_sep(const char *s) { const char *b = strrchr(s, '\\'), *f = strrchr(s, '/'); return b > f ? b : f; } /* a file of the game folder */ static int game_file(char *out, size_t cap, const char *name) { char exe[MAX_PATH]; const DWORD n = hos_game_path_a(exe, sizeof exe); const char *slash = n && n < sizeof exe ? last_sep(exe) : NULL; if (!slash) return 0; return snprintf(out, cap, "%.*s%s", (int)(slash + 1 - exe), exe, name) < (int)cap; } /* a text file of the game folder whole, its end a 0 (freed by the caller); NULL when it is not there or over `most` */ static char *read_text(const char *name, long most) { char path[MAX_PATH + 40]; if (!game_file(path, sizeof path, name)) return NULL; FILE *f = fopen(path, "rb"); if (!f) return NULL; char *text = NULL; long size = 0; if (!fseek(f, 0, SEEK_END) && (size = ftell(f)) >= 0 && size <= most && !fseek(f, 0, SEEK_SET) && (text = (char *)malloc((size_t)size + 1))) { const size_t got = fread(text, 1, (size_t)size, f); text[got] = 0; for (size_t i = 0; i < got; i++) if (!text[i]) text[i] = ' '; /* (a 0 in the middle is not the text's end) */ } fclose(f); return text; } static void earned_save(void) { static char text[ACH_MAX * (ACH_NAME + 40) + ACH_ORPHANS * ACH_LINE + 200]; char path[MAX_PATH + 40], fresh[MAX_PATH + 48]; const size_t n = ach_earned_write(&list, text, sizeof text); if (!n || !game_file(path, sizeof path, EARNED_FILE)) return; snprintf(fresh, sizeof fresh, "%s.new", path); FILE *f = fopen(fresh, "wb"); /* (its lines end the same on Windows and Linux) */ const int ok = f && fwrite(text, 1, n, f) == n; if ((f && fclose(f)) || !ok || !MoveFileExA(fresh, path, MOVEFILE_REPLACE_EXISTING)) { remove(fresh); hos_log("achievements: %s could not be written: what was earned is kept until the game ends", path); } } static void lists_read(void) { int refused = 0; const int built_in = ach_parse(&list, ACH_BUILTIN, &refused); char *own = read_text(OWN_FILE, OWN_MOST); const int own_n = own ? ach_parse(&list, own, &refused) : 0; free(own); char *earned = read_text(EARNED_FILE, OWN_MOST); const int marked = earned ? ach_earned_read(&list, earned) : 0; free(earned); hos_log("achievements: %d (%d of " OWN_FILE "%s), %d earned so far%s", list.n, own_n, own ? "" : ", which is not there", marked, built_in + own_n ? "" : ": none to earn"); if (refused) hos_log("achievements: %d left out: a name that is empty, over %d bytes or another's, no condition, over %d of them, or a " "line that is not about:, icon: [], item [] or flag ", refused, ACH_NAME, ACH_CONDS); } /* ---- the menu's side: gpu/vk/achievement_rows.h ---- */ static void to_menu(const char *text) { for (const char *p = text; *p; p++) rx_option_set(HOS_OPT_ACH_TEXT, (unsigned char)*p); } /* the day of a unix time, as the page shows it ("" when it is not known) */ static void day_of(int64_t when, char *out, size_t cap) { const time_t t = (time_t)when; const struct tm *tm = when > 0 ? localtime(&t) : NULL; if (!tm || !strftime(out, cap, "%Y-%m-%d", tm)) out[0] = 0; } static void name_list(void) { for (int i = 0; i < list.n; i++) { const Ach *a = &list.a[i]; char day[16]; day_of(a->earned ? a->when : 0, day, sizeof day); to_menu(a->name), to_menu("\n"), to_menu(a->about), to_menu("\n"), to_menu(day); rx_option_set(HOS_OPT_ACH_ADD, (a->icon & 255) | (a->earned ? HOS_ACH_EARNED : 0)); } rx_option_set(HOS_OPT_ACH_COUNT, list.n); } /* On unless HOS_ACHIEVEMENTS=0 (11-22: on by default). Read once */ int hos_achievements_on(void) { static int on = -1; if (on < 0) { const char *e = getenv("HOS_ACHIEVEMENTS"); on = !(e && e[0] == '0' && !e[1]); } return on; } /* host/nvn_native.c, at each present of the game's: the lists read at the first, and named to the menu once it answers */ void hos_achievements_frame(void) { static int done; /* (the present's thread alone) */ if (done || !hos_achievements_on()) return; lock(); if (!ready) ready = 1, lists_read(); if (rx_option(HOS_OPT_ACH_ASK, 0) > 0) named = done = 1, name_list(); unlock(); } /* host/hos_main.c, once a second at Link's update while a save is loaded (now_ns: hos_now_ns): what the save data has earned since the last call is marked, kept and shown */ void hos_achievements_check(AchProbe probe, void *ctx, int64_t now_ns) { if (!hos_achievements_on()) return; lock(); if (!ready) ready = 1, lists_read(); int fresh[ACH_MAX]; const int got = ach_check(&list, probe, ctx, (int64_t)time(NULL), fresh, ACH_MAX); for (int i = 0; i < got; i++) { hos_log("achievements: earned \"%s\" (%d of %d)", list.a[fresh[i]].name, ach_earned_count(&list) - (got - 1 - i), list.n); if (nwaiting < ACH_MAX) waiting[nwaiting++] = fresh[i]; } if (got) earned_save(); if (nwaiting && named && now_ns >= next_shown) { /* (not before the menu has the list: it shows them by their place in it) */ const int at = waiting[0]; char day[16]; memmove(waiting, waiting + 1, sizeof waiting[0] * (size_t)--nwaiting); next_shown = now_ns + SHOWN_MS * 1000000LL; day_of(list.a[at].when, day, sizeof day); to_menu(day); rx_option_set(HOS_OPT_ACH_EARNED, at); hos_menu_sound_play(HOS_MENU_SOUND_SYS_DECIDE, now_ns); } unlock(); }