Merge the fixes line: the stutter, memory, flicker and crash fixes, co-op, the mesh shader fallback and the launcher fixes.

Brought onto the line with FSR 4, its frame generation and the debug menu. Where both sides had added to the same
place, both stay: the patch table keeps the pickup count and the free camera, the settings list keeps both sides'
names, the menu includes the co-op page and the debug menu, and the launcher builder takes the pipeline list and the
FSR 4 folder. Both sides had added the menu's typed-character event; one definition stays, with the window code that
passes characters outside the basic plane.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-11 08:18:01 -05:00
213 changed files with 39872 additions and 1598 deletions

596
tests/action_prompt_check.c Normal file
View File

@@ -0,0 +1,596 @@
// runtime/action_prompt.c: the action prompt's diamond of face buttons laid out by label. The layout file here is made
// by the test from the format as action_prompt.c's header comment describes it, with the values the game's 1.1.1
// DoCommand_00.buil holds (A right, B bottom, X top, Y left), so the reader is checked against a writer of its own;
// the archives and zstd dictionaries are made here too. No game files. Given romfs.bin and the offsets and sizes of
// the layout archive and Pack/ZsDic.pack.zs in it, it also checks the game's own archive (game_archive). Built and
// run by tests/action_prompt_check.py; prints each failed check and exits 1 on any.
#include <math.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <zdict.h>
#include <zstd.h>
#include "action_prompt.h"
#include "sarc.h"
static int fails;
#define CHECK(c, ...) \
do { \
if (!(c)) { \
fails++; \
printf("FAIL line %d: ", __LINE__); \
printf(__VA_ARGS__); \
printf("\n"); \
} \
} while (0)
// ---- what action_prompt.c reaches in the runtime
static char last_log[512];
void rt_log(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vsnprintf(last_log, sizeof last_log, fmt, ap);
va_end(ap);
}
uint64_t plat_now_ns(void) { return (uint64_t)((double)clock() / CLOCKS_PER_SEC * 1e9); }
// ---- a BFFH layout as the game's DoCommand_00.buil has it
enum { SIZE = 0x8000, PANE_REC = 0x44 };
static uint8_t file[SIZE];
static size_t top; // the next free byte
static uint64_t alloc(size_t n) {
top = (top + 7) & ~(size_t)7;
uint64_t at = top;
top += n;
if (top > SIZE) {
printf("the test's layout is too small\n");
exit(2);
}
return at;
}
static void w32(uint64_t at, uint32_t v) { memcpy(file + at, &v, 4); }
static void w64(uint64_t at, uint64_t v) { memcpy(file + at, &v, 8); }
static void wf(uint64_t at, float v) { memcpy(file + at, &v, 4); }
static void set_f(uint8_t *p, uint64_t at, float v) { memcpy(p + at, &v, 4); }
static float rf(const uint8_t *p, uint64_t at) {
float v;
memcpy(&v, p + at, 4);
return v;
}
static uint32_t r32(const uint8_t *p, uint64_t at) {
uint32_t v;
memcpy(&v, p + at, 4);
return v;
}
static uint64_t r64(const uint8_t *p, uint64_t at) {
uint64_t v;
memcpy(&v, p + at, 8);
return v;
}
static uint64_t str(const char *s) {
uint64_t at = alloc(strlen(s) + 1);
memcpy(file + at, s, strlen(s) + 1);
return at;
}
#define Z (-2.842171e-14f) // the game stores some zeros so
static const struct {
const char *name;
float x, y, sx, sy;
uint32_t flags;
} panes[] = {
{"RootPane", 0, 0, 1, 1, 0x02555000},
{"P_BaseA_00", 0, 0, 1, 1, 0x02555089}, {"P_B_00", 0, -80, 1, 1, 0x0255508b}, {"P_X_00", 0, 80, 1, 1, 0x0255508b},
{"P_Y_00", -80, 0, 1, 1, 0x0255508b}, {"N_BtnA_00", 80, 0, 1, 1, 0x0255500b},
{"P_BaseB_00", Z, 0, 1, 1, 0x02555089}, {"P_A_00", 80, 0, 1, 1, 0x0255508b}, {"P_X_02", Z, 80, 1, 1, 0x0255508b},
{"P_Y_02", -80, 0, 1, 1, 0x0255508b}, {"N_BtnB_00", 0, -80, 1, 1, 0x0255500b},
{"P_BaseX_00", Z, 0, 1, -1, 0x02555089}, {"P_A_02", 80, 0, 1, 1, 0x0255508b}, {"P_B_03", Z, -80, 1, 1, 0x0255508b},
{"P_Y_04", -80, 0, 1, 1, 0x0255508b}, {"N_BtnX_00", 0, 80, 1, 1, 0x0255500b},
{"W_BaseX_00", -80, 0, 1, 1, 0x02554889}, {"W_BaseXDecide_00", -80, 0, 1, 1, 0x02554889},
{"T_GuideX_00", 64, 0, 1, 1, 0x02554909},
{"P_BaseY_00", Z, 0, -1, 1, 0x02555089}, {"P_A_01", 80, 0, 1, 1, 0x0255508b}, {"P_B_02", Z, -80, 1, 1, 0x0255508b},
{"P_X_03", Z, 80, 1, 1, 0x0255508b}, {"N_BtnY_00", -80, 0, 1, 1, 0x0255500b},
{"W_BaseY_00", 80, 0, -1, 1, 0x02554889}, {"W_BaseYDecide_00", 80, 0, -1, 1, 0x02554889},
{"T_GuideY_00", -68, 0, 1, 1, 0x02555909},
// left as they are: A's and L's bubbles, A's Decide null
{"W_BaseA_00", -80, 0, 1, 1, 0x02554889}, {"N_BaseADecide_00", -80, 0, 1, 1, 0x02555002},
{"T_GuideL_00", 66, 0, 1, 1, 0x02554909},
};
enum { NPANES = sizeof panes / sizeof *panes };
static uint64_t pane_at[NPANES];
static const char *const materials[] = {"P_BaseA_00", "P_BaseB_00", "P_BaseX_00", "P_BaseY_00", "P_B_00"};
static const char *const textures[][2] = {{"DoCommandCrossBase_A_00", "DoCommandCrossBaseGrd_A_00"},
{"DoCommandCrossBase_B_00", "DoCommandCrossBaseGrd_B_00"},
{"DoCommandCrossBase_B_00", "DoCommandCrossBaseGrd_B_00"},
{"DoCommandCrossBase_A_00", "DoCommandCrossBaseGrd_A_00"},
{"DoCommandSubBase_00", "DoCommandSubBaseGrd_00"}};
enum { NMATS = sizeof materials / sizeof *materials };
static uint64_t binding_at[NMATS][2]; // where each material's first two texture name offsets are
static uint64_t name_a[2], name_b[2]; // the offsets of the A and B pictures' names
static size_t make_layout(void) {
memset(file, 0, sizeof file);
top = 0x240;
memcpy(file, "BFFH", 4);
// the names first, A's and B's pictures shared as the game shares them
name_a[0] = str(textures[0][0]), name_a[1] = str(textures[0][1]);
name_b[0] = str(textures[1][0]), name_b[1] = str(textures[1][1]);
// panes
uint64_t names = alloc(8 * NPANES), records = alloc(8 * NPANES);
for (int i = 0; i < NPANES; i++) {
uint64_t r = alloc(PANE_REC);
for (int k = 0; k < PANE_REC; k += 4) w32(r + k, 0xa5a50000u + (uint32_t)(i << 8) + (uint32_t)k);
wf(r + 0x10, panes[i].sx), wf(r + 0x14, panes[i].sy), wf(r + 0x1c, panes[i].x), wf(r + 0x20, panes[i].y);
w32(r + 0x38, panes[i].flags);
w64(names + 8 * i, str(panes[i].name)), w64(records + 8 * i, r);
pane_at[i] = r;
}
w64(0x118, names), w64(0x120, records), w32(0x130, NPANES), w32(0x134, 8);
// materials and their texture blocks
uint64_t mnames = alloc(8 * NMATS), mrecords = alloc(8 * NMATS), blocks = alloc(8 * NMATS);
for (int i = 0; i < NMATS; i++) {
uint64_t m = alloc(0x40), block = alloc(0x40), bindings = alloc(3 * 8);
w32(m + 0x14, (uint32_t)i);
w64(block, bindings), w32(block + 8, 3), w32(block + 12, 8);
for (int k = 0; k < 3; k++) {
uint64_t b = alloc(16);
w32(b, 0x01010202);
uint64_t s = k == 2 ? str("DecoNoise_00")
: i < 4 ? ((i == 0 || i == 3) ? name_a[k] : name_b[k])
: str(textures[i][k]);
w64(b + 8, s);
w64(bindings + 8 * k, b);
if (k < 2) binding_at[i][k] = b + 8;
}
w64(mnames + 8 * i, str(materials[i])), w64(mrecords + 8 * i, m), w64(blocks + 8 * i, block);
}
w64(0x138, mnames), w64(0x140, mrecords), w32(0x150, NMATS), w32(0x154, 8);
w64(0x228, blocks), w32(0x230, NMATS), w32(0x234, 8);
return top;
}
static int pane(const char *name) {
for (int i = 0; i < NPANES; i++)
if (!strcmp(panes[i].name, name)) return i;
printf("no pane %s in the test's table\n", name);
exit(2);
}
static float px(const uint8_t *p, const char *name, int off) { return rf(p, pane_at[pane(name)] + off); }
// 1 when a differing byte at `at` is one the layout by label may change.
static int may_change(uint64_t at) {
static const char *const moved[] = {"P_B_00", "P_X_00", "P_Y_00", "N_BtnA_00", "P_A_00", "P_X_02", "P_Y_02",
"N_BtnB_00", "P_A_02", "P_B_03", "P_Y_04", "N_BtnX_00", "P_A_01", "P_B_02",
"P_X_03", "N_BtnY_00", "P_BaseX_00", "P_BaseY_00", "W_BaseX_00",
"W_BaseXDecide_00", "T_GuideX_00", "W_BaseY_00", "W_BaseYDecide_00",
"T_GuideY_00"};
for (size_t i = 0; i < sizeof moved / sizeof *moved; i++) {
uint64_t r = pane_at[pane(moved[i])];
if ((at >= r + 0x10 && at < r + 0x18) || (at >= r + 0x1c && at < r + 0x24) || (at >= r + 0x38 && at < r + 0x3c))
return 1;
}
for (int i = 0; i < 4; i++)
for (int k = 0; k < 2; k++)
if (at >= binding_at[i][k] && at < binding_at[i][k] + 8) return 1;
return 0;
}
// ---- a SARC: one other member, then the given layouts in order, each named `name`
enum { MAX_COPIES = 17 };
static size_t member_at[MAX_COPIES]; // where each layout copy starts in the last archive made
static size_t make_sarc(uint8_t *out, size_t cap, const uint8_t *const *layouts, int copies, size_t n,
const char *name) {
static const char other[] = "Other_00.buil";
static const uint8_t other_data[64] = {1, 2, 3, 4, 5, 6, 7, 8};
int count = 1 + copies;
size_t name_len = (strlen(name) + 1 + 3) & ~(size_t)3, other_len = (sizeof other + 3) & ~(size_t)3;
size_t sfat = 0x14, sfnt = sfat + 0xc + 16 * (size_t)count;
size_t data = (sfnt + 8 + other_len + name_len * copies + 0xff) & ~(size_t)0xff;
size_t total = (data + sizeof other_data + 0xff) & ~(size_t)0xff;
for (int c = 0; c < copies; c++) member_at[c] = total, total = (total + n + 0xff) & ~(size_t)0xff;
if (total > cap) return 0;
memset(out, 0, total);
memcpy(out, "SARC", 4);
uint16_t hl = 0x14, bom = 0xfeff, v = 0x100, fl = 0xc, cnt = (uint16_t)count, eight = 8;
uint32_t t32 = (uint32_t)total, d32 = (uint32_t)data, key = 0x65;
memcpy(out + 4, &hl, 2), memcpy(out + 6, &bom, 2);
memcpy(out + 8, &t32, 4), memcpy(out + 12, &d32, 4), memcpy(out + 16, &v, 2);
memcpy(out + sfat, "SFAT", 4), memcpy(out + sfat + 4, &fl, 2), memcpy(out + sfat + 6, &cnt, 2);
memcpy(out + sfat + 8, &key, 4);
memcpy(out + sfnt, "SFNT", 4), memcpy(out + sfnt + 4, &eight, 2);
size_t names = sfnt + 8;
memcpy(out + names, other, sizeof other);
uint32_t node[4] = {1, 0x01000000u, 0, (uint32_t)sizeof other_data};
memcpy(out + sfat + 0xc, node, sizeof node);
memcpy(out + data, other_data, sizeof other_data);
for (int c = 0; c < copies; c++) {
size_t s = names + other_len + name_len * c;
memcpy(out + s, name, strlen(name) + 1);
uint32_t e[4] = {2, 0x01000000u | (uint32_t)((s - names) / 4), (uint32_t)(member_at[c] - data),
(uint32_t)(member_at[c] - data + n)};
memcpy(out + sfat + 0xc + 16 * (size_t)(c + 1), e, sizeof e);
memcpy(out + member_at[c], layouts[c], n);
}
return total;
}
// Points copy c of an archive make_sarc made (its SFAT entry c + 1) at s[at..at + size).
static void set_range(uint8_t *s, int c, size_t at, size_t size) {
uint32_t data, lo, hi;
memcpy(&data, s + 12, 4);
lo = (uint32_t)(at - data), hi = (uint32_t)(at + size - data);
memcpy(s + 0x20 + 16 * (size_t)(c + 1) + 8, &lo, 4);
memcpy(s + 0x20 + 16 * (size_t)(c + 1) + 12, &hi, 4);
}
// A zstd dictionary with the id `id`, its content and samples taken from src[0..n); its size, 0 when it cannot be made.
static size_t make_dict(uint8_t *out, size_t cap, unsigned id, const uint8_t *src, size_t n) {
enum { SAMPLES = 32 };
size_t sizes[SAMPLES];
for (int i = 0; i < SAMPLES; i++) sizes[i] = n / SAMPLES;
ZDICT_params_t params = {3, 0, id};
size_t r = ZDICT_finalizeDictionary(out, cap, src, n < 2048 ? n : 2048, src, sizes, SAMPLES, params);
return ZDICT_isError(r) ? 0 : r;
}
// ---- the game's own archive, read from romfs.bin
static uint8_t *read_part(const char *path, const char *off, const char *size, size_t *n) {
long long at = strtoll(off, NULL, 10);
*n = (size_t)strtoull(size, NULL, 10);
FILE *f = fopen(path, "rb");
uint8_t *p = f && *n ? malloc(*n) : NULL;
#ifdef _WIN32
int ok = p && !_fseeki64(f, at, SEEK_SET) && fread(p, 1, *n, f) == *n;
#else
int ok = p && !fseeko(f, (off_t)at, SEEK_SET) && fread(p, 1, *n, f) == *n;
#endif
if (f) fclose(f);
if (!ok) free(p);
return ok ? p : NULL;
}
// The .zs file z[0..zn) unpacked with the dictionary it names from the unpacked dictionaries dics[0..dn); NULL when it
// does not unpack.
static uint8_t *unpack(const void *z, size_t zn, const uint8_t *dics, size_t dn, size_t *n) {
unsigned id = ZSTD_getDictID_fromFrame(z, zn);
unsigned long long rn = ZSTD_getFrameContentSize(z, zn);
size_t at = 0, size = 0;
if (rn == ZSTD_CONTENTSIZE_UNKNOWN || rn == ZSTD_CONTENTSIZE_ERROR || (id && !zsdic_find(dics, dn, id, &at, &size)))
return NULL;
uint8_t *raw = malloc((size_t)rn);
ZSTD_DCtx *d = ZSTD_createDCtx();
size_t got = !raw || !d ? 0
: id ? ZSTD_decompress_usingDict(d, raw, (size_t)rn, z, zn, dics + at, size)
: ZSTD_decompressDCtx(d, raw, (size_t)rn, z, zn);
ZSTD_freeDCtx(d);
if (ZSTD_isError(got) || got != rn) {
free(raw);
return NULL;
}
*n = (size_t)rn;
return raw;
}
// By label: made once, with the game's dictionary and unpacked size, and byte for byte the archive laid out by label,
// which differs from the game's only inside its four copies of the layout; then nothing more to make by label, nothing
// to make by position on the game's own, and by position again on the one by label.
static void game_archive(char **argv) {
size_t an, dn, pn = 0, rn = 0, on = 0, bn = 0;
uint8_t *a = read_part(argv[1], argv[2], argv[3], &an), *d = read_part(argv[1], argv[4], argv[5], &dn);
uint8_t *dics = d ? zsdic_unpack(d, dn, 64u << 20, &pn) : NULL;
uint8_t *raw = a && dics ? unpack(a, an, dics, pn, &rn) : NULL;
CHECK(raw != NULL, "game: cannot read and unpack the layout archive from %s", argv[1]);
if (!raw) {
free(dics), free(a), free(d);
return;
}
size_t at[16], size[16];
unsigned next = 0;
int copies = 0;
while (copies < 16 && sarc_next(raw, rn, "DoCommand_00.buil", &next, &at[copies], &size[copies]) == 1) copies++;
char why[200] = "";
uint8_t *expect = malloc(rn), *expect_back = malloc(rn);
memcpy(expect, raw, rn);
int fields = action_prompt_archive(expect, rn, 1, why, sizeof why);
CHECK(copies == 4 && fields == 4 * 52, "game: %d copies of the layout, %d fields changed (%s)", copies, fields, why);
memcpy(expect_back, expect, rn);
action_prompt_archive(expect_back, rn, 0, why, sizeof why);
void *out = NULL, *again = NULL;
size_t out_n = 0, again_n = 0;
int made = action_prompt_build(a, an, d, dn, 1, &out, &out_n);
CHECK(made == 1, "game: build by label %d (%s)", made, last_log);
if (made == 1) {
printf("game: %s, %zu bytes packed (the game's %zu)\n", last_log, out_n, an);
CHECK(ZSTD_getDictID_fromFrame(out, out_n) == ZSTD_getDictID_fromFrame(a, an) &&
ZSTD_getFrameContentSize(out, out_n) == rn,
"game: the built frame names another dictionary or size");
uint8_t *got = unpack(out, out_n, dics, pn, &on);
CHECK(got && on == rn && !memcmp(got, expect, rn), "game: the built archive is not the archive laid out by label");
size_t outside = 0;
for (size_t i = 0; got && on == rn && i < rn; i++) {
if (got[i] == raw[i]) continue;
int inside = 0;
for (int c = 0; c < copies; c++) inside |= i >= at[c] && i < at[c] + size[c];
outside += !inside;
}
CHECK(!outside, "game: %zu bytes changed outside the layout's copies", outside);
CHECK(action_prompt_build(out, out_n, d, dn, 1, &again, &again_n) == 0 && !again, "game: second build: %s",
last_log);
made = action_prompt_build(out, out_n, d, dn, 0, &again, &again_n);
uint8_t *back = made == 1 ? unpack(again, again_n, dics, pn, &bn) : NULL;
CHECK(back && bn == rn && !memcmp(back, expect_back, rn), "game: by position again: %d (%s)", made, last_log);
free(back);
free(again);
free(got);
}
again = NULL;
CHECK(action_prompt_build(a, an, d, dn, 0, &again, &again_n) == 0 && !again,
"game: build by position on the game's archive: %s", last_log);
free(out);
free(expect);
free(expect_back);
free(raw);
free(dics);
free(a);
free(d);
}
int main(int argc, char **argv) {
char why[200];
size_t n = make_layout();
static uint8_t before[SIZE], p[SIZE];
memcpy(before, file, n);
// By label: every place, cross, flip and bubble the game's diamond needs, and nothing else.
memcpy(p, before, n);
int changed = action_prompt_lay_out(p, n, 1, why, sizeof why);
CHECK(changed == 52, "fields changed by label: %d (%s)", changed, changed < 0 ? why : "");
static const struct { const char *pane; float x, y; } places[] = {
{"N_BtnA_00", 0, -80}, {"N_BtnB_00", 80, 0}, {"N_BtnX_00", -80, 0}, {"N_BtnY_00", 0, 80},
{"P_B_00", 80, 0}, {"P_X_00", -80, 0}, {"P_Y_00", 0, 80}, {"P_A_00", 0, -80},
{"P_X_02", -80, 0}, {"P_Y_02", 0, 80}, {"P_A_02", 0, -80}, {"P_B_03", 80, 0},
{"P_Y_04", 0, 80}, {"P_A_01", 0, -80}, {"P_B_02", 80, 0}, {"P_X_03", -80, 0}};
for (size_t i = 0; i < sizeof places / sizeof *places; i++)
CHECK(px(p, places[i].pane, 0x1c) == places[i].x && px(p, places[i].pane, 0x20) == places[i].y,
"%s at %g %g, expected %g %g", places[i].pane, px(p, places[i].pane, 0x1c), px(p, places[i].pane, 0x20),
places[i].x, places[i].y);
// The crosses: A lit down (picture B), B right (A), X left (A flipped in x), Y up (B flipped in y).
CHECK(r64(p, binding_at[0][0]) == name_b[0] && r64(p, binding_at[0][1]) == name_b[1], "A's cross is not picture B");
CHECK(r64(p, binding_at[1][0]) == name_a[0] && r64(p, binding_at[1][1]) == name_a[1], "B's cross is not picture A");
CHECK(r64(p, binding_at[2][0]) == name_a[0] && r64(p, binding_at[2][1]) == name_a[1], "X's cross is not picture A");
CHECK(r64(p, binding_at[3][0]) == name_b[0] && r64(p, binding_at[3][1]) == name_b[1], "Y's cross is not picture B");
CHECK(px(p, "P_BaseA_00", 0x10) == 1 && px(p, "P_BaseA_00", 0x14) == 1, "A's cross flipped");
CHECK(px(p, "P_BaseX_00", 0x10) == -1 && px(p, "P_BaseX_00", 0x14) == 1, "X's cross not flipped in x only");
CHECK(px(p, "P_BaseY_00", 0x10) == 1 && px(p, "P_BaseY_00", 0x14) == -1, "Y's cross not flipped in y only");
// X's bubble opens to the left, Y's to the right; the text origin follows.
CHECK(px(p, "W_BaseX_00", 0x10) == -1 && px(p, "W_BaseX_00", 0x1c) == 80, "X's bubble");
CHECK(px(p, "W_BaseXDecide_00", 0x10) == -1 && px(p, "W_BaseXDecide_00", 0x1c) == 80, "X's Decide bubble");
CHECK(px(p, "T_GuideX_00", 0x1c) == -68 && r32(p, pane_at[pane("T_GuideX_00")] + 0x38) == 0x02555909, "X's name");
CHECK(px(p, "W_BaseY_00", 0x10) == 1 && px(p, "W_BaseY_00", 0x1c) == -80, "Y's bubble");
CHECK(px(p, "W_BaseYDecide_00", 0x10) == 1 && px(p, "W_BaseYDecide_00", 0x1c) == -80, "Y's Decide bubble");
CHECK(px(p, "T_GuideY_00", 0x1c) == 64 && r32(p, pane_at[pane("T_GuideY_00")] + 0x38) == 0x02554909, "Y's name");
int stray = 0;
for (size_t i = 0; i < n; i++)
if (p[i] != before[i] && !may_change(i)) stray++;
CHECK(!stray, "%d bytes changed outside the diamond's fields", stray);
// Laid out by label already: nothing changes, and it says so.
uint8_t *again = malloc(n);
memcpy(again, p, n);
CHECK(action_prompt_lay_out(again, n, 1, why, sizeof why) == 0 && !memcmp(again, p, n), "a second run changed it");
// And back by position: the game's file again, its stored -2.8e-14 zeros now plain zeros.
changed = action_prompt_lay_out(again, n, 0, why, sizeof why);
CHECK(changed == 52, "fields changed back by position: %d (%s)", changed, changed < 0 ? why : "");
uint8_t *expect = malloc(n);
memcpy(expect, before, n);
for (int i = 0; i < NPANES; i++)
if (panes[i].x == Z && strncmp(panes[i].name, "P_Base", 6)) set_f(expect, pane_at[i] + 0x1c, 0.0f);
CHECK(!memcmp(again, expect, n), "by label and back is not the game's layout");
// A file that is not the expected one is refused before anything is written.
struct { const char *pane; int off; float v; } wrong[] = {
{"P_Y_04", 0x20, 1.0f}, {"N_BtnA_00", 0x20, -80.0f}, {"P_BaseY_00", 0x10, 1.0f}, {"T_GuideY_00", 0x1c, -60.0f}};
for (size_t i = 0; i < sizeof wrong / sizeof *wrong; i++) {
memcpy(p, before, n);
memcpy(p + pane_at[pane(wrong[i].pane)] + wrong[i].off, &wrong[i].v, 4);
memcpy(again, p, n);
int r = action_prompt_lay_out(p, n, 1, why, sizeof why);
CHECK(r == -1 && !memcmp(p, again, n) && strstr(why, wrong[i].pane), "%s changed: %d, %s", wrong[i].pane, r, why);
}
// Half laid out (A moved, the rest not): refused, untouched.
memcpy(p, before, n);
set_f(p, pane_at[pane("N_BtnA_00")] + 0x1c, 0), set_f(p, pane_at[pane("N_BtnA_00")] + 0x20, -80);
memcpy(again, p, n);
CHECK(action_prompt_lay_out(p, n, 1, why, sizeof why) == -1 && !memcmp(p, again, n), "half laid out: %s", why);
// A cross with another picture, a missing pane, a missing material: refused.
memcpy(p, before, n);
memcpy(p + binding_at[0][0], &name_b[0], 8);
CHECK(action_prompt_lay_out(p, n, 1, why, sizeof why) == -1 && strstr(why, "P_BaseA_00"), "cross picture: %s", why);
memcpy(p, before, n);
uint64_t s = r64(p, r64(p, 0x118) + 8 * (uint64_t)pane("P_X_03"));
p[s] = 'Q';
CHECK(action_prompt_lay_out(p, n, 1, why, sizeof why) == -1 && strstr(why, "P_X_03"), "missing pane: %s", why);
memcpy(p, before, n);
memset(p + 0x150, 0, 4); // no materials
CHECK(action_prompt_lay_out(p, n, 1, why, sizeof why) == -1 && strstr(why, "P_BaseA_00"), "no materials: %s", why);
// Offsets out of the file, a cut file and a wrong magic: refused, never read past the end.
memcpy(p, before, n);
uint64_t far = (uint64_t)n + 100;
memcpy(p + 0x118, &far, 8);
CHECK(action_prompt_lay_out(p, n, 1, why, sizeof why) == -1, "pane names past the end");
memcpy(p, before, n);
far = UINT64_MAX - 4;
memcpy(p + 0x120, &far, 8);
CHECK(action_prompt_lay_out(p, n, 1, why, sizeof why) == -1, "pane records at 2^64");
memcpy(p, before, n);
CHECK(action_prompt_lay_out(p, 0x200, 1, why, sizeof why) == -1, "a cut file");
CHECK(action_prompt_lay_out(p, 3, 1, why, sizeof why) == -1, "a 3-byte file");
p[0] = 'X';
CHECK(action_prompt_lay_out(p, n, 1, why, sizeof why) == -1 && strstr(why, "BFFH"), "wrong magic: %s", why);
// A label other than 1 counts as by label.
memcpy(p, before, n);
CHECK(action_prompt_lay_out(p, n, 2, why, sizeof why) == 52 && px(p, "N_BtnA_00", 0x20) == -80, "label 2: %s", why);
// Both bubbles of X on one record, their values the same in both layouts: laid out, the record moved once.
memcpy(p, before, n);
uint64_t shared = pane_at[pane("W_BaseX_00")];
memcpy(p + r64(p, 0x120) + 8 * (uint64_t)pane("W_BaseXDecide_00"), &shared, 8);
changed = action_prompt_lay_out(p, n, 1, why, sizeof why);
CHECK(changed == 52 && px(p, "W_BaseX_00", 0x10) == -1 && px(p, "W_BaseX_00", 0x1c) == 80,
"bubbles on one record: %d (%s)", changed, why);
// P_B_00's record laid over P_BaseA_00's material, its x on the material's block index (0 as x is 0.0): the file
// reads as expected, but moving the dot would lose A's cross. Refused, and every byte as it was.
memcpy(p, before, n);
uint64_t mat = r64(p, r64(p, 0x140)), over = mat + 0x14 - 0x1c;
memcpy(p + r64(p, 0x120) + 8 * (uint64_t)pane("P_B_00"), &over, 8);
set_f(p, mat + 0x18, -80.0f);
memcpy(again, p, n);
changed = action_prompt_lay_out(p, n, 1, why, sizeof why);
CHECK(changed == -1 && !memcmp(p, again, n) && strstr(why, "overlaps"), "a pane on a material: %d, %s", changed,
why);
// Random damage, mostly to the header's tables: a refusal or a layout, never a crash. Each copy is an allocation
// of its own, so that AddressSanitizer, where the runner has it, sees a read past its end.
srand(1);
for (int t = 0; t < 20000; t++) {
uint8_t *q = malloc(n);
memcpy(q, before, n);
for (int k = 0; k < 1 + rand() % 4; k++) {
size_t at = rand() % 2 ? 0x110 + rand() % 0x130 : rand() % n;
q[at] ^= (uint8_t)(1 << rand() % 8);
}
action_prompt_lay_out(q, n, 1, why, sizeof why);
free(q);
}
// The archive, holding the layout four times as the game's does: every copy changes, nothing else; one copy
// that is not the expected layout leaves all of them as they were; without the layout, refused.
static uint8_t sarc[SIZE * 20], sarc2[SIZE * 20];
const uint8_t *four[MAX_COPIES] = {before, before, before, before};
size_t sn = make_sarc(sarc, sizeof sarc, four, 4, n, "DoCommand_00.buil");
memcpy(sarc2, sarc, sn);
changed = action_prompt_archive(sarc2, sn, 1, why, sizeof why);
memcpy(p, before, n);
action_prompt_lay_out(p, n, 1, why, sizeof why);
CHECK(changed == 4 * 52 && !memcmp(sarc2, sarc, member_at[0]), "archive: %d", changed);
for (int c = 0; c < 4; c++) CHECK(!memcmp(sarc2 + member_at[c], p, n), "copy %d not laid out by label", c);
// Two copies laid out already and two not (an archive half laid out by hand): the other two are laid out.
const uint8_t *mixed[MAX_COPIES] = {p, before, p, before};
sn = make_sarc(sarc2, sizeof sarc2, mixed, 4, n, "DoCommand_00.buil");
changed = action_prompt_archive(sarc2, sn, 1, why, sizeof why);
CHECK(changed == 2 * 52 && !memcmp(sarc2 + member_at[1], p, n) && !memcmp(sarc2 + member_at[3], p, n),
"archive half laid out: %d (%s)", changed, why);
memcpy(p, before, n);
set_f(p, pane_at[pane("P_Y_04")] + 0x20, 1.0f);
const uint8_t *one_off[MAX_COPIES] = {before, p, before, before};
sn = make_sarc(sarc2, sizeof sarc2, one_off, 4, n, "DoCommand_00.buil");
memcpy(sarc, sarc2, sn);
CHECK(action_prompt_archive(sarc2, sn, 1, why, sizeof why) == -1 && !memcmp(sarc, sarc2, sn) &&
strstr(why, "P_Y_04"),
"archive with one copy not as expected: %s", why);
sn = make_sarc(sarc2, sizeof sarc2, four, 1, n, "DoCommand_01.buil");
CHECK(action_prompt_archive(sarc2, sn, 1, why, sizeof why) == -1 && strstr(why, "DoCommand_00.buil"),
"archive without the layout: %s", why);
// More copies than it lays out: refused, not the first 16 laid out.
const uint8_t *many[MAX_COPIES];
for (int c = 0; c < MAX_COPIES; c++) many[c] = before;
sn = make_sarc(sarc2, sizeof sarc2, many, MAX_COPIES, n, "DoCommand_00.buil");
memcpy(sarc, sarc2, sn);
CHECK(sn && action_prompt_archive(sarc2, sn, 1, why, sizeof why) == -1 && !memcmp(sarc, sarc2, sn) &&
strstr(why, "more than"),
"archive with %d copies: %s", MAX_COPIES, why);
// Copies that share their bytes are laid out once; copies that share some of them, refused.
sn = make_sarc(sarc2, sizeof sarc2, four, 4, n, "DoCommand_00.buil");
set_range(sarc2, 1, member_at[0], n);
changed = action_prompt_archive(sarc2, sn, 1, why, sizeof why);
CHECK(changed == 3 * 52, "archive with two copies on the same bytes: %d (%s)", changed, why);
sn = make_sarc(sarc2, sizeof sarc2, four, 4, n, "DoCommand_00.buil");
set_range(sarc2, 1, member_at[0] + 0x100, n);
memcpy(sarc, sarc2, sn);
CHECK(action_prompt_archive(sarc2, sn, 1, why, sizeof why) == -1 && !memcmp(sarc, sarc2, sn) &&
strstr(why, "overlap"),
"archive with overlapping copies: %s", why);
// A copy that runs past the archive's end: damaged, refused.
sn = make_sarc(sarc2, sizeof sarc2, four, 4, n, "DoCommand_00.buil");
set_range(sarc2, 2, member_at[2], sn);
memcpy(sarc, sarc2, sn);
CHECK(action_prompt_archive(sarc2, sn, 1, why, sizeof why) == -1 && !memcmp(sarc, sarc2, sn) &&
strstr(why, "damaged"),
"archive with a copy past its end: %s", why);
sn = make_sarc(sarc, sizeof sarc, four, 4, n, "DoCommand_00.buil");
// The zstd file without a dictionary: made once, then nothing to make; a damaged frame refused.
size_t cap = ZSTD_compressBound(sn), zn;
uint8_t *z = malloc(cap);
zn = ZSTD_compress(z, cap, sarc, sn, 3);
uint8_t dic_frame[64];
size_t dic_n = ZSTD_compress(dic_frame, sizeof dic_frame, "SARC", 4, 1);
void *out = NULL;
size_t out_n = 0;
int made = action_prompt_build(z, zn, dic_frame, dic_n, 1, &out, &out_n);
uint8_t *back = malloc(sn);
CHECK(made == 1 && out && ZSTD_decompress(back, sn, out, out_n) == sn, "build: %d (%s)", made, last_log);
memcpy(sarc2, sarc, sn);
action_prompt_archive(sarc2, sn, 1, why, sizeof why);
CHECK(made == 1 && !memcmp(back, sarc2, sn), "the built archive is not the archive laid out by label");
void *out2 = NULL;
CHECK(action_prompt_build(out, out_n, dic_frame, dic_n, 1, &out2, &out_n) == 0 && !out2 &&
strstr(last_log, "already"),
"build on a laid out archive: %s", last_log);
CHECK(action_prompt_build(z, zn / 2, dic_frame, dic_n, 1, &out2, &out_n) == -1 && !out2 &&
strstr(last_log, "Switch places"),
"build on a cut frame: %s", last_log);
free(out);
// The zstd file with a dictionary, as the game's: the pack of dictionaries holds two (ids 1234 and 77) beside a
// member that is not one; the archive's frame names 1234. Made, with that dictionary named again; a frame that
// names one the pack does not hold (4321), refused.
enum { DICT_CAP = 8192 };
static uint8_t dicts[3][DICT_CAP];
static const unsigned ids[3] = {1234, 77, 4321};
for (int i = 0; i < 3; i++)
CHECK(make_dict(dicts[i], DICT_CAP, ids[i], sarc + member_at[0], n), "dictionary %u not made", ids[i]);
static uint8_t pack[SIZE * 4];
const uint8_t *two[MAX_COPIES] = {dicts[0], dicts[1]};
size_t pack_n = make_sarc(pack, sizeof pack, two, 2, DICT_CAP, "zs.zsdic");
uint8_t *pack_z = malloc(ZSTD_compressBound(pack_n));
size_t pack_zn = ZSTD_compress(pack_z, ZSTD_compressBound(pack_n), pack, pack_n, 3);
for (int i = 0; i < 3; i += 2) {
ZSTD_CDict *cd = ZSTD_createCDict(dicts[i], DICT_CAP, 3);
ZSTD_CCtx *cc = ZSTD_createCCtx();
zn = ZSTD_compress_usingCDict(cc, z, cap, sarc, sn, cd);
ZSTD_freeCCtx(cc);
ZSTD_freeCDict(cd);
out = NULL;
made = action_prompt_build(z, zn, pack_z, pack_zn, 1, &out, &out_n);
if (i == 2) {
CHECK(made == -1 && !out && strstr(last_log, "no dictionary 4321"), "a frame of another dictionary: %s",
last_log);
break;
}
CHECK(made == 1 && ZSTD_getDictID_fromFrame(out, out_n) == 1234, "build with a dictionary: %d (%s)", made,
last_log);
ZSTD_DCtx *dc = ZSTD_createDCtx();
size_t got = made == 1 ? ZSTD_decompress_usingDict(dc, back, sn, out, out_n, dicts[0], DICT_CAP) : 0;
ZSTD_freeDCtx(dc);
CHECK(got == sn && !memcmp(back, sarc2, sn), "the archive built with a dictionary is not the one laid out");
free(out);
}
free(pack_z);
free(back);
free(z);
free(again);
free(expect);
if (argc == 6) game_archive(argv);
else if (argc != 1) printf("usage: action_prompt_check [romfs.bin archive-offset size zsdic-offset size]\n"), fails++;
if (fails) printf("%d checks failed\n", fails);
else printf("action prompt: all checks passed\n");
return fails != 0;
}

