Lay out the action prompt's face buttons as the pad layout has them.

The action prompt at the top right of the HUD draws the four face buttons as a diamond in the console's places: A right, B bottom, X top, Y left. Under pad_layout = xbox, playstation or auto the A action is the bottom button, yet the diamond kept A on the right, so the prompt pointed at the wrong button. At start the runtime now edits the prompt's layout in the shared interface archive for such a layout (the buttons, the dots and the lit crosses move, and the bubbles of X and Y flip) and serves the result from the profile folder; with nintendo, the default, nothing is changed. Every field is found and checked before anything is written, so a layout a mod changed is left as it is, and the archive is packed again with the game's own dictionary at the same unpacked size. One SARC reader and dictionary lookup now serve this, the camera pack and the menu's assets. When the layout changes, the menu's Restart counts the diamond.

tests/action_prompt_check.py passes without game files and on the game's own archive (4 copies, 208 fields, 13 ms at start).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-10 23:10:04 -05:00
parent 4eea3d1c2f
commit b1d08b06a8
13 changed files with 1279 additions and 68 deletions

406
runtime/action_prompt.c Normal file
View File

@@ -0,0 +1,406 @@
// The action prompt's diamond of face buttons for the pad layout at start. The action prompt (top right) shows the
// four face buttons as a diamond in the Switch's places, A on the right. Under a layout by label (NX2NAVI_PAD_LAYOUT =
// xbox, playstation or auto; input_map.c input_pad_layout_pick) A is the bottom button, so romfs.c
// (action_prompt_overlay) serves the shared interface layouts with the diamond laid out as such a pad has its buttons:
// A bottom, B right, X left, Y top. The game reads the layouts once at start, so a change of layout shows after a
// restart.
//
// The layout is DoCommand_00.buil in the SARC UI/Layout2Archive/Common.Product.100.Nin_Ounce_NVN2.blarc.zs, a BFFH
// file of little-endian records that point at each other by 64-bit file offsets:
// - The header has a group of slots per table: at +0x00 the offset of the list of name offsets, at +0x08 that of the
// list of record offsets, at +0x10 that of the names' hashes, at +0x18 the count. Panes at 0x118, materials at 0x138.
// The group at 0x218 is the materials' texture blocks: no names, the list of the blocks' offsets in the third slot
// (0x228), the count at 0x230.
// - A pane record holds scale x and y at +0x10 and +0x14, translate x and y at +0x1c and +0x20, and flags at +0x38, in
// which 0x1000 puts a text pane's origin on its right edge.
// - A material record holds its block's index at +0x14. A block starts with the offset of its texture bindings and
// their count; a binding is 16 bytes, the offset of the texture's name at +0x08.
// Each face button K has a group N_K_00, shown while K's action is offered: P_BaseK_00, a cross whose lit arm points at
// K; three dots, the other buttons, each 80 units out in its own direction; and N_BtnK_00 at K's place, with K's
// letter, the bubble behind the action's name (W_BaseK_00, and W_BaseKDecide_00 under the null N_BaseKDecide_00, which
// the Decide animation scales) and the name (T_GuideK_00). The bubble opens to the right, or to the left for the left
// button, so the name stays clear of the diamond. The cross has two pictures, the lit arm right
// (DoCommandCrossBase_A_00 and DoCommandCrossBaseGrd_A_00) or down (_B_00), flipped for left and up. Each cross's
// material has texture bindings of its own; only the names' strings are shared, so a cross changes picture by taking
// the other picture's name offsets. No animation keys these positions, flips or flags, so the layout holds while the
// prompt animates.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zstd.h>
#include "action_prompt.h"
#include "host.h"
#include "platform/platform.h"
#include "sarc.h"
#define MEMBER "DoCommand_00.buil"
#define MAX_RAW (64u << 20)
enum { PANE_TABLE = 0x118, MATERIAL_TABLE = 0x138, BLOCKS = 0x228, BLOCK_COUNT = 0x230 };
// The pane record's fields this reads; PANE_NEEDS: the bytes a record needs to hold all of them. The same for a
// material record.
enum { PANE_SCALE_X = 0x10, PANE_SCALE_Y = 0x14, PANE_X = 0x1c, PANE_Y = 0x20, PANE_FLAGS = 0x38, PANE_NEEDS = 0x3c };
enum { MATERIAL_BLOCK = 0x14, MATERIAL_NEEDS = 0x18, BINDING_SIZE = 16, BINDING_NAME = 0x08, TEXT_RIGHT = 0x1000 };
// ---- the diamond in each layout
enum { EAST, SOUTH, NORTH, WEST };
// A field is a float of a pane record, the text origin of a pane (F_RIGHT: 1 on its right edge, else 0) or the picture
// of a cross (F_CROSS: 0 the lit arm right, 1 down). The last two are held as floats too, so that one comparison checks
// every field.
enum { F_X, F_Y, F_SCALE_X, F_SCALE_Y, F_RIGHT, F_CROSS };
static const unsigned field_at[] = {
[F_X] = PANE_X, [F_Y] = PANE_Y, [F_SCALE_X] = PANE_SCALE_X, [F_SCALE_Y] = PANE_SCALE_Y, [F_RIGHT] = PANE_FLAGS};
static const char *const field_name[] = {"x", "y", "scale x", "scale y", "text origin", "cross picture"};
static const char buttons[4] = {'A', 'B', 'X', 'Y'};
static const int direction[2][4] = {{EAST, SOUTH, NORTH, WEST}, // by position: the Switch's places
{SOUTH, EAST, WEST, NORTH}}; // by label: an Xbox pad's
static const float place[4][2] = {[EAST] = {80, 0}, [SOUTH] = {0, -80}, [NORTH] = {0, 80}, [WEST] = {-80, 0}};
// The cross lit toward each direction: its picture and the flips that turn it.
static const struct { int picture; float sx, sy; } cross[4] = {
[EAST] = {0, 1, 1}, [SOUTH] = {1, 1, 1}, [NORTH] = {1, 1, -1}, [WEST] = {0, -1, 1}};
// The panes at a face button's place: the other buttons' dots in each group, and each button (index into buttons).
static const struct { const char *pane; int button; } at_button[] = {
{"P_B_00", 1}, {"P_X_00", 2}, {"P_Y_00", 3}, {"P_A_00", 0}, {"P_X_02", 2}, {"P_Y_02", 3},
{"P_A_02", 0}, {"P_B_03", 1}, {"P_Y_04", 3}, {"P_A_01", 0}, {"P_B_02", 1}, {"P_X_03", 2},
{"N_BtnA_00", 0}, {"N_BtnB_00", 1}, {"N_BtnX_00", 2}, {"N_BtnY_00", 3}};
enum { AT_BUTTON = sizeof at_button / sizeof *at_button };
typedef struct {
char pane[24]; // the pane; for F_CROSS the cross's pane, whose material has the same name
int field;
float value;
} Field;
// What diamond() sets: x and y of each pane at a button's place; each button's cross (picture, flips); for X and Y,
// which may be the left button, the flip and x of both bubbles and the name's x and origin.
enum { MAX_FIELDS = 2 * AT_BUTTON + 4 * 3 + 2 * 6 };
static int put(Field *f, int k, const char *pane, int field, float value) {
if (k >= MAX_FIELDS) return k + 1; // counted, not stored: action_prompt_lay_out refuses a count it does not expect
snprintf(f[k].pane, sizeof f[k].pane, "%s", pane);
f[k].field = field, f[k].value = value;
return k + 1;
}
// Every field the diamond's layout sets, by label (label = 1) or by position; their count.
static int diamond(int label, Field *f) {
int k = 0;
char name[24];
for (size_t i = 0; i < AT_BUTTON; i++) {
const float *at = place[direction[label][at_button[i].button]];
k = put(f, k, at_button[i].pane, F_X, at[0]);
k = put(f, k, at_button[i].pane, F_Y, at[1]);
}
for (int b = 0; b < 4; b++) {
int d = direction[label][b];
snprintf(name, sizeof name, "P_Base%c_00", buttons[b]);
k = put(f, k, name, F_CROSS, (float)cross[d].picture);
k = put(f, k, name, F_SCALE_X, cross[d].sx);
k = put(f, k, name, F_SCALE_Y, cross[d].sy);
if (b < 2) continue; // A and B are never the left button: their bubbles open to the right in both layouts
int left = d == WEST;
snprintf(name, sizeof name, "W_Base%c_00", buttons[b]);
k = put(f, k, name, F_SCALE_X, left ? -1.0f : 1.0f);
k = put(f, k, name, F_X, left ? 80.0f : -80.0f);
snprintf(name, sizeof name, "W_Base%cDecide_00", buttons[b]);
k = put(f, k, name, F_SCALE_X, left ? -1.0f : 1.0f);
k = put(f, k, name, F_X, left ? 80.0f : -80.0f);
snprintf(name, sizeof name, "T_Guide%c_00", buttons[b]);
k = put(f, k, name, F_X, left ? -68.0f : 64.0f);
k = put(f, k, name, F_RIGHT, (float)left);
}
return k;
}
// ---- reading the layout, every offset checked against its size
static int u32_at(const uint8_t *p, size_t n, uint64_t off, uint32_t *v) {
if (off > n || n - off < 4) return 0;
memcpy(v, p + off, 4);
return 1;
}
static int u64_at(const uint8_t *p, size_t n, uint64_t off, uint64_t *v) {
if (off > n || n - off < 8) return 0;
memcpy(v, p + off, 8);
return 1;
}
static const char *str_at(const uint8_t *p, size_t n, uint64_t off) {
return off < n && memchr(p + off, 0, n - off) ? (const char *)p + off : NULL;
}
// The record of the entry `name` of the table whose header slots start at slot, with at least size bytes; 0 when it
// is not there.
static uint64_t table_find(const uint8_t *p, size_t n, size_t slot, const char *name, size_t size) {
uint64_t names, records;
uint32_t count;
if (!u64_at(p, n, slot, &names) || !u64_at(p, n, slot + 8, &records) || !u32_at(p, n, slot + 24, &count) ||
names > n || records > n || (n - names) / 8 < count || (n - records) / 8 < count)
return 0;
for (uint32_t i = 0; i < count; i++) {
uint64_t s, r;
const char *t;
if (!u64_at(p, n, names + 8 * (uint64_t)i, &s) || !(t = str_at(p, n, s)) || strcmp(t, name)) continue;
return u64_at(p, n, records + 8 * (uint64_t)i, &r) && r && r <= n && n - r >= size ? r : 0;
}
return 0;
}
// The offsets of the name offsets of the cross `pane`'s picture and gradient (its material's first two texture
// bindings); 0 when they are not there.
static int cross_names(const uint8_t *p, size_t n, const char *pane, uint64_t at[2]) {
uint64_t m = table_find(p, n, MATERIAL_TABLE, pane, MATERIAL_NEEDS), blocks, block, bindings;
uint32_t index, count, bound;
if (!m || !u32_at(p, n, m + MATERIAL_BLOCK, &index) || !u64_at(p, n, BLOCKS, &blocks) ||
!u32_at(p, n, BLOCK_COUNT, &count) || index >= count || !u64_at(p, n, blocks + 8 * (uint64_t)index, &block) ||
!u64_at(p, n, block, &bindings) || !u32_at(p, n, block + 8, &bound) || bound < 2)
return 0;
for (int k = 0; k < 2; k++) {
uint64_t b, s;
if (!u64_at(p, n, bindings + 8 * (uint64_t)k, &b) || b > n || n - b < BINDING_SIZE ||
!u64_at(p, n, b + BINDING_NAME, &s))
return 0;
at[k] = b + BINDING_NAME;
}
return 1;
}
// Which picture the names at at[] give the cross: 0 the lit arm right (A), 1 down (B), -1 neither.
static int cross_picture(const uint8_t *p, size_t n, const uint64_t at[2]) {
static const char *const names[2][2] = {{"DoCommandCrossBase_A_00", "DoCommandCrossBaseGrd_A_00"},
{"DoCommandCrossBase_B_00", "DoCommandCrossBaseGrd_B_00"}};
uint64_t s[2];
const char *t[2];
for (int k = 0; k < 2; k++)
if (!u64_at(p, n, at[k], &s[k]) || !(t[k] = str_at(p, n, s[k]))) return -1;
for (int c = 0; c < 2; c++)
if (!strcmp(t[0], names[c][0]) && !strcmp(t[1], names[c][1])) return c;
return -1;
}
// Where a field is: the offset of its 4-byte word (the pane's flags for F_RIGHT), or for F_CROSS the offsets of the
// cross's two name offsets. Every offset is checked against the file's size when it is found.
typedef struct {
uint64_t at[2];
} Where;
static int field_find(const uint8_t *p, size_t n, const Field *f, Where *w) {
if (f->field == F_CROSS) return cross_names(p, n, f->pane, w->at);
uint64_t r = table_find(p, n, PANE_TABLE, f->pane, PANE_NEEDS);
w->at[0] = r + field_at[f->field], w->at[1] = 0;
return r != 0;
}
// A found field's value; 0 when it is not one this knows (a cross with another picture).
static int field_get(const uint8_t *p, size_t n, const Field *f, const Where *w, float *v) {
if (f->field == F_CROSS) {
int c = cross_picture(p, n, w->at);
*v = (float)c;
return c >= 0;
}
if (f->field == F_RIGHT) {
uint32_t flags;
memcpy(&flags, p + w->at[0], 4);
*v = (flags & TEXT_RIGHT) ? 1.0f : 0.0f;
return 1;
}
memcpy(v, p + w->at[0], 4);
return 1;
}
// The game stores some zeros as -2.8e-14.
static int same(float a, float b) { return fabsf(a - b) < 1e-3f; }
int action_prompt_lay_out(uint8_t *p, size_t n, int label, char *why, size_t why_n) {
label = !!label;
Field from[MAX_FIELDS], to[MAX_FIELDS];
int count = diamond(!label, from);
if (count != MAX_FIELDS || diamond(label, to) != MAX_FIELDS) {
snprintf(why, why_n, "the diamond has %d fields, not %d", count, MAX_FIELDS);
return -1;
}
if (n < 4 || memcmp(p, "BFFH", 4)) {
snprintf(why, why_n, "%s is not a BFFH layout", MEMBER);
return -1;
}
// Find and read every field before anything is written.
Where where[MAX_FIELDS];
float now[MAX_FIELDS];
int done = 1;
for (int k = 0; k < count; k++) {
if (!field_find(p, n, &from[k], &where[k]) || !field_get(p, n, &from[k], &where[k], &now[k])) {
snprintf(why, why_n, "%s has no %s %s", MEMBER, from[k].pane, field_name[from[k].field]);
return -1;
}
done &= same(now[k], to[k].value);
}
if (done) return 0;
for (int k = 0; k < count; k++)
if (!same(now[k], from[k].value)) {
snprintf(why, why_n, "%s %s %s is %g, not %g", MEMBER, from[k].pane, field_name[from[k].field],
(double)now[k], (double)from[k].value);
return -1;
}
// The name offsets of both pictures, from the crosses that show them now (the diamond by either layout shows both).
uint64_t names[2][2];
for (int k = 0; k < count; k++)
if (from[k].field == F_CROSS)
for (int j = 0; j < 2; j++) memcpy(&names[(int)now[k]][j], p + where[k].at[j], 8);
// Write, keeping each word's bytes as they were.
uint8_t undo[MAX_FIELDS][2][8];
int changed = 0;
for (int k = 0; k < count; k++) {
int words = to[k].field == F_CROSS ? 2 : 1, size = to[k].field == F_CROSS ? 8 : 4;
for (int j = 0; j < words; j++) memcpy(undo[k][j], p + where[k].at[j], size);
if (same(now[k], to[k].value)) continue;
changed++;
if (to[k].field == F_CROSS) {
for (int j = 0; j < 2; j++) memcpy(p + where[k].at[j], &names[(int)to[k].value][j], 8);
} else if (to[k].field == F_RIGHT) {
uint32_t flags;
memcpy(&flags, p + where[k].at[0], 4);
flags = to[k].value != 0 ? flags | TEXT_RIGHT : flags & ~(uint32_t)TEXT_RIGHT;
memcpy(p + where[k].at[0], &flags, 4);
} else {
memcpy(p + where[k].at[0], &to[k].value, 4);
}
}
// Read the layout again as the game will: each field where it was, with its new value. A file whose fields lie on
// the records that lead to them (or on each other) reads otherwise; it gets its bytes back, last written first.
for (int k = 0; k < count; k++) {
Where w;
float v;
if (field_find(p, n, &to[k], &w) && !memcmp(&w, &where[k], sizeof w) && field_get(p, n, &to[k], &w, &v) &&
same(v, to[k].value))
continue;
for (int i = count - 1; i >= 0; i--) {
int words = to[i].field == F_CROSS ? 2 : 1, size = to[i].field == F_CROSS ? 8 : 4;
for (int j = words - 1; j >= 0; j--) memcpy(p + where[i].at[j], undo[i][j], size);
}
snprintf(why, why_n, "%s %s %s overlaps another field or the records that lead to it", MEMBER, to[k].pane,
field_name[to[k].field]);
return -1;
}
return changed;
}
// The game's archive holds each layout four times under the same name (628 members, 157 names; the four copies of
// DoCommand_00.buil are the same bytes, each at its own offset). Every copy is laid out, each checked first on a copy
// of its own, so either all of them change or none.
int action_prompt_archive(uint8_t *sarc, size_t n, int label, char *why, size_t why_n) {
enum { MAX_COPIES = 16 };
size_t at[MAX_COPIES], size[MAX_COPIES], a, s;
unsigned next = 0;
int copies = 0, total = 0, found;
while ((found = sarc_next(sarc, n, MEMBER, &next, &a, &s)) == 1) {
if (copies == MAX_COPIES) {
snprintf(why, why_n, "the layout archive holds %s more than %d times", MEMBER, MAX_COPIES);
return -1;
}
at[copies] = a, size[copies++] = s;
}
if (found < 0) {
snprintf(why, why_n, "the layout archive is damaged");
return -1;
}
if (!copies) {
snprintf(why, why_n, "the layout archive has no %s", MEMBER);
return -1;
}
// Copies that share their bytes are laid out once; copies that share only some of them cannot be laid out alone.
for (int c = 0; c < copies; c++)
for (int d = 0; d < c; d++)
if (!(at[c] == at[d] && size[c] == size[d]) && at[c] < at[d] + size[d] && at[d] < at[c] + size[c]) {
snprintf(why, why_n, "two copies of %s in the layout archive overlap", MEMBER);
return -1;
}
for (int c = 0; c < copies; c++) {
uint8_t *trial = malloc(size[c] ? size[c] : 1);
if (!trial) {
snprintf(why, why_n, "out of memory");
return -1;
}
memcpy(trial, sarc + at[c], size[c]);
int r = action_prompt_lay_out(trial, size[c], label, why, why_n);
free(trial);
if (r < 0) return -1;
}
for (int c = 0; c < copies; c++) {
int r = action_prompt_lay_out(sarc + at[c], size[c], label, why, why_n);
if (r < 0) return -1; // not after a trial of the same bytes; kept so that a change here cannot hide one
total += r;
}
return total;
}
int action_prompt_build(const void *in, size_t n, const void *zsdic, size_t zsdic_n, int label, void **out,
size_t *out_n) {
uint64_t t0 = plat_now_ns();
label = !!label;
*out = NULL;
int result = -1;
uint8_t *dics = NULL, *raw = NULL;
void *z = NULL;
ZSTD_DDict *dd = NULL;
ZSTD_CDict *cd = NULL;
char why[200] = "";
// The frame names its dictionary by id (0: none); Pack/ZsDic.pack.zs holds them.
unsigned id = ZSTD_getDictID_fromFrame(in, n);
size_t dn = 0, dic_at = 0, dic_n = 0;
if (id && (!(dics = zsdic_unpack(zsdic, zsdic_n, MAX_RAW, &dn)) || !zsdic_find(dics, dn, id, &dic_at, &dic_n))) {
snprintf(why, sizeof why, "no dictionary %u in Pack/ZsDic.pack.zs", id);
goto done;
}
if (id && (!(dd = ZSTD_createDDict(dics + dic_at, dic_n)) || !(cd = ZSTD_createCDict(dics + dic_at, dic_n, 3)))) {
snprintf(why, sizeof why, "dictionary %u of Pack/ZsDic.pack.zs cannot be loaded", id);
goto done;
}
unsigned long long rn = ZSTD_getFrameContentSize(in, n);
if (rn == ZSTD_CONTENTSIZE_UNKNOWN || rn == ZSTD_CONTENTSIZE_ERROR || !rn || rn > MAX_RAW) {
snprintf(why, sizeof why, "the layout archive is not a zstd frame with a size");
goto done;
}
if (!(raw = malloc((size_t)rn))) {
snprintf(why, sizeof why, "out of memory");
goto done;
}
ZSTD_DCtx *dctx = ZSTD_createDCtx();
size_t got = !dctx ? 0 : dd ? ZSTD_decompress_usingDDict(dctx, raw, (size_t)rn, in, n, dd)
: ZSTD_decompressDCtx(dctx, raw, (size_t)rn, in, n);
ZSTD_freeDCtx(dctx);
if (ZSTD_isError(got) || got != rn) {
snprintf(why, sizeof why, "the layout archive does not decompress");
goto done;
}
int changed = action_prompt_archive(raw, (size_t)rn, label, why, sizeof why);
if (changed <= 0) {
result = changed;
goto done;
}
// Level 3: the archive unpacks to about 10 MB and is packed again at every start (about 15 ms).
size_t cap = ZSTD_compressBound((size_t)rn), zn = 0;
ZSTD_CCtx *cctx = ZSTD_createCCtx();
if (cctx && (z = malloc(cap)))
zn = cd ? ZSTD_compress_usingCDict(cctx, z, cap, raw, (size_t)rn, cd)
: ZSTD_compressCCtx(cctx, z, cap, raw, (size_t)rn, 3);
ZSTD_freeCCtx(cctx);
if (!z || !zn || ZSTD_isError(zn)) {
snprintf(why, sizeof why, "the layout archive cannot be compressed again");
goto done;
}
*out = z, *out_n = zn, z = NULL;
result = 1;
rt_log("action prompt: face buttons laid out %s, %d fields (%.0f ms)", label ? "by label (A bottom)" : "by position",
changed, (double)(plat_now_ns() - t0) / 1e6);
done:
if (result == 0)
rt_log("action prompt: the layout archive has the face buttons %s already", label ? "by label" : "by position");
if (result < 0) rt_log("action prompt: %s; the diamond keeps the Switch places", why);
free(z);
free(raw);
free(dics);
ZSTD_freeDDict(dd);
ZSTD_freeCDict(cd);
return result;
}

