/* Achievements on the host's side: host/achievements.c's own text (achievements_actual.inc, cut by tests/test_achievements.py) against a stand-in save data, a stand-in menu and a game folder of the test's. Run three times: "off" with HOS_ACHIEVEMENTS=0, then "first" and "second" with it on the same folder, the second the next start. */ #include #include #include #include #include #include static int fails; #define CHECK(c) do { if (!(c)) { printf("FAIL line %d: %s\n", __LINE__, #c); fails++; } } while (0) /* ---- what host/achievements.c takes from hos.h, hos_paths.h, windows.h and menu_sound.c ---- */ #define MAX_PATH 260 typedef unsigned long DWORD; #define MOVEFILE_REPLACE_EXISTING 1 static char game_dir[400]; static DWORD hos_game_path_a(char *buf, DWORD size) { return (DWORD)snprintf(buf, size, "%s/Triforce.exe", game_dir); } static int MoveFileExA(const char *from, const char *to, int flags) { (void)flags; remove(to); return rename(from, to) == 0; } static char logged[1 << 14]; static void hos_log(const char *fmt, ...) { va_list ap; va_start(ap, fmt); const size_t at = strlen(logged); vsnprintf(logged + at, sizeof logged - at - 2, fmt, ap); va_end(ap); strcat(logged, "\n"); } static int said(const char *text) { /* how often the log has it */ int n = 0; for (const char *p = logged; (p = strstr(p, text)) != NULL; p++) n++; return n; } #include "achievements_constants.inc" /* (host/save_files.c: off but for the "file1" and "file2" runs, which play a save file of that folder under the game's) */ static char files_dir[440]; static int files_first; const char *hos_save_files_dir(int *first) { if (!files_dir[0]) return NULL; *first = files_first; return files_dir; } static int sounds; static int64_t sound_at; static void hos_menu_sound_play(int id, int64_t now_ns) { CHECK(id == HOS_MENU_SOUND_SYS_DECIDE); sounds++, sound_at = now_ns; } /* ---- the menu's side of HOS_OPT_ACH_* (gpu/vk/achievement_rows.h is tested by itself) ---- */ static int renderer; /* 0: no renderer yet (rx_option answers what it is given) */ static char incoming[600], row_text[160][400], notice_day[16][32]; static int incoming_n, row_bits[160], rows_added, rows_shown = -1, notices[16], nnotices; static int row_on = 1, row_notices = 1; /* (the System tab's Achievements and Achievement notices rows) */ int rx_option(int id, int otherwise) { if (id == HOS_OPT_ACH_ON) return renderer ? row_on : otherwise; if (id == HOS_OPT_ACH_NOTICES) return renderer ? row_notices : otherwise; if (!renderer) return otherwise; CHECK(id == HOS_OPT_ACH_ASK); return 1; } void rx_option_set(int id, int value) { CHECK(renderer); if (id == HOS_OPT_ACH_TEXT) { CHECK(value > 0 && value < 256 && incoming_n + 1 < (int)sizeof incoming); incoming[incoming_n++] = (char)value; } else if (id == HOS_OPT_ACH_ADD) { incoming[incoming_n] = 0, incoming_n = 0; CHECK(rows_shown < 0 && rows_added < 160); snprintf(row_text[rows_added], sizeof row_text[0], "%s", incoming); row_bits[rows_added++] = value; } else if (id == HOS_OPT_ACH_COUNT) { CHECK(rows_shown < 0 && !incoming_n); rows_shown = value; } else if (id == HOS_OPT_ACH_EARNED) { incoming[incoming_n] = 0, incoming_n = 0; CHECK(rows_shown >= 0 && value >= 0 && value < rows_shown && nnotices < 16); /* (never before the list is whole) */ snprintf(notice_day[nnotices], sizeof notice_day[0], "%s", incoming); notices[nnotices++] = value; } else CHECK(!"an option of another feature set"); } #include "achievements.h" #include "achievements_actual.inc" /* ---- a stand-in for the save data ---- */ static struct { int kind; const char *what; int value; } have[16]; static int nhave, asked; static void set(int kind, const char *what, int value) { have[nhave].kind = kind, have[nhave].what = what, have[nhave++].value = value; } static int probe(void *ctx, int kind, const char *what) { (void)ctx; asked++; for (int i = 0; i < nhave; i++) if (have[i].kind == kind && !strcmp(have[i].what, what)) return have[i].value; return -1; } #define S 1000000000LL static void at(int64_t seconds_x10) { hos_achievements_check(probe, NULL, seconds_x10 * (S / 10)); } static int row_of(const char *name) { /* the place the menu was given an achievement at, -1 none */ const size_t n = strlen(name); for (int i = 0; i < rows_added; i++) if (!strncmp(row_text[i], name, n) && row_text[i][n] == '\n') return i; return -1; } static int is_day(const char *text) { /* "2026-11-18" */ return strlen(text) == 10 && text[4] == '-' && text[7] == '-' && text[0] == '2'; } static char file_text[8192]; static const char *file(const char *name) { /* a file of the game folder as it is now ("" when there is none) */ char path[600]; snprintf(path, sizeof path, "%s/%s", game_dir, name); FILE *f = fopen(path, "rb"); file_text[f ? fread(file_text, 1, sizeof file_text - 1, f) : 0] = 0; if (f) fclose(f); return file_text; } static int lines(const char *text) { int n = 0; for (; *text; text++) n += *text == '\n'; return n; } int main(int argc, char **argv) { if (argc != 3) return 2; snprintf(game_dir, sizeof game_dir, "%s", argv[1]); set(ACH_ITEM, "Weapon_Sword_Kokiri", 1), set(ACH_ITEM, "Item_Stone_Forest", 1), set(ACH_ITEM, "Item_Stone_Fire", 1); set(ACH_ITEM, "Item_Stone_Water", 1), set(ACH_FLAG, "My_Flag", 1), set(ACH_ITEM, "Item_Token_Gold", 9); if (!strcmp(argv[2], "off")) { /* turned off: nothing asked of the save data or the menu, nothing read, written or said */ CHECK(!hos_achievements_on()); renderer = 1; hos_achievements_frame(), at(10), hos_achievements_frame(), at(100); CHECK(!asked && !logged[0] && !rows_added && rows_shown < 0 && !nnotices && !sounds && !ready && !file("triforce_achievements.txt")[0]); } else if (!strcmp(argv[2], "rows")) { /* the System tab's two rows, in a game folder of its own without a list of the player's */ renderer = 1, row_on = 0; hos_achievements_frame(); /* (the list is named all the same: its page shows what was earned) */ CHECK(rows_shown == 35); at(10), at(20); /* Achievements Off: nothing asked of the save data, nothing earned or written */ CHECK(!asked && !said("earned \"") && !nnotices && !file("triforce_achievements.txt")[0]); row_on = 1, row_notices = 0; /* On again, the notices Off: earned at the next check, told to the list one a check, no sound */ at(30); CHECK(asked && said("achievements: earned \"") == 5 && lines(file("triforce_achievements.txt")) == 6 && nnotices == 1); at(31), at(32), at(33), at(34), at(35); /* (a tenth of a second apart: no notice's time between them) */ CHECK(nnotices == 5 && !sounds && notices[4] == row_of("Three Stones")); row_notices = 1; /* the notices On again: the next one earned has its sound */ set(ACH_ITEM, "Item_Medal_Light", 1); at(40); CHECK(nnotices == 6 && sounds == 1 && notices[5] == row_of("Sage of Light")); } else if (!strcmp(argv[2], "file2")) { /* the save files on, File 2 played: none of what the game folder earned is its */ snprintf(files_dir, sizeof files_dir, "%s/user-2", game_dir); const int lines_before = lines(file("triforce_achievements.txt")); renderer = 1; hos_achievements_frame(); CHECK(said("achievements: 36 (1 of achievements.txt), 0 earned so far") == 1 && rows_shown == 36 && !(row_bits[0] & HOS_ACH_EARNED)); at(20); CHECK(said("achievements: earned \"") == 6 && said("earned \"Mine\" (6 of 36)") == 1 && !said("could not be written")); CHECK(lines(file("user-2/triforce_achievements.txt")) == 7 && strstr(file_text, " Mine\n") && !strstr(file_text, "Spider Hunter")); CHECK(lines(file("triforce_achievements.txt")) == lines_before && strstr(file_text, " Spider Hunter\n")); /* (the game folder's as it was) */ } else if (!strcmp(argv[2], "file1")) { /* File 1, never played with files: it starts from what the game folder earned */ snprintf(files_dir, sizeof files_dir, "%s/user", game_dir), files_first = 1; renderer = 1; hos_achievements_frame(); CHECK(said("achievements: 36 (1 of achievements.txt), 7 earned so far") == 1 && (row_bits[row_of("Spider Hunter")] & HOS_ACH_EARNED)); at(10); CHECK(!said("earned \"") && !file("user/triforce_achievements.txt")[0]); /* (nothing new: nothing written yet) */ set(ACH_ITEM, "Item_Medal_Light", 1); at(20); CHECK(said("earned \"Sage of Light\" (8 of 36)") == 1); /* its own file from here: the eight, and the line no list has, kept; the game folder's without the eighth */ CHECK(lines(file("user/triforce_achievements.txt")) == 10 && strstr(file_text, " Sage of Light\n") && strstr(file_text, "\ndeadbeef 5 Gone\n")); CHECK(lines(file("triforce_achievements.txt")) == 9 && !strstr(file_text, "Sage of Light")); } else if (!strcmp(argv[2], "again1")) { /* File 1's next start: its own file now, not the game folder's */ snprintf(files_dir, sizeof files_dir, "%s/user", game_dir), files_first = 1; renderer = 1; hos_achievements_frame(); CHECK(said("achievements: 36 (1 of achievements.txt), 8 earned so far") == 1); } else if (!strcmp(argv[2], "first")) { CHECK(hos_achievements_on()); hos_achievements_frame(); /* the first present, before the renderer answers: the lists are read, nothing is named */ CHECK(said("achievements: 36 (1 of achievements.txt), 0 earned so far") == 1 && said("achievements: 2 left out") == 1); CHECK(!rows_added && rows_shown < 0); at(20); /* six earned before the menu has the list: written and said at once, shown only once it has */ CHECK(said("achievements: earned \"") == 6 && said("earned \"A Sword of Your Own\" (1 of 36)") == 1 && said("earned \"Mine\" (6 of 36)") == 1); CHECK(said("earned \"Three Stones\"") == 1 && !said("Spider Hunter") && !said("could not be written")); CHECK(lines(file("triforce_achievements.txt")) == 7 && strstr(file_text, " A Sword of Your Own\n") && strstr(file_text, " Mine\n")); CHECK(!nnotices && !sounds); renderer = 1; hos_achievements_frame(); /* the list named, whole, in its order: name, the line about it, the day; the icon and earned */ CHECK(rows_added == 36 && rows_shown == 36 && !nnotices); CHECK(!strncmp(row_text[0], "A Sword of Your Own\nFind a blade in the forest.\n", 48) && is_day(row_text[0] + 48)); CHECK(row_bits[0] == (1 | 1 << 4 | HOS_ACH_EARNED)); /* (sword green, earned) */ CHECK(!strcmp(row_text[1], "First Melody\nLearn the royal family's song.\n") && row_bits[1] == 4); /* (a note, gold; not earned: no day) */ CHECK(row_of("Mine") == 35 && row_bits[35] == (2 | 3 << 4 | HOS_ACH_EARNED) && row_bits[row_of("Twice as Tough")] == (6 | 7 << 4)); hos_achievements_frame(), hos_achievements_frame(); CHECK(rows_added == 36); /* (named once) */ /* shown one at a time, each for its 4.5 s, in the list's order, with the sound and the day */ at(30); CHECK(nnotices == 1 && sounds == 1 && sound_at == 3 * S && notices[0] == 0 && is_day(notice_day[0])); at(40), at(74); CHECK(nnotices == 1); at(75); CHECK(nnotices == 2 && sounds == 2 && notices[1] == row_of("Forest's Thanks")); for (int t = 120; t <= 400; t += 10) at(t); CHECK(nnotices == 6 && sounds == 6 && notices[5] == 35); CHECK(said("achievements: earned \"") == 6 && said("achievements: 36 (") == 1 && lines(file("triforce_achievements.txt")) == 7); } else { renderer = 1; hos_achievements_frame(); /* the next start: what was earned is, and is neither said nor shown again */ CHECK(said("achievements: 36 (1 of achievements.txt), 6 earned so far") == 1 && rows_shown == 36); at(10); CHECK(!said("earned \"") && !nnotices && !sounds); CHECK((row_bits[0] & HOS_ACH_EARNED) && !(row_bits[row_of("Spider Hunter")] & HOS_ACH_EARNED)); have[5].value = 10; /* (the tenth token) */ at(20); CHECK(said("earned \"Spider Hunter\" (7 of 36)") == 1 && nnotices == 1 && notices[0] == row_of("Spider Hunter") && is_day(notice_day[0])); /* the file whole again: the seven, and the line of an achievement no list has now, as it was */ CHECK(lines(file("triforce_achievements.txt")) == 9 && strstr(file_text, " Spider Hunter\n") && strstr(file_text, "\ndeadbeef 5 Gone\n")); CHECK(!file("triforce_achievements.txt.new")[0]); } if (!fails) printf("PASS\n"); return fails != 0; }