View File

@@ -0,0 +1,66 @@
"""Build and run tests/action_prompt_check.c: runtime/action_prompt.c, the action prompt's diamond of face buttons laid
out by label, on a layout file and archives the test makes itself. No game files.
With --romfs, also on the game's own archive: the folder holds romfs.bin and its index.json, and the program reads the
archive and Pack/ZsDic.pack.zs from romfs.bin at the offsets the index gives. Nothing is written.
The program is built with the undefined-behaviour sanitizer (traps), with locals that start as a pattern rather than
whatever the stack held, and with AddressSanitizer where the toolchain has it (not MSYS2's).
Usage: action_prompt_check.py [--romfs DIR]
"""
import argparse
import json
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
ARCHIVE = 'UI/Layout2Archive/Common.Product.100.Nin_Ounce_NVN2.blarc.zs'
DICTIONARIES = 'Pack/ZsDic.pack.zs'
def compiler():
if os.environ.get('CC'):
return os.environ['CC']
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
return str(MSYS_BIN / 'clang.exe')
return shutil.which('clang') or 'clang'
def game_args(folder):
"""romfs.bin and the offset and size of the archive and the dictionaries in it."""
files = {f['path']: f for f in json.loads((folder / 'index.json').read_text(encoding='utf-8'))['files']}
for rel in (ARCHIVE, DICTIONARIES):
if rel not in files:
raise SystemExit(f'{folder / "index.json"} has no {rel}')
return [str(folder / 'romfs.bin')] + [str(files[rel][k]) for rel in (ARCHIVE, DICTIONARIES)
for k in ('offset', 'size')]
def main():
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
ap.add_argument('--romfs', type=Path, help='a folder with romfs.bin and index.json: also check the game\'s archive')
args = ap.parse_args()
work = ROOT / 'build' / 'test' / 'action_prompt'
work.mkdir(parents=True, exist_ok=True)
exe = work / ('action_prompt_check.exe' if os.name == 'nt' else 'action_prompt_check')
env = dict(os.environ)
if os.name == 'nt' and MSYS_BIN.exists():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
sanitize = ['-fsanitize=undefined', '-fsanitize-trap=all', '-ftrivial-auto-var-init=pattern']
if os.name != 'nt':
sanitize.append('-fsanitize=address')
rt = ROOT / 'runtime'
subprocess.check_call([compiler(), '-O1', '-g', '-Wall', '-Wextra', '-Werror', *sanitize, '-I', str(rt),
str(ROOT / 'tests' / 'action_prompt_check.c'), str(rt / 'action_prompt.c'),
str(rt / 'sarc.c'), '-o', str(exe), '-lzstd', '-lm'], env=env)
run = [str(exe)] + (game_args(args.romfs) if args.romfs else [])
return subprocess.run(run, env=env, timeout=300).returncode
if __name__ == '__main__':
sys.exit(main())

View File

