"""(hookshot) The button prompts of other controllers: the game's button-icon font redrawn per controller face, from the user's RomFS and Kenney's Input Prompts (CC0, third_party/kenney_input). python -I nx2/prompt_assets.py --romfs --out /prompts Writes //External.bfotf for each face (switch, xbox, ps4, ps5, steamdeck). Every text in the game draws its button icons from Font2/Scalable/External.*.bfotf (dialogue, the control hints, menus): A B X Y at U+E040..E043, L R ZL ZR at E044..E047, Plus and Minus at E04B and E04C, the stick presses at E056 and E057, the d-pad at E060..E063. Each written font has one icon per pad input at U+F700 + HOS_PAD_* (gpu/vk/vk_api.h), and each of those Switch buttons' code points in a cmap segment of its own: the game (host/prompts.c) points each one at the icon of the pad input bound to that button when it serves the font, so the icons follow the controls, changed ones too. The textures with button icons (the ocarina's notes, the letters on black, the controller in the pause menu) the same way, with Pillow, astc_encoder and texture2ddecoder: /Common.bntx is the game's UI/Layout2Archive/Common bntx decompressed, and /textures.bin the encoded blocks to write over it for each Switch button on each pad input, which the game picks by the controls when it serves the file. The Talk ring's buttons and the ocarina hint's icons (the layouts of UI/Layout2Archive/Common, /Common.blarc) the same way: /ring.bin and /hint.bin say what to write over them for the controls. Each face's images (the notes, the letters, the controller picture drawn by nx2/prompt_draw.py, the leader lines) are made as layers first: --dump writes them as PNG, and a PNG of the same name in nx2/prompt_faces// takes a layer's place, to rework one by hand (nx2/prompt_faces/README.md). The faces' fonts are larger than the game's, and the game drops a file larger than its entry in the resource size table: / rsizetable.zs is the game's table with the font's entry raised to fit the largest, served in its place. A .bfotf is an OpenType font behind an 8-byte header and XORed per little-endian word: the header is the magic 0x18029a7f and the font's size (big-endian), each XORed with the same key as the font. The .zs files are zstd frames made with the dictionary zs.zsdic of Pack/ZsDic.pack.zs. """ import argparse import io import os import struct import sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) # (python -I leaves the script folder off) from prompt_faces import FACES, override, parts # noqa: E402 (the faces: their icons, drawings and their parts) KENNEY = os.path.join(os.path.dirname(HERE), "third_party", "kenney_input") FONT = "Font2/Scalable/External.Nin_Ounce_NVN2.bfotf.zs" MAGIC = 0x18029A7F PAD_BASE = 0xF700 WIDE = 1.6 # (how much wider than the game's icon a wide Kenney icon may be) # the HOS_PAD_* inputs (gpu/vk/vk_api.h), in order PADS = ["south", "east", "west", "north", "back", "guide", "start", "lstick", "rstick", "lshoulder", "rshoulder", "up", "down", "left", "right", "ltrigger", "rtrigger"] # the Switch buttons' code points, in HOS_SW_* order (A B X Y LS RS L R ZL ZR Plus Minus Left Up Right Down), and hookshot's # default controls for them (host/nn_hid.c pad_default: by position, A the right face button) SWITCH = [0xE040, 0xE041, 0xE042, 0xE043, 0xE056, 0xE057, 0xE044, 0xE045, 0xE046, 0xE047, 0xE04B, 0xE04C, 0xE062, 0xE060, 0xE063, 0xE061] DEFAULT = ["east", "south", "north", "west", "lstick", "rstick", "lshoulder", "rshoulder", "ltrigger", "rtrigger", "start", "back", "left", "up", "right", "down"] # the game's own icon for each pad input: what the Switch face shows SWITCH_ICON = {"south": 0xE041, "east": 0xE040, "west": 0xE043, "north": 0xE042, "back": 0xE04C, "guide": 0xE04A, "start": 0xE04B, "lstick": 0xE056, "rstick": 0xE057, "lshoulder": 0xE044, "rshoulder": 0xE045, "up": 0xE060, "down": 0xE061, "left": 0xE062, "right": 0xE063, "ltrigger": 0xE046, "rtrigger": 0xE047} # the icons that do not follow the controls, by the Kenney glyph's name after the face's prefix: the whole d-pad (the sticks keep the # game's icons, an L or R cap with its arrows: right for any controller, where Kenney's are drawn in perspective) STATIC = {0xE049: "dpad", 0xE064: "dpad_vertical_outline", 0xE065: "dpad_horizontal_outline"} def zs_dictionary(romfs): """zs.zsdic, out of Pack/ZsDic.pack.zs (a SARC, itself zstd without a dictionary)""" import zstandard with open(os.path.join(romfs, "Pack", "ZsDic.pack.zs"), "rb") as f: b = zstandard.ZstdDecompressor().decompressobj().decompress(f.read()) data, = struct.unpack_from("I", struct.pack("I", len(otf)))[0] ^ key) return head + b"".join(struct.pack("HHHHHHH", 4, 16 + 8 * n, 0, 2 * n, search, (search // 2).bit_length() - 1, 2 * n - search) sub += b"".join(struct.pack(">H", s[1]) for s in segs) + b"\0\0" sub += b"".join(struct.pack(">H", s[0]) for s in segs) sub += b"".join(struct.pack(">H", s[2]) for s in segs) sub += b"\0\0" * n return struct.pack(">HHHHIHHI", 0, 2, 0, 3, 20, 3, 1, 20) + sub def face_font(otf, face): """the game's font (decrypted) with this face's icons: a pad icon per input at PAD_BASE + its number, and the Switch buttons' code points on the default controls' icons""" from fontTools.pens.boundsPen import BoundsPen from fontTools.pens.t2CharStringPen import T2CharStringPen from fontTools.pens.transformPen import TransformPen from fontTools.ttLib import TTFont from fontTools.ttLib.tables.DefaultTable import DefaultTable game = TTFont(io.BytesIO(otf)) gset, cmap = game.getGlyphSet(), dict(game.getBestCmap()) top = game["CFF "].cff.topDictIndex[0] chars = top.CharStrings order = list(game.getGlyphOrder()) def bounds(glyphset, name): p = BoundsPen(glyphset) glyphset[name].draw(p) return p.bounds def add(name, charstring, width): chars.charStringsIndex.append(charstring) chars.charStrings[name] = len(chars.charStringsIndex) - 1 top.charset.append(name) order.append(name) game["hmtx"].metrics[name] = (width, 0) if face == "switch": icon = {p: cmap[SWITCH_ICON[p]] for p in PADS} for i, p in enumerate(PADS): src = chars[icon[p]] src.decompile() add(f"pad_{p}", src, game["hmtx"][icon[p]][0]) else: f = FACES[face] prefix, names = f["prefix"], f["icons"] kenney = TTFont(os.path.join(KENNEY, f["font"])) kset = kenney.getGlyphSet() def kname(s): return s[1:] if s.startswith("/") else prefix + s def draw(target, kn): """the Kenney glyph scaled into the box of the game's glyph `target` (as a square around its centre); a wide one (the Menu and View pills on the square Plus and Minus) as high as the box, up to WIDE times as wide, the glyph widened""" gb, kb = bounds(gset, target), bounds(kset, kn) side = max(gb[2] - gb[0], gb[3] - gb[1]) s = min(side * WIDE / (kb[2] - kb[0]), side / (kb[3] - kb[1])) over = max(0, s * (kb[2] - kb[0]) - side) dx = (gb[0] + gb[2] + over) / 2 - s * (kb[0] + kb[2]) / 2 dy = (gb[1] + gb[3]) / 2 - s * (kb[1] + kb[3]) / 2 width = game["hmtx"][target][0] + round(over) pen = T2CharStringPen(width, gset) kset[kn].draw(TransformPen(pen, (s, 0, 0, s, dx, dy))) cs = chars[target] return pen.getCharString(private=cs.private, globalSubrs=cs.globalSubrs), width for p in PADS: kn = kname(names[p]) if p in names else None if kn not in kset: # (no such button on this face, the PlayStation's PS button: the game's own icon) kn = None target = cmap[SWITCH_ICON[p]] if kn: add(f"pad_{p}", *draw(target, kn)) else: src = chars[target] src.decompile() add(f"pad_{p}", src, game["hmtx"][target][0]) for c, s in STATIC.items(): kn = prefix + s if c in cmap and kn in kset: cs, _ = draw(cmap[c], kn) chars[cmap[c]] = cs game.setGlyphOrder(order) gid = {n: i for i, n in enumerate(order)} mapping = {c: gid[n] for c, n in cmap.items() if c <= 0xFFFF} for i, p in enumerate(PADS): mapping[PAD_BASE + i] = gid[f"pad_{p}"] for c, p in zip(SWITCH, DEFAULT): mapping[c] = gid[f"pad_{p}"] t = DefaultTable("cmap") t.data = cmap4(mapping, set(SWITCH)) game["cmap"] = t out = io.BytesIO() game.save(out) return out.getvalue() # ---- the textures with button icons (UI/Layout2Archive/Common.*.bntx: 878 textures, ASTC 4x4 and BC4, the Switch 2's block-linear # layout). prompts/Common.bntx is the game's file, decompressed; prompts//textures.bin the blocks to write over it, for each # Switch button and each pad input it can be bound to (host/prompts.c writes those of the controls in use): # "HPTX", u32 count, then count times: u8 Switch button (HOS_SW_*), u8 pad input (HOS_PAD_*), u16 0, u32 runs, # runs times: u32 offset in the file, u32 length, the bytes # Each face's images are made first, as layers (face_layers), then encoded: --dump writes the layers as PNG, and a PNG of the same # name in nx2/prompt_faces// takes a layer's place (prompt_faces.override). A layer's name: the texture's, then for a Switch # button's image on a pad input "." (the notes, the letters; the leader lines "..": only the # line, drawn over the texture's lines that stay), ".png". TEXTURES = "UI/Layout2Archive/Common.Product.100.Nin_Ounce_NVN2.bntx.zs" SW = {n: i for i, n in enumerate(["a", "b", "x", "y", "ls", "rs", "l", "r", "zl", "zr", "plus", "minus", "left", "up", "right", "down"])} # the ocarina's notes (white icons, dark letters) and the white letters on black (KeyTexFont, BC4) NOTES = {"OcarinaButtonA_00": "a", "OcarinaButtonB_00": "b", "OcarinaButtonX_00": "x", "OcarinaButtonY_00": "y", "OcarinaButtonL_00": "l", "OcarinaButtonR_00": "r"} LETTERS = {"KeyTexFontA_00": "a", "KeyTexFontB_00": "b", "KeyTexFontX_00": "x", "KeyTexFontY_00": "y"} # the Pro Controller in the pause menu's controls page (System): the picture, the page's copy of it (tinted: PICTURE_TINT) and its # shadow (PICTURE_SHADOW); a face's own picture (nx2/prompt_faces.py) takes their place, else the face buttons' discs (centre, # radius in pixels) by the pad input there get the face's labels (a picture of the controller: its own labels, whatever the controls) PICTURES = ("Controller_Pro", "Controller_Pro1") PICTURE_TINT, PICTURE_SHADOW = "Controller_Pro1", "ControllerSh_Pro1" PICTURE_BUTTONS = {"east": (869, 227, 31), "south": (800, 286, 31), "north": (800, 168, 31), "west": (732, 227, 31)} # the page's leader lines (BC4, white on black, drawn over the picture: GUIDE_SCALE and GUIDE_AT take its pixels to theirs), from # the labels' bars (BARS: the left ones' right edge, the right ones' left edge, kept from the game's) to the picture's parts # (nx2/prompt_faces.py): each line's texture, bar (0 left, 1 right), row (its top; 6 pixels thick) and end: a Switch button (the # part of the pad input bound to it: one line per pad input, chosen by the controls; a face button or the d-pad: a frame round the # group, as the game's round its face buttons, the label's icon telling which) or a pad input (fixed: the sticks) GUIDES = ("ControllerGuide_Pro_00", "ControllerGuide_Pro_01") GUIDE_SCALE, GUIDE_AT, BARS = 1.1136, (131.3, 462.6), (147, 1407) GUIDE_LINES = [(0, 0, 333, "zl"), (1, 0, 561, "minus"), (0, 0, 667, "lstick"), (0, 1, 558, "plus"), (0, 1, 713, "a"), (0, 1, 1225, "rstick")] def bntx_textures(b): """{name: (BRTI offset, format, width, height, GOBs per block, data offset)} of a Switch 2 BNTX (platform "Ounc")""" count, = struct.unpack_from("> 5) << 8 | ((y & 7) >> 2) << 7 | ((x & 31) >> 4) << 6 | ((y >> 1) & 1) << 5 | (y & 1) << 4 | (x & 15)) def decode(b, t): """a texture as a PIL image (RGBA, or L for BC4)""" import texture2ddecoder from PIL import Image _, fmt, w, h, gobs, data = t bpe = 16 if fmt >> 8 == 0x2D else 8 bw, bh = (w + 3) // 4, (h + 3) // 4 pitch = (bw * bpe + 63) // 64 lin = b"".join(b[data + o:data + o + bpe] for o in (gob_offset(x * bpe, y, gobs, pitch) for y in range(bh) for x in range(bw))) if bpe == 16: return Image.frombytes("RGBA", (bw * 4, bh * 4), texture2ddecoder.decode_astc(lin, bw * 4, bh * 4, 4, 4), "raw", "BGRA").crop((0, 0, w, h)) return Image.frombytes("L", (bw * 4, bh * 4), lin, "bcn", 4).crop((0, 0, w, h)) def encode_bc4(im): """BC4 by each block's darkest and lightest, the 6 levels between""" import numpy as np v = np.asarray(im, dtype=np.int32) v = v.reshape(im.height // 4, 4, im.width // 4, 4).transpose(0, 2, 1, 3).reshape(-1, 16) hi, lo = v.max(1), v.min(1) span = np.maximum(hi - lo, 1)[:, None] t = np.where((hi > lo)[:, None], np.rint((v - lo[:, None]) * 7 / span), 7).astype(np.uint64) code = np.where(t == 7, 0, np.where(t == 0, 1, 8 - t)).astype(np.uint64) # (0 the lightest, 1 the darkest, 2..7 between) idx = (code << (np.arange(16, dtype=np.uint64) * 3)).sum(1, dtype=np.uint64) out = np.zeros((len(v), 8), np.uint8) out[:, 0], out[:, 1] = hi, lo out[:, 2:] = idx.astype("> 8 == 0x2D else 8 bw, bh = (w + 3) // 4, (h + 3) // 4 x0, y0, x1, y1 = box or (mask and mask.getbbox()) or (0, 0, w, h) bx0, by0, bx1, by1 = max(0, x0 // 4), max(0, y0 // 4), min(bw, (x1 + 3) // 4), min(bh, (y1 + 3) // 4) part = Image.new(im.mode, (bw * 4, bh * 4)) # (only the box's blocks are encoded) part.paste(im, (0, 0)) part = part.crop((bx0 * 4, by0 * 4, bx1 * 4, by1 * 4)) blocks = encode_astc(part.convert("RGBA"), (fmt & 0xFF) == 6) if bpe == 16 else encode_bc4(part.convert("L")) pw = bx1 - bx0 pitch = (bw * bpe + 63) // 64 if mask: # (the blocks with any of the mask) import numpy as np m = np.zeros((bh * 4, bw * 4), bool) m[:h, :w] = np.asarray(mask) > 0 cover = m.reshape(bh, 4, bw, 4).any((1, 3)) pieces = sorted((data + gob_offset(x * bpe, y, gobs, pitch), blocks[((y - by0) * pw + x - bx0) * bpe:((y - by0) * pw + x - bx0 + 1) * bpe]) for y in range(by0, by1) for x in range(bx0, bx1) if not mask or cover[y, x]) out = [] for off, piece in pieces: # (blocks next to each other in the file: one run) if out and out[-1][0] + len(out[-1][1]) == off: out[-1][1] += piece else: out.append([off, bytearray(piece)]) return out class Icons: """a face's icon for each pad input, drawn from a font (the game's own for the Switch face, else Kenney's)""" def __init__(self, face, otf): from fontTools.ttLib import TTFont if face == "switch": self.path, self.code = io.BytesIO(otf), {p: SWITCH_ICON[p] for p in PADS} else: f = FACES[face] prefix, names = f["prefix"], f["icons"] self.path = os.path.join(KENNEY, f["font"]) rev = {n: c for c, n in TTFont(self.path).getBestCmap().items()} self.code = {} for p in PADS: n = names.get(p) n = n and (n[1:] if n.startswith("/") else prefix + n) if n in rev: self.code[p] = rev[n] def masks(self, pad, size): """(the icon, its silhouette with the holes filled, the holes) as L masks of size x size, the icon centred in it""" from PIL import Image, ImageChops, ImageDraw, ImageFilter, ImageFont if isinstance(self.path, io.BytesIO): self.path.seek(0) f = ImageFont.truetype(self.path, size * 4) g = Image.new("L", (size * 8, size * 8), 0) ImageDraw.Draw(g).text((size * 2, size * 2), chr(self.code[pad]), font=f, fill=255) g = g.crop(g.getbbox()) s = size / max(g.size) g = g.resize((max(1, round(g.width * s)), max(1, round(g.height * s))), Image.LANCZOS) icon = Image.new("L", (size, size), 0) icon.paste(g, ((size - g.width) // 2, (size - g.height) // 2)) outside = Image.new("L", (size + 4, size + 4), 255) # (a margin: every side's outside joins the corner's) outside.paste(ImageChops.invert(icon.point(lambda v: 255 if v > 127 else 0)), (2, 2)) ImageDraw.floodfill(outside, (0, 0), 128) # (what the corner reaches: the outside) shape = outside.crop((2, 2, size + 2, size + 2)).point(lambda v: 0 if v == 128 else 255) inner = shape.filter(ImageFilter.MinFilter(5)) # (the soft rim of the icon is not a hole) holes = ImageChops.multiply(inner, ImageChops.invert(icon)) return icon, shape, holes class Layer: """an image of a texture's: written whole, or its box (pixels) of it, or (over: a layer's name) drawn over that layer where it has any of itself; for a Switch button (0xff: always) on a pad input""" def __init__(self, name, texture, sw, pad, im, box=None, over=None): self.name, self.texture, self.sw, self.pad, self.im, self.box, self.over = name, texture, sw, pad, im, box, over def face_layers(b, tex, face, otf): """this face's images, as layers (in textures.bin's order), each a hand-made one if nx2/prompt_faces// has it""" from PIL import Image, ImageDraw import prompt_draw icons = Icons(face, otf) layers = [] def keep(sw, pad): # (the Switch face's default controls: the game's own textures) return pad in icons.code and not (face == "switch" and DEFAULT[SW[sw]] == pad) for name, sw in NOTES.items(): t = tex[name] w, h = t[2], t[3] for pad in PADS: if not keep(sw, pad): continue icon, shape, holes = icons.masks(pad, int(min(w, h) * 0.9)) im = Image.new("RGBA", (w, h), (0, 0, 0, 0)) o = ((w - icon.width) // 2, (h - icon.height) // 2) im.paste(Image.new("RGBA", icon.size, (255, 255, 255, 255)), o, shape) im.paste(Image.new("RGBA", icon.size, (0, 0, 0, 255)), o, holes) layers.append(Layer(f"{name}.{pad}.png", name, SW[sw], PADS.index(pad), im)) for name, sw in LETTERS.items(): t = tex[name] w, h = t[2], t[3] for pad in PADS: if not keep(sw, pad): continue icon, shape, holes = icons.masks(pad, int(min(w, h) * 0.9)) letter = holes if holes.getbbox() else icon # (an icon with no letter in it: its shape) im = Image.new("L", (w, h), 0) im.paste(letter, ((w - letter.width) // 2, (h - letter.height) // 2)) layers.append(Layer(f"{name}.{pad}.png", name, SW[sw], PADS.index(pad), im)) own = None if face != "switch": hand = override(face, PICTURES[0] + ".png") own = Image.open(hand).convert("RGBA") if hand else prompt_draw.picture( face, decode(b, tex[PICTURES[0]]).convert("RGBA"), Icons("switch", otf)) if own: layers += picture_layers(b, tex, own) for name in PICTURES if face != "switch" and not own else (): t = tex[name] im = decode(b, t) px = im.load() for pad, (cx, cy, r) in PICTURE_BUTTONS.items(): fill, ink = px[cx + r - 6, cy], (205, 205, 205, 255) ImageDraw.Draw(im).ellipse((cx - r, cy - r, cx + r, cy + r), fill=fill) size = int(2 * r * 0.92) icon, shape, holes = icons.masks(pad, size) disc = sum(i * n for i, n in enumerate(shape.histogram())) / 255 / size / size > 0.7 # (a disc with a letter in it: the letter) mark = holes if disc else icons.masks(pad, int(size * 0.7))[0] im.paste(Image.new("RGBA", mark.size, ink), (cx - mark.width // 2, cy - mark.height // 2), mark) xs, ys = [c[0] for c in PICTURE_BUTTONS.values()], [c[1] for c in PICTURE_BUTTONS.values()] r = max(c[2] for c in PICTURE_BUTTONS.values()) layers.append(Layer(name + ".png", name, 0xFF, 0, im, (min(xs) - r, min(ys) - r, max(xs) + r + 1, max(ys) + r + 1))) layers += guide_layers_of(b, tex, face) for layer in layers: hand = override(face, layer.name) if hand: im = Image.open(hand).convert(layer.im.mode) if im.size != layer.im.size: raise SystemExit(f"prompts: {hand} is {im.size[0]}x{im.size[1]}, the texture {layer.im.size[0]}x{layer.im.size[1]}") layer.im = im return layers def face_textures(b, tex, layers): """textures.bin's entries for these layers: [(Switch button, pad input, runs)], Switch button 0xff for those always written""" from PIL import ImageChops entries, made = [], {} for layer in layers: t, im = tex[layer.texture], layer.im made[layer.name] = im if layer.over: entries.append((layer.sw, layer.pad, runs(t, ImageChops.lighter(made[layer.over], im), mask=im))) elif layer.box: x0, y0, x1, y1 = layer.box if im.getbbox(): # (a hand-made image may reach further) bx0, by0, bx1, by1 = im.getbbox() x0, y0, x1, y1 = min(x0, bx0), min(y0, by0), max(x1, bx1), max(y1, by1) entries.append((layer.sw, layer.pad, runs(t, im, (x0, y0, x1, y1)))) else: entries.append((layer.sw, layer.pad, runs(t, im))) return entries # the groups a line frames, ending on the frame, when its pad input is one of them; the face buttons' labels (Switch A's line, under # one bar with B, X and Y's) always frame one: the d-pad when A is on it, else the face buttons GUIDE_GROUPS = (("north", "west", "east", "south"), ("up", "left", "right", "down")) GUIDE_FACE = "a" # the lines' router: a grid of GUIDE_GRID pixels; a bend costs GUIDE_BEND pixels of length, crossing a line that stays GUIDE_CROSS, # coming to a part other than from above GUIDE_SIDE; a line keeps GUIDE_CLEAR pixels off the other parts (the sticks GUIDE_CLEAR / 2) GUIDE_GRID, GUIDE_BEND, GUIDE_CROSS, GUIDE_SIDE, GUIDE_CLEAR = 8, 64, 40, 48, 16 def guide_at(part): """a part (picture pixels: x, y, r) in the lines' pixels""" return part[0] * GUIDE_SCALE + GUIDE_AT[0], part[1] * GUIDE_SCALE + GUIDE_AT[1], part[2] * GUIDE_SCALE def guide_frame_at(where, group): """a group's frame: its centre and its corners' distance from it (the lines' pixels)""" pts = [guide_at(where[p]) for p in group] cx, cy = sum(p[0] for p in pts) / 4, sum(p[1] for p in pts) / 4 return cx, cy, max(max(abs(x - cx), abs(y - cy)) + r for x, y, r in pts) + 8 # (from the centre to a corner, as the game's) def guide_route(size, bar, row, where, pad, stays, frame=None): """the way (points, the lines' pixels) of a line from a bar's row to the pad input's part, round the picture's other parts and with the fewest bends, crossing the lines that stay (L) as little as it can: it comes to a stick's centre, to a part just off its edge (above it best), or to a frame's corner (frame: centre, corners' distance)""" import heapq import math import numpy as np from PIL import ImageFilter g = GUIDE_GRID x0, y0 = BARS[0] if bar == 0 else BARS[1], row + 3 ilo, ihi = (0, (BARS[1] - x0) // g) if bar == 0 else (-((x0 - BARS[0]) // g), 0) jlo, jhi = -((y0 - 250) // g), (min(size[1] - 20, 1420) - y0) // g xs, ys = x0 + g * np.arange(ilo, ihi + 1), y0 + g * np.arange(jlo, jhi + 1) X, Y = np.meshgrid(xs, ys, indexing="ij") blocked = np.zeros(X.shape, bool) own = set(next((grp for grp in GUIDE_GROUPS if pad in grp), ())) if frame else {pad} for p, part in where.items(): if len(part) == 4: # (a box to keep off) (bx0, by0, _), (bx1, by1, _) = guide_at(part[:2] + (0,)), guide_at(part[2:] + (0,)) m = GUIDE_CLEAR / 2 blocked |= (X > bx0 - m) & (X < bx1 + m) & (Y > by0 - m) & (Y < by1 + m) continue x, y, r = guide_at(part) if p in own: continue clear = GUIDE_CLEAR / 2 if p in ("lstick", "rstick") else GUIDE_CLEAR blocked |= (X - x) ** 2 + (Y - y) ** 2 < (r + clear) ** 2 for grp in GUIDE_GROUPS: # (a group: inside its frame, which another line may draw, its own inside it) if all(p in where for p in grp): cx, cy, e = guide_frame_at(where, grp) if frame and pad in grp: blocked |= (X - cx) ** 2 + (Y - cy) ** 2 < (e - 8) ** 2 else: # (the frame: a square on its corner, its sides 1.15 corners' distance off in x + y) blocked |= np.abs(X - cx) + np.abs(Y - cy) < 1.15 * e + GUIDE_CLEAR blocked |= ((np.abs(X - BARS[0]) < 14) | (np.abs(X - BARS[1]) < 14)) & (Y != y0) # (off the bars but on its own row) st = np.asarray(stays.filter(ImageFilter.MaxFilter(9))) pen = np.where(st[np.clip(Y, 0, size[1] - 1), np.clip(X, 0, size[0] - 1)] > 0, GUIDE_CROSS, 0) dirs = ((1, 0), (-1, 0), (0, 1), (0, -1)) goals = {} # ((i, j, dir): (the end, extra cost)): the cell two before the end, coming the way it ends def goal(ex, ey, d, extra): i, j = round((ex - x0) / g) - 2 * dirs[d][0], round((ey - y0) / g) - 2 * dirs[d][1] cells = [(i + k * dirs[d][0], j + k * dirs[d][1]) for k in range(3)] if all(ilo <= ci <= ihi and jlo <= cj <= jhi and not blocked[ci - ilo, cj - jlo] for ci, cj in cells[:2]): goals[(i, j, d)] = ((ex, ey), extra) x, y, r = guide_at(where[pad]) if frame: # (a corner, or beside it on the grid's nearest line, along the frame's side) cx, cy, e = frame for k in (0, 1): sy, sx = y0 + g * (math.floor((cy - y0) / g) + k) - cy, x0 + g * (math.floor((cx - x0) / g) + k) - cx for (ex, ey), d, extra in (((cx + e - abs(sy), cy + sy), 1, 0), ((cx + sx, cy - e + abs(sx)), 2, GUIDE_SIDE), ((cx - e + abs(sy), cy + sy), 0, GUIDE_SIDE), ((cx + sx, cy + e - abs(sx)), 3, GUIDE_SIDE)): goal(ex, ey, d, extra) elif pad in ("lstick", "rstick"): for d in range(4): goal(x, y, d, 0) else: for (ex, ey), d, extra in (((x, y - r - 13), 2, 0), ((x, y + r + 13), 3, GUIDE_SIDE), ((x - r - 13, y), 0, GUIDE_SIDE), ((x + r + 13, y), 1, GUIDE_SIDE)): if r or d >= 2: goal(ex, ey, d, extra) start = (0, 0, 0 if bar == 0 else 1) best, came, heap = {start: 0}, {}, [(0, start)] while heap: c, (i, j, d) = heapq.heappop(heap) if c > best.get((i, j, d), 1e18): continue if (i, j, d) in goals: (ex, ey), _ = goals[(i, j, d)] way, k = [], (i, j, d) while k: way.append(k) k = came.get(k) way.reverse() pts = [(x0 + g * way[0][0], y0 + g * way[0][1])] for p0, p1 in zip(way, way[1:]): if p0[2] != p1[2]: pts.append((x0 + g * p0[0], y0 + g * p0[1])) pts.append((ex, ey) if len(pts) > 1 or abs(ey - y0) > g else (ex, y0)) # (one leg: on its row) if len(pts) > 2: # (the last leg's corner moved onto the end's line: its leg before only longer or shorter) cx_, cy_ = pts[-2] pts[-2] = (cx_, ey) if dirs[d][0] else (ex, cy_) return _square(pts) for nd in range(4): if nd == d ^ 1 or (nd != d and j == 0 and abs(i) < 5): # (no turning back; the label's stub first) continue ni, nj = i + dirs[nd][0], j + dirs[nd][1] if not (ilo <= ni <= ihi and jlo <= nj <= jhi) or blocked[ni - ilo, nj - jlo]: continue nc = c + g + pen[ni - ilo, nj - jlo] + (GUIDE_BEND if nd != d else 0) + goals.get((ni, nj, nd), (0, 0))[1] if nc < best.get((ni, nj, nd), 1e18): best[(ni, nj, nd)], came[(ni, nj, nd)] = nc, (i, j, d) heapq.heappush(heap, (nc, (ni, nj, nd))) return None def _square(pts): """a way's points with every leg level or upright (each point shares x or y with the one before)""" out = [pts[0]] for x, y in pts[1:]: px, py = out[-1] if (x, y) == (px, py): continue if x != px and y != py: out.append((px, y) if len(out) > 1 and out[-2][0] == px else (x, py)) out.append((x, y)) return out def guide_draw(size, way, dot): """a line along its way (6 pixels thick, square corners), ending on a dot""" from PIL import Image, ImageDraw im = Image.new("L", size, 0) d = ImageDraw.Draw(im) for (x0, y0), (x1, y1) in zip(way, way[1:]): x0, x1, y0, y1 = round(min(x0, x1)), round(max(x0, x1)), round(min(y0, y1)), round(max(y0, y1)) d.rectangle((x0 - 3, y0 - 3, x1 + 2, y1 + 2), fill=255) if dot: d.regular_polygon((round(way[-1][0]), round(way[-1][1]), 11), 5, rotation=18, fill=255) return im def guide_line(size, bar, row, where, pad, stays): """a leader line (L, size) from a bar's row to the pad input's part (where: the picture's parts), ending on a dot""" way = guide_route(size, bar, row, where, pad, stays) if not way: # (no way round: straight along the row, then up or down) x, y, r = guide_at(where[pad]) y += 0 if pad in ("lstick", "rstick") else -(r + 13) if row < y else r + 13 way = [(BARS[0] if bar == 0 else BARS[1], row + 3), (x, row + 3), (x, y)] return guide_draw(size, way, True) def guide_frame(size, bar, row, where, group, stays): """a group's frame (L, size; a square on its corner, rounded, round its parts) and its line from a bar's row""" import math from PIL import Image, ImageChops, ImageDraw cx, cy, e = guide_frame_at(where, group) ss, round_ = 4, 0.36 * e a = (e + (math.sqrt(2) - 1) * round_) / math.sqrt(2) n = int(2 * a * ss) + 8 sq = Image.new("L", (n, n), 0) ImageDraw.Draw(sq).rounded_rectangle((4, 4, n - 4, n - 4), radius=round_ * ss, outline=255, width=6 * ss) sq = sq.rotate(45, Image.BICUBIC, expand=True) sq = sq.resize((sq.width // ss, sq.height // ss), Image.LANCZOS) im = Image.new("L", size, 0) im.paste(sq, (round(cx - sq.width / 2), round(cy - sq.height / 2))) way = guide_route(size, bar, row, where, group[0], stays, (cx, cy, e - 3)) or \ [(BARS[0] if bar == 0 else BARS[1], row + 3), (cx + e - 3 if bar else cx - e + 3, row + 3)] return ImageChops.lighter(im, guide_draw(size, way, False)) def guide_layers(game, face): """the page's leader lines for this face's picture, from the game's (L images, GUIDES): each texture's lines that stay (the Switch face: the game's), and {texture: [(Switch button, {pad input: its line})]}""" from PIL import Image, ImageChops, ImageFilter where, size = parts(face), game[0].size keep = [] # (the game's bars, or on the Switch face all the game's but the lines that move) for im in game: k = Image.new("L", size, 0) if face == "switch": k.paste(im) else: k.paste(im.crop((0, 0, BARS[0], size[1])), (0, 0)) k.paste(im.crop((BARS[1], 0, size[0], size[1])), (BARS[1], 0)) keep.append(k) for tx, bar, row, end in GUIDE_LINES: # (the lines that stay first: those that move keep off them) if end not in SW and face != "switch": keep[tx] = ImageChops.lighter(keep[tx], guide_line(size, bar, row, where, end, keep[tx])) moving = {} for tx, bar, row, end in GUIDE_LINES: if end not in SW: continue stays, frames = keep[tx], {} for group in GUIDE_GROUPS: if all(p in where for p in group): im = guide_frame(size, bar, row, where, group, stays) frames.update((p, im) for p in group) lines = {pad: frames.get(pad) or (frames["south"] if end == GUIDE_FACE and "south" in frames else guide_line(size, bar, row, where, pad, stays)) for pad in PADS if pad in where} if face == "switch": # (the game's own line where the pad input's is hookshot's default's: taken out of what stays) own = lines[DEFAULT[SW[end]]] around = Image.new("L", size, 0) grown = own.filter(ImageFilter.MaxFilter(25 if DEFAULT[SW[end]] in sum(GUIDE_GROUPS, ()) else 15)) around.paste(grown.crop((BARS[0] + 1, 0, BARS[1], size[1])), (BARS[0] + 1, 0)) game_own = ImageChops.multiply(game[tx], around) for pad, im in lines.items(): if im is own: lines[pad] = game_own keep[tx] = ImageChops.subtract(keep[tx], around) moving.setdefault(tx, []).append((end, lines)) return keep, moving def guide_layers_of(b, tex, face): """the layers of the page's leader lines: each texture's lines that stay (0xff), then each Switch button's line for each pad input, drawn over them (host/prompts.c writes the entries in order)""" from PIL import Image, ImageChops if not parts(face): return [] game = [decode(b, tex[n]) for n in GUIDES] keep, moving = guide_layers(game, face) layers = [] for tx, name in enumerate(GUIDES): box = Image.new("L", game[tx].size, 0) # (every place a line is or may be) for im in [game[tx], keep[tx]] + [im for _, lines in moving.get(tx, []) for im in lines.values()]: box = ImageChops.lighter(box, im) layers.append(Layer(name + ".png", name, 0xFF, 0, keep[tx], box.getbbox())) for end, lines in moving.get(tx, []): for pad, im in lines.items(): layers.append(Layer(f"{name}.{end}.{pad}.png", name, SW[end], PADS.index(pad), im, over=name + ".png")) return layers def picture_layers(b, tex, im): """the layers putting a face's controller picture (1160x680 RGBA, drawn like Controller_Pro) in place of the Pro Controller: the picture, the page's tinted copy (each channel a line of the grey level, fitted on the game's pair) and the shadow (the outline's left part squeezed into ControllerSh_Pro1 and blurred, fitted on the game's)""" import numpy as np from PIL import Image, ImageFilter plain, tinted = (np.asarray(decode(b, tex[n]).convert("RGBA")).astype(float) for n in (PICTURES[0], PICTURE_TINT)) solid = plain[..., 3] > 250 grey = plain[solid][:, :3].sum(1) lines = [np.polyfit(grey, tinted[solid][:, c], 1) for c in range(3)] px = np.asarray(im).astype(float) level = px[..., :3].sum(2) tint = np.dstack([np.clip(np.polyval(lines[c], level), 0, 255) for c in range(3)] + [px[..., 3]]) sh = tex[PICTURE_SHADOW] shadow = im.getchannel("A").crop((40, 0, 480, 680)).resize((sh[2], sh[3])).filter(ImageFilter.GaussianBlur(24)) shadow = shadow.point(lambda v: round(v * 0.7)) return [Layer(PICTURES[0] + ".png", PICTURES[0], 0xFF, 0, im), Layer(PICTURE_TINT + ".png", PICTURE_TINT, 0xFF, 0, Image.fromarray(tint.astype("uint8"))), Layer(PICTURE_SHADOW + ".png", PICTURE_SHADOW, 0xFF, 0, shadow)] def write_textures(path, entries): with open(path, "wb") as f: f.write(b"HPTX" + struct.pack("= 0: starts.append(at) at += 4 return [(a, z) for a, z in zip(starts, starts[1:] + [len(blarc)]) if blarc.find(b"\0" + name.encode() + b"\0RootPane\0", a, z) > 0] def ring_entries(blarc): """ring.bin's entries: [(group, Switch button placed, place, runs)], or None when the layout is not the one known""" def pane(k): # (a pane's record: its transform (rotation, scale, translation) first, then parent, size, material) return 0x968 + k * 0x44 - 0x28 copies = [a for a, _ in layout_copies(blarc, "DoCommand_00")] if not copies: return None lay = blarc[copies[0]:copies[0] + 0x2000] if any(struct.unpack_from("<2f", lay, pane(base) + 0x2c) != (320.0, 320.0) for base, _, _ in RING_GROUPS.values()): return None out = [] for group, (base, small, btn) in RING_GROUPS.items(): for place, xy in RING_PLACES.items(): tb, _, tbtn = RING_GROUPS[RING_AT[place]] parts = [(pane(base) + 0x10, lay[pane(tb) + 0x10:pane(tb) + 0x18]), # (the base's scale, its material) (pane(base) + 0x3c, lay[pane(tb) + 0x3c:pane(tb) + 0x40]), (pane(btn) + 0x1c, lay[pane(tbtn) + 0x1c:pane(tbtn) + 0x24])] # (the button, its banner and text) parts += [(pane(btn + c), lay[pane(tbtn + c):pane(tbtn + c) + 0x24]) for c in range(1, 6)] out.append((SW[group], SW[group], list(RING_PLACES).index(place), [[c + o, bytearray(v)] for c in copies for o, v in parts])) for other, k in small.items(): out.append((SW[group], SW[other], list(RING_PLACES).index(place), [[c + pane(k) + 0x1c, bytearray(struct.pack("<2f", *xy))] for c in copies])) return out def write_ring(path, entries): with open(path, "wb") as f: f.write(b"HPRG" + struct.pack("/hint.bin: "HPHN", u32 count of PADS, each pad input's icon's extra width (f32, layout units), u32 # panes, then each: u32 offset (its x in prompts/Common.blarc), u8 slot (0-4 the notes' icons in the hint's order, 5 the text), # u8 0 x3, f32 its x HINTS = ("PaGuideKeyOcarinaInput_00", "PaGuideKeyOcarinaInput_01", "PaGuideKeyCapture_00") HINT_PANES = ["T_Icon_00", "T_Icon_01", "T_Icon_02", "T_Icon_03", "T_Icon_04", "T_Guide_00"] HINT_FITS = 720 # (the widest advance the game's spacing holds: its own shoulder icons; font units) HINT_SCALE = 0.1 # (layout units per font unit: the icons' panes draw the font at 100 per 1000 em) def layout_panes(lay): """a BFFH layout's panes: {name: its record's offset} (the record: rotation, scale at +0x10, translation at +0x1c, size at +0x2c); the header gives the offsets of the names' table (+0x118), the records' table (+0x120) and the count (+0x130)""" names, recs = struct.unpack_from(" len(lay): continue v = struct.unpack_from(f"<{n}f", lay, at) if v[0] in xs and all(k == v[0] for k in v[::2]) and not any(v[1::2]): out += [(a + at + 8 * k, xs[v[0]], v[0]) for k in range(n // 2)] return out or None def write_hint(path, otf, panes): from fontTools.ttLib import TTFont hmtx = TTFont(io.BytesIO(otf))["hmtx"] with open(path, "wb") as f: f.write(b"HPHN" + struct.pack("/game/") a = ap.parse_args() dictionary = zs_dictionary(a.romfs) game_font = read_zs(a.romfs, FONT, dictionary) key, otf = unwrap(game_font) textures = None if not a.no_textures: try: import astc_encoder # noqa: F401 import PIL # noqa: F401 import texture2ddecoder # noqa: F401 textures = read_zs(a.romfs, TEXTURES, dictionary) except ImportError: print("prompts: the textures keep the Switch's icons (no astc_encoder, Pillow or texture2ddecoder)", file=sys.stderr) if textures: os.makedirs(a.out, exist_ok=True) with open(os.path.join(a.out, "Common.bntx"), "wb") as f: f.write(textures) tex = bntx_textures(textures) blarc = read_zs(a.romfs, LAYOUTS, dictionary) hint = hint_panes(blarc) if not hint: print("prompts: the ocarina's hint keeps the game's spacing (its layout is not the one known)", file=sys.stderr) largest = 0 for face in a.faces.split(","): d = os.path.join(a.out, face) os.makedirs(d, exist_ok=True) plain = face_font(otf, face) font = wrap(key, plain) if hint: write_hint(os.path.join(d, "hint.bin"), plain, hint) largest = max(largest, len(font)) with open(os.path.join(d, "External.bfotf"), "wb") as f: f.write(font) if textures: layers = face_layers(textures, tex, face, otf) write_textures(os.path.join(d, "textures.bin"), face_textures(textures, tex, layers)) if a.dump: dump(a.dump, face, textures, tex, layers) print(f"prompts: {face}", file=sys.stderr) ring = ring_entries(blarc) if ring or hint: with open(os.path.join(a.out, "Common.blarc"), "wb") as f: f.write(blarc) if ring: write_ring(os.path.join(a.out, "ring.bin"), ring) print("prompts: the Talk ring", file=sys.stderr) else: print("prompts: the Talk ring keeps the Switch's places (its layout is not the one known)", file=sys.stderr) table, path = size_table(a.romfs, dictionary, len(game_font), largest), os.path.join(a.out, "rsizetable.zs") if table: import zstandard with open(path, "wb") as f: f.write(zstandard.ZstdCompressor(level=3).compress(table)) print(f"prompts: the size table lets the font have {largest} bytes", file=sys.stderr) elif os.path.exists(path): os.remove(path) if __name__ == "__main__": main()