30
runtime/action_prompt.h Normal file
View File

@@ -0,0 +1,30 @@
// The action prompt's diamond of face buttons laid out for a pad layout (action_prompt.c).
#ifndef ACTION_PROMPT_H
#define ACTION_PROMPT_H
#include <stddef.h>
#include <stdint.h>
// The shared interface layouts of the game, which hold the action prompt (DoCommand_00).
#define ACTION_PROMPT_ARCHIVE "UI/Layout2Archive/Common.Product.100.Nin_Ounce_NVN2.blarc.zs"
// Lays the diamond of DoCommand_00.buil (p[0..n), in place) out for the face buttons by label (label nonzero: A
// bottom, B right, X left, Y top) or by position (label = 0: the Switch's places, A right, B bottom, X top, Y left).
// The runtime asks for label 1 only (romfs.c action_prompt_overlay); label 0 takes a layout by label back. Every field
// is found and checked before any is written, and read again after. Returns the number of fields changed; 0 when the
// diamond is laid out that way already; -1 when the file is not the layout this expects (why says which field
// differs; the file is left as it was).
int action_prompt_lay_out(uint8_t *p, size_t n, int label, char *why, size_t why_n);
// The same on the decompressed Common layout archive (a SARC holding DoCommand_00.buil), on every copy of the layout
// it holds: all of them change or none. The count is over all copies; -1 also for a damaged archive, more than 16
// copies, or copies that share some of their bytes.
int action_prompt_archive(uint8_t *sarc, size_t n, int label, char *why, size_t why_n);
// The Common layout archive's zstd file in[0..n) with the diamond laid out for label, compressed again with the
// dictionary of Pack/ZsDic.pack.zs (zsdic) the original names: *out (malloc'd) and *out_n. Returns 1 when *out was
// made, 0 when the diamond is laid out that way already (nothing to serve), -1 when it cannot be (logged).
int action_prompt_build(const void *in, size_t n, const void *zsdic, size_t zsdic_n, int label, void **out,
size_t *out_n);
#endif