@@ -1,7 +1,21 @@
/* No game data or physical microphone: exercise conversion, bounded capture and the polling ABI. */
#include "../runtime/audio_capture.c"
#include "../runtime/ov_audio_input.c"
#include <SDL3/SDL.h>
#include <assert.h>
static bool deny_recording;
static int device_open_calls;
static SDL_AudioDeviceID open_test_device(SDL_AudioDeviceID device, const SDL_AudioSpec *spec) {
device_open_calls++;
if (deny_recording) {
SDL_SetError("simulated recording permission denial");
return 0;
}
return SDL_OpenAudioDevice(device, spec);
}
#define SDL_OpenAudioDevice open_test_device
#include "../runtime/audio_capture.c"
#undef SDL_OpenAudioDevice
#include "../runtime/ov_audio_input.c"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
@@ -20,6 +34,10 @@ void *ipc_guest(uint64_t addr, uint64_t size) {
return (void *)(uintptr_t)addr;
}
void rt_log(const char *fmt, ...) { (void)fmt; }
int rt_knob(const char *name, const char *def) {
assert(strcmp(name, "MICROPHONE") == 0);
return atoi(def);
}
static void ring_check(void) {
assert(SDL_InitSubSystem(SDL_INIT_AUDIO));
@@ -59,17 +77,38 @@ static void polling_check(void) {
c.x[5] = 4800;
open_input_locked(&c);
assert(c.x[0] == 0);
MicCapture *m = find(key)->mic;
assert(SDL_GetAudioStreamDevice(m->stream) == 0);
c.x[0] = key;
state_locked(&c);
assert(c.x[0] == 1);
deny_recording = true;
c.x[0] = key;
start_input_locked(&c);
assert(c.x[0] == BAD_INPUT);
assert(SDL_GetAudioStreamDevice(m->stream) == 0);
c.x[0] = key;
state_locked(&c);
assert(c.x[0] == 1);
deny_recording = false;
c.x[0] = key;
start_input_locked(&c);
assert(c.x[0] == 0);
SDL_AudioDeviceID active = SDL_GetAudioStreamDevice(m->stream);
assert(active);
c.x[0] = key;
start_input_locked(&c);
assert(c.x[0] == 0 && SDL_GetAudioStreamDevice(m->stream) == active);
c.x[0] = key;
state_locked(&c);
assert(c.x[0] == 2);
SDL_Delay(350); /* overflow the 100 ms ring, not system memory */
c.x[0] = key;
stop_input_locked(&c);
SDL_AudioSpec spec;
assert(!SDL_GetAudioDeviceFormat(active, &spec, NULL));
assert(SDL_GetAudioStreamDevice(m->stream) == 0);
c.x[0] = key;
filled_locked(&c);
assert(c.x[0] > 0 && c.x[0] <= 4800);
c.x[0] = key;
@@ -78,6 +117,9 @@ static void polling_check(void) {
read_input_locked(&c);
assert(c.x[0] > 0 && c.x[0] <= 4800);
c.x[0] = key;
start_input_locked(&c);
assert(c.x[0] == 0 && SDL_GetAudioStreamDevice(m->stream) != 0);
c.x[0] = key;
stop_input_locked(&c);
c.x[0] = key;
purge_input_locked(&c);
@@ -120,13 +162,34 @@ static void polling_check(void) {
assert(mic_available(NULL) == 0);
}
static void independent_streams_check(void) {
int previous_calls = device_open_calls;
MicCapture *a = mic_open(2, 1, 48000, 4800);
MicCapture *b = mic_open(2, 1, 48000, 4800);
assert(a && b);
assert(device_open_calls == previous_calls);
assert(SDL_GetAudioStreamDevice(a->stream) == 0 && SDL_GetAudioStreamDevice(b->stream) == 0);
assert(mic_start(a));
assert(SDL_GetAudioStreamDevice(b->stream) == 0);
assert(mic_start(b));
assert(device_open_calls == previous_calls + 2);
SDL_AudioDeviceID active = SDL_GetAudioStreamDevice(b->stream);
assert(active && active != SDL_GetAudioStreamDevice(a->stream));
mic_close(a);
assert(SDL_GetAudioStreamDevice(b->stream) == active);
mic_stop(b);
assert(SDL_GetAudioStreamDevice(b->stream) == 0);
mic_close(b);
}
int main(void) {
assert(SDL_SetHint(SDL_HINT_AUDIO_DRIVER, "dummy"));
registry_lock = SDL_CreateMutex();
assert(registry_lock);
ring_check();
polling_check();
independent_streams_check();
SDL_DestroyMutex(registry_lock);
puts("PASS: sample order, bounded overflow, polling frame units, reopen, purge and failed-open regression");
puts("PASS: bounded capture, idle streams, start/stop, failed start and retry, independent streams and purge");
return 0;
}

65
tests/build_gate_check.c Normal file
View File

@@ -0,0 +1,65 @@
// runtime/vk_build_gate.h: the memory gate of the background pipeline builds (runtime/vk_pipeline.c gate_enter). Built
// and run by tests/build_gate_check.py; prints each failed check and exits 1 on any.
#include <stdio.h>
#include "vk_build_gate.h"
static int fails;
#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
int main(void) {
// A machine with 73,154 MB of commit and 4,096 MB kept free; each build is estimated at 64 MB.
const double limit = 73154, keep = 4096, est = 64;
BuildGate g = {.free_mb = keep};
// Plenty of room: every one of 16 builders gets in, and each one's estimate is reserved.
for (int i = 0; i < 16; i++) CHECK(build_gate_enter(&g, 42000, limit, est), "builder %d refused with 31 GB free", i);
CHECK(g.reserved_mb == 16 * est, "reserved %.0f MB, expected %.0f", g.reserved_mb, 16 * est);
for (int i = 0; i < 16; i++) build_gate_leave(&g, est);
CHECK(g.reserved_mb == 0, "reserved %.0f MB after every build ended", g.reserved_mb);
// The case the reservations are for: the commit in use does not move while the builders start (their compiles
// have taken nothing yet), and the room above the kept part holds four estimates. A gate without reservations
// lets all sixteen in; this one lets four in and refuses the rest.
double used = limit - keep - 4 * est;
int in = 0;
for (int i = 0; i < 16; i++) in += build_gate_enter(&g, used, limit, est);
CHECK(in == 4, "%d builders started in room for 4", in);
CHECK(g.reserved_mb == 4 * est, "a refused build reserved something: %.0f MB", g.reserved_mb);
// One ends: exactly one more fits.
build_gate_leave(&g, est);
CHECK(build_gate_enter(&g, used, limit, est), "no build started after one ended");
CHECK(!build_gate_enter(&g, used, limit, est), "a fifth build started");
for (int i = 0; i < 4; i++) build_gate_leave(&g, est);
// The boundary: exactly the kept part plus one estimate free is enough, one MB less is not.
CHECK(build_gate_admits(&g, limit - keep - est, limit, est), "refused with exactly enough room");
CHECK(!build_gate_admits(&g, limit - keep - est + 1, limit, est), "admitted one MB short");
// The commit of the night this was written for: 66,000 of 73,154 MB in use. One ordinary build fits (7,154 MB
// free against 4,160 needed); a large compute build estimated at 4,096 MB does not, and while one of those runs
// with 8,254 MB free (room for it, and then 4,158 MB for the rest), ordinary builds are refused too.
CHECK(build_gate_admits(&g, 66000, limit, est), "an ordinary build refused at 66 of 73 GB");
CHECK(!build_gate_admits(&g, 66000, limit, 4096), "a large compute build admitted at 66 of 73 GB");
CHECK(build_gate_enter(&g, 64900, limit, 4096), "a large compute build refused with 8,254 MB free");
CHECK(!build_gate_admits(&g, 64900, limit, est), "an ordinary build admitted beside a large one with no room for both");
build_gate_leave(&g, 4096);
// Above the limit already (the commit in use can pass a limit that shrank): refused, not a negative room taken
// for a large one.
CHECK(!build_gate_admits(&g, limit + 500, limit, est), "admitted above the limit");
// No check: the setting at 0, or a platform that reports no limit (-1). Reservations are still counted, so the
// numbers a log line prints stay true.
BuildGate off = {.free_mb = 0};
CHECK(build_gate_enter(&off, limit, limit, est), "refused with the gate off");
CHECK(off.reserved_mb == est, "the gate that is off did not count its build");
CHECK(build_gate_admits(&g, 1e9, -1, est), "refused with an unknown limit");
// More leaves than enters never leave a negative reservation behind, which would admit builds for nothing.
build_gate_leave(&g, est);
CHECK(g.reserved_mb == 0, "reserved %.0f MB after a leave too many", g.reserved_mb);
printf("build_gate_check: %s\n", fails ? "FAILED" : "passed");
return fails ? 1 : 0;
}

73
tests/build_gate_check.py Normal file
View File

@@ -0,0 +1,73 @@
"""Build and run tests/build_gate_check.c: runtime/vk_build_gate.h, the memory gate of the background pipeline builds:
what it admits and refuses by the system's commit, and that the builds under way count through their reservations.
With the game executable's path it also runs the executable's --probe-buildgate (runtime/vk_pipeline.c
vk_pipeline_probe_buildgate: the gate as the builders call it, with this machine's commit; no device, no window, no
build) three times: with the settings in effect, with build_free_mb = 0, where every build must be admitted, and
with build_free_mb at a value no machine has free, where every build must be refused.
Usage: build_gate_check.py [PATH_TO_GAME_EXE]
"""
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tests' / 'diff'))
try:
from common import MSYS_BIN, compiler # noqa: E402
except ImportError: # a Python without the differential tests' packages: the same compiler choice without them
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
def compiler():
if os.environ.get('CC'):
return os.environ['CC']
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
return str(MSYS_BIN / 'clang.exe')
return shutil.which('clang') or 'clang'
def main():
work = ROOT / 'build' / 'test' / 'build_gate'
work.mkdir(parents=True, exist_ok=True)
exe = work / ('build_gate_check.exe' if os.name == 'nt' else 'build_gate_check')
env = dict(os.environ)
if os.name == 'nt' and MSYS_BIN.exists():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
subprocess.check_call([compiler(), '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'),
str(ROOT / 'tests' / 'build_gate_check.c'), '-o', str(exe)], env=env)
rc = subprocess.run([str(exe)], env=env).returncode
if len(sys.argv) > 1:
rc |= probe(sys.argv[1])
return rc
def probe(game):
"""The executable's --probe-buildgate under three settings; 0 when each run passes and says what it must."""
fails = 0
for free_mb, must in ((None, None), ('0', 'an ordinary build is admitted now'),
('1048576', 'an ordinary build is refused now')):
env = dict(os.environ)
env.pop('NX2NAVI_BUILD_FREE_MB', None)
if free_mb is not None:
env['NX2NAVI_BUILD_FREE_MB'] = free_mb
r = subprocess.run([game, '--probe-buildgate'], env=env, capture_output=True, text=True, errors='replace')
lines = [ln for ln in r.stdout.splitlines() if ln.startswith('probe-buildgate:')]
print('\n'.join(lines))
ok = r.returncode == 0 and lines and lines[-1].endswith('passed') and (must is None or must in lines[-1])
if free_mb == '0':
ok = ok and any('16 of 16 ordinary builds admitted' in ln and 'admitted' in ln.split(';')[-1] for ln in lines)
if free_mb == '1048576':
ok = ok and any('0 of 16 ordinary builds admitted' in ln and 'refused' in ln.split(';')[-1] for ln in lines)
if not ok:
fails += 1
print('build_gate_check: FAIL: --probe-buildgate with build_free_mb %s (exit code %d)'
% (free_mb or 'as set', r.returncode))
print('build_gate_check: the executable\'s gate %s' % ('FAILED' if fails else 'passed'))
return 1 if fails else 0
if __name__ == '__main__':
sys.exit(main())

844
tests/coop_page_check.cpp Normal file
View File

@@ -0,0 +1,844 @@
// The menu's Co-op tab (runtime/ui/coop_page.cpp) without the game: the page's own code, the menu's rows and shapes, the
// real room-secret parser and rules (coop_proto.c, coop_state.c) and Dear ImGui, driven frame by frame with the key
// events the menu hands ImGui (menu.cpp drain_events and pad_events: a key's down and up, a held key's repeats as more
// downs, a typed letter as its key and its character), the co-op layer and the settings answered here. Checks typing a
// name, a relay and a secret with the letters that switch tabs elsewhere, Make, Join with a wrong secret and a pasted
// one, the offer accepted and declined, a held or repeated confirm never accepting, removing, resetting or leaving, the
// maker's rules and the others' read-only ones, the maker's key to compare, a player not identified (Accept greyed,
// Remove by member id only while the same session holds it), Remove by key, the pools' contributors and their reset,
// the three warnings before the first Accept, the relay's states; with the keyboard and with the pad.
// tests/coop_page_check.py builds and runs it; the exit status is the number of failed checks. COOP_PAGE_FAULT=1 puts
// the cursor on each window's acting button as the window opens (Right, as a player would), a fault the checks must
// catch.
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "../runtime/ui/coop_page.cpp" // its statics (the window's step, the field) are checked below
#include "imgui_internal.h"
#include "ui/menu_art.h"
// ---- the settings: the co-op rows of cfg.c's table, in memory
static CfgMeta kMetas[] = {
{"COOP", "0", "0|1", 1},
{"COOP_NAME", "", NULL, 1},
{"COOP_SERVER", "", NULL, 1},
{"COOP_PROGRESS", "shared", "shared|own", 1},
{"COOP_ITEMS", "shared", "shared|own", 1},
{"COOP_SONGS", "shared", "shared|own", 1},
{"COOP_QUEST", "shared", "shared|own", 1},
{"COOP_KEYS", "pool", "pool|own", 1},
{"COOP_RUPEES", "own", "own|pool", 1},
{"COOP_AMMO", "own", "own|pool", 1},
};
enum { N_METAS = sizeof kMetas / sizeof kMetas[0] };
static char g_set[N_METAS][160];
static bool g_is_set[N_METAS];
static unsigned g_cfg_version = 1;
static int meta_index(const char *key) {
if (!strncmp(key, "NX2NAVI_", 8)) key += 8;
for (int i = 0; i < N_METAS; i++)
if (!strcmp(kMetas[i].key, key)) return i;
return -1;
}
static void settings_clear(void) {
memset(g_is_set, 0, sizeof g_is_set);
g_cfg_version++;
}
// ---- the co-op layer, as the test sets it
static CoopView g_fake;
static CoopOffer g_fake_offer;
static CoopContributor g_fake_contrib[8];
static int g_fake_ncontrib;
static bool g_fake_maker, g_fault;
static int n_create, n_join, n_leave, n_remove, last_remove, n_accept, n_remove_key, n_reset;
static uint32_t last_accept;
static char last_join[64], g_clipboard[256], g_copied_text[64], last_remove_key[24];
static bool g_paste_asked;
extern "C" {
const CfgMeta *cfg_meta(const char *key) {
int i = meta_index(key);
return i >= 0 ? &kMetas[i] : NULL;
}
const CfgMeta *cfg_meta_at(int i) { return i >= 0 && i < N_METAS ? &kMetas[i] : NULL; }
const char *cfg_get(const char *key) {
int i = meta_index(key);
return i >= 0 && g_is_set[i] ? g_set[i] : NULL;
}
const char *cfg_value(const char *key) {
int i = meta_index(key);
if (i < 0) return "";
return g_is_set[i] && g_set[i][0] ? g_set[i] : kMetas[i].def;
}
const char *cfg_default(const char *key) {
int i = meta_index(key);
return i >= 0 ? kMetas[i].def : NULL;
}
int cfg_int(const char *key) { return atoi(cfg_value(key)); }
double cfg_double(const char *key) { return atof(cfg_value(key)); }
int cfg_bool(const char *key) { return strcmp(cfg_value(key), "0") != 0 && cfg_value(key)[0]; }
int cfg_from_env(const char *) { return 0; }
int cfg_pending(const char *) { return 0; } // the rows' "after restart" note: no setting here waits for a restart
void cfg_set(const char *key, const char *value) {
int i = meta_index(key);
if (i < 0) return;
snprintf(g_set[i], sizeof g_set[i], "%s", value);
g_is_set[i] = true;
g_cfg_version++;
}
int cfg_save(void) { return 1; }
int cfg_reset(const char *const *keys, int n) {
for (int k = 0; k < n; k++)
if (int i = meta_index(keys[k]); i >= 0) g_is_set[i] = false;
g_cfg_version++;
return n;
}
unsigned cfg_version(void) { return g_cfg_version; }
void menu_sound_play(MenuSound) {}
const char *ov_language(void) { return "en-US"; }
const char *plat_ui_language(void) { return "en"; }
uint64_t plat_now_ns(void) {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec;
}
void rt_log(const char *, ...) {}
int menu_art(MenuArt, ImTextureID *, float *, float *) { return 0; }
void plat_mutex_lock(PlatMutex *) {}
void plat_mutex_unlock(PlatMutex *) {}
int menu_is_open(void) { return 1; }
void coop_view_get(CoopView *out) { *out = g_fake; }
int coop_command(CoopCmd cmd, const char *secret) {
if (g_fake.state == COOP_OFF) return 0; // as the layer: nothing while co-op is off
if (cmd == COOP_CMD_CREATE) n_create++;
if (cmd == COOP_CMD_JOIN) n_join++, snprintf(last_join, sizeof last_join, "%s", secret ? secret : "");
if (cmd == COOP_CMD_LEAVE) n_leave++;
return 1;
}
int coop_remove_member(int member) { return n_remove++, last_remove = member, 1; }
int coop_game_offer(CoopOffer *out) {
*out = g_fake_offer;
return out->token != 0;
}
// As the layer: refused while a member in the room has not said who they are.
int coop_game_accept(uint32_t token) {
if (!token || token != g_fake_offer.token || g_fake_offer.unidentified) return 0;
return n_accept++, last_accept = token, 1;
}
int coop_game_remove_key(const char *fingerprint) {
if (!g_fake_maker) return 0;
return n_remove_key++, snprintf(last_remove_key, sizeof last_remove_key, "%s", fingerprint), 1;
}
int coop_game_reset_pools(void) { return g_fake_maker ? (n_reset++, 1) : 0; }
int coop_game_contributors(CoopContributor *out, int max) {
int n = g_fake_ncontrib < max ? g_fake_ncontrib : max;
memcpy(out, g_fake_contrib, sizeof *out * (size_t)n);
return n;
}
int coop_game_is_maker(void) { return g_fake_maker; }
int plat_clipboard_set(const char *utf8) { return snprintf(g_copied_text, sizeof g_copied_text, "%s", utf8), 1; }
void gfx_window_paste(void) { g_paste_asked = true; }
int menu_paste_take(char *out, size_t cap) {
if (!g_paste_asked) return 0;
g_paste_asked = false;
snprintf(out, cap, "%s", g_clipboard);
return 1;
}
}
// ---- frames and keys
static int failures, n_tab_keys;
#define CHECK(c, ...) \
do { \
if (!(c)) { \
failures++; \
printf("FAIL %s:%d: ", __FILE__, __LINE__); \
printf(__VA_ARGS__); \
printf("\n"); \
} \
} while (0)
static ImGuiTextBuffer g_text; // the text the last frame drew with ImGui's text calls (lines, windows)
static int g_fault_release; // frames until the fault's Right comes up again; 0 none down
static bool g_fault_due; // a window opened: Right goes down once no item is held active (ImGui moves no cursor then)
static void frame(void) {
ImGuiIO &io = ImGui::GetIO();
io.DisplaySize = ImVec2(1280, 720);
io.DeltaTime = 1 / 60.0f;
ImGui::NewFrame();
ImGui::SetNextWindowPos(ImVec2(40, 40), ImGuiCond_Always);
ImGui::SetNextWindowSize(ImVec2(1200, 640), ImGuiCond_Always);
ImGui::Begin("##menu", nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings);
ImGui::LogToBuffer();
opt_begin("##coop", 1); // as menu.cpp tab_coop
coop_rows(1);
opt_end();
const bool fresh = g_page_new;
coop_after_page(1);
g_text.clear();
g_text.append(GImGui->LogBuffer.c_str());
ImGui::LogFinish();
// menu.cpp layout_menu's tab keys, which the page's window and a text field keep for themselves
const bool keeps_keys = coop_modal() || io.WantTextInput;
if (!keeps_keys && (ImGui::IsKeyPressed(ImGuiKey_E, false) || ImGui::IsKeyPressed(ImGuiKey_Q, false))) n_tab_keys++;
ImGui::End();
ImGui::Render();
// the deliberate fault: a window that acts on the room opens with the cursor moved onto its acting button
if (g_fault_release && !--g_fault_release) io.AddKeyEvent(ImGuiKey_RightArrow, false);
if (g_fault && fresh && g_open && !field_page()) g_fault_due = true;
if (g_fault_due && !g_open) g_fault_due = false;
// ImGui moves no cursor while the key that opened the window is held or in the window's first frames
const bool held = ImGui::IsKeyDown(ImGuiKey_Enter) || ImGui::IsKeyDown(ImGuiKey_GamepadFaceDown) || GImGui->ActiveId;
if (g_fault_due && !fresh && !held && !g_fault_release)
io.AddKeyEvent(ImGuiKey_RightArrow, true), g_fault_release = 2, g_fault_due = false;
}
static void frames(int n) {
for (int i = 0; i < n; i++) frame();
}
static void key(ImGuiKey k, bool down) { ImGui::GetIO().AddKeyEvent(k, down); }
// A press and its release, a few frames apart as a player's are.
static void tap(ImGuiKey k) {
key(k, true);
frames(3);
key(k, false);
frames(3);
}
// Held for n frames with the system's repeats (another down every frame), then released.
static void hold(ImGuiKey k, int n) {
for (int i = 0; i < n; i++) {
key(k, true);
frame();
}
key(k, false);
frames(3);
}
// Typed on a keyboard: a letter's key goes down, its character arrives, the key comes up (SDL's order).
static void type(const char *s) {
for (; *s; s++) {
char c = *s;
ImGuiKey k = c >= 'a' && c <= 'z' ? (ImGuiKey)(ImGuiKey_A + (c - 'a'))
: c >= 'A' && c <= 'Z' ? (ImGuiKey)(ImGuiKey_A + (c - 'A'))
: ImGuiKey_None;
if (k != ImGuiKey_None) key(k, true);
ImGui::GetIO().AddInputCharacter((unsigned)c);
frame();
if (k != ImGuiKey_None) key(k, false);
frame();
}
}
struct Input {
const char *name;
ImGuiKey act, back, up, down, right, left;
};
static const Input kKeyboard = {"keyboard", ImGuiKey_Enter, ImGuiKey_Escape, ImGuiKey_UpArrow, ImGuiKey_DownArrow,
ImGuiKey_RightArrow, ImGuiKey_LeftArrow};
static const Input kPad = {"gamepad", ImGuiKey_GamepadFaceDown, ImGuiKey_GamepadFaceRight, ImGuiKey_GamepadDpadUp,
ImGuiKey_GamepadDpadDown, ImGuiKey_GamepadDpadRight, ImGuiKey_GamepadDpadLeft};
static ImGuiID window_id(const char *label) {
ImGuiWindow *w = ImGui::FindWindowByName("##coopwin");
return w ? w->GetID(label) : 0;
}
static const char *nav_name(void) {
static char out[64];
static const char *const labels[] = {"Not now##no", "Accept##yes", "Cancel##no", "Remove##yes", "Reset the pools##yes",
"Stay##no", "Leave the room##yes", "OK##yes", "Join a room##yes", "Paste",
"##coop_field"};
for (const char *l : labels)
if (GImGui->NavId && GImGui->NavId == window_id(l)) return l;
snprintf(out, sizeof out, "%08x", GImGui->NavId);
return out;
}
static bool shows(UiTextId id) { return strstr(g_text.c_str(), menu_text(id)) != nullptr; }
// The cursor on a row of the page, as a move of the D-pad would put it there; then act on it.
static void row(const char *key_of_row) {
opt_focus_key(key_of_row);
frames(3);
}
static void press_row(const Input &in, const char *key_of_row) {
row(key_of_row);
tap(in.act);
}
static void close_window(void) {
coop_cancel();
frames(4);
}
static void counters_clear(void) {
n_create = n_join = n_leave = n_remove = n_accept = n_remove_key = n_reset = 0;
last_remove = 0, last_accept = 0, last_join[0] = last_remove_key[0] = 0, n_tab_keys = 0;
}
// ---- the views the layer hands out
static void member(CoopView &v, int id, const char *name, bool maker, uint32_t heard_ms) {
CoopPeer &m = v.member[v.n++];
memset(&m, 0, sizeof m);
m.id = (uint8_t)id, m.maker = maker, m.heard_ms = heard_ms, m.session = 0x5e550000u + (uint64_t)id;
snprintf(m.name, sizeof m.name, "%s", name);
}
static void out_of_room(CoopState st, const char *status) {
memset(&g_fake, 0, sizeof g_fake);
memset(&g_fake_offer, 0, sizeof g_fake_offer);
g_fake_ncontrib = 0, g_fake_maker = false;
g_fake.state = st;
g_fake.version++;
snprintf(g_fake.status, sizeof g_fake.status, "%s", status);
}
static const char kAliceKey[] = "a11c-e000-0000-0001", kBobKey[] = "b0b0-0000-0000-0002", kDaveKey[] = "da7e-0000-0000-0004";
// An entry of the offer: the other member id, by the name and key its game signed (heard), or not identified.
static void offer_member_add(int id, const char *name, const char *key, bool maker, bool heard) {
CoopOfferMember &m = g_fake_offer.member[g_fake_offer.n++];
memset(&m, 0, sizeof m);
m.id = id, m.maker = maker, m.heard = heard;
snprintf(m.name, sizeof m.name, "%s", name); // the relay's name until heard (coop_game.c offer_of)
snprintf(m.key, sizeof m.key, "%s", heard ? key : "");
}
// Bob in Alice's room (Alice made it), or Alice in her own room with Bob; the offer waiting or not.
static void in_room_as(bool maker, uint32_t token, bool accepted) {
memset(&g_fake, 0, sizeof g_fake);
g_fake.state = COOP_JOINED;
snprintf(g_fake.code, sizeof g_fake.code, "K7M3PR-BCDFGH-234567");
member(g_fake, 1, "Alice", true, maker ? 0 : 1200);
member(g_fake, 2, "Bob", false, maker ? 2300 : 0);
g_fake.self = maker ? 1 : 2, g_fake.maker = 1, g_fake.made = maker;
snprintf(g_fake.name, sizeof g_fake.name, "%s", maker ? "Alice" : "Bob");
g_fake.version++;
g_fake_maker = maker, g_fake_ncontrib = 0;
memset(&g_fake_offer, 0, sizeof g_fake_offer);
g_fake_offer.token = token, g_fake_offer.accepted = accepted, g_fake_offer.copy = accepted;
if (maker) offer_member_add(2, "Bob", kBobKey, false, true);
else offer_member_add(1, "Alice", kAliceKey, true, true);
snprintf(g_fake_offer.maker_key, sizeof g_fake_offer.maker_key, "%s", kAliceKey);
coop_rules_default(&g_fake_offer.rules);
g_fake_offer.rules.epoch = 1, g_fake_offer.rules.maker = 1;
}
// Carol in the room, who has not said who she is: her game never identified itself.
static void carol_unidentified(void) {
member(g_fake, 3, "Carol", false, UINT32_MAX);
g_fake.version++;
offer_member_add(3, "Carol", "", false, false);
g_fake_offer.unidentified = 1;
}
static bool shows_text(const char *s) { return strstr(g_text.c_str(), s) != nullptr; }
// ---- the checks
static void check_fields_by_keyboard(void) {
printf("keyboard text:\n");
settings_clear();
out_of_room(COOP_OFF, "");
counters_clear();
frames(4);
// with no relay, Make and Join are greyed: a press makes nothing
press_row(kKeyboard, "##coop_make");
press_row(kKeyboard, "##coop_join");
CHECK(n_create == 0 && !g_open, "Make and Join with no relay: %d rooms, window %d", n_create, g_open);
// the name: the field takes the cursor, Q and E are letters (no tab switch), Enter keeps it
press_row(kKeyboard, "NX2NAVI_COOP_NAME");
CHECK(g_open && g_page == P_NAME && GImGui->ActiveId == window_id("##coop_field"), "Name: window %d step %d, field %s",
g_open, g_page, GImGui->ActiveId == window_id("##coop_field") ? "active" : "not active");
type("Zelda Qe");
CHECK(n_tab_keys == 0 && coop_modal(), "typing Q and E in the name switched tabs %d times", n_tab_keys);
tap(ImGuiKey_Enter);
CHECK(!g_open && !strcmp(cfg_value("NX2NAVI_COOP_NAME"), "Zelda Qe"), "Name: '%s' kept, window %d",
cfg_value("NX2NAVI_COOP_NAME"), g_open);
printf(" name typed with Q and E: '%s', %d tab switches\n", cfg_value("NX2NAVI_COOP_NAME"), n_tab_keys);
// the same letters with no field: the tab keys work (the gate above is not always shut)
tap(ImGuiKey_E);
CHECK(n_tab_keys == 1, "E on the page itself: %d tab switches", n_tab_keys);
n_tab_keys = 0;
// Esc in a field drops the edit and closes the window
press_row(kKeyboard, "NX2NAVI_COOP_NAME");
type("x");
tap(ImGuiKey_Escape);
CHECK(!g_open && !strcmp(cfg_value("NX2NAVI_COOP_NAME"), "Zelda Qe"), "Esc in the name: '%s', window %d",
cfg_value("NX2NAVI_COOP_NAME"), g_open);
// the relay
press_row(kKeyboard, "NX2NAVI_COOP_SERVER");
type("127.0.0.1:24680");
tap(ImGuiKey_Enter);
CHECK(!strcmp(cfg_value("NX2NAVI_COOP_SERVER"), "127.0.0.1:24680"), "Relay: '%s'", cfg_value("NX2NAVI_COOP_SERVER"));
printf(" relay typed: '%s'\n", cfg_value("NX2NAVI_COOP_SERVER"));
// Make while co-op is off: the switch goes on and the room is made once the coop thread is up
press_row(kKeyboard, "##coop_make");
CHECK(cfg_bool("NX2NAVI_COOP") && n_create == 0, "Make with co-op off: switch %d, %d rooms before it is up",
cfg_bool("NX2NAVI_COOP"), n_create);
out_of_room(COOP_LEFT, "");
frames(2);
CHECK(n_create == 1, "Make once co-op is up: %d rooms", n_create);
printf(" Make with co-op off: switch on, then %d room made\n", n_create);
// Join with a wrong secret: refused in the window, nothing sent; then one in lower case with dashes
press_row(kKeyboard, "##coop_join");
CHECK(g_open && g_page == P_JOIN && GImGui->ActiveId == window_id("##coop_field"), "Join: the field is not active");
type("k7m3pq-bcdfgh-23456e"); // E and Q are not in the alphabet of secrets
tap(ImGuiKey_Enter);
frames(2);
CHECK(n_join == 0 && g_open && g_field_bad && shows(UI_MENU_COOP_SECRET_BAD) && n_tab_keys == 0,
"a wrong secret: %d joins, window %d, said %d, %d tab switches", n_join, g_open, shows(UI_MENU_COOP_SECRET_BAD),
n_tab_keys);
printf(" wrong secret: %d joins, the window says it is not one\n", n_join);
ImGui::ClearActiveID();
frames(2);
tap(ImGuiKey_Escape);
press_row(kKeyboard, "##coop_join");
type("k7m3pr-bcdfgh-234567");
tap(ImGuiKey_Enter);
CHECK(n_join == 1 && !strcmp(last_join, "K7M3PRBCDFGH234567") && !g_open, "the secret in lower case: %d joins of '%s'",
n_join, last_join);
printf(" secret in lower case with dashes: %d join of %s\n", n_join, last_join);
// the relay knows no such room: the page says the secret is wrong
out_of_room(COOP_LEFT, "no such room");
frames(3);
CHECK(shows(UI_MENU_COOP_NO_ROOM), "no such room: the page does not say the secret may be wrong");
printf(" the relay has no such room: the page says so\n");
}
static void check_join_by_pad(void) {
printf("gamepad join:\n");
out_of_room(COOP_LEFT, "");
counters_clear();
frames(3);
// the window opens with the cursor on Cancel: A, A goes back
press_row(kPad, "##coop_join");
CHECK(g_open && g_page == P_JOIN && !strcmp(nav_name(), "Cancel##no"), "Join by pad: cursor on %s", nav_name());
tap(kPad.act);
CHECK(!g_open && n_join == 0, "Join, A, A: window %d, %d joins", g_open, n_join);
// Paste: the clipboard's secret with a line end and spaces, then Join
snprintf(g_clipboard, sizeof g_clipboard, " k7m3pr-bcdfgh-234567\n");
press_row(kPad, "##coop_join");
tap(kPad.up);
CHECK(!strcmp(nav_name(), "Paste"), "up from Cancel: %s", nav_name());
tap(kPad.act);
frames(2);
CHECK(!strcmp(g_field, "K7M3PR-BCDFGH-234567"), "pasted: '%s'", g_field);
tap(kPad.down);
tap(kPad.right);
CHECK(!strcmp(nav_name(), "Join a room##yes"), "down, right: %s", nav_name());
tap(kPad.act);
CHECK(n_join == 1 && !strcmp(last_join, "K7M3PRBCDFGH234567"), "pasted secret: %d joins of '%s'", n_join, last_join);
// an empty clipboard says so
snprintf(g_clipboard, sizeof g_clipboard, "\n");
press_row(kPad, "##coop_join");
tap(kPad.up);
tap(kPad.act);
frames(2);
CHECK(g_paste_empty && shows(UI_MENU_COOP_PASTE_EMPTY), "an empty clipboard is not said");
close_window();
printf(" pasted secret joined by pad: %d join; an empty clipboard is said\n", n_join);
}
static void check_offer(const Input &in) {
printf("%s offer:\n", in.name);
in_room_as(false, 0x1234567u, false);
counters_clear();
g_accepted_once = false; // as at a start of the game: the warnings come before the first Accept
frames(4);
CHECK(shows(UI_MENU_COOP_OFFER_TRUST) == false && strstr(g_text.c_str(), "Alice (made the room)"),
"the page does not name the room's members");
CHECK(shows_text(kAliceKey) && shows(UI_MENU_COOP_OFFER_COMPARE), "the page does not show the maker's key to compare");
CHECK(!shows(UI_MENU_COOP_POOLS) && !shows(UI_MENU_COOP_RESET), "a player who is not the maker sees the pools' remedies");
// the row's press opens the window with the cursor on Not now; the same button again goes back
press_row(in, "##coop_answer");
CHECK(g_open && g_page == P_ACCEPT && !strcmp(nav_name(), "Not now##no"), "Accept window: cursor on %s", nav_name());
CHECK(shows(UI_MENU_COOP_OFFER_COPY) && shows(UI_MENU_COOP_OFFER_TRUST), "the window does not say what accepting does");
CHECK(shows(UI_MENU_COOP_WARN_SAVE) && shows(UI_MENU_COOP_WARN_ROOM) && shows(UI_MENU_COOP_WARN_KEY) && shows_text(kAliceKey),
"the first Accept window lacks the three warnings or the maker's key");
tap(in.act);
CHECK(n_accept == 0 && !g_open, "Accept or not, act, act: %d accepts, window %d", n_accept, g_open);
// held on the row through the window's opening, then held again in it
row("##coop_answer");
hold(in.act, 120);
CHECK(n_accept == 0, "act held 2 s on the row: %d accepts", n_accept);
if (!g_open) press_row(in, "##coop_answer");
hold(in.act, 120);
CHECK(n_accept == 0, "act held again in the window: %d accepts", n_accept);
// declined: Not now, and B
close_window();
press_row(in, "##coop_answer");
tap(in.back);
CHECK(n_accept == 0 && !g_open, "back: %d accepts", n_accept);
// a new player while the cursor is on Accept: the window shows the new offer and the cursor goes back to Not now
press_row(in, "##coop_answer");
tap(in.right);
CHECK(!strcmp(nav_name(), "Accept##yes"), "right from Not now: %s", nav_name());
g_fake_offer.token = 0x7654321u, g_fake_offer.n = 2;
snprintf(g_fake_offer.member[1].name, sizeof g_fake_offer.member[1].name, "Carol");
frames(3);
CHECK(!strcmp(nav_name(), "Not now##no") && strstr(g_text.c_str(), "Carol"), "a new offer under the cursor: %s",
nav_name());
tap(in.act);
CHECK(n_accept == 0, "a new offer, act: %d accepts", n_accept);
printf(" act, act / held / back / a new offer: %d accepts\n", n_accept);
// accepted on purpose: right, act
press_row(in, "##coop_answer");
tap(in.right);
tap(in.act);
CHECK(n_accept == 1 && last_accept == 0x7654321u && !g_open, "Accept chosen: %d accepts of %08x", n_accept, last_accept);
printf(" right, act: %d accept of the offer shown\n", n_accept);
g_fake_offer.accepted = 1, g_fake_offer.copy = 1;
frames(3);
CHECK(shows(UI_MENU_COOP_ACCEPTED), "accepted: the page does not say so");
// a later offer (another player): the warnings were read before the first Accept, the key is still there
g_fake_offer.token = 0x7654322u, g_fake_offer.accepted = 0, g_fake_offer.copy = 0;
press_row(in, "##coop_answer");
CHECK(g_open && !shows(UI_MENU_COOP_WARN_SAVE) && shows_text(kAliceKey), "a later Accept window: warnings %d, key %d",
shows(UI_MENU_COOP_WARN_SAVE), shows_text(kAliceKey));
close_window();
printf(" the maker's key shown to compare; the three warnings before the first Accept only\n");
g_fake_offer.accepted = 1, g_fake_offer.copy = 1;
frames(3);
// the rules of someone else's room: read only
const char *before = cfg_value("NX2NAVI_COOP_RUPEES");
press_row(in, "##coop_room_rule5");
row("##coop_room_rule5");
tap(in.right);
CHECK(!strcmp(cfg_value("NX2NAVI_COOP_RUPEES"), before), "a member changed the room's rule: rupees %s",
cfg_value("NX2NAVI_COOP_RUPEES"));
printf(" a member's press and right on a rule: rupees still %s\n", cfg_value("NX2NAVI_COOP_RUPEES"));
}
static void check_maker(const Input &in) {
printf("%s maker:\n", in.name);
settings_clear();
in_room_as(true, 0x2222223u, true);
counters_clear();
frames(4);
// a rule the maker changes: rupees into a pool; items cannot leave a shared world (greyed)
row("NX2NAVI_COOP_RUPEES");
tap(in.right);
CHECK(!strcmp(cfg_value("NX2NAVI_COOP_RUPEES"), "pool"), "the maker's rupees: %s", cfg_value("NX2NAVI_COOP_RUPEES"));
row("NX2NAVI_COOP_ITEMS");
tap(in.right);
tap(in.act);
CHECK(!strcmp(cfg_value("NX2NAVI_COOP_ITEMS"), "shared"), "items with progress shared: %s", cfg_value("NX2NAVI_COOP_ITEMS"));
printf(" rupees -> %s; items stay %s while progress is shared\n", cfg_value("NX2NAVI_COOP_RUPEES"),
cfg_value("NX2NAVI_COOP_ITEMS"));
// Remove: the cursor on Cancel; act again, held, back: nothing; right, act: removed by its key
CHECK(shows_text(kAliceKey), "the maker's page does not show its own key to read out");
press_row(in, "##coop_member2");
CHECK(g_open && g_page == P_REMOVE && !strcmp(nav_name(), "Cancel##no") && shows_text(kBobKey),
"Remove window: cursor on %s, key shown %d", nav_name(), shows_text(kBobKey));
tap(in.act);
CHECK(n_remove + n_remove_key == 0 && !g_open, "Remove, act, act: %d removals", n_remove + n_remove_key);
row("##coop_member2");
hold(in.act, 120);
if (!g_open) press_row(in, "##coop_member2");
hold(in.act, 120);
CHECK(n_remove + n_remove_key == 0, "act held on a player and in the window: %d removals", n_remove + n_remove_key);
close_window();
press_row(in, "##coop_member1"); // the maker's own row opens nothing
CHECK(!g_open, "the maker's own row opened a window");
press_row(in, "##coop_member2");
tap(in.right);
tap(in.act);
CHECK(n_remove == 0 && n_remove_key == 1 && !strcmp(last_remove_key, kBobKey), "Remove chosen: %d by id, %d by key (%s)",
n_remove, n_remove_key, last_remove_key);
printf(" Remove: act, act / held: none; right, act: %d removal of key %s\n", n_remove_key, last_remove_key);
// a player who never said who they are: by member id, and only while the same session holds it
carol_unidentified();
frames(3);
CHECK(shows_text("Players in the room who are not identified"), "the maker's page does not say Carol is not identified");
press_row(in, "##coop_member3");
CHECK(g_open && g_page == P_REMOVE && !strcmp(nav_name(), "Cancel##no") && shows(UI_MENU_COOP_NOT_IDENTIFIED),
"Remove Carol: cursor on %s, not identified said %d", nav_name(), shows(UI_MENU_COOP_NOT_IDENTIFIED));
tap(in.right);
g_fake.member[2].session++, g_fake.version++; // Carol left and someone else got her id meanwhile
frames(3);
CHECK(n_remove == 0 && !g_open, "another session under the window: %d removals by id, window %d", n_remove, g_open);
press_row(in, "##coop_member3");
tap(in.right);
tap(in.act);
CHECK(n_remove == 1 && last_remove == 3 && n_remove_key == 1, "Remove of a player not identified: %d by id (%d)", n_remove,
last_remove);
printf(" a player not identified: removed by id only while the same session holds it (%d removal)\n", n_remove);
// the pools: Dave, who left, took 3 keys beyond what he brought; Bob none; the maker's own row is left out
g_fake_ncontrib = 3;
memset(g_fake_contrib, 0, sizeof g_fake_contrib);
snprintf(g_fake_contrib[0].key, sizeof g_fake_contrib[0].key, "%s", kAliceKey), g_fake_contrib[0].self = 1;
g_fake_contrib[0].spent[0] = 9;
snprintf(g_fake_contrib[1].key, sizeof g_fake_contrib[1].key, "%s", kBobKey), g_fake_contrib[1].member = 2;
snprintf(g_fake_contrib[1].name, sizeof g_fake_contrib[1].name, "Bob");
snprintf(g_fake_contrib[2].key, sizeof g_fake_contrib[2].key, "%s", kDaveKey), g_fake_contrib[2].spent[0] = 3;
counters_clear();
frames(3);
char spent_row[48];
snprintf(spent_row, sizeof spent_row, "##coop_spent%s", kDaveKey);
CHECK(shows(UI_MENU_COOP_POOLS) && !shows(UI_MENU_COOP_SPENT_NONE), "the maker's pools list nobody who took more");
press_row(in, spent_row);
CHECK(g_open && g_page == P_REMOVE && !strcmp(nav_name(), "Cancel##no") && shows_text(kDaveKey) &&
shows(UI_MENU_COOP_NOT_IN_ROOM),
"Remove Dave's key: cursor on %s, key %d", nav_name(), shows_text(kDaveKey));
tap(in.act);
row(spent_row);
hold(in.act, 120);
if (!g_open) press_row(in, spent_row);
hold(in.act, 120);
CHECK(n_remove_key == 0, "act, act / held on Dave's key: %d removals", n_remove_key);
close_window();
press_row(in, spent_row);
tap(in.right);
tap(in.act);
CHECK(n_remove_key == 1 && !strcmp(last_remove_key, kDaveKey), "Dave's key chosen: %d removals of %s", n_remove_key,
last_remove_key);
// Reset the pools: the cursor on Cancel; act again, held: nothing; right, act: reset
press_row(in, "##coop_reset");
CHECK(g_open && g_page == P_RESET && !strcmp(nav_name(), "Cancel##no"), "Reset window: cursor on %s", nav_name());
tap(in.act);
row("##coop_reset");
hold(in.act, 120);
if (!g_open) press_row(in, "##coop_reset");
hold(in.act, 120);
CHECK(n_reset == 0, "act, act / held on Reset the pools: %d resets", n_reset);
close_window();
press_row(in, "##coop_reset");
tap(in.right);
tap(in.act);
CHECK(n_reset == 1 && !g_open, "Reset chosen: %d resets", n_reset);
printf(" pools: Dave's key removed (%d), act, act / held: nothing; Reset: %d\n", n_remove_key, n_reset);
g_fake_ncontrib = 0;
// the secret: hidden until asked (its row's press shows it, a second hides it), copied on its row
CHECK(!g_show_secret, "the secret shows unasked");
press_row(in, "##coop_secret");
CHECK(g_show_secret, "the secret's row did not show it");
press_row(in, "##coop_secret");
CHECK(!g_show_secret, "the secret's row did not hide it again");
press_row(in, "##coop_copy");
CHECK(!strcmp(g_copied_text, "K7M3PR-BCDFGH-234567"), "copied: '%s'", g_copied_text);
// Leave: the cursor on Stay, the maker told what leaving costs; act again, held: stays; right, act: leaves
press_row(in, "##coop_leave");
CHECK(g_open && g_page == P_LEAVE && !strcmp(nav_name(), "Stay##no") && shows(UI_MENU_COOP_LEAVE_MAKER),
"Leave window: cursor on %s", nav_name());
tap(in.act);
CHECK(n_leave == 0 && !g_open, "Leave, act, act: %d leaves", n_leave);
row("##coop_leave");
hold(in.act, 120);
if (!g_open) press_row(in, "##coop_leave");
hold(in.act, 120);
CHECK(n_leave == 0, "act held on Leave and in the window: %d leaves", n_leave);
close_window();
press_row(in, "##coop_leave");
tap(in.right);
tap(in.act);
CHECK(n_leave == 1, "Leave chosen: %d leaves", n_leave);
printf(" Leave: act, act / held: stays; right, act: %d leave\n", n_leave);
}
// A player in the room who is not identified: their game never said who they are, or two keys present themselves for
// their session (the engine disputes it: heard 0, counted in unidentified, the same to the page). The page names them
// and says why, the Accept window says it too with Accept greyed (right does not reach it, act does nothing); once they
// are identified Accept works.
static void check_unidentified(const Input &in) {
printf("%s not identified:\n", in.name);
in_room_as(false, 0x3333333u, false);
carol_unidentified();
counters_clear();
frames(4);
CHECK(shows(UI_MENU_COOP_NOT_IDENTIFIED) && shows_text("Carol (") && shows_text("Players in the room who are not identified"),
"the page does not say Carol is not identified and why the room cannot be accepted");
press_row(in, "##coop_answer");
CHECK(g_open && !strcmp(nav_name(), "Not now##no") && shows_text("Players in the room who are not identified"),
"Accept window with Carol not identified: cursor on %s", nav_name());
tap(in.right);
tap(in.act);
CHECK(n_accept == 0, "right, act with a player not identified: %d accepts", n_accept);
close_window();
g_fake_offer.unidentified = 0, g_fake_offer.member[1].heard = 1;
snprintf(g_fake_offer.member[1].name, sizeof g_fake_offer.member[1].name, "Carol");
snprintf(g_fake_offer.member[1].key, sizeof g_fake_offer.member[1].key, "c4r0-0000-0000-0003");
g_fake_offer.token = 0x3333334u;
press_row(in, "##coop_answer");
tap(in.right);
tap(in.act);
CHECK(n_accept == 1 && last_accept == 0x3333334u, "Carol identified, right, act: %d accepts", n_accept);
printf(" Accept greyed while Carol is not identified; 1 accept once she is\n");
}
// What of a row's note the Co-op tab shows. option_rows.cpp draws a row's note at 0.72 of the font size, at most 60 % of
// (row width - 2 pad) wide, and clips the rest. At the harness's 1903x1030 window Review T measured, from a picture of
// the tab, a row 689 px wide with a 23 px pad (a note shows at most 386 px) and the note "press to show" 113 px wide;
// the menu's font is Roboto Medium (runtime/ui/fonts, the menu's text below the CJK glyphs it takes from the game), here
// scaled to that size. The first part of the note up to its first " · " must show whole. zh-Hans is not measured: its
// glyphs come from the game's own files.
static ImFont *g_roboto;
static void note_fits(const char *what, const char *note) {
CHECK(g_roboto, "the menu's font (COOP_PAGE_FONT) did not load: the note's width is not measured");
if (!g_roboto) return;
const float row = 689, pad = 23, ref_px = 113, cap = (row - 2 * pad) * 0.6f;
const float size = 26.0f * 0.72f * ref_px / g_roboto->CalcTextSizeA(26.0f * 0.72f, 1e9f, 0, "press to show").x;
const char *end = strstr(note, " · ");
const float width = g_roboto->CalcTextSizeA(size, 1e9f, 0, note, end ? end : note + strlen(note)).x;
CHECK(width <= cap, "%s: the note's first part is %.0f px, of which the row shows %.0f: '%s'", what, width, cap, note);
printf(" %s: the note's first part %.0f px of the %.0f px a row shows\n", what, width, cap);
}
// A key the player accepted that now signs another name (the seventh review: it took another member's place in the
// room): Dave holds member 3 with the key the player accepted as Carol. The engine makes it a new offer
// (CoopOfferMember.accepted_as); the page says the name it was accepted as in the offer, in Dave's row and in the Accept
// window, and Accept takes the new offer only.
static void check_moved_key(const Input &in) {
printf("%s a key accepted under another name:\n", in.name);
static const char kCarolKey[] = "c4r0-0000-0000-0003";
in_room_as(false, 0x4444444u, false);
member(g_fake, 3, "Dave", false, 800);
g_fake.version++;
offer_member_add(3, "Dave", kCarolKey, false, true);
snprintf(g_fake_offer.member[1].accepted_as, sizeof g_fake_offer.member[1].accepted_as, "Carol");
counters_clear();
frames(4);
char moved[96], in_offer[128], note[400];
snprintf(moved, sizeof moved, menu_text(UI_MENU_COOP_ACCEPTED_AS), "Carol");
snprintf(in_offer, sizeof in_offer, "Dave (%s)", moved);
// the row's note is drawn into the draw list, not logged as text: it is checked as the page builds it, and a row
// shows only the start of a long note, so the name the key was accepted as comes first
member_note(g_fake.member[2], offer_member(g_fake_offer, 3), false, note, sizeof note);
CHECK(shows_text(in_offer), "the offer does not say Dave's key was accepted as Carol");
CHECK(!strncmp(note, moved, strlen(moved)), "Dave's row does not begin with his key's accepted name: '%s'", note);
note_fits("Dave's row", note);
static const char *const others[] = {"ru", "pt-BR"}; // the same part in the other languages; zh-Hans: see note_fits
for (const char *l : others) {
char other[160];
snprintf(other, sizeof other, ui_text_in(l, UI_MENU_COOP_ACCEPTED_AS), "Carol");
note_fits(l, other);
}
press_row(in, "##coop_answer");
CHECK(g_open && g_page == P_ACCEPT && !strcmp(nav_name(), "Not now##no") && shows_text(in_offer),
"Accept window with Carol's key under Dave: cursor on %s", nav_name());
tap(in.right);
tap(in.act);
CHECK(n_accept == 1 && last_accept == 0x4444444u, "Carol's key under Dave, right, act: %d accepts of %08x", n_accept,
last_accept);
printf(" the offer, Dave's row and the Accept window say 'a key you accepted as Carol'; %d accept of the new offer\n",
n_accept);
}
// The relay's states the page says in words: its limit on joins from one network, and a room it lost.
static void check_states(void) {
printf("relay states:\n");
out_of_room(COOP_CONNECTING, "too many joins from this network; trying again in a minute");
frames(3);
CHECK(shows(UI_MENU_COOP_JOIN_LIMIT), "the relay's join limit is not said");
out_of_room(COOP_CONNECTING, "the room was lost; waiting for it");
frames(3);
CHECK(shows(UI_MENU_COOP_LOST), "a room the relay lost is not said");
printf(" the join limit and a lost room are said\n");
}
// The conversions of a printf format in order ("%s%u" for "heard %u s ago, %s"), "%%" left out.
static void conversions(const char *f, char *out, size_t cap) {
size_t k = 0;
for (; *f && k + 3 < cap; f++) {
if (*f != '%') continue;
if (f[1] == '%') {
f++;
continue;
}
while (f[1] && !strchr("sdiuxXcfgp", f[1])) f++;
out[k++] = '%', out[k++] = f[1];
}
out[k] = 0;
}
// The checks --probe-services makes of the catalog (ov_probe.c probe_menu_text), for the Co-op tab's texts: English,
// Russian, Brazilian Portuguese and Simplified Chinese, none empty, each short enough for the description pane, and
// the same printf conversions in every language as in English. The other menu texts that lack a language are counted
// and named, not failed: they are not this page's.
static void check_catalog(void) {
static const char *const langs[] = {"en", "ru", "pt-BR", "zh-Hans"};
for (int id = UI_MENU_TAB_COOP; id <= UI_MENU_COOP_ACCEPTED_AS; id++) {
char en[32], other[32];
conversions(ui_text_in("en", (UiTextId)id), en, sizeof en);
for (const char *l : langs) {
CHECK(ui_text_defined(l, (UiTextId)id), "co-op text %d ('%s') lacks %s", id, ui_text_in("en", (UiTextId)id), l);
CHECK(strlen(ui_text_in(l, (UiTextId)id)) + 256 < UI_DESC_BYTES, "co-op text %d in %s is too long", id, l);
conversions(ui_text_in(l, (UiTextId)id), other, sizeof other);
CHECK(!strcmp(en, other), "co-op text %d: %s has '%s', English '%s'", id, l, other, en);
}
}
printf("catalog: the %d co-op texts in en, ru, pt-BR and zh-Hans, with English's conversions\n",
UI_MENU_COOP_ACCEPTED_AS - UI_MENU_TAB_COOP + 1);
int gaps = 0;
for (int id = UI_MENU_FIRST; id < UI_MENU_TAB_COOP; id++)
for (const char *l : langs)
if (!ui_text_defined(l, (UiTextId)id)) {
if (!gaps++) printf(" not this page's, lacking a language:");
printf(" %d (%s, '%s')", id, l, ui_text_in("en", (UiTextId)id));
}
if (gaps) printf("\n");
}
int main(void) {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
io.IniFilename = nullptr;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard | ImGuiConfigFlags_NavEnableGamepad; // as menu.cpp
io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
io.Fonts->AddFontDefault(); // the font the page is drawn with here; Roboto only measures notes (note_fits)
static const ImWchar ranges[] = {0x20, 0x24f, 0x400, 0x4ff, 0x2000, 0x206f, 0};
if (const char *f = getenv("COOP_PAGE_FONT")) g_roboto = io.Fonts->AddFontFromFileTTF(f, 26.0f, nullptr, ranges);
unsigned char *px;
int w, h;
io.Fonts->GetTexDataAsRGBA32(&px, &w, &h);
frames(2);
g_fault = getenv("COOP_PAGE_FAULT") && atoi(getenv("COOP_PAGE_FAULT"));
if (g_fault) printf("deliberate fault: each window that acts on the room opens with Right pressed\n");
check_fields_by_keyboard();
check_join_by_pad();
check_offer(kKeyboard);
check_offer(kPad);
check_maker(kKeyboard);
check_maker(kPad);
check_unidentified(kKeyboard);
check_unidentified(kPad);
check_moved_key(kKeyboard);
check_moved_key(kPad);
check_states();
check_catalog();
ImGui::DestroyContext();
printf("coop page: %s\n", failures ? "see FAIL lines" : "ok");
return failures;
}

60
tests/coop_page_check.py Normal file
View File

@@ -0,0 +1,60 @@
"""Build and run tests/coop_page_check.cpp: the menu's Co-op tab (runtime/ui/coop_page.cpp) without the game, driven with
the key and pad events the menu hands Dear ImGui, the co-op layer answered by the test. Typing a name, a relay and a
secret (with the letters that switch tabs elsewhere), Make, Join with a wrong secret and a pasted one, the room's offer
accepted and declined, the maker's key, a player not identified, a key accepted under another name, Remove by key, the
pools' contributors and their reset, Leave; no confirmation acts on a repeated or held press, the maker changes a rule
and the others cannot.
Then the same checks again with a deliberate fault (COOP_PAGE_FAULT=1: each window that acts on the room opens with the
cursor moved onto its acting button), which they must catch: the test passes only when the first run has no failed
check and the second has some.
Usage: coop_page_check.py (needs clang++ or g++; no game files)
"""
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
RT = ROOT / 'runtime'
SOURCES = [ROOT / 'tests' / 'coop_page_check.cpp', RT / 'ui' / 'option_rows.cpp', RT / 'ui' / 'menu_skin.cpp',
RT / 'ui_text.c', RT / 'coop_proto.c', RT / 'coop_state.c', RT / 'sha256.c',
RT / 'third_party' / 'monocypher' / 'monocypher.c'] + sorted((RT / 'third_party' / 'imgui').glob('imgui*.cpp'))
def main():
cxx = os.environ.get('CXX') or shutil.which('clang++') or shutil.which('g++')
if not cxx:
sys.exit('needs clang++ or g++ on PATH')
work = ROOT / 'build' / 'test' / 'coop_page'
work.mkdir(parents=True, exist_ok=True)
objs = []
for src in SOURCES:
obj = work / (src.stem + '.o')
lang = ['-x', 'c', '-std=gnu11'] if src.suffix == '.c' else ['-x', 'c++', '-std=gnu++17']
# the page's own file builds with the game's warnings; the rest as the game builds them, unchecked here
warn = ['-Wall', '-Wextra', '-Werror'] if src.name == 'coop_page_check.cpp' else ['-w']
if subprocess.run([cxx, *lang, '-c', '-O1', '-g', *warn, '-march=x86-64-v2', '-I', str(RT),
'-I', str(RT / 'third_party' / 'imgui'), str(src), '-o', str(obj)]).returncode:
sys.exit(f'{src.name} did not compile')
objs.append(str(obj))
exe = work / ('coop_page_check.exe' if os.name == 'nt' else 'coop_page_check')
if subprocess.run([cxx, *objs, '-o', str(exe), '-lm']).returncode:
sys.exit('the link failed')
env = {**os.environ, 'COOP_PAGE_FONT': str(RT / 'ui' / 'fonts' / 'Roboto-Medium.ttf')} # measures the rows' notes
r = subprocess.run([str(exe)], env=env)
if r.returncode:
sys.exit(r.returncode)
fault = subprocess.run([str(exe)], env={**env, 'COOP_PAGE_FAULT': '1'}, capture_output=True, text=True)
fails = [line for line in fault.stdout.splitlines() if line.startswith('FAIL ')]
print(f'deliberate fault (the cursor moved onto the acting button as a window opens): {len(fails)} checks fail')
for line in fails[:3]:
print(' ' + line)
if not fails:
sys.exit('the deliberate fault went unnoticed')
if __name__ == '__main__':
main()

1247
tests/coop_relay_test.py Normal file

File diff suppressed because it is too large Load Diff

97
tests/coop_vectors.txt Normal file
View File

@@ -0,0 +1,97 @@
# Byte vectors of the co-op wire format, protocol 2. Written by `python tests/coop_relay_test.py --write-vectors`,
# read by tests/coop_relay_test.py and by runtime/coop_probe.c (--probe-coop); each side checks its codec against them.
# Hex is lower case, "-" is empty. Fields are tokens; a name is hex of its UTF-8 bytes.
#
# hmac <name> <key> <message> <HMAC-SHA256> RFC 4231 cases and a locator as the key
# secret <canonical|-> <text, the rest of the line> normalisation: the secret a text means, - for none
# keys <secret> <locator> <message key> BLAKE2b keyed with the secret (coop_proto.h)
# dgram <name> <secret|-> <ver> <type> <room> <src> <dst> <seq> <payload> <datagram> parse, verify the tag with the
# secret's locator (- : a zero tag), pack again
# badtag <name> <secret> <datagram> parses, and the tag does not verify
# connect|challenge|roster|joined|error|kick ... one message payload: parse, check the fields, pack again
# sealed <name> <secret> <src> <dst> <session> <counter> <kind> <message> <payload> opens to kind and message, seals
# to the same bytes
# pos <name> <payload> <pseq> <t_ms> <flags> <area> <x> <y> <z> <yaw> <node:frame>... a POS (the message after its
# kind byte): parse, check the fields, pack again
# reject <kind> <name> <bytes> parse must fail
hmac rfc4231-1 0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b 4869205468657265 b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7
hmac rfc4231-2 4a656665 7768617420646f2079612077616e7420666f72206e6f7468696e673f 5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843
hmac rfc4231-6 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a65204b6579202d2048617368204b6579204669727374 60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54
hmac locator fc4cd2239930b86edfecd4da8b83e9d6 000102030405060708090a0b0c0d0e0f10111213141516171819 42ba9d60a8d078abc7dc0d56693013f027ed42415b19b3624563e3939a8d84a0
hmac empty e15751bff9c5734c17af60d9028975bf - a5eb888727aa1acf14896b6ac56df6cdca6df201be0d370e30e5c665dda275b9
secret H7K3WMBCDFGH234567 h7k3wm-bcdfgh-234567
secret H7K3WMBCDFGH234567 H7K 3WM BCD FGH 234 567
secret H7K3WMBCDFGH234567 H7K3WMBCDFGH234567
secret - H7K3WMBCDFGH23456
secret - H7K3WMBCDFGH2345678
secret - H7K3WMBCDFGH23456E
secret - H7K3WMBCDFGH23456Q
secret - H7K3WMBCDFGH234560
secret - H7K3WM.BCDFGH234567
secret -
keys H7K3WMBCDFGH234567 fc4cd2239930b86edfecd4da8b83e9d6 1f1e9bacbbdecdf42c996b5ceed8d0ee00b365fc083b38976ac5faa410d580d2
keys 23BCDF23BCDF23BCDF e15751bff9c5734c17af60d9028975bf 61bf28cf7c9a7352c6f0760bc290a9cbc4088529604549ba4a194b69d07fb0f2
keys ZZZZZZZZZZZZZZZZZZ a5b67e4a3b31acd59d91075a950fe08b 1b832c032d001b1582d5d940e7af25456c2776f2fadc968469618575bcc150d5
dgram connect-create H7K3WMBCDFGH234567 2 1 0 0 0 7 0100fc4cd2239930b86edfecd4da8b83e9d6efcdab8967452301887766554433221100ffeeddccbbaa9905416c6963650000000000000000000000000000000000000000000000000000000000000000 4e43020100000000000050000700000005e25e108234ba2b0100fc4cd2239930b86edfecd4da8b83e9d6efcdab8967452301887766554433221100ffeeddccbbaa9905416c6963650000000000000000000000000000000000000000000000000000000000000000
dgram connect-join H7K3WMBCDFGH234567 2 1 0 0 0 7 0200fc4cd2239930b86edfecd4da8b83e9d6efcdab8967452301887766554433221100ffeeddccbbaa9903426f62a0a1a2a3a4a5a6a7a8a9aaabacadaeaf303132333435363738393a3b3c3d3e3f 4e4302010000000000004e0007000000e27557d80346f66c0200fc4cd2239930b86edfecd4da8b83e9d6efcdab8967452301887766554433221100ffeeddccbbaa9903426f62a0a1a2a3a4a5a6a7a8a9aaabacadaeaf303132333435363738393a3b3c3d3e3f
dgram challenge H7K3WMBCDFGH234567 2 2 0 0 0 1 00ffeeddccbbaa99a0a1a2a3a4a5a6a7a8a9aaabacadaeaf 4e430202000000000000180001000000b6d92fe0328ccb5a00ffeeddccbbaa99a0a1a2a3a4a5a6a7a8a9aaabacadaeaf
dgram joined H7K3WMBCDFGH234567 2 3 3405643777 0 2 2 0200ffeeddccbbaa99303132333435363738393a3b3c3d3e3f020102887766554433221105416c69636502010700000000000000045a6fc3ab 4e4302030100feca0002390002000000367a7ec9d8226b970200ffeeddccbbaa99303132333435363738393a3b3c3d3e3f020102887766554433221105416c69636502010700000000000000045a6fc3ab
dgram error H7K3WMBCDFGH234567 2 5 0 0 0 3 0100ffeeddccbbaa990c6e6f207375636820726f6f6d 4e4302050000000000001600030000004decf2596a67e08d0100ffeeddccbbaa990c6e6f207375636820726f6f6d
dgram roster H7K3WMBCDFGH234567 2 6 3405643777 0 1 4 020102887766554433221105416c69636502010700000000000000045a6fc3ab 4e4302060100feca00012000040000003a125ec4034e095c020102887766554433221105416c69636502010700000000000000045a6fc3ab
dgram ping H7K3WMBCDFGH234567 2 7 3405643777 3 0 99 efbeadde 4e4302070100feca0300040063000000ccc370c727afd881efbeadde
dgram pong H7K3WMBCDFGH234567 2 8 3405643777 0 3 100 efbeadde 4e4302080100feca000304006400000067c59c7e243fc04defbeadde
dgram leave H7K3WMBCDFGH234567 2 9 3405643777 3 0 101 - 4e4302090100feca0300000065000000447675f8e1fef961
dgram kick H7K3WMBCDFGH234567 2 11 3405643777 1 0 102 020700000000000000 4e43020b0100feca01000900660000002b0bcf6376d31a69020700000000000000
dgram data-sealed H7K3WMBCDFGH234567 2 16 3405643777 3 255 103 01efcdab896745230101000000d2244219967816dfefab11d765441dbffe22c6b5b160 4e4302100100feca03ff2300670000005b9f21795c3c7e4101efcdab896745230101000000d2244219967816dfefab11d765441dbffe22c6b5b160
dgram data-max H7K3WMBCDFGH234567 2 16 1 8 255 0 00070e151c232a31383f464d545b626970777e858c939aa1a8afb6bdc4cbd2d9e0e7eef5fc030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b727980878e959ca3aab1b8bfc6cdd4dbe2e9f0f7fe050c131a21282f363d444b525960676e757c838a91989fa6adb4bbc2c9d0d7dee5ecf3fa01080f161d242b323940474e555c636a71787f868d949ba2a9b0b7bec5ccd3dae1e8eff6fd040b121920272e353c434a51585f666d747b828990979ea5acb3bac1c8cfd6dde4ebf2f900070e151c232a31383f464d545b626970777e858c939aa1a8afb6bdc4cbd2d9e0e7eef5fc030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b727980878e959ca3aab1b8bfc6cdd4dbe2e9f0f7fe050c131a21282f363d444b525960676e757c838a91989fa6adb4bbc2c9d0d7dee5ecf3fa01080f161d242b323940474e555c636a71787f868d949ba2a9b0b7bec5ccd3dae1e8eff6fd040b121920272e353c434a51585f666d747b828990979ea5acb3bac1c8cfd6dde4ebf2f900070e151c232a31383f464d545b626970777e858c939aa1a8afb6bdc4cbd2d9e0e7eef5fc030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b727980878e959ca3aab1b8bfc6cdd4dbe2e9f0f7fe050c131a21282f363d444b525960676e757c838a91989fa6adb4bbc2c9d0d7dee5ecf3fa01080f161d242b323940474e555c636a71787f868d949ba2a9b0b7bec5ccd3dae1e8eff6fd040b121920272e353c434a51585f666d747b828990979ea5acb3bac1c8cfd6dde4ebf2f900070e151c232a31383f464d545b626970777e858c939aa1a8afb6bdc4cbd2d9e0e7eef5fc030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b727980878e959ca3aab1b8bfc6cdd4dbe2e9f0f7fe050c131a21282f363d444b525960676e757c838a91989fa6adb4bbc2c9d0d7dee5ecf3fa01080f161d242b323940474e555c636a71787f868d949ba2a9b0b7bec5ccd3dae1e8eff6fd040b121920272e353c434a51585f666d747b828990979ea5acb3bac1c8cfd6dde4ebf2f900070e151c232a31383f464d545b626970777e858c939aa1a8afb6bdc4cbd2d9e0e7eef5fc030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b727980878e959ca3aab1b8bfc6cdd4dbe2e9f0f7fe050c131a21 4e4302100100000008ff980400000000ce73d251ac722a7300070e151c232a31383f464d545b626970777e858c939aa1a8afb6bdc4cbd2d9e0e7eef5fc030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b727980878e959ca3aab1b8bfc6cdd4dbe2e9f0f7fe050c131a21282f363d444b525960676e757c838a91989fa6adb4bbc2c9d0d7dee5ecf3fa01080f161d242b323940474e555c636a71787f868d949ba2a9b0b7bec5ccd3dae1e8eff6fd040b121920272e353c434a51585f666d747b828990979ea5acb3bac1c8cfd6dde4ebf2f900070e151c232a31383f464d545b626970777e858c939aa1a8afb6bdc4cbd2d9e0e7eef5fc030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b727980878e959ca3aab1b8bfc6cdd4dbe2e9f0f7fe050c131a21282f363d444b525960676e757c838a91989fa6adb4bbc2c9d0d7dee5ecf3fa01080f161d242b323940474e555c636a71787f868d949ba2a9b0b7bec5ccd3dae1e8eff6fd040b121920272e353c434a51585f666d747b828990979ea5acb3bac1c8cfd6dde4ebf2f900070e151c232a31383f464d545b626970777e858c939aa1a8afb6bdc4cbd2d9e0e7eef5fc030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b727980878e959ca3aab1b8bfc6cdd4dbe2e9f0f7fe050c131a21282f363d444b525960676e757c838a91989fa6adb4bbc2c9d0d7dee5ecf3fa01080f161d242b323940474e555c636a71787f868d949ba2a9b0b7bec5ccd3dae1e8eff6fd040b121920272e353c434a51585f666d747b828990979ea5acb3bac1c8cfd6dde4ebf2f900070e151c232a31383f464d545b626970777e858c939aa1a8afb6bdc4cbd2d9e0e7eef5fc030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b727980878e959ca3aab1b8bfc6cdd4dbe2e9f0f7fe050c131a21282f363d444b525960676e757c838a91989fa6adb4bbc2c9d0d7dee5ecf3fa01080f161d242b323940474e555c636a71787f868d949ba2a9b0b7bec5ccd3dae1e8eff6fd040b121920272e353c434a51585f666d747b828990979ea5acb3bac1c8cfd6dde4ebf2f900070e151c232a31383f464d545b626970777e858c939aa1a8afb6bdc4cbd2d9e0e7eef5fc030a11181f262d343b424950575e656c737a81888f969da4abb2b9c0c7ced5dce3eaf1f8ff060d141b222930373e454c535a61686f767d848b9299a0a7aeb5bcc3cad1d8dfe6edf4fb020910171e252c333a41484f565d646b727980878e959ca3aab1b8bfc6cdd4dbe2e9f0f7fe050c131a21
badtag flipped-tag H7K3WMBCDFGH234567 4e4302100100feca03ff2300670000005a9f21795c3c7e4101efcdab896745230101000000d2244219967816dfefab11d765441dbffe22c6b5b160
badtag wrong-secret 23BCDF23BCDF23BCDF 4e4302100100feca03ff2300670000005b9f21795c3c7e4101efcdab896745230101000000d2244219967816dfefab11d765441dbffe22c6b5b160
dgram other-version - 1 7 0 0 0 5 00000000 4e430107000000000000040005000000000000000000000000000000
connect create 1 0 fc4cd2239930b86edfecd4da8b83e9d6 0123456789abcdef 1122334455667788 99aabbccddeeff00 416c696365 00000000000000000000000000000000 00000000000000000000000000000000 0100fc4cd2239930b86edfecd4da8b83e9d6efcdab8967452301887766554433221100ffeeddccbbaa9905416c6963650000000000000000000000000000000000000000000000000000000000000000
connect join 2 0 fc4cd2239930b86edfecd4da8b83e9d6 0123456789abcdef 1122334455667788 99aabbccddeeff00 5a6fc3ab a0a1a2a3a4a5a6a7a8a9aaabacadaeaf 303132333435363738393a3b3c3d3e3f 0200fc4cd2239930b86edfecd4da8b83e9d6efcdab8967452301887766554433221100ffeeddccbbaa99045a6fc3aba0a1a2a3a4a5a6a7a8a9aaabacadaeaf303132333435363738393a3b3c3d3e3f
connect long-name 2 1 fc4cd2239930b86edfecd4da8b83e9d6 0000000000000001 0000000000000002 0000000000000003 787878787878787878787878787878787878787878787878 a0a1a2a3a4a5a6a7a8a9aaabacadaeaf 303132333435363738393a3b3c3d3e3f 0201fc4cd2239930b86edfecd4da8b83e9d601000000000000000200000000000000030000000000000018787878787878787878787878787878787878787878787878a0a1a2a3a4a5a6a7a8a9aaabacadaeaf303132333435363738393a3b3c3d3e3f
challenge one 00ffeeddccbbaa99a0a1a2a3a4a5a6a7a8a9aaabacadaeaf 99aabbccddeeff00 a0a1a2a3a4a5a6a7a8a9aaabacadaeaf
roster two 020102887766554433221105416c69636502010700000000000000045a6fc3ab 1:2:1122334455667788:416c696365 2:1:0000000000000007:5a6fc3ab
roster empty 00
roster full 080100010000000000000002503102000200000000000000025032030003000000000000000250330400040000000000000002503405000500000000000000025035060006000000000000000250360700070000000000000002503708000800000000000000025038 1:0:0000000000000001:5031 2:0:0000000000000002:5032 3:0:0000000000000003:5033 4:0:0000000000000004:5034 5:0:0000000000000005:5035 6:0:0000000000000006:5036 7:0:0000000000000007:5037 8:0:0000000000000008:5038
joined two 0200ffeeddccbbaa99303132333435363738393a3b3c3d3e3f020102887766554433221105416c69636502010700000000000000045a6fc3ab 2 99aabbccddeeff00 303132333435363738393a3b3c3d3e3f 1:2:1122334455667788:416c696365 2:1:0000000000000007:5a6fc3ab
error no-room 0100ffeeddccbbaa990c6e6f207375636820726f6f6d 1 99aabbccddeeff00 6e6f207375636820726f6f6d
error bare 06000000000000000000 6 0000000000000000 -
kick two 028877665544332211 2 1122334455667788
sealed test H7K3WMBCDFGH234567 3 255 0123456789abcdef 1 240 68656c6c6f 01efcdab896745230101000000d2244219967816dfefab11d765441dbffe22c6b5b160
sealed empty H7K3WMBCDFGH234567 1 2 fedcba9876543210 4294967295 3 - 011032547698badcfeffffffffe5632ae3fe1043b0359b661003ee21dbb9
pos walk 020700000040e20100020b487972756c654669656c6400509a44000061c2004845440000c03f020457616c6b000040410a4c6f7765725f57616c6b00006040 7 123456 2 487972756c654669656c64 1234.5 -56.25 789.125 1.5 57616c6b:12.0 4c6f7765725f57616c6b:3.5
pos adult-no-slots 02ffffffffffffffff030c466f7265737454656d706c650000000000000000000000bf000040c000 4294967295 4294967295 3 466f7265737454656d706c65 0.0 0.0 -0.5 -3.0
pos loading 02010000000200000000000000000000000000000000000000000000 1 2 0 - 0.0 0.0 0.0 0.0
pos long-names 020900000009000000022f61616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161610000803f00000040000040400000803e042f6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e000000002f6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e0000803f2f6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e000000402f6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e00004040 9 9 2 6161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161 1.0 2.0 3.0 0.25 6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e:0.0 6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e:1.0 6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e:2.0 6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e6e:3.0
reject dgram short 0000000000000000000000000000000000000000000000
reject dgram magic 00000207000000000000040001000000000000000000000061626364
reject dgram length-long 4e430207000000000000050001000000000000000000000061626364
reject dgram trailing 4e43020700000000000004000100000000000000000000006162636478
reject connect name-too-long 0200fc4cd2239930b86edfecd4da8b83e9d601000000000000000200000000000000030000000000000019000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
reject connect no-token 0200fc4cd2239930b86edfecd4da8b83e9d601000000000000000200000000000000030000000000000003426f6200000000000000000000000000000000000000000000000000000000000000
reject connect cut 0200fc4cd2239930b86edfecd4da8b83e9d60100000000000000
reject challenge long 00000000000000000000000000000000000000000000000000
reject challenge short 0000000000000000000000000000000000000000000000
reject roster count-9 0900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
reject roster cut-name 010100000000000000000009616263
reject roster cut-entry 020100000000000000000001410200
reject joined cut 010000000000000000000000000000000000000000
reject error cut-text 01000000000000000009616263
reject error cut 010000000000000000
reject kick long 00000000000000000000
reject sealed short 0100000000000000000000000000000000000000000000000000000000
reject sealed version 0200000000000000000000000000000000000000000000000000000000000000000000000000000000
reject pos version-1 010700000040e20100020b487972756c654669656c6400509a44000061c2004845440000c03f020457616c6b000040410a4c6f7765725f57616c6b00006040
reject pos cut 020700000040e20100020b487972756c654669656c6400509a44000061c2004845440000c03f020457616c6b000040410a4c6f7765725f57616c6b000060
reject pos trailing 020700000040e20100020b487972756c654669656c6400509a44000061c2004845440000c03f020457616c6b000040410a4c6f7765725f57616c6b0000604078
reject pos area-48 02000000000000000000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
reject pos slots-5 0200000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000
reject pos node-48 020000000000000000000000000000000000000000000000000000013000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
reject pos area-newline 020100000002000000020c487972756c650a4669656c640000000000000000000000000000000000
reject pos area-utf8 02010000000200000002045a6fc3ab0000000000000000000000000000000000
reject pos node-nul 020100000002000000020b487972756c654669656c640000000000000000000000000000000001045761006b0000803f
reject pos x-nan 020100000002000000020b487972756c654669656c640000c07f00000000000000000000000000
reject pos yaw-inf 020100000002000000020b487972756c654669656c640000000000000000000000000000807f00
reject pos frame-inf 020100000002000000020b487972756c654669656c6400000000000000000000000000000000010457616c6b000080ff
reject pos empty -

View File

@@ -0,0 +1,70 @@
"""Run the game executable's --probe-devlost and check how a run ends when the GPU device is lost
(runtime/vk_device.c vk_device_lost_check, runtime/vk_sync.c vk_sync_probe_devlost).
The probe needs a Vulkan device and opens no window. It does not lose the device: it marks it lost, and then has four
threads report a loss at the same moment. Checked here:
- on the healthy device a completion callback runs (so the next check can fail);
- with the device marked lost no callback runs, no value counts as done, and the submit, upload and wait paths return
at once;
- the process ends with exit code 72 (dispatch.h RT_LOST_EXIT), with one device report and one FATAL line, well
before the watchdog's 10 seconds.
What a driver answers after a real loss is not covered.
Usage: device_lost_check.py PATH_TO_GAME_EXE
"""
import subprocess
import sys
import time
LOST_EXIT = 72
def main():
if len(sys.argv) != 2:
print(__doc__)
return 2
p = subprocess.Popen([sys.argv[1], '--probe-devlost'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
text=True, errors='replace')
lines, reported_at = [], None
for line in p.stdout:
lines.append(line.rstrip('\n'))
if 'four threads report the loss now' in line:
reported_at = time.monotonic()
code = p.wait()
ended = time.monotonic()
text = '\n'.join(lines)
if 'probe-devlost: SKIP' in text:
print('device_lost_check: SKIP (no Vulkan device)')
return 0
fails = []
def check(ok, what):
if not ok:
fails.append(what)
check('probe-devlost: healthy device: the callback of value' in text, 'no callback ran on the healthy device')
marked = [ln for ln in lines if ln.startswith('probe-devlost: device marked lost:')]
check(len(marked) == 1 and 'not run, done stayed, submit not recorded' in marked[0],
'a path went on after the device was marked lost: %s' % (marked or ['no line']))
check('probe-devlost: FAILED' not in text, 'the probe reports a failure')
check(code == LOST_EXIT, 'exit code %d, expected %d' % (code, LOST_EXIT))
fatal = [ln for ln in lines if ln.startswith('FATAL: ')]
check(len(fatal) == 1 and 'the GPU device was lost' in fatal[0], '%d FATAL lines, expected one: %s' % (len(fatal), fatal))
reports = [ln for ln in lines if 'video memory when the device was lost' in ln]
check(len(reports) == 1, '%d device reports, expected one' % len(reports))
took = ended - reported_at if reported_at else -1
check(0 <= took < 5, 'the process ended %.1f s after the loss was reported (the watchdog, not the reporting thread?)' % took)
for f in fails:
print('device_lost_check: FAIL:', f)
if fails:
print(text[-3000:])
return 1
print('device_lost_check: passed (exit code %d, one report, one FATAL line, %.2f s from the report to the exit)'
% (code, took))
return 0
if __name__ == '__main__':
sys.exit(main())

83
tests/ffx_cache_check.py Normal file
View File

@@ -0,0 +1,83 @@
"""Run the game executable's --probe-ffx-cache twice on pipeline cache files of the test's own
(runtime/vk_ffx.cpp vk_ffx_probe_cache: one FSR 3.1 upscaling context, without a window or the game).
FSR 3.1's library creates its pipelines itself and names no pipeline cache; the runtime hands it pipeline creates that
go through the pipeline caches kept across runs (runtime/vk_pipeline.c vk_pipeline_create_foreign). Checked here:
- the library's pipelines come through those creates (the first run counts them);
- a second run, which reads what the first one saved, compiles none of them: each comes from the caches;
- with ffx_pipe_cache = 0 none comes through the runtime.
The executable needs amd_fidelityfx_vk.dll beside it and a Vulkan device; without either the probe says SKIP and so
does this test. The pipeline cache files go to build/test/ffx_cache, never beside the executable.
Usage: ffx_cache_check.py PATH_TO_GAME_EXE
"""
import os
import re
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
LINE = re.compile(r'probe-ffx-cache: context (created|not created) in \d+ ms; pipelines made through the runtime\'s '
r'creates: (\d+) \(from the pipeline caches (\d+), compiled into the play cache (\d+), through the '
r'one cache (\d+), with no cache (\d+)\)')
def run(game, cache, setting):
"""One probe run -> (exit code, last probe line, the counts or None)."""
env = dict(os.environ)
env['NX2NAVI_PIPELINE_CACHE'] = str(cache)
env.pop('NX2NAVI_PIPE_BINARIES', None)
env['NX2NAVI_FFX_PIPE_CACHE'] = setting
r = subprocess.run([game, '--probe-ffx-cache'], env=env, capture_output=True, text=True, errors='replace')
lines = [ln for ln in r.stdout.splitlines() if ln.startswith('probe-ffx-cache:')]
counts = None
for ln in lines:
m = LINE.match(ln)
if m:
counts = dict(zip(('made', 'cached', 'compiled', 'one', 'none'), map(int, m.groups()[1:])))
print('\n'.join(lines))
return r.returncode, lines[-1] if lines else '', counts
def main():
if len(sys.argv) != 2:
print(__doc__)
return 2
game = sys.argv[1]
work = ROOT / 'build' / 'test' / 'ffx_cache'
shutil.rmtree(work, ignore_errors=True)
work.mkdir(parents=True)
cache = work / 'pipeline-cache.bin'
rc, last, first = run(game, cache, '1')
if 'SKIP' in last:
print('ffx_cache_check: SKIP')
return 0
fails = []
if rc or not last.endswith('passed') or not first:
fails.append('the first run did not pass')
elif not first['made']:
fails.append('the first run made no pipeline through the runtime')
rc, last, second = run(game, cache, '1')
if rc or not last.endswith('passed') or not second:
fails.append('the second run did not pass')
elif first and (second['made'] != first['made'] or second['compiled']):
fails.append('the second run made %d pipelines and compiled %d of them: with the first run\'s files it must '
'make the same %d and compile none' % (second['made'], second['compiled'], first['made']))
elif first and not first['one'] and not first['none'] and second['cached'] != second['made']:
fails.append('with a play cache every pipeline of the second run comes from the caches: %d of %d did'
% (second['cached'], second['made']))
rc, last, off = run(game, cache, '0')
if rc or not last.endswith('passed') or not off or off['made']:
fails.append('with ffx_pipe_cache = 0 the run must pass with no pipeline made through the runtime')
for f in fails:
print('ffx_cache_check: FAIL: ' + f)
print('ffx_cache_check: %s' % ('FAILED' if fails else 'passed'))
return 1 if fails else 0
if __name__ == '__main__':
sys.exit(main())

View File

@@ -32,12 +32,15 @@ int ov_movie_live(void) { return 0; }
int vk_fg_generating(void) { return 0; }
int vk_fg_active(void) { return 0; }
int vk_fg_wanted(void) { return 0; }
int vk_dlssg_multiplier(void) { return 1; }
int vk_dlssg_active(void) { return 0; }
static int fg_mult = 1; // frames on screen per game frame
int vk_dlssg_multiplier(void) { return fg_mult; }
int vk_dlssg_active(void) { return fg_mult > 1; }
void rtss_limit_want(int64_t millihz) { (void)millihz; }
int rtss_limit_active(void) { return 0; }
int plat_mem_read(uint64_t a, void *out, size_t n) { (void)a, (void)out, (void)n; return 0; }
int plat_mem_readable(uint64_t a, size_t n) { (void)a, (void)n; return 0; }
void mem_kernel_access(uint64_t a, uint64_t n) { (void)a, (void)n; }
int mem_host_readable(uint64_t a, uint64_t n) { (void)a, (void)n; return 0; }
uint64_t kern_now_ns(void) { return 0; }
// ---- one present: the pacer asks for the period, then reports the display time. Returns the step in 60 Hz frames.
@@ -135,6 +138,21 @@ int main(int argc, char **argv) {
float g = present(0.0011); // 900 fps, as a quick menu without the pacer would run
CHECK(g >= 0.125f, "short frames: %.4f frames", g);
}
// The limit counts the frames the game draws: frame generation's multiplier leaves the pace of a numeric limit and
// of unlimited alone, and divides only the monitor's rate (144 Hz here), which is a rate on the screen.
static const struct { const char *l; int gen; double hz, game_hz; } caps[] = {
{"60", 1, 60, 60}, {"60", 4, 60, 60}, {"120", 2, 120, 120}, {"240", 4, 240, 240},
{"display", 1, 144, 144}, {"display", 4, 36, 36}, {"unlimited", 1, 240, 0}, {"unlimited", 4, 240, 0}};
for (unsigned i = 0; i < sizeof caps / sizeof *caps; i++) {
set_limit(caps[i].l);
fg_mult = caps[i].gen;
double hz = 1e9 / (double)frame_step_cap_ns(), game = frame_step_game_hz();
CHECK(fabs(hz - caps[i].hz) < 0.01, "limit %s at %dx paces the game at %.3f fps, not %.0f", caps[i].l, caps[i].gen,
hz, caps[i].hz);
CHECK(fabs(game - caps[i].game_hz) < 0.01, "limit %s at %dx reports %.3f game fps, not %.0f", caps[i].l,
caps[i].gen, game, caps[i].game_hz);
}
fg_mult = 1;
printf("frame_step_check: %s\n", fails ? "FAILED" : "ok");
return fails != 0;
}

View File

@@ -23,6 +23,10 @@ void plat_mutex_lock(PlatMutex *m) { (void)m; }
void plat_mutex_unlock(PlatMutex *m) { (void)m; }
static int out_of_blocks_lines;
void perf_size(PerfSizes *s, const char *name, uint64_t count, uint64_t limit, uint64_t bytes) {
(void)s, (void)name, (void)count, (void)limit, (void)bytes; // vk_gpa_sizes is not called here
}
void rt_log(const char *fmt, ...) {
char line[512];
va_list ap;

File diff suppressed because it is too large Load Diff

View File

@@ -39,7 +39,7 @@ def main():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
rt = ROOT / 'runtime'
plat = [rt / 'platform' / ('win32.c' if win else 'linux.c'), rt / 'platform' / 'common.c']
libs = ['-lbcrypt', '-lpsapi', '-lwinmm', '-ldbghelp', '-lshell32', '-lole32', '-luser32', '-ladvapi32'] if win \
libs = ['-lbcrypt', '-lws2_32', '-lpsapi', '-lwinmm', '-ldbghelp', '-lshell32', '-lole32', '-luser32', '-ladvapi32'] if win \
else ['-lpthread']
subprocess.check_call([compiler(), '-O1', '-g', '-Wall', '-Wno-unused-function', '-fno-strict-aliasing',
'-march=x86-64-v3', '-I', str(rt), '-I', str(work),

42
tests/mem_need_check.c Normal file
View File

@@ -0,0 +1,42 @@
// runtime/mem_need.h: whether the machine has the commit the game needs (runtime/memory.c mem_start_note). Built and
// run by tests/mem_need_check.py; prints each failed check and exits 1 on any.
#include <stdio.h>
#include "mem_need.h"
static int fails;
#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
int main(void) {
const double need = MEM_NEED_DEFAULT_MB;
// The machine the need was measured on, before the game starts: 41,515 of 73,154 MB in use, the process at 369 MB.
// 32,008 MB for the game: enough.
CHECK(!mem_need_short(369, 41515, 73154, need), "short with 32 GB for the game");
CHECK(mem_need_room(369, 41515, 73154) == 73154 - 41515 + 369, "room %.0f", mem_need_room(369, 41515, 73154));
// The same machine with the game in play, 65,840 of 73,154 MB in use and 24,000 MB of them the game's: what the
// game holds counts toward its need, so a check made late does not call a fitting game short.
CHECK(!mem_need_short(24000, 65840, 73154, need), "short in play although the game's own commit fits");
// A 32 GB machine with an 8 GB page file and 12 GB in use by other programs: 28 GB free, enough; with 20 GB in
// use, 20 GB free: short.
CHECK(!mem_need_short(300, 12000, 40960, need), "short with 29 GB free");
CHECK(mem_need_short(300, 20000, 40960, need), "enough with 21 GB free");
// A 16 GB machine with a 4 GB page file never fits.
CHECK(mem_need_short(300, 6000, 20480, need), "enough on a 16 GB machine");
// The boundary: exactly the need is enough, one MB less is short.
CHECK(!mem_need_short(0, 1000, 1000 + need, need), "short with exactly the need free");
CHECK(mem_need_short(0, 1001, 1000 + need, need), "enough one MB below the need");
// Not asked (0), and a platform without a known limit (-1): never short. A process reading of -1 (unknown) adds
// nothing to the room.
CHECK(!mem_need_short(300, 20000, 20480, 0), "short with the check off");
CHECK(!mem_need_short(300, 20000, -1, need), "short without a known limit");
CHECK(mem_need_room(-1, 20000, 40960) == 20960, "an unknown process commit changed the room");
printf("mem_need_check: %s\n", fails ? "FAILED" : "passed");
return fails ? 1 : 0;
}

67
tests/mem_need_check.py Normal file
View File

@@ -0,0 +1,67 @@
"""Build and run tests/mem_need_check.c: runtime/mem_need.h, whether the machine has the commit the game needs.
With the game executable's path it also runs the executable's --probe-memneed (runtime/memory.c mem_start_note: it
reads this machine's commit, prints the answer and starts nothing) three times: with mem_need_mb = 0 (not checked),
with 1 (any machine has it) and with a value no machine has, where the log's warning line must appear.
Usage: mem_need_check.py [PATH_TO_GAME_EXE]
"""
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tests' / 'diff'))
try:
from common import MSYS_BIN, compiler # noqa: E402
except ImportError: # a Python without the differential tests' packages: the same compiler choice without them
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
def compiler():
if os.environ.get('CC'):
return os.environ['CC']
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
return str(MSYS_BIN / 'clang.exe')
return shutil.which('clang') or 'clang'
def main():
work = ROOT / 'build' / 'test' / 'mem_need'
work.mkdir(parents=True, exist_ok=True)
exe = work / ('mem_need_check.exe' if os.name == 'nt' else 'mem_need_check')
env = dict(os.environ)
if os.name == 'nt' and MSYS_BIN.exists():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
subprocess.check_call([compiler(), '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'),
str(ROOT / 'tests' / 'mem_need_check.c'), '-o', str(exe)], env=env)
rc = subprocess.run([str(exe)], env=env).returncode
if len(sys.argv) > 1:
rc |= probe(sys.argv[1])
return rc
def probe(game):
"""The executable's --probe-memneed under three settings; 0 when each answers as it must."""
fails = 0
for need, answer, warns in (('0', 'not checked', False), ('1', 'enough', False), ('1048576', 'short', True)):
env = dict(os.environ, NX2NAVI_MEM_NEED_MB=need)
r = subprocess.run([game, '--probe-memneed'], env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
text=True, errors='replace')
lines = r.stdout.splitlines()
said = [ln for ln in lines if ln.startswith('probe-memneed:')]
warned = [ln for ln in lines if 'mem: WARNING: this machine has' in ln]
print('\n'.join(said + [ln[:160] + ' ...' for ln in warned]))
# without a commit limit (a Linux machine with none) nothing is short: the forced case cannot be shown there
if said and said[0].endswith('no limit known'):
continue
if r.returncode or len(said) != 1 or not said[0].endswith(': ' + answer) or bool(warned) != warns:
fails += 1
print('mem_need_check: FAIL: --probe-memneed with mem_need_mb = %s (exit code %d)' % (need, r.returncode))
print('mem_need_check: the executable\'s answer %s' % ('FAILED' if fails else 'passed'))
return 1 if fails else 0
if __name__ == '__main__':
sys.exit(main())

View File

@@ -9,17 +9,21 @@ import subprocess
import tempfile
ROOT = Path(__file__).resolve().parents[1]
HARNESS = r'''
# What both harnesses stand in for the rest of the runtime with.
STUBS = r'''
#include <assert.h>
#include <stdio.h>
#include "mouse_direct.c"
static int raw_x, raw_y, takes, captured=1, foreground=1, allowed=1, mode=1, patched=1, selecting;
static float ratio=0.5f;
static float ratio=0.5f, frames=1, speed=1;
static double sens=1;
static uint64_t now=1000000000;
static GuestFn hook, activity_hook, begin_hook, angles_hook, hand_hook;
const char *cfg_get(const char *key) { (void)key; return mode ? "direct" : "stick"; }
const char *cfg_get(const char *key) { return strcmp(key, "NX2NAVI_MOUSE_MODE") ? NULL : mode ? "direct" : "stick"; }
const char *cfg_value(const char *key) { return cfg_get(key); }
double cfg_double(const char *key) { (void)key; return 1; }
double cfg_double(const char *key) { (void)key; return sens; }
float frame_step_frames(void) { return frames; }
float cam_speed_applied(void) { return speed; }
int rt_patch_set(const char *s) { (void)s; return 0; }
int rt_patch_enabled(int s) { (void)s; return patched; }
int ikb_captured(void) { return captured; }
@@ -44,8 +48,12 @@ void plat_mutex_lock(PlatMutex *m) { (void)m; }
void plat_mutex_unlock(PlatMutex *m) { (void)m; }
void rt_log(const char *fmt, ...) { (void)fmt; }
static float scalar(__uint128_t v) { float f; memcpy(&f,&v,4); return f; }
'''
HARNESS = STUBS + r'''
const int rt_game_version = 143;
int main(void) {
assert(hook && activity_hook);
assert(hook && activity_hook && begin_hook && angles_hook && hand_hook);
Cpu c, expected;
unsigned char target[0x500] = {0};
memset(&c, 0x5a, sizeof c);
@@ -208,6 +216,82 @@ int main(void) {
}
'''
# Ocarina 1.1.1 (recomp/patches-v111.txt, set mouse_direct): the two sites of camera_input_oot and camera_activity_oot.
OOT_HARNESS = STUBS + r'''
const int rt_game_version = 111;
int main(void) {
assert(hook && activity_hook && !begin_hook && !angles_hook && !hand_hook); // no orbit or held-object site
Cpu c, expected;
unsigned char target[0x600] = {0};
memset(&c, 0x5a, sizeof c);
c.x[20]=(uintptr_t)target; c.x[21]=0; c.v[1]=c.v[0]=0;
expected=c;
// s1 and s0 carry the processed x and y. 100 counts to the right and 50 up, at one frame per update.
raw_x=100; raw_y=-50;
hook(&c);
assert(fabsf(scalar(c.v[1])-100*(float)INPUT_OOT_COUNT_X)<1e-5f);
assert(fabsf(scalar(c.v[0])-50*(float)INPUT_OOT_COUNT_Y)<1e-5f);
// The displaced store of s1 at +0x538 carries the mouse's part; the store of s0 at +0x53c is the game's own.
float stored; memcpy(&stored,target+0x538,4); assert(stored==scalar(c.v[1]));
memcpy(&stored,target+0x53c,4); assert(stored==0);
expected.v[1]=c.v[1]; expected.v[0]=c.v[0]; assert(!memcmp(&expected,&c,sizeof c));
assert(input_direct_mouse_active() && input_direct_mouse_available());
// The next update sees no repeated movement.
expected=c; hook(&c); assert(!memcmp(&expected,&c,sizeof c));
// The game multiplies the processed stick by the frames it steps and by its camera speed: an equal mouse distance
// gives an equal turn at 30, 60 and 144 fps and at every stick camera speed, and nothing clamps a fast flick.
float steps[]={2,1,60/144.0f}, speeds[]={1,3};
for(int i=0;i<3;i++) for(int j=0;j<2;j++) {
frames=steps[i]; speed=speeds[j]; raw_x=10000; raw_y=10000; c.v[1]=c.v[0]=0; hook(&c);
assert(fabsf(scalar(c.v[1])*frames*speed-10000*(float)INPUT_OOT_COUNT_X)<0.01f);
assert(fabsf(scalar(c.v[0])*frames*speed+10000*(float)INPUT_OOT_COUNT_Y)<0.01f);
}
frames=speed=1;
// One count is not discarded by a dead zone, and the sensitivity scales it.
raw_x=1; raw_y=0; c.v[1]=c.v[0]=0; hook(&c); assert(scalar(c.v[1])>0);
float one=scalar(c.v[1]);
sens=2.5; raw_x=1; c.v[1]=c.v[0]=0; hook(&c); assert(fabsf(scalar(c.v[1])-2.5f*one)<1e-6f);
sens=0; raw_x=1; c.v[1]=c.v[0]=0; hook(&c); assert(c.v[1]==0 && c.v[0]==0); sens=1;
// The stick's own processed value stays and the mouse adds to it.
{ float px=0.25f, py=-0.5f; uint32_t b; memcpy(&b,&px,4); c.v[1]=b; memcpy(&b,&py,4); c.v[0]=b; }
raw_x=10; raw_y=0; hook(&c);
assert(fabsf(scalar(c.v[1])-(0.25f+10*(float)INPUT_OOT_COUNT_X))<1e-6f && scalar(c.v[0])==-0.5f);
// The idle bit of the flags (w8 at the second site) is cleared for an update the mouse moved in; the site's
// displaced ldp d9, d8, [sp, #0x10] is performed. The game's next instruction stores w8, so memory is not touched.
uint64_t stack[4]={0,0,0x123456789abcdef0ull,0x456789abcdef0123ull}; c.sp=(uintptr_t)stack;
uint32_t idle=0x38, mem=0x77; memcpy(target+0x530,&mem,4);
c.x[8]=idle; activity_hook(&c);
assert((uint32_t)c.x[8]==0x28);
uint32_t after; memcpy(&after,target+0x530,4); assert(after==mem);
assert(c.v[9]==stack[2] && c.v[8]==stack[3]);
// A controller-only update keeps the game's idle bit.
c.v[1]=c.v[0]=0; hook(&c); c.x[8]=idle; activity_hook(&c); assert((uint32_t)c.x[8]==idle);
// While the pause after a camera reset runs (+0x5e0) or camera input is off (w21 bit 0), the game reads no stick:
// the counts are left where they are and the registers as the game made them.
float pause=12; memcpy(target+0x5e0,&pause,4);
raw_x=42; int before=takes; c.v[1]=c.v[0]=0; expected=c; hook(&c);
assert(takes==before && raw_x==42 && !memcmp(&expected,&c,sizeof c));
assert(!input_direct_mouse_active());
pause=0; memcpy(target+0x5e0,&pause,4);
c.x[21]=1; hook(&c);
assert(takes==before && raw_x==42 && !input_direct_mouse_active()); c.x[21]=0;
selecting=1; hook(&c); assert(takes==before && raw_x==42); selecting=0;
// Focus loss and host menus do not move the camera.
captured=0; hook(&c); assert(takes==before); captured=1;
foreground=0; hook(&c); assert(takes==before); foreground=1;
allowed=0; hook(&c); assert(takes==before); allowed=1;
// Stick emulation: the counts stay for the stick path.
mode=0; hook(&c);
assert(takes==before && raw_x==42 && !input_direct_mouse_active()); mode=1;
patched=0; hook(&c);
assert(takes==before && !input_direct_mouse_active() && !input_direct_mouse_available()); patched=1;
raw_x=0; hook(&c); assert(input_direct_mouse_active());
now+=150000001; assert(!input_direct_mouse_active());
puts("direct mouse 1.1.1: register contract, displaced store and load, equal turn at every frame rate and stick speed passed");
puts("direct mouse 1.1.1: single count, sensitivity, stick kept, idle bit, reset pause, capture and mode checks passed");
}
'''
INPUT_HARNESS = r'''
#include <assert.h>
#include "input.c"
@@ -216,6 +300,11 @@ InputMap input_map;
// Other input entry points are linked by PE unwind metadata; reaching them is outside this script-only test.
void ikb_cursor_update(void) { abort(); }
void ikb_menu_closed(void) { abort(); }
// The gamepad thread's (sdl_enter, probe_gamepad): not reached by a script.
void plat_wait_ns(uint64_t ns) { (void)ns; abort(); }
int plat_mutex_trylock(PlatMutex *m) { (void)m; abort(); }
int cfg_setenv(const char *name, const char *value) { (void)name; (void)value; abort(); }
const char *SDL_GetGamepadName(SDL_Gamepad *g) { (void)g; abort(); }
int cfg_bool(const char *key) { (void)key; abort(); }
int input_menu_gate(int *h,int open,uint64_t *b,int32_t s[4]) { (void)h;(void)open;(void)b;(void)s;abort(); }
int input_overlay_gate(int *h,uint64_t *b) { (void)h;(void)b;abort(); }
@@ -292,12 +381,13 @@ def main():
folder = Path(tmp)
(folder / 'patches.h').write_text('#define RT_PATCH_PAGE 0x7003adf000ull\n')
source = folder / 'check.c'
source.write_text(HARNESS)
exe = folder / ('check.exe' if os.name == 'nt' else 'check')
link = [] if os.name == 'nt' else ['-lm']
subprocess.run([args.cc, '-O2', '-Wall', '-Wextra', '-I', str(folder), '-I', str(ROOT/'runtime'),
str(source), *link, '-o', str(exe)], check=True)
subprocess.run([str(exe)], check=True)
for harness in (HARNESS, OOT_HARNESS):
source.write_text(harness)
subprocess.run([args.cc, '-O2', '-Wall', '-Wextra', '-I', str(folder), '-I', str(ROOT/'runtime'),
str(source), *link, '-o', str(exe)], check=True)
subprocess.run([str(exe)], check=True)
if args.deps:
source.write_text(INPUT_HARNESS)
script = folder / 'input.txt'

61
tests/mouse_stick_check.c Normal file
View File

@@ -0,0 +1,61 @@
// The mouse camera in stick mode (runtime/input_map.c input_mouse_stick): raw counts to a right-stick deflection with
// the camera speed taken out of it. The game multiplies the right stick's turn rates by the camera speed, so the
// deflection is divided by it, and the division comes before the limit to full tilt: a slow movement turns as far as
// at camera speed 1, and a fast flick reaches full tilt, which the game turns into its top speed with the factor in
// it. No game files. tests/mouse_stick_check.py builds and runs this with runtime/input_map.c.
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "input_map.h"
// what input_map.c needs from the rest of the runtime; none of it is reached from input_mouse_stick
const char *cfg_get(const char *key) { (void)key; return NULL; }
double cfg_double(const char *key) { (void)key; return 0; }
void rt_log(const char *fmt, ...) { (void)fmt; }
#define FULL 32767
static int fails;
#define CHECK(cond, ...) do { if (!(cond)) { printf("FAIL: " __VA_ARGS__); putchar('\n'); fails++; } } while (0)
int main(void) {
static const float cams[] = {0.1f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f};
int32_t st[2], one[2];
for (unsigned i = 0; i < sizeof cams / sizeof *cams; i++) {
float cam = cams[i];
// a slow movement: the deflection is the one at camera speed 1 divided by the factor (while that is below full
// tilt), so the game's multiplication gives the same turn
for (int counts = 1; counts <= 40; counts += 3) {
input_mouse_stick(counts, 0, 1.0 / 60, 1, 1, one);
input_mouse_stick(counts, 0, 1.0 / 60, 1, cam, st);
double at_one = FULL * (counts * 60.0) / 3000.0; // full tilt at 3000 counts a second
double want = at_one / cam > FULL ? FULL : at_one / cam;
CHECK(fabs(one[0] - at_one) <= 0.5, "camera speed 1, %d counts: %d, expected %.1f", counts, one[0], at_one);
CHECK(fabs(st[0] - want) <= 0.5 && st[1] == 0, "camera speed %.1f, %d counts: %d, expected %.1f", cam, counts,
st[0], want);
}
// a fast flick reaches full tilt at every camera speed, in each direction
input_mouse_stick(5000, 0, 1.0 / 60, 1, cam, st);
CHECK(st[0] == FULL && st[1] == 0, "camera speed %.1f: a flick right gives %d %d", cam, st[0], st[1]);
input_mouse_stick(0, 5000, 1.0 / 60, 1, cam, st);
CHECK(st[0] == 0 && st[1] == -FULL, "camera speed %.1f: a flick down gives %d %d", cam, st[0], st[1]);
input_mouse_stick(-5000, -5000, 1.0 / 60, 1, cam, st);
double len = sqrt((double)st[0] * st[0] + (double)st[1] * st[1]);
CHECK(st[0] < 0 && st[1] > 0 && st[0] == -st[1] && len <= FULL + 1.0 && len >= FULL - 2.0,
"camera speed %.1f: a diagonal flick gives %d %d", cam, st[0], st[1]);
}
// the case that was wrong: at camera speed 1.5 a flick stopped at two thirds of full tilt, the top speed of camera
// speed 1
input_mouse_stick(80, 0, 1.0 / 60, 1, 1.5f, st);
CHECK(st[0] == FULL, "camera speed 1.5, 80 counts in 1/60 s: %d, expected full tilt", st[0]);
input_mouse_stick(30, 0, 1.0 / 60, 1, 1.5f, st);
CHECK(st[0] == 13107, "camera speed 1.5, 30 counts in 1/60 s: %d, expected 13107", st[0]);
// nothing to divide by: no deflection
input_mouse_stick(30, 0, 1.0 / 60, 1, 0, st);
CHECK(st[0] == 0 && st[1] == 0, "camera speed 0 gives %d %d", st[0], st[1]);
input_mouse_stick(30, 0, 1.0 / 60, 1, -1, st);
CHECK(st[0] == 0 && st[1] == 0, "a negative camera speed gives %d %d", st[0], st[1]);
if (!fails) puts("mouse stick: the camera speed divides the deflection before the limit to full tilt");
return fails != 0;
}

View File

@@ -0,0 +1,35 @@
"""Build and run tests/mouse_stick_check.c with runtime/input_map.c: the mouse camera's stick deflection at each camera
speed. No game files.
Usage: python tests/mouse_stick_check.py [--cc <clang>]
"""
import argparse
import os
import subprocess
import sys
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
def main():
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument('--cc', help='the C compiler (default: MSYS2 UCRT64 clang on Windows, clang elsewhere)')
args = ap.parse_args()
win = os.name == 'nt'
cc = args.cc or (str(MSYS_BIN / 'clang.exe') if win and MSYS_BIN.exists() else 'clang')
env = dict(os.environ)
if win and MSYS_BIN.exists():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
with tempfile.TemporaryDirectory(prefix='nx2navi-mouse-stick-') as tmp:
exe = Path(tmp) / ('check.exe' if win else 'check')
subprocess.check_call([cc, '-O2', '-Wall', '-fno-strict-aliasing', '-I', str(ROOT / 'runtime'),
str(ROOT / 'tests' / 'mouse_stick_check.c'), str(ROOT / 'runtime' / 'input_map.c'),
*([] if win else ['-lm']), '-o', str(exe)], env=env)
return subprocess.run([str(exe)], env=env).returncode
if __name__ == '__main__':
sys.exit(main())

View File

@@ -0,0 +1,114 @@
// runtime/vk_cache_file.h: the read of pipeline-cache.bin (runtime/vk_pipeline.c cache_load) with 64-bit sizes. Makes a
// sparse file just over 2 GB in the folder given as argv[1], with a marker at its start and at its end, and checks that
// it is measured and read whole, that a size limit refuses it by its real size without reading it, and the small cases.
// Built and run by tests/pipeline_cache_file_check.py; prints each failed check and exits 1 on any.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "vk_cache_file.h"
#ifdef _WIN32
#include <windows.h>
#include <winioctl.h>
#else
#include <fcntl.h>
#include <unistd.h>
#endif
static int fails;
#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
#define HEADER 32 // the size of the header every pipeline cache starts with (VkPipelineCacheHeaderVersionOne)
// A file of len bytes: head at its start, tail as its last bytes, a hole between them that takes no disk space.
static int make_sparse(const char *path, uint64_t len, const char *head, const char *tail) {
size_t hn = strlen(head), tn = strlen(tail);
#ifdef _WIN32
HANDLE h = CreateFileA(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE) return 0;
DWORD got = 0;
LARGE_INTEGER at = {.QuadPart = (LONGLONG)(len - tn)};
int ok = DeviceIoControl(h, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &got, NULL) &&
WriteFile(h, head, (DWORD)hn, &got, NULL) && got == hn && SetFilePointerEx(h, at, NULL, FILE_BEGIN) &&
WriteFile(h, tail, (DWORD)tn, &got, NULL) && got == tn;
CloseHandle(h);
return ok;
#else
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0) return 0;
int ok = write(fd, head, hn) == (ssize_t)hn && pwrite(fd, tail, tn, (off_t)(len - tn)) == (ssize_t)tn;
close(fd);
return ok;
#endif
}
int main(int argc, char **argv) {
if (argc < 2) return printf("usage: pipeline_cache_file_check <folder>\n"), 2;
char path[1024];
uint64_t len = 0;
int why = -1;
// no file: a first run, which is not a refusal
snprintf(path, sizeof path, "%s/none.bin", argv[1]);
remove(path);
CHECK(!cache_file_read(path, HEADER, 0, &len, &why) && why == CACHE_FILE_NONE && len == 0, "no file: why %d", why);
// a file of a header alone holds no pipeline: refused with its size
snprintf(path, sizeof path, "%s/short.bin", argv[1]);
FILE *f = fopen(path, "wb");
for (int i = 0; f && i < HEADER; i++) fputc(i, f);
if (f) fclose(f);
CHECK(!cache_file_read(path, HEADER, 0, &len, &why) && why == CACHE_FILE_SHORT && len == HEADER,
"header only: why %d, len %llu", why, (unsigned long long)len);
remove(path);
// a small file: read whole
f = fopen(path, "wb");
for (int i = 0; f && i < 4096; i++) fputc(i & 0xff, f);
if (f) fclose(f);
uint8_t *d = cache_file_read(path, HEADER, 1 << 20, &len, &why);
CHECK(d && why == CACHE_FILE_OK && len == 4096 && d[4095] == 0xff && d[257] == 1, "small file: why %d, len %llu", why,
(unsigned long long)len);
free(d);
remove(path);
// just over 2 GB: a 32-bit tell measures it as negative
const uint64_t big = (2ull << 30) + 4096;
snprintf(path, sizeof path, "%s/big.bin", argv[1]);
if (!make_sparse(path, big, "HEAD-OF-THE-CACHE", "TAIL-OF-THE-CACHE")) {
printf("FAIL: cannot make the sparse file %s\n", path);
remove(path);
return 1;
}
f = fopen(path, "rb");
fseek(f, 0, SEEK_END);
long l32 = ftell(f);
fclose(f);
printf("pipeline cache file: %llu bytes; ftell into a long (%d bits here) gives %ld\n", (unsigned long long)big,
(int)sizeof(long) * 8, l32);
// under a limit: refused before any read, and its real size comes back for the log line
d = cache_file_read(path, HEADER, 512ull << 20, &len, &why);
CHECK(!d && why == CACHE_FILE_OVER_CAP && len == big, "over the limit: why %d, len %llu", why, (unsigned long long)len);
CHECK(strlen(cache_file_why(why)) > 0, "a refusal has words for the log");
free(d);
// with no limit, and with a limit above its size: read whole
for (int pass = 0; pass < 2; pass++) {
d = cache_file_read(path, HEADER, pass ? 4096ull << 20 : 0, &len, &why);
CHECK(d && why == CACHE_FILE_OK && len == big, "pass %d: over 2 GB: why %d (%s), len %llu", pass, why,
cache_file_why(why), (unsigned long long)len);
if (d) {
CHECK(!memcmp(d, "HEAD-OF-THE-CACHE", 17), "pass %d: the start of the file is the start of the block", pass);
CHECK(!memcmp(d + big - 17, "TAIL-OF-THE-CACHE", 17), "pass %d: the last bytes are past 2 GB in the block", pass);
CHECK(d[1ull << 31] == 0 && d[(1ull << 30) + 5] == 0, "pass %d: the hole reads as zeros", pass);
}
free(d);
}
remove(path);
if (!fails) printf("pipeline cache file: all checks passed\n");
return fails != 0;
}

View File

@@ -0,0 +1,38 @@
"""Build and run tests/pipeline_cache_file_check.c: runtime/vk_cache_file.h, the read of pipeline-cache.bin with 64-bit
sizes. The test makes a sparse file just over 2 GB in build/test/pipeline_cache_file (it takes no disk space, and about
2 GB of memory for the seconds the read lasts) and removes it.
Usage: pipeline_cache_file_check.py
"""
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
def compiler():
if os.environ.get('CC'):
return os.environ['CC']
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
return str(MSYS_BIN / 'clang.exe')
return shutil.which('clang') or 'clang'
def main():
work = ROOT / 'build' / 'test' / 'pipeline_cache_file'
work.mkdir(parents=True, exist_ok=True)
exe = work / ('pipeline_cache_file_check.exe' if os.name == 'nt' else 'pipeline_cache_file_check')
env = dict(os.environ)
if os.name == 'nt' and MSYS_BIN.exists():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
subprocess.check_call([compiler(), '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'),
str(ROOT / 'tests' / 'pipeline_cache_file_check.c'), '-o', str(exe)], env=env)
return subprocess.run([str(exe), str(work)], env=env, timeout=600).returncode
if __name__ == '__main__':
sys.exit(main())

132
tests/pipeline_list_test.py Normal file
View File

@@ -0,0 +1,132 @@
"""tools/pipeline_list.py on made-up game folders: records folded once, a key whose shader the folder's answers do not
name left out, --mesh-only, a folder of another record layout left out, the file read back, and what
tools/prepare_shaders.py takes from a list for its seed. Also tools/check_content.py refusing such files in a commit.
No game files; stdlib only.
Run: python tests/pipeline_list_test.py
"""
import hashlib
import struct
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT / 'tools'))
import check_content # noqa: E402
import pipeline_list as pl # noqa: E402
REC = 464 # a KeyRec of the runtime that wrote the test's files: three ids, two flags, the state
VS, FS, TS, MS = 0, 4, 6, 7
def h(seed):
return hashlib.sha256(seed.encode()).digest()
def answer(seed, stage, salt=0):
"""A shader-answers.bin record: stage, code hash, input id, 147 bytes of answers, the pad byte."""
return struct.pack('<I', stage) + h('code ' + seed) + h('id ' + seed) + bytes([salt]) * 147 + b'\0'
def key(vs, fs=None, ts=None, state=0):
ids = h('id ' + vs) + (h('id ' + fs) if fs else bytes(32)) + (h('id ' + ts) if ts else bytes(32))
return ids + struct.pack('<II', fs is not None, ts is not None) + bytes([state]) * (REC - 104)
def folder(root, name, keys, answers, version=4, rec=REC):
d = root / name
d.mkdir()
(d / 'pipeline-keys.bin').write_bytes(b'NX2NPKEY' + struct.pack('<II', version, rec) + b''.join(keys))
(d / 'shader-answers.bin').write_bytes(pl.ANS_MAGIC + struct.pack('<II', 1, pl.ANS_REC) + b''.join(answers))
return d
class PipelineList(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.root = Path(self.tmp.name)
self.log = []
def tearDown(self):
self.tmp.cleanup()
def merge(self, dirs, **kw):
return pl.merge(dirs, log=self.log.append, **kw)
def test_merge(self):
a = folder(self.root, 'a', [key('v1', 'f1'), key('m1', 'f2', 't1'), key('v1', 'f1', state=1)],
[answer('v1', VS), answer('f1', FS), answer('m1', MS), answer('f2', FS), answer('t1', TS)])
b = folder(self.root, 'b', [key('v1', 'f1'), key('m2', 'f1'), key('v9', 'f1')],
[answer('v1', VS), answer('f1', FS), answer('m2', MS)]) # v9's answers are not recorded there
header, keys, answers = self.merge([a, b])
self.assertEqual(header, (b'NX2NPKEY', 4, REC))
self.assertEqual(keys, [key('v1', 'f1'), key('m1', 'f2', 't1'), key('v1', 'f1', state=1), key('m2', 'f1')])
self.assertEqual({x[36:68] for x in answers}, {h('id ' + s) for s in ('v1', 'f1', 'm1', 'f2', 't1', 'm2')})
self.assertIn('1 without their shaders\' answers', self.log[1])
def test_mesh_only(self):
a = folder(self.root, 'a', [key('v1', 'f1'), key('m1', 'f2', 't1'), key('m1', None)],
[answer('v1', VS), answer('f1', FS), answer('m1', MS), answer('f2', FS), answer('t1', TS)])
_, keys, answers = self.merge([a], mesh_only=True)
self.assertEqual(keys, [key('m1', 'f2', 't1'), key('m1', None)])
self.assertEqual({x[36:68] for x in answers}, {h('id ' + s) for s in ('m1', 'f2', 't1')})
def test_other_layout_left_out(self):
a = folder(self.root, 'a', [key('v1', 'f1')], [answer('v1', VS), answer('f1', FS)])
b = folder(self.root, 'b', [key('v2', 'f1')], [answer('v2', VS), answer('f1', FS)], version=5)
_, keys, _ = self.merge([a, b])
self.assertEqual(keys, [key('v1', 'f1')])
self.assertIn('another layout', self.log[1])
self.assertIsNone(self.merge([self.root / 'none']))
def test_file(self):
a = folder(self.root, 'a', [key('v1', 'f1'), key('m1', 'f1')],
[answer('v1', VS), answer('f1', FS), answer('m1', MS)])
header, keys, answers = self.merge([a])
data = pl.pack(header, keys, answers)
got_answers, image = pl.unpack(data)
self.assertEqual(got_answers, answers)
self.assertEqual(image, (a / 'pipeline-keys.bin').read_bytes()) # the layout the runtime reads as it is
self.assertEqual(pl.read_keys(image)[3], keys)
self.assertIsNone(pl.unpack(data[:-1]))
self.assertIsNone(pl.unpack(b'NX2NPKEY' + data[8:]))
self.assertIsNone(pl.unpack(b''))
def test_seed_variants(self):
"""prepare_shaders.list_variants: variants of shaders the game version has, once, with the id left to the
runtime."""
try:
import prepare_shaders as ps
except ImportError as e: # its shader inventory needs Python 3.14's compression.zstd
self.skipTest(f'tools/prepare_shaders.py does not import here: {e}')
recs = [answer('v1', VS), answer('f1', FS), answer('gone', FS), answer('v1', VS, salt=1)]
have = {h('code v1'), h('code f1')}
seen = {(h('code f1'), bytes(147))} # the seed has f1's variant already (its default one, say)
out, absent = ps.list_variants(recs, have, seen)
self.assertEqual(absent, 1)
self.assertEqual(len(out), 2)
for r in out:
self.assertEqual(len(r), pl.ANS_REC)
self.assertEqual(r[4:36], h('code v1'))
self.assertEqual(r[36:68], bytes(32))
self.assertEqual(r[-1], ps.LIST_PRIORITY)
self.assertEqual([r[68] for r in out], [0, 1])
self.assertEqual(ps.list_variants(recs, have, seen), ([], 1)) # all in the seed now
def test_refused_in_a_commit(self):
a = folder(self.root, 'a', [key('v1', 'f1')], [answer('v1', VS), answer('f1', FS)])
lst = self.root / 'pipeline_list.bin'
lst.write_bytes(pl.pack(*self.merge([a])))
for p in (lst, a / 'pipeline-keys.bin', a / 'shader-answers.bin'):
self.assertTrue(check_content.recorded_in_play(p, None), p.name)
self.assertTrue(check_content.recorded_in_play(p, p.read_bytes()), p.name)
other = self.root / 'notes.txt'
other.write_text('NX2N is the prefix of the recorded files\n')
self.assertFalse(check_content.recorded_in_play(other, None))
self.assertFalse(check_content.recorded_in_play(self.root / 'missing.bin', None))
if __name__ == '__main__':
unittest.main()

View File

@@ -0,0 +1,82 @@
// plat_restart on Linux (runtime/platform/linux.c): the game it starts may run on every processor this one could
// before plat_cpu_reserve took a core for the recording thread, whichever thread asks for the restart. No game files.
// Build and run from the repository root:
// clang -O2 -ffunction-sections -fdata-sections -Iruntime -c runtime/platform/linux.c -o /tmp/plat.o
// clang -O2 -Wall -Iruntime tests/platform_restart_mask_test.c /tmp/plat.o -Wl,--gc-sections -lpthread -ldl -o /tmp/restart_mask
// /tmp/restart_mask (exit 77: this machine has no core plat_cpu_reserve would take)
// The test starts itself again through plat_restart; that copy only writes how many processors it may run on.
#define _GNU_SOURCE
#include <pthread.h>
#include <sched.h>
#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "platform/platform.h"
#define OUT "RESTART_MASK_TEST_OUT"
static int allowed(void) {
cpu_set_t set;
return sched_getaffinity(0, sizeof set, &set) ? -1 : CPU_COUNT(&set);
}
// What the copy plat_restart started wrote, waiting up to 5 s for it; -1 when it never came.
static int restarted_count(const char *path) {
for (int i = 0; i < 500; i++) {
FILE *f = fopen(path, "r");
int n = -1;
if (f) {
int got = fscanf(f, "%d", &n);
fclose(f);
if (got == 1) {
remove(path);
return n;
}
}
usleep(10000);
}
return -1;
}
static atomic_int go, done;
// A thread that existed before the core was taken, as the game's main thread does: it asks for the restart.
static void *other_thread(void *arg) {
(void)arg;
while (!atomic_load(&go)) usleep(1000);
plat_restart();
atomic_store(&done, 1);
return NULL;
}
int main(void) {
const char *out = getenv(OUT);
if (getenv("NX2NAVI_RESTART_WAIT") && out) { // the copy plat_restart started
FILE *f = fopen(out, "w");
if (f) fprintf(f, "%d\n", allowed()), fclose(f);
return 0;
}
char path[64];
snprintf(path, sizeof path, "/tmp/restart-mask-%d.txt", (int)getpid());
setenv(OUT, path, 1);
remove(path);
int before = allowed(), fails = 0;
pthread_t th;
pthread_create(&th, NULL, other_thread, NULL);
char which[128];
int core = plat_cpu_reserve(which, sizeof which);
if (!core) { puts("SKIP: no core to reserve on this machine"); return 77; }
atomic_store(&go, 1);
while (!atomic_load(&done)) usleep(1000);
pthread_join(th, NULL);
int from_other = restarted_count(path);
plat_restart(); // and from the thread that holds the core
int from_holder = restarted_count(path);
printf("%d processors before; the core taken: %s; a restart from another thread starts with %d, from the holder with %d\n",
before, which, from_other, from_holder);
if (from_other != before) puts("FAIL: the restarted process lacks processors (asked by another thread)"), fails++;
if (from_holder != before) puts("FAIL: the restarted process lacks processors (asked by the holder)"), fails++;
if (!fails) puts("restart mask: the restarted process has every processor");
return fails != 0;
}

66
tests/prp_file_check.c Normal file
View File

@@ -0,0 +1,66 @@
// runtime/prp_file.h: the layout of shader-prepared.bin. Checks the parse of a version 1 list, of a version 2 list and
// of damaged ones, then writes a version 2 list into the folder given as argv[1] for tests/prp_file_check.py, which
// reads its head with tools/make_game.py prepared_from. Prints each failed check and exits 1 on any.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "prp_file.h"
static int fails;
#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
int main(int argc, char **argv) {
if (argc < 2) return printf("usage: prp_file_check <folder>\n"), 2;
uint8_t file[PRP_HEAD + PRP_FROM_SIZE + 3 * PRP_REC + 8];
PrpFrom from, got;
for (int i = 0; i < 32; i++) from.programs[i] = (uint8_t)i, from.answers[i] = (uint8_t)(0x40 + i), from.translator[i] = (uint8_t)(0x80 + i);
size_t n = 99;
int has = 9;
// version 2: the head, then three records
size_t head = prp_head_write(file, &from);
CHECK(head == PRP_HEAD + PRP_FROM_SIZE && head == 112, "the head is %zu bytes", head);
for (int r = 0; r < 3; r++) {
uint8_t *rec = file + head + r * PRP_REC;
memset(rec, 0, PRP_REC);
rec[0] = (uint8_t)(r + 1), rec[1] = (uint8_t)r, memset(rec + 4, 0xa0 + r, 32);
}
const uint8_t *recs = prp_parse(file, head + 3 * PRP_REC, &n, &got, &has);
CHECK(recs == file + head && n == 3 && has == 1, "version 2: %zu records, has_from %d", n, has);
CHECK(!memcmp(&got, &from, sizeof from), "version 2: what it was prepared from comes back");
CHECK(recs && recs[PRP_REC * 2] == 3 && recs[PRP_REC * 2 + 4] == 0xa2, "version 2: the third record");
// a cut last record is not counted
recs = prp_parse(file, head + 2 * PRP_REC + 35, &n, &got, &has);
CHECK(recs && n == 2, "a cut record: %zu", n);
// the head alone, or less: no list
CHECK(!prp_parse(file, head, &n, &got, &has) && n == 0, "no records");
CHECK(!prp_parse(file, head - 1, &n, &got, &has) && n == 0, "a cut head");
CHECK(!prp_parse(NULL, 0, &n, &got, &has), "no file");
// version 1: records right after the 16 bytes, and nothing about where they came from
uint8_t v1[PRP_HEAD + 2 * PRP_REC];
uint32_t one = 1, rec = PRP_REC;
memcpy(v1, PRP_MAGIC, 8), memcpy(v1 + 8, &one, 4), memcpy(v1 + 12, &rec, 4);
memset(v1 + PRP_HEAD, 0x11, 2 * PRP_REC);
recs = prp_parse(v1, sizeof v1, &n, &got, &has);
CHECK(recs == v1 + PRP_HEAD && n == 2 && has == 0, "version 1: %zu records, has_from %d", n, has);
// another version, record size or magic: not read
uint32_t three = 3, other = 40;
memcpy(v1 + 8, &three, 4);
CHECK(!prp_parse(v1, sizeof v1, &n, &got, &has), "version 3");
memcpy(v1 + 8, &one, 4), memcpy(v1 + 12, &other, 4);
CHECK(!prp_parse(v1, sizeof v1, &n, &got, &has), "another record size");
memcpy(v1 + 12, &rec, 4), v1[0] = 'X';
CHECK(!prp_parse(v1, sizeof v1, &n, &got, &has), "another magic");
char path[1024];
snprintf(path, sizeof path, "%s/shader-prepared.bin", argv[1]);
FILE *f = fopen(path, "wb");
CHECK(f && fwrite(file, 1, head + 3 * PRP_REC, f) == head + 3 * PRP_REC, "cannot write %s", path);
if (f) fclose(f);
if (!fails) printf("prepared list file: all C checks passed\n");
return fails != 0;
}

53
tests/prp_file_check.py Normal file
View File

@@ -0,0 +1,53 @@
"""Build and run tests/prp_file_check.c: runtime/prp_file.h, the layout of shader-prepared.bin with what the list was
prepared from. The list the C side writes is then read by tools/make_game.py prepared_from, so the game's writer and
the build's reader cannot drift apart. No game files, no GPU.
Usage: prp_file_check.py
"""
import importlib.util
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
def compiler():
if os.environ.get('CC'):
return os.environ['CC']
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
return str(MSYS_BIN / 'clang.exe')
return shutil.which('clang') or 'clang'
def main():
work = ROOT / 'build' / 'test' / 'prp_file'
work.mkdir(parents=True, exist_ok=True)
exe = work / ('prp_file_check.exe' if os.name == 'nt' else 'prp_file_check')
env = dict(os.environ)
if os.name == 'nt' and MSYS_BIN.exists():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
subprocess.check_call([compiler(), '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'),
str(ROOT / 'tests' / 'prp_file_check.c'), '-o', str(exe)], env=env)
rc = subprocess.run([str(exe), str(work)], env=env).returncode
if rc:
return rc
sys.path.insert(0, str(ROOT / 'tools'))
spec = importlib.util.spec_from_file_location('make_game', ROOT / 'tools' / 'make_game.py')
mg = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mg)
want = {'programs': bytes(range(32)).hex(), 'answers': bytes(range(0x40, 0x60)).hex(),
'translator': bytes(range(0x80, 0xa0)).hex()}
got = mg.prepared_from(work)
if got != want:
print(f'FAIL: tools/make_game.py prepared_from reads {got}, the game wrote {want}')
return 1
print('prepared list file: tools/make_game.py reads what the game writes')
return 0
if __name__ == '__main__':
sys.exit(main())

65
tests/save_retry_check.c Normal file
View File

@@ -0,0 +1,65 @@
// runtime/vk_save_retry.h: when the autosave thread saves the pipeline caches and when it tries again after a save
// that did not happen (runtime/vk_pipeline.c autosave_once). Built and run by tests/save_retry_check.py; prints each
// failed check and exits 1 on any.
#include <stdio.h>
#include "vk_save_retry.h"
static int fails;
#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
int main(void) {
uint64_t t = 1000000; // ms since the machine started, as the thread's clock gives them
// Nothing built, nothing owed: no save, however long the run.
SaveRetry s = {0};
CHECK(!save_due(&s, 0, t, 0), "a save with nothing new in the caches");
CHECK(!save_due(&s, 0, t + 3600000, 0), "a save after an hour with nothing new");
// A new pipeline: a save, and after it none until the next one; never two within the interval.
CHECK(save_due(&s, 1, t, 0), "no save for a new pipeline");
CHECK(!save_tried(&s, 1, t, 1), "a good save counted as the last try");
CHECK(!save_due(&s, 2, t + SAVE_EVERY_MS - 1, 0), "two saves within the interval");
CHECK(save_due(&s, 2, t + SAVE_EVERY_MS, 0), "no save for the next pipeline once the interval passed");
save_tried(&s, 2, t + SAVE_EVERY_MS, 1);
CHECK(!save_due(&s, 2, t + 10 * SAVE_EVERY_MS, 0), "a save again with nothing new after a good one");
// A save that did not happen is tried again at the interval with nothing new built, SAVE_RETRIES times; the
// last of them says so, and then nothing is tried until a new pipeline.
s = (SaveRetry){0};
uint64_t now = t;
CHECK(save_due(&s, 5, now, 0), "no first save");
CHECK(!save_tried(&s, 5, now, 0), "the first failed try counted as the last");
int tries = 0, last = 0;
for (int i = 0; i < 100 && !last; i++) {
CHECK(!save_due(&s, 5, now + SAVE_EVERY_MS - 1, 0), "a retry within the interval");
now += SAVE_EVERY_MS;
if (!save_due(&s, 5, now, 0)) break;
tries++;
last = save_tried(&s, 5, now, 0);
}
CHECK(tries == SAVE_RETRIES && last, "%d retries after a failed save, %d wanted, the last one said: %d", tries,
SAVE_RETRIES, last);
CHECK(!save_due(&s, 5, now + 100 * SAVE_EVERY_MS, 0), "a try after the last of the row with nothing new");
CHECK(save_due(&s, 6, now + SAVE_EVERY_MS, 0), "no try for a new pipeline after the row ended");
CHECK(!save_tried(&s, 6, now + SAVE_EVERY_MS, 0), "new content did not start a row of its own");
CHECK(save_due(&s, 6, now + 2 * SAVE_EVERY_MS, 0), "no retry in the new row");
// A retry that saves ends the row: nothing more until something new.
s = (SaveRetry){0};
save_tried(&s, 3, t, 0);
CHECK(save_due(&s, 3, t + SAVE_EVERY_MS, 0), "no retry after one failed save");
CHECK(!save_tried(&s, 3, t + SAVE_EVERY_MS, 1), "a good retry counted as the last try");
CHECK(!save_due(&s, 3, t + 2 * SAVE_EVERY_MS, 0), "a save again after a good retry");
// A save owed from the start (the play file folded in) is tried with nothing built, and bounded the same way.
s = (SaveRetry){0};
CHECK(save_due(&s, 0, t, 1), "no save for the one owed from the start");
now = t, tries = 1;
save_tried(&s, 0, now, 0);
while (tries < 100 && save_due(&s, 0, now += SAVE_EVERY_MS, 1)) tries++, save_tried(&s, 0, now, 0);
CHECK(tries == SAVE_RETRIES + 1, "%d tries of an owed save that keeps failing, %d wanted", tries, SAVE_RETRIES + 1);
printf("save_retry_check: %s\n", fails ? "FAILED" : "passed");
return fails ? 1 : 0;
}