View File

@@ -24,6 +24,7 @@
#include "cfg.h"
#include "host.h"
#include "platform/platform.h"
#include "sarc.h"
#define MEMBER "Component/PlayerCameraParam/PlayerCamera.game__component__PlayerCameraParam.bgyml"
#define MAX_RAW (64u << 20)
@@ -46,32 +47,6 @@ static int u32_at(const uint8_t *p, size_t n, size_t off, uint32_t *v) {
return 1;
}
// SARC member `name` of the archive p[0..n): its start and size; 0 when it is not there.
static int sarc_member(const uint8_t *p, size_t n, const char *name, size_t *at, size_t *size) {
uint32_t data;
uint16_t head, count, bom;
if (n < 0x14 || memcmp(p, "SARC", 4)) return 0;
memcpy(&head, p + 4, 2);
memcpy(&bom, p + 6, 2);
if (bom != 0xfeff || !u32_at(p, n, 12, &data) || head + 12u > n || memcmp(p + head, "SFAT", 4)) return 0;
memcpy(&count, p + head + 6, 2);
size_t names = head + 12 + (size_t)count * 16;
if (names + 8 > n || memcmp(p + names, "SFNT", 4)) return 0;
names += 8;
for (unsigned i = 0; i < count; i++) {
uint32_t attr, lo, hi;
size_t e = head + 12 + (size_t)i * 16;
if (!u32_at(p, n, e + 4, &attr) || !u32_at(p, n, e + 8, &lo) || !u32_at(p, n, e + 12, &hi)) return 0;
size_t s = names + (size_t)(attr & 0xffffff) * 4, len = strlen(name);
if (!(attr >> 24) || s + len + 1 > n || memcmp(p + s, name, len + 1)) continue;
if (lo > hi || data + (size_t)hi > n) return 0;
*at = data + lo;
*size = hi - lo;
return 1;
}
return 0;
}
// The StickSensitivity floats of the BYML p[0..n), multiplied by k; their count, 0 when the file has no such array.
static int scale_sensitivity(uint8_t *p, size_t n, float k, float was[8]) {
uint32_t keys, root, head;
@@ -117,21 +92,19 @@ int cam_pack_build(const void *in, size_t n, const void *zsdic, size_t zsdic_n,
ZSTD_DDict *dd = NULL;
ZSTD_CDict *cd = NULL;
void *z = NULL;
// The dictionaries: Pack/ZsDic.pack.zs is a SARC compressed without one.
unsigned long long dn = ZSTD_getFrameContentSize(zsdic, zsdic_n);
size_t dic_at, dic_n;
if (dn == ZSTD_CONTENTSIZE_UNKNOWN || dn == ZSTD_CONTENTSIZE_ERROR || dn > MAX_RAW || !(dics = malloc((size_t)dn)) ||
ZSTD_decompress(dics, (size_t)dn, zsdic, zsdic_n) != dn ||
!sarc_member(dics, (size_t)dn, "pack.zsdic", &dic_at, &dic_n)) {
rt_log("camera speed: no pack.zsdic in Pack/ZsDic.pack.zs; the camera keeps its own speed");
// The frame names its dictionary (pack.zsdic) by id; Pack/ZsDic.pack.zs holds them.
unsigned id = ZSTD_getDictID_fromFrame(in, n);
size_t dn, dic_at, dic_n;
if (!id || !(dics = zsdic_unpack(zsdic, zsdic_n, MAX_RAW, &dn)) || !zsdic_find(dics, dn, id, &dic_at, &dic_n)) {
rt_log("camera speed: Pack/ZsDic.pack.zs has no dictionary %u for Pack/Actor/PlayerCamera.pack.zs; the camera "
"keeps its own speed", id);
goto done;
}
unsigned long long rn = ZSTD_getFrameContentSize(in, n);
unsigned id = ZSTD_getDictID_fromFrame(in, n);
dd = ZSTD_createDDict(dics + dic_at, dic_n);
if (!dd || id != ZSTD_getDictID_fromDDict(dd) || rn == ZSTD_CONTENTSIZE_UNKNOWN ||
rn == ZSTD_CONTENTSIZE_ERROR || rn > MAX_RAW || !(raw = malloc((size_t)rn))) {
rt_log("camera speed: Pack/Actor/PlayerCamera.pack.zs is not a pack.zsdic frame with a size; the camera keeps its "
if (!dd || rn == ZSTD_CONTENTSIZE_UNKNOWN || rn == ZSTD_CONTENTSIZE_ERROR || rn > MAX_RAW ||
!(raw = malloc((size_t)rn))) {
rt_log("camera speed: Pack/Actor/PlayerCamera.pack.zs is not a zstd frame with a size; the camera keeps its "
"own speed");
goto done;
}

View File

@@ -51,6 +51,9 @@ enum { ROMFS_PROMPTS_OK, ROMFS_PROMPTS_MISSING, ROMFS_PROMPTS_FAILED, ROMFS_PROM
const char *romfs_prompts(void);
int romfs_prompts_ready(const char *folder);
int romfs_prompts_off(const char *folder);
// The pad layout (PAD_LAYOUT_*, input_map.h) the action prompt's diamond of face buttons has from start
// (action_prompt.c); -1 when the layout by label was asked for but the layout file could not be changed.
int romfs_action_prompt(void);
const char *romfs_overlay(size_t k, const char **file, uint64_t *size, int *is_added); // RomFS path of overlay k
void fs_trace(const char *what, const char *path); // NX2NAVI_TRACE=fs: one line per open

View File

@@ -19,6 +19,7 @@
#include "fs.h"
#include "host.h"
#include "menu_assets.h"
#include "sarc.h"
#include "tex.h"
#define MAX_RAW (512u << 20)
@@ -28,26 +29,6 @@ static uint16_t le16(const uint8_t *p) { return (uint16_t)(p[0] | p[1] << 8); }
static uint64_t le64(const uint8_t *p) { return (uint64_t)le32(p) | (uint64_t)le32(p + 4) << 32; }
static uint32_t be32(const uint8_t *p) { return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3]; }
// SARC member `name`: start and size inside p[0..n); 0 when absent or out of range.
static int sarc_member(const uint8_t *p, size_t n, const char *name, size_t *at, size_t *size) {
if (n < 0x14 || memcmp(p, "SARC", 4)) return 0;
size_t hl = le16(p + 4), data = le32(p + 0xc);
if (hl + 0xc > n || memcmp(p + hl, "SFAT", 4)) return 0;
size_t count = le16(p + hl + 6), ents = hl + 0xc, names = ents + 16 * count + 8;
if (names > n || memcmp(p + ents + 16 * count, "SFNT", 4)) return 0;
for (size_t i = 0; i < count; i++) {
const uint8_t *e = p + ents + 16 * i;
size_t no = names + (size_t)(le32(e + 4) & 0xffff) * 4, s = le32(e + 8), end = le32(e + 12);
if (no >= n || data + end > n || s > end) continue;
size_t len = strnlen((const char *)p + no, n - no);
if (len == strlen(name) && !memcmp(p + no, name, len)) {
*at = data + s, *size = end - s;
return 1;
}
}
return 0;
}
static ZSTD_DDict *dicts[8];
static unsigned dict_ids[8];
static int ndicts = -1;
@@ -59,13 +40,11 @@ static void dicts_load(void) {
uint8_t *z = romfs_load("Pack/ZsDic.pack.zs", &zn64);
zn = (size_t)zn64;
if (!z) return;
unsigned long long rn = ZSTD_getFrameContentSize(z, zn);
uint8_t *raw = rn && rn <= MAX_RAW ? malloc((size_t)rn) : NULL;
if (raw && ZSTD_decompress(raw, (size_t)rn, z, zn) == rn) {
static const char *const names[] = {"zs.zsdic", "pack.zsdic", "bcett.byml.zsdic", "bkprb.zsdic"};
for (int i = 0; i < 4 && ndicts < 8; i++) {
size_t at, size;
if (!sarc_member(raw, (size_t)rn, names[i], &at, &size)) continue;
size_t rn = 0, at, size;
unsigned next = 0;
uint8_t *raw = zsdic_unpack(z, zn, MAX_RAW, &rn);
if (raw) {
while (ndicts < 8 && sarc_next(raw, rn, NULL, &next, &at, &size) == 1) {
ZSTD_DDict *d = ZSTD_createDDict(raw + at, size);
if (!d) continue;
dict_ids[ndicts] = ZSTD_getDictID_fromDDict(d);

View File

@@ -30,6 +30,7 @@
#include <string.h>
#include <strings.h>
#include "action_prompt.h"
#include "cfg.h"
#include "fs.h"
#include "host.h"
@@ -782,6 +783,32 @@ static void camera_overlay(void) {
if (ok && serve_generated(CAMPACK, base, out, out_n, "camera", "camera speed", keeps)) cam_pack_served();
}
// The action prompt's diamond of face buttons for the pad layout at start (action_prompt.c). By position (the
// Switch's places) the game's own layout is right; by label the Common layout archive the game would read (a mod's, else
// the RomFS's) is served with the diamond laid out again, written to <profile>/action-prompt/. A pad is not open yet
// this early, so auto counts as by label, as for the prompts' folders (input_map.c input_prompts_folder).
static int action_prompt_for = -1; // romfs_action_prompt
static void action_prompt_overlay(void) {
int layout = input_pad_layout_pick(cfg_value("NX2NAVI_PAD_LAYOUT"), 0);
if (layout == PAD_LAYOUT_NINTENDO) {
action_prompt_for = layout;
return;
}
static const char keeps[] = "the action prompt keeps the Switch places";
Overlay *base, *dic_base;
size_t n, dn, out_n = 0;
void *in = read_served(ACTION_PROMPT_ARCHIVE, &base, &n, keeps), *out = NULL;
void *dic = in ? read_served("Pack/ZsDic.pack.zs", &dic_base, &dn, keeps) : NULL;
int made = in && dic ? action_prompt_build(in, n, dic, dn, 1, &out, &out_n) : -1;
free(in);
free(dic);
if (made == 0 || (made > 0 && serve_generated(ACTION_PROMPT_ARCHIVE, base, out, out_n, "action-prompt",
"face buttons by label", keeps)))
action_prompt_for = layout;
}
int romfs_action_prompt(void) { return action_prompt_for; }
// The button prompts' folders (input_prompts_folder) and whether each had a manifest next to romfs-index.bin at start;
// the one the settings asked for at start, the one overlay_init loaded (NULL: the game's own prompts) and why the first
// did not load (ROMFS_PROMPTS_*). The menu compares them with the settings in effect (ui/menu.cpp prompts_note).
@@ -938,6 +965,7 @@ static void overlay_init(void) {
restbl_overlay();
heapdef_overlay();
camera_overlay();
action_prompt_overlay();
added_init();
if (noverlay) qsort(overlays, noverlay, sizeof *overlays, overlay_order);
for (size_t k = 1; k < noverlay; k++) // overlay_contest left one per file; the generated ones replace in place

71
runtime/sarc.c Normal file
View File

@@ -0,0 +1,71 @@
// SARC archives and the zstd dictionaries of Pack/ZsDic.pack.zs. A SARC is a little-endian header ("SARC", its
// length at +4, the byte-order mark 0xfeff at +6, the start of the members' data at +12), then SFAT: its member count
// at +6 and from +12 one 16-byte entry per member (name hash, attributes, start and end relative to the data). An
// entry's attributes hold a nonzero top byte when the member has a name, and in the low 24 bits the name's offset in
// SFNT's string table divided by 4; SFNT's 8-byte header follows the entries. The game's archives may hold several
// members under one name (UI/Layout2Archive/Common...blarc.zs holds each layout four times), so members are walked
// one by one.
#include <stdlib.h>
#include <string.h>
#include <zstd.h>
#include "sarc.h"
int sarc_next(const uint8_t *p, size_t n, const char *name, unsigned *next, size_t *at, size_t *size) {
uint32_t data;
uint16_t head, count, bom;
if (n < 0x14 || memcmp(p, "SARC", 4)) return -1;
memcpy(&head, p + 4, 2);
memcpy(&bom, p + 6, 2);
memcpy(&data, p + 12, 4);
if (bom != 0xfeff || (size_t)head + 12 > n || memcmp(p + head, "SFAT", 4)) return -1;
memcpy(&count, p + head + 6, 2);
size_t names = (size_t)head + 12 + (size_t)count * 16; // the entries end where SFNT starts
if (names + 8 > n || memcmp(p + names, "SFNT", 4)) return -1;
names += 8;
size_t len = name ? strlen(name) : 0;
for (unsigned i = *next; i < count; i++) {
uint32_t attr, lo, hi;
const uint8_t *e = p + head + 12 + (size_t)i * 16;
memcpy(&attr, e + 4, 4);
memcpy(&lo, e + 8, 4);
memcpy(&hi, e + 12, 4);
if (name) {
size_t s = names + (size_t)(attr & 0xffffff) * 4;
if (!(attr >> 24) || s > n || n - s < len + 1 || memcmp(p + s, name, len + 1)) continue;
}
if (lo > hi || (size_t)data + hi > n) return -1;
*at = (size_t)data + lo;
*size = hi - lo;
*next = i + 1;
return 1;
}
*next = count;
return 0;
}
int sarc_member(const uint8_t *p, size_t n, const char *name, size_t *at, size_t *size) {
unsigned next = 0;
return sarc_next(p, n, name, &next, at, size) == 1;
}
uint8_t *zsdic_unpack(const void *z, size_t zn, size_t max, size_t *n) {
unsigned long long rn = ZSTD_getFrameContentSize(z, zn);
if (rn == ZSTD_CONTENTSIZE_UNKNOWN || rn == ZSTD_CONTENTSIZE_ERROR || !rn || rn > max) return NULL;
uint8_t *raw = malloc((size_t)rn);
size_t got = raw ? ZSTD_decompress(raw, (size_t)rn, z, zn) : 0;
if (!raw || ZSTD_isError(got) || got != rn) {
free(raw);
return NULL;
}
*n = (size_t)rn;
return raw;
}
int zsdic_find(const uint8_t *dics, size_t n, unsigned id, size_t *at, size_t *size) {
unsigned next = 0;
if (!id) return 0;
while (sarc_next(dics, n, NULL, &next, at, size) == 1)
if (ZSTD_getDictID_fromDict(dics + *at, *size) == id) return 1;
return 0;
}

24
runtime/sarc.h Normal file
View File

@@ -0,0 +1,24 @@
// SARC archives and the zstd dictionaries of Pack/ZsDic.pack.zs (sarc.c).
#ifndef SARC_H
#define SARC_H
#include <stddef.h>
#include <stdint.h>
// The next member of the SARC p[0..n) from member *next on that is named `name` (any member when name is NULL): 1 with
// its start and size, *next moved past it; 0 when no such member is left; -1 when the archive is damaged (its header,
// or the found member's range).
int sarc_next(const uint8_t *p, size_t n, const char *name, unsigned *next, size_t *at, size_t *size);
// The first member named `name`: 1 with its start and size; 0 when there is none or the archive is damaged.
int sarc_member(const uint8_t *p, size_t n, const char *name, size_t *at, size_t *size);
// Pack/ZsDic.pack.zs's archive (z[0..zn), a frame compressed without a dictionary) unpacked: malloc'd, *n its size;
// NULL when it is not a zstd frame that says its size, or that size is over max.
uint8_t *zsdic_unpack(const void *z, size_t zn, size_t max, size_t *n);
// The dictionary with id `id` among the members of that archive (dics[0..n)): 1 with its start and size; 0 when none
// has it (and always for id 0, the id of a frame without a dictionary).
int zsdic_find(const uint8_t *dics, size_t n, unsigned id, size_t *at, size_t *size);
#endif

View File

@@ -1268,13 +1268,17 @@ static void row_walk_key(void) {
// layout, as a layout that puts A on the right button needs the face buttons named the other way round), but the game
// reads its fonts and UI archive once at start (romfs.c overlay_init). While the folder in use differs: "files
// missing" when the setup did not write the one asked for, why it was left out at start (damaged, a game resolution
// that is not 16:9, a RomFS mod with the same files, NX2NAVI_ROMFS_PATCHES), else "prompts change after restart", and only then
// *restart is set: a restart would bring the prompts asked for. NULL when they match.
// that is not 16:9, a RomFS mod with the same files, NX2NAVI_ROMFS_PATCHES), else "prompts change after restart" with
// *restart set: a restart would bring the prompts asked for. The action prompt's diamond of face buttons is laid out
// for the pad layout at start too (romfs.c action_prompt_overlay), whatever the prompts: while the layout asks for the
// other places, a restart would lay it out again, so *restart is set whatever the note, and with the folder in use the
// note is "prompts change after restart". NULL when both match.
static const char *prompts_note(bool *restart) {
*restart = false;
const char *layout = cfg_value("NX2NAVI_PAD_LAYOUT"), *used = romfs_prompts();
const char *want = input_prompts_folder(input_prompts_pick(cfg_get("NX2NAVI_PROMPTS"), layout), layout);
if (want == used || (want && used && !strcmp(want, used))) return nullptr;
int diamond = romfs_action_prompt(); // -1 when it could not be laid out: a restart would not change it
*restart = diamond >= 0 && diamond != input_pad_layout_pick(layout, 0);
if (want == used || (want && used && !strcmp(want, used))) return *restart ? menu_text(UI_MENU_PROMPTS_RESTART) : nullptr;
// A game resolution of another shape needs a folder of its own for the PC prompts (romfs-patches-pc-<w>x<h>), which
// the setup does not write: the shape is the reason then, not the setup.
int off = romfs_prompts_off(want);