40
tests/save_retry_check.py Normal file
View File

@@ -0,0 +1,40 @@
"""Build and run tests/save_retry_check.c: runtime/vk_save_retry.h, when the autosave thread saves the pipeline caches
and when it tries again after a save that did not happen (no copy of the data, a file that cannot be written).
Usage: save_retry_check.py
"""
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tests' / 'diff'))
try:
from common import MSYS_BIN, compiler # noqa: E402
except ImportError: # a Python without the differential tests' packages: the same compiler choice without them
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
def compiler():
if os.environ.get('CC'):
return os.environ['CC']
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
return str(MSYS_BIN / 'clang.exe')
return shutil.which('clang') or 'clang'
def main():
work = ROOT / 'build' / 'test' / 'save_retry'
work.mkdir(parents=True, exist_ok=True)
exe = work / ('save_retry_check.exe' if os.name == 'nt' else 'save_retry_check')
env = dict(os.environ)
if os.name == 'nt' and MSYS_BIN.exists():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
subprocess.check_call([compiler(), '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'),
str(ROOT / 'tests' / 'save_retry_check.c'), '-o', str(exe)], env=env)
return subprocess.run([str(exe)], env=env).returncode
if __name__ == '__main__':
sys.exit(main())

View File

@@ -60,8 +60,13 @@ NEVER(fs_init) NEVER(hostfn_override) NEVER(hostfn_register) NEVER(mem_kernel_al
NEVER(mem_thaw) NEVER(romfs_close) NEVER(romfs_entry) NEVER(romfs_entry_type) NEVER(romfs_file_count)
NEVER(romfs_handle) NEVER(romfs_image_path) NEVER(romfs_list) NEVER(romfs_open) NEVER(romfs_overlay)
NEVER(romfs_overlay_count) NEVER(romfs_read) NEVER(romfs_size) NEVER(rt_call)
NEVER(perf_size) // fs_host_sizes: only the perf trace's size lines call it
int mem_probing(void) { return 0; } // the write-back probe is off
int romfs_owns(const char *path) { (void)path; return 0; }
int mem_probing(void) { return 0; } // write_file asks before each write: no probe, no lazy heap here
void mem_kernel_access(uint64_t a, uint64_t n) { (void)a, (void)n; }
void fs_trace(const char *what, const char *path) { (void)what, (void)path; }
void coop_game_rom_open(const char *path) { (void)path; } // co-op watches for the title screen's start: not here
int main(int argc, char **argv) {
if (argc < 3) return 2;

View File

@@ -55,7 +55,7 @@ def main():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
rt = ROOT / 'runtime'
plat = ['platform/win32.c', 'platform/common.c'] if os.name == 'nt' else ['platform/linux.c', 'platform/common.c']
libs = ['-lbcrypt', '-lwinmm', '-lpsapi', '-lshell32', '-lole32', '-luser32'] if os.name == 'nt' else \
libs = ['-lbcrypt', '-lws2_32', '-lwinmm', '-lpsapi', '-lshell32', '-lole32', '-luser32'] if os.name == 'nt' else \
['-lpthread', '-ldl', '-lm']
subprocess.check_call([compiler(), '-std=gnu11', '-O1', '-g', '-w', '-I', str(rt), str(ROOT / 'tests' /
'save_share_check.c'), *(str(rt / f) for f in ('fs_host.c', 'save_share.c', 'save_zip.c',

View File

@@ -28,6 +28,9 @@ void ipc_handle_closed(int kind, void *obj) { (void)kind, (void)obj; }
const char *ldr_describe(uint64_t a, char *buf, int n) { snprintf(buf, (size_t)n, "%#llx", (unsigned long long)a); return buf; }
uint64_t ldr_symbol(const char *name) { (void)name; return 0; }
void perf_main_thread(void) {}
void perf_size(PerfSizes *s, const char *name, uint64_t count, uint64_t limit, uint64_t bytes) {
(void)s, (void)name, (void)count, (void)limit, (void)bytes; // kern_sizes is not called here
}
int rt_linked_modules(RtModule *out, int max) { (void)out, (void)max; return 0; }
const RtGuestLayout *mem_layout(void) { return NULL; }
void *mem_kernel_alloc(uint64_t size, uint64_t align) {

View File

@@ -39,7 +39,7 @@ def main():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
rt = ROOT / 'runtime'
plat = [rt / 'platform' / ('win32.c' if win else 'linux.c'), rt / 'platform' / 'common.c']
libs = ['-lbcrypt', '-lpsapi', '-lwinmm', '-ldbghelp', '-lshell32', '-lole32', '-luser32', '-ladvapi32'] if win \
libs = ['-lbcrypt', '-lws2_32', '-lpsapi', '-lwinmm', '-ldbghelp', '-lshell32', '-lole32', '-luser32', '-ladvapi32'] if win \
else ['-lpthread']
subprocess.check_call([compiler(), '-O1', '-g', '-Wall', '-Wno-unused-function', '-fno-strict-aliasing',
'-march=x86-64-v3', '-I', str(rt), '-I', str(work),

120
tests/target_frames_check.c Normal file
View File

@@ -0,0 +1,120 @@
// runtime/vk_target_frames.h: which passes count as rendered frame after frame, so that a draw into them is skipped
// while its pipeline builds instead of holding the replay thread. Built and run by tests/target_frames_check.py;
// prints each failed check and exits 1 on any.
// The frame numbers of the first block are those of a player's log: an area change after 238 frames of loading
// screen, where every pass read as one-shot and six mesh draws waited 52 to 660 ms each.
#include <stdio.h>
#include <string.h>
#include "vk_target_frames.h"
static int fails;
#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
static TfrTarget T(uint64_t image, uint32_t level, uint32_t layer, uint64_t owner) {
return (TfrTarget){image, level, layer, owner};
}
// The verdict with the steady rule and without it; *by: only the steady rule made it per-frame.
static int pass(const TfrTarget *c, int n, const TfrTarget *d, uint64_t stamp, int any, int steady, int *by) {
int b = 0, r = tfr_pass_recent(c, n, d, stamp, any, steady, &b);
if (by) *by = b;
return r;
}
int main(void) {
int by;
// ---- the area change of the log
TfrTarget scene = T(0x1000, 0, 0, 11), scene_d = T(0x1001, 0, 0, 12); // drawn every frame
TfrTarget probe = T(0x2000, 0, 0, 21), probe_d = T(0x2001, 0, 0, 22); // every second frame
TfrTarget probe_l3 = T(0x2000, 0, 3, 21); // a layer drawn every 15th frame
TfrTarget half = T(0x3000, 0, 0, 31), half_d = T(0x3001, 0, 0, 32); // the half-resolution buffers
for (uint64_t f = 16000; f <= 17925; f++) {
tfr_note(&scene, f), tfr_note(&scene_d, f), tfr_note(&half, f), tfr_note(&half_d, f);
if (f % 2) tfr_note(&probe, f), tfr_note(&probe_d, f);
if (f % 15 == 0) tfr_note(&probe_l3, f);
}
// the frame after: per-frame by being recent, with or without the rule
CHECK(pass(&scene, 1, &scene_d, 17926, 1, 0, NULL) == 1, "a pass drawn the frame before is per-frame");
CHECK(pass(&scene, 1, &scene_d, 17926, 0, 0, NULL) == 1, "strict: every target recent");
CHECK(pass(&scene, 1, &scene_d, 17926, 1, 1, &by) == 1 && !by, "recent passes are not counted as steady ones");
// 238 frames of loading screen later (its own targets are other images)
const uint64_t now = 18163;
TfrTarget load = T(0x9000, 0, 0, 91);
for (uint64_t f = 17926; f < now; f++) tfr_note(&load, f);
CHECK(pass(&scene, 1, &scene_d, now, 1, 0, NULL) == 0, "without the rule the scene pass reads as one-shot after the pause");
CHECK(pass(&scene, 1, &scene_d, now, 1, 1, &by) == 1 && by, "the scene pass is per-frame after the pause");
CHECK(pass(&scene, 1, &scene_d, now, 0, 1, &by) == 1 && by, "strict: the scene pass too");
CHECK(pass(&probe, 1, &probe_d, now, 1, 1, NULL) == 1, "the probe pass (128x128, seen 17925/17923) is per-frame");
CHECK(pass(&probe_l3, 1, &probe_d, now, 1, 0, NULL) == 0, "a layer drawn every 15th frame is never recent");
CHECK(pass(&probe_l3, 1, &probe_d, now, 1, 1, NULL) == 1, "a layer drawn every 15th frame, in 128 frames, is steady");
// the 960x540 pass of the log: one colour target made new, one steady, the depth target drawn earlier this frame
TfrTarget fresh = T(0x4000, 0, 0, 41), cc[2] = {fresh, half};
tfr_note(&fresh, now), tfr_note(&half, now), tfr_note(&half_d, now); // the pass begins: its targets are noted
CHECK(pass(cc, 2, &half_d, now, 1, 0, NULL) == 0, "without the rule: no recent colour target, a new one beside");
CHECK(pass(cc, 2, &half_d, now, 1, 1, &by) == 1 && by, "a steady colour target beside a new one: per-frame");
CHECK(pass(cc, 2, &half_d, now, 0, 1, NULL) == 0, "strict: the new colour target keeps the pass one-shot");
// the next frame all of it is recent again
tfr_note(&scene, now), tfr_note(&scene_d, now);
CHECK(pass(&scene, 1, &scene_d, now + 1, 1, 0, NULL) == 1, "recent again in the next frame");
CHECK(pass(&fresh, 1, &half_d, now + 1, 1, 0, NULL) == 1, "the new target is recent in its second frame");
// ---- what stays one-shot
TfrTarget lut = T(0x5000, 0, 0, 51);
CHECK(pass(&lut, 1, NULL, now, 1, 1, NULL) == 0, "a target never rendered is one-shot");
tfr_note(&lut, now);
CHECK(pass(&lut, 1, NULL, now, 1, 1, NULL) == 0, "its first frame, already noted by its pass, is still one-shot");
tfr_note(&lut, now); // a second pass into it in the same frame
for (int load_no = 1; load_no < TFR_STEADY; load_no++) { // generated again at each area load, far apart
uint64_t f = now + (uint64_t)load_no * 5000;
CHECK(pass(&lut, 1, NULL, f, 1, 1, NULL) == 0, "a table generated at %d area loads is one-shot at the next", load_no);
tfr_note(&lut, f);
}
CHECK(pass(&lut, 1, NULL, now + (uint64_t)TFR_STEADY * 5000, 1, 1, NULL) == 1,
"after %d loads the table counts as steady: its pipelines are recorded by then", TFR_STEADY);
// a volume generated a few slices per frame: each slice is its own target and is rendered once
for (uint32_t z = 0; z < 64; z++) {
TfrTarget slice = T(0x6000, 0, z, 61);
CHECK(pass(&slice, 1, NULL, now + 10 + z / 4, 1, 1, NULL) == 0, "volume slice %u is one-shot", z);
tfr_note(&slice, now + 10 + z / 4);
}
// the item icon capture: a new colour target with the per-frame depth buffer, once
TfrTarget icon = T(0x7000, 0, 0, 71);
uint64_t f2 = now + 1;
tfr_note(&scene_d, f2), tfr_note(&icon, f2);
CHECK(pass(&icon, 1, &scene_d, f2, 1, 1, NULL) == 0, "an icon's first capture is one-shot though its depth buffer is recent");
CHECK(pass(&icon, 1, &scene_d, f2, 1, 0, NULL) == 0, "and was before the rule");
// an image handed on to a texture at other storage starts again: the per-frame history of its handle is not the
// new texture's
TfrTarget reused = T(scene.image, 0, 0, 500);
CHECK(tfr_steady(&scene, f2 + 300) == 1, "the scene target is steady");
CHECK(pass(&reused, 1, NULL, f2 + 300, 1, 1, NULL) == 0, "the same image as another texture is not steady");
tfr_note(&reused, f2 + 300);
CHECK(tfr_steady(&scene, f2 + 301) == 0, "and the texture that had it lost its count with the image");
CHECK(tfr_find(&reused, 0)->n == 1, "the new texture's count begins at one");
// a pass with no target at all
CHECK(pass(NULL, 0, NULL, now, 1, 1, NULL) == 0 && pass(NULL, 0, NULL, now, 0, 1, NULL) == 0, "no targets: not per-frame");
// ---- the table keeps the steady targets through an area's first frame
// 400 per-frame targets, a pause, then 600 targets made new are rendered before the old ones come round again
memset(tfr, 0, sizeof tfr);
for (uint64_t f = 1; f <= 100; f++)
for (uint32_t i = 0; i < 400; i++) {
TfrTarget t = T(0x7f3a00000000ull + (uint64_t)(i / 4) * 0x2c0, 0, i % 4, 1000 + i / 4);
tfr_note(&t, f);
}
for (uint32_t i = 0; i < 600; i++) {
TfrTarget t = T(0x7f3b00000000ull + (uint64_t)i * 0x2c0, 0, 0, 5000 + i);
tfr_note(&t, 400);
}
int kept = 0;
for (uint32_t i = 0; i < 400; i++) {
TfrTarget t = T(0x7f3a00000000ull + (uint64_t)(i / 4) * 0x2c0, 0, i % 4, 1000 + i / 4);
kept += tfr_steady(&t, 400);
}
CHECK(kept == 400, "%d of 400 steady targets still known after 600 new ones in one frame", kept);
if (fails) return printf("%d checks failed\n", fails), 1;
printf("target_frames_check: ok\n");
return 0;
}

View File

@@ -0,0 +1,40 @@
"""Build and run tests/target_frames_check.c: runtime/vk_target_frames.h, which render passes count as drawn frame
after frame, so that a draw into them is skipped while its pipeline builds instead of waited for.
No GPU, no game files, any Python.
Usage: target_frames_check.py
"""
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
def compiler():
"""$CC, else MSYS2 UCRT64's clang, else clang on PATH (as tests/diff/common.py picks it, without its Unicorn)."""
if os.environ.get('CC'):
return os.environ['CC']
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
return str(MSYS_BIN / 'clang.exe')
return shutil.which('clang') or 'clang'
def main():
work = ROOT / 'build' / 'test' / 'target_frames'
work.mkdir(parents=True, exist_ok=True)
exe = work / ('target_frames_check.exe' if os.name == 'nt' else 'target_frames_check')
env = dict(os.environ)
if os.name == 'nt' and MSYS_BIN.exists():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
subprocess.check_call([compiler(), '-std=gnu11', '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'),
str(ROOT / 'tests' / 'target_frames_check.c'), '-o', str(exe)], env=env)
return subprocess.run([str(exe)], env=env).returncode
if __name__ == '__main__':
sys.exit(main())

View File

@@ -0,0 +1,157 @@
"""tools/validate_shader_cache.py on a fake shader cache: the entries the game would refuse (a cut or zero-filled
module, another magic number, a reflection that is not one JSON object naming its stage, a missing half of a pair) are
named, packed and loose entries are both read with the loose one standing for its packed copy, --every samples, and,
where spirv-val is installed, a whole module that is not valid SPIR-V is refused by it. No game files.
Run: python tests/validate_shader_cache_test.py
"""
import hashlib
import struct
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT / 'tools'))
import pack_shader_cache as psc # noqa: E402
import validate_shader_cache as vsc # noqa: E402
RES = b'{"stage":"cs","textures":[]}'
def name(seed):
return hashlib.sha256(seed.encode()).hexdigest()
def insn(op, *operands):
return struct.pack('<%dI' % (1 + len(operands)), (1 + len(operands)) << 16 | op, *operands)
def text(s):
b = s.encode() + b'\0'
b += b'\0' * (-len(b) % 4)
return struct.unpack('<%dI' % (len(b) // 4), b)
def module(memory_model=True):
"""A compute shader that does nothing, SPIR-V 1.3: ids 1 main, 2 void, 3 the function type, 4 the label."""
body = insn(17, 1) # OpCapability Shader
if memory_model:
body += insn(14, 0, 1) # OpMemoryModel Logical GLSL450
body += insn(15, 5, 1, *text('main')) # OpEntryPoint GLCompute %1 "main"
body += insn(16, 1, 17, 1, 1, 1) # OpExecutionMode %1 LocalSize 1 1 1
body += insn(19, 2) + insn(33, 3, 2) # OpTypeVoid, OpTypeFunction
body += insn(54, 2, 1, 0, 3) + insn(248, 4) + insn(253) + insn(56) # OpFunction, OpLabel, OpReturn, OpFunctionEnd
return struct.pack('<5I', vsc.SPIRV_MAGIC, 0x00010300, 0, 5, 0) + body
def cut_module():
"""A module whose last instruction says three words and has one: what a write cut short leaves."""
return module() + struct.pack('<I', 3 << 16 | 0)
def put(cache, seed, spv=None, res=None):
if spv is not None:
(cache / (name(seed) + '.spv')).write_bytes(spv)
if res is not None:
(cache / (name(seed) + '.res.json')).write_bytes(res)
return name(seed)
class ValidateTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.cache = Path(self.tmp.name) / 'shader-cache'
self.cache.mkdir()
def tearDown(self):
self.tmp.cleanup()
def check(self, **kw):
checked, failures = vsc.validate(self.cache, say=lambda s: None, **kw)
return checked, {k: (o, p) for k, o, p in failures}
def test_game_checks(self):
good = module()
ok = put(self.cache, 'ok', good, RES)
cut = put(self.cache, 'cut', good[:-6], RES) # not a whole number of words
short = put(self.cache, 'short', cut_module(), RES) # the last instruction runs past the end
zero = put(self.cache, 'zero', good[:40] + b'\0' * (len(good) - 40), RES)
magic = put(self.cache, 'magic', b'\x01' + good[1:], RES)
nul = put(self.cache, 'nul', good, b'\0' * len(RES))
stage = put(self.cache, 'stage', good, b'{"stage":"xx"}')
array = put(self.cache, 'array', good, b'[1]')
no_res = put(self.cache, 'no-res', good, None)
no_spv = put(self.cache, 'no-spv', None, RES)
checked, bad = self.check()
self.assertEqual(checked, 10)
self.assertNotIn(ok, bad)
self.assertIn('not a whole SPIR-V module', bad[cut][1])
self.assertIn('past the end', bad[short][1])
self.assertIn('zero words', bad[zero][1])
self.assertIn('magic', bad[magic][1])
self.assertIn('NUL', bad[nul][1])
self.assertIn('no stage', bad[stage][1])
self.assertIn('not one JSON object', bad[array][1])
self.assertEqual(bad[no_res][1], 'no .res.json')
self.assertEqual(bad[no_spv][1], 'no .spv')
self.assertEqual(len(bad), 9)
self.assertTrue(all(o == 'loose' for o, _ in bad.values()))
def test_stages_and_spaces(self):
for st in ('vs', 'ps', 'cs', 'ts', 'ms'):
self.assertEqual(vsc.reflection_problem(b' \n{ "stage" : "%s" }\n' % st.encode()), '')
self.assertNotEqual(vsc.reflection_problem(b'{"stage":"gs"}'), '')
self.assertNotEqual(vsc.reflection_problem(b'{}'), '')
self.assertEqual(vsc.spirv_problem(module()), '')
def test_pack_and_loose(self):
good = module()
packed_ok = put(self.cache, 'packed-ok', good, RES)
packed_bad = put(self.cache, 'packed-bad', cut_module(), RES)
fixed = put(self.cache, 'fixed', cut_module(), RES)
psc.pack(self.cache)
self.assertFalse(list(self.cache.glob('*.spv')))
put(self.cache, 'fixed', good, None) # a newer translation beside the pack's bad copy
loose_bad = put(self.cache, 'loose-bad', good, b'{}')
checked, bad = self.check()
self.assertEqual(checked, 4)
self.assertEqual(set(bad), {packed_bad, loose_bad})
self.assertEqual(bad[packed_bad][0], 'pack')
self.assertEqual(bad[loose_bad][0], 'loose')
self.assertNotIn(fixed, bad)
self.assertNotIn(packed_ok, bad)
def test_every_and_out(self):
for i in range(10):
put(self.cache, 'n%d' % i, cut_module() if i % 2 else module(), RES)
checked, bad = self.check()
self.assertEqual((checked, len(bad)), (10, 5))
checked, _ = self.check(every=3)
self.assertEqual(checked, 4)
out = Path(self.tmp.name) / 'failed.txt'
self.check(out=out)
lines = out.read_text(encoding='utf-8').splitlines()
self.assertEqual(len(lines), 5)
self.assertTrue(all(ln.split()[0] in bad and ln.split()[1] == 'loose' for ln in lines))
def test_groups(self):
a = vsc.group_of("spirv-val: error: line 12: ID '34[%34]' has not been defined")
b = vsc.group_of("spirv-val: error: line 907: ID '5[%main]' has not been defined")
self.assertEqual(a, b)
@unittest.skipUnless(vsc.find_spirv_val(), 'spirv-val is not installed')
def test_spirv_val(self):
ok = put(self.cache, 'ok', module(), RES)
invalid = put(self.cache, 'invalid', module(memory_model=False), RES) # whole, and not valid SPIR-V
checked, bad = self.check(val=vsc.find_spirv_val())
self.assertEqual(checked, 2)
self.assertNotIn(ok, bad)
self.assertTrue(bad[invalid][1].startswith('spirv-val: '), bad[invalid][1])
_, without = self.check() # the game's checks alone take both
self.assertEqual(without, {})
if __name__ == '__main__':
unittest.main()

72
tests/wb_lazy_check.c Normal file
View File

@@ -0,0 +1,72 @@
// runtime/wb_lazy_choice.h: whether the lazy write-back runs, by the player's setting, the platform and the GPU driver
// (runtime/vk_memory.c vk_mem_lazy_decide). Built and run by tests/wb_lazy_check.py; prints each failed check and
// exits 1 on any.
#include <stdio.h>
#include <string.h>
#include "wb_lazy_choice.h"
static int fails;
#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
// VkDriverId values (vulkan_core.h)
enum { AMD_PROPRIETARY = 1, AMD_OPEN_SOURCE = 2, MESA_RADV = 3, NVIDIA_PROPRIETARY = 4, INTEL_WINDOWS = 5, MESA_NVK = 24 };
enum { LINUX = 0, WINDOWS = 1 };
static int has(const char *s, const char *part) { return s && strstr(s, part) != NULL; }
int main(void) {
static const unsigned drivers[] = {AMD_PROPRIETARY, AMD_OPEN_SOURCE, MESA_RADV, NVIDIA_PROPRIETARY, INTEL_WINDOWS,
MESA_NVK, 0};
enum { ND = sizeof drivers / sizeof *drivers };
// Nothing set, Windows: on, except on AMD's own driver (both of its branches report this id).
WblChoice nv = wb_lazy_choice(NULL, WINDOWS, NVIDIA_PROPRIETARY);
CHECK(nv.on == 1, "Windows, NVIDIA's driver, nothing set: on");
CHECK(has(nv.why, "default") && has(nv.why, "wb_lazy = 0"), "it says it is the default and how to turn it off: %s", nv.why);
CHECK(wb_lazy_choice(NULL, WINDOWS, INTEL_WINDOWS).on == 1, "Windows, Intel's driver, nothing set: on");
CHECK(wb_lazy_choice(NULL, WINDOWS, 0).on == 1, "Windows, a driver that reports no id: on, as 0.7.1 had it");
WblChoice amd = wb_lazy_choice(NULL, WINDOWS, AMD_PROPRIETARY);
CHECK(amd.on == 0, "Windows, AMD's driver, nothing set: off");
CHECK(has(amd.why, "AMD") && has(amd.why, "wb_lazy = 1"), "it names the driver and how to turn it on: %s", amd.why);
CHECK(has(amd.why, "frame rate"), "it says what off costs: %s", amd.why);
CHECK(has(amd.why, "default"), "it says it is a default: %s", amd.why);
// An empty value is no value (cfg.c add: "key =" leaves the setting at its default).
CHECK(wb_lazy_choice("", WINDOWS, AMD_PROPRIETARY).on == 0, "an empty value on AMD: the default, off");
CHECK(wb_lazy_choice("", WINDOWS, NVIDIA_PROPRIETARY).on == 1, "an empty value on NVIDIA: the default, on");
// Nothing set, elsewhere: off on every driver.
for (int i = 0; i < ND; i++) {
WblChoice c = wb_lazy_choice(NULL, LINUX, drivers[i]);
CHECK(c.on == 0 && has(c.why, "wb_lazy = 1"), "Linux, driver %u, nothing set: off, and how to turn it on: %s",
drivers[i], c.why);
}
// What the player sets wins on every platform and driver, and the reason says it was set.
for (int os = LINUX; os <= WINDOWS; os++)
for (int i = 0; i < ND; i++) {
WblChoice on = wb_lazy_choice("1", os, drivers[i]), off = wb_lazy_choice("0", os, drivers[i]);
CHECK(on.on == 1 && has(on.why, "set"), "os %d driver %u, wb_lazy = 1: on (%s)", os, drivers[i], on.why);
CHECK(off.on == 0 && has(off.why, "set"), "os %d driver %u, wb_lazy = 0: off (%s)", os, drivers[i], off.why);
CHECK(!has(on.why, "default") && !has(off.why, "default"), "os %d driver %u: a set value is not a default", os,
drivers[i]);
}
// Any value but 0 counts as on, as cfg_bool reads it for memory.c mem_init.
CHECK(wb_lazy_choice("on", WINDOWS, AMD_PROPRIETARY).on == 1, "wb_lazy = on: on");
// The exception is the driver, not the vendor: another driver for AMD cards follows its platform's default.
CHECK(wb_lazy_choice(NULL, WINDOWS, AMD_OPEN_SOURCE).on == 1 && wb_lazy_choice(NULL, WINDOWS, MESA_RADV).on == 1,
"the exception is AMD's own driver");
CHECK(WBL_DRIVER_AMD == AMD_PROPRIETARY, "WBL_DRIVER_AMD is VK_DRIVER_ID_AMD_PROPRIETARY");
// The game passes the platform it was built for.
#ifdef _WIN32
CHECK(WBL_ON_WINDOWS == 1, "a Windows build says so");
#else
CHECK(WBL_ON_WINDOWS == 0, "a build elsewhere says so");
#endif
if (!fails) printf("wb lazy choice: all checks passed\n");
return fails != 0;
}

42
tests/wb_lazy_check.py Normal file
View File

@@ -0,0 +1,42 @@
"""Build and run tests/wb_lazy_check.c: runtime/wb_lazy_choice.h, whether the lazy write-back runs, by the player's
setting, the platform and the GPU driver.
Needs a C compiler only: no Vulkan headers, no GPU and no game files.
Usage: wb_lazy_check.py
"""
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
def compiler():
"""$CC, else MSYS2 UCRT64's clang, else clang on PATH (as tests/diff/common.py picks it, without its Unicorn)."""
if os.environ.get('CC'):
return os.environ['CC']
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
return str(MSYS_BIN / 'clang.exe')
return shutil.which('clang') or 'clang'
def main():
work = ROOT / 'build' / 'test' / 'wb_lazy'
work.mkdir(parents=True, exist_ok=True)
exe = work / ('wb_lazy_check.exe' if os.name == 'nt' else 'wb_lazy_check')
env = dict(os.environ)
if os.name == 'nt' and MSYS_BIN.exists():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
# every platform's and driver's choice is checked through wb_lazy_choice's arguments; WBL_ON_WINDOWS on the one
# built for
subprocess.check_call([compiler(), '-std=gnu11', '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'),
str(ROOT / 'tests' / 'wb_lazy_check.c'), '-o', str(exe)], env=env)
return subprocess.run([str(exe)], env=env).returncode
if __name__ == '__main__':
sys.exit(main())