mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-19 16:20:01 -04:00
In-game mods (GameBanana browser, search and installs, with the Mask Salesman), code-patch and cheat mods, the devs' test18 to test45 work (full performance overlay, frame pacing on, steady frame rate, save export and import, bug capture, new options rows, Setup's Mods page and Add to Steam), occlusion queries and new defaults, Auto resolution that scales, replay helper threads and robust buffers off by default, the options in sections.
210 lines
11 KiB
Python
210 lines
11 KiB
Python
"""The standalone build's lift-time pieces (lift.py --standalone): which SDK functions are replaced by the native
|
|
runtime in host/, the stubs for the OS-facing ones it does not have yet, and the boot constants hos_main.c reads.
|
|
|
|
Adapted from a64-kit's build.py (TOTKPort e88744a: implemented, gen_stubs, OS_NAMESPACES, nested_name, os_facing,
|
|
thread_hooks, write_image_c), for MKW's Eden-layout image in the 64-bit window.
|
|
|
|
The model is TOTKPort's: the SDK is replaced at the nn:: boundary where it talks to system services. A function the
|
|
runtime defines as `HLE(<mangled name>)` / `void imp_<mangled name>(Ctx *c)` is hooked where the game imports it and
|
|
at the sdk's own definition. Every other free function of an OS-facing namespace the game imports gets a generated
|
|
stub that logs its first call and returns 0. The rest of the SDK (libc, nn::util, nn::mem, std, the NVN driver)
|
|
runs as lifted code.
|
|
"""
|
|
import os
|
|
import re
|
|
import struct
|
|
import subprocess
|
|
import sys
|
|
|
|
import kit
|
|
import toolchain
|
|
|
|
HOST = os.path.join(kit.HERE, "..", "host")
|
|
|
|
# a64-kit's list (TOTK), plus the Switch 2 SDK's service namespaces MKW imports (imports.py)
|
|
OS_NAMESPACES = {"audio", "hid", "oe", "prepo", "nfp", "time", "account", "vi", "swkbd", "pl", "err", "codec", "diag",
|
|
"detail", "settings", "ns", "applet", "album", "friends", "nifm", "ssl", "socket", "ec", "bcat",
|
|
"web", "ldn", "irsensor", "aoc", "olsc", "pctl", "apm", "ro",
|
|
"chat", "camera", "gs", "movie", "npns", "news", "fatal", "lbl", "capture", "syncpt"}
|
|
|
|
|
|
def implemented(host=HOST):
|
|
"""C names of the HLE functions in host/*.c: `void imp_x(Ctx` definitions and `HLE(x)` macros"""
|
|
names = set()
|
|
for f in os.listdir(host):
|
|
if f.endswith(".c"):
|
|
src = open(os.path.join(host, f), errors="replace").read()
|
|
names |= set(re.findall(r"^void (imp_\w+)\(Ctx", src, re.M))
|
|
names |= {"imp_" + n for n in re.findall(r"^HLE\((\w+)\)", src, re.M)}
|
|
return names
|
|
|
|
|
|
def nested_name(sym):
|
|
"""The components of an Itanium nested name (_ZN[K]<len><id>...E), or None if it is not that simple shape"""
|
|
m = re.match(r"_ZNK?", sym)
|
|
if not m:
|
|
return None
|
|
i, parts = m.end(), []
|
|
while i < len(sym) and sym[i].isdigit():
|
|
j = i
|
|
while sym[j].isdigit():
|
|
j += 1
|
|
n = int(sym[i:j])
|
|
parts.append(sym[j:j + n])
|
|
i = j + n
|
|
return parts if i < len(sym) and sym[i] == "E" else None
|
|
|
|
|
|
def os_facing(sym):
|
|
"""A free function of an OS-facing namespace: nn::<OS namespace>::f (class members and data stay lifted)"""
|
|
parts = nested_name(sym)
|
|
# (the Switch 2 SDK keeps many service calls a level down: nn::camera::sdk::CreateCameraContext)
|
|
return bool(parts) and parts[0] == "nn" and parts[1:2] and parts[1] in OS_NAMESPACES and (
|
|
len(parts) == 3 or (len(parts) == 4 and parts[2] == "sdk"))
|
|
|
|
|
|
def nvn_exports(modules):
|
|
"""Every NVN function the sdk defines (the console driver's C entry points): all are hooked to host/nvn2.c's
|
|
procs (gen/nvn2_exports.c), so the driver never runs, whether the game reaches a function through
|
|
nvnDeviceGetProcAddress or imports it (nvnNativeImageInitialize, for one)"""
|
|
sdk = next(m for m in modules if m.name == "sdk")
|
|
have = {n[4:] for n in implemented()}
|
|
return sorted(s["name"] for s in sdk.symbols if s["defined"] and s["name"] and nvn_proc_name(s["name"])
|
|
and s["type"] == "func" and s["name"] not in have)
|
|
|
|
|
|
def nvn_proc_name(sym):
|
|
"""The NVN proc a symbol is: a C name (nvnFoo), or a C++ one (_Z<len>nvnFoo<args>: the Switch 2 driver exports
|
|
some, nvnNativeImageInitialize among them); None for anything else"""
|
|
if sym.startswith("nvn"):
|
|
return sym
|
|
m = re.match(r"_Z(\d+)(nvn\w+)", sym)
|
|
return m.group(2)[:int(m.group(1))] if m else None
|
|
|
|
|
|
def write_nvn_exports(gen, names):
|
|
lines = ["/* Generated by nx2/lift.py --standalone: the sdk's NVN functions, routed to host/nvn2.c. */",
|
|
'#include "a64.h"', "void nvn2_dispatch(Ctx *c, const char *name, void **cache);", ""]
|
|
for n in names:
|
|
lines.append('void imp_%s(Ctx *A64_NOESC c) { static void *k[2]; nvn2_dispatch(c, "%s", k); }'
|
|
% (n, nvn_proc_name(n)))
|
|
text = "\n".join(lines) + "\n"
|
|
path = os.path.join(gen, "nvn2_exports.c")
|
|
if not os.path.exists(path) or open(path).read() != text:
|
|
open(path, "w", newline="\n").write(text)
|
|
|
|
|
|
def hle_set(modules):
|
|
"""(HLE'd symbols, stubbed symbols): the runtime's imp_ functions some module has, the sdk's NVN functions, and
|
|
the OS-facing functions the game modules import that it does not define"""
|
|
hle = {n[4:] for n in implemented()}
|
|
known = {s["name"] for m in modules for s in m.symbols if s["name"]}
|
|
# (TOTK's SDK functions that MKW's newer SDK no longer has: their HLE stays unused; a typo shows here too)
|
|
missing = sorted(hle - known)
|
|
if missing:
|
|
print("host/ defines %d imp_ functions for symbols no module has (unused):\n %s"
|
|
% (len(missing), "\n ".join(missing)))
|
|
hle -= set(missing)
|
|
kind = {s["name"]: s["type"] for m in modules for s in m.symbols if s["defined"] and s["name"]}
|
|
stubbed = set()
|
|
for m in modules:
|
|
if m.name not in ("sdk", "rtld"): # (every game module: main and the subsdk* the exefs has)
|
|
stubbed |= {s["name"] for s in m.symbols if not s["defined"] and s["name"] and os_facing(s["name"])
|
|
and kind.get(s["name"], "func") == "func"}
|
|
return hle | set(nvn_exports(modules)), stubbed - hle
|
|
|
|
|
|
def demangle(syms):
|
|
try:
|
|
out = subprocess.run([toolchain.find("llvm-cxxfilt")], input="\n".join(syms), capture_output=True,
|
|
text=True).stdout.splitlines()
|
|
return dict(zip(syms, out)) if len(out) == len(syms) else {s: s for s in syms}
|
|
except (OSError, TypeError):
|
|
return {s: s for s in syms}
|
|
|
|
|
|
def gen_stubs(gen, meta):
|
|
have = implemented() | {"imp_" + h["sym"] for h in meta["hooks"] if nvn_proc_name(h["sym"])}
|
|
hooks = [h for h in meta["hooks"] if h["c"] not in have]
|
|
dm = demangle([h["sym"] for h in hooks])
|
|
lines = ['#include "a64.h"', "void a64_unimplemented(Ctx *A64_NOESC c, const char *sym, int *seen);", ""]
|
|
for h in hooks:
|
|
lines.append("/* %s */" % dm[h["sym"]].replace("*/", "* /"))
|
|
lines.append('void %s(Ctx *A64_NOESC c) { static int seen; a64_unimplemented(c, "%s", &seen); }'
|
|
% (h["c"], h["sym"]))
|
|
text = "\n".join(lines) + "\n"
|
|
path = os.path.join(gen, "a64_stubs.c")
|
|
if not os.path.exists(path) or open(path).read() != text:
|
|
open(path, "w", newline="\n").write(text)
|
|
return len(hooks)
|
|
|
|
|
|
def thread_hooks(img, meta):
|
|
"""The SDK thread entry's start and exit hooks (nnmusl's per-thread TLS setup and teardown): the entry starts
|
|
`mrs x1, tpidrro_el0; add x1, x1, #0x1f8; str x0, [x1]` and loads each hook through a GOT slot"""
|
|
for m in meta["modules"]:
|
|
for pc in range(m["base"], m["base"] + m["size"] - 12, 4):
|
|
if not img.in_code(pc) or img.word(pc) != 0xD53BD061:
|
|
continue
|
|
if img.word(pc + 4) != 0x9107E021 or img.word(pc + 8) != 0xF9000020:
|
|
continue
|
|
hooks, page = [], {}
|
|
for q in range(pc + 12, pc + 12 + 4 * 40, 4):
|
|
w = img.word(q)
|
|
if (w & 0x9F000000) == 0x90000000: # ADRP
|
|
imm = ((w >> 3) & 0x1FFFFC) | ((w >> 29) & 3)
|
|
imm = imm - (1 << 21) if imm & (1 << 20) else imm
|
|
page[w & 31] = (q & ~0xFFF) + (imm << 12)
|
|
elif (w & 0xFFC00000) == 0xF9400000 and ((w >> 5) & 31) in page: # LDR Xt, [Xn, #imm]
|
|
got = page.pop((w >> 5) & 31) + ((w >> 10) & 0xFFF) * 8
|
|
hooks.append(struct.unpack("<Q", img.read(got, 8))[0])
|
|
if len(hooks) == 2:
|
|
return hooks
|
|
return [0, 0]
|
|
|
|
|
|
def write_image_c(gen, img, meta, exefs):
|
|
"""Boot constants for hos_main.c: the image span, each module's DT_INIT in rtld's order (rtld itself is not run:
|
|
the image is already relocated), the entry points, and the main thread's stack, priority and core (main.npdm)"""
|
|
ex = meta["exports"]
|
|
mods = [m for m in meta["modules"] if m["init"] and m["name"] != "rtld"]
|
|
npdm = open(os.path.join(exefs, "main.npdm"), "rb").read()
|
|
if npdm[:4] != b"META":
|
|
sys.exit("main.npdm: no META header")
|
|
prio, core = npdm[0xE], npdm[0xF]
|
|
stack = int.from_bytes(npdm[0x1C:0x20], "little")
|
|
aoff, asz = int.from_bytes(npdm[0x70:0x74], "little"), int.from_bytes(npdm[0x74:0x78], "little")
|
|
aci = npdm[aoff:aoff + asz]
|
|
program_id = int.from_bytes(aci[0x10:0x18], "little") if aci[:4] == b"ACI0" else 0
|
|
main_build = next((m["build_id"] for m in meta["modules"] if m["name"] == "main"), "")
|
|
lines = ["/* Generated by nx2/lift.py --standalone. */", "#include <stdint.h>",
|
|
"const uint64_t hos_main_stack_size = %#xULL;" % stack,
|
|
"const int hos_main_priority = %d, hos_main_core = %d;" % (prio, core),
|
|
"const uint64_t hos_image_base = %#xULL, hos_image_end = %#xULL;" % (meta["base"], meta["end"]),
|
|
"const int hos_module_count = %d;" % len(mods),
|
|
"const uint64_t hos_module_init[] = {%s};" % ", ".join("%#xULL" % m["init"] for m in mods),
|
|
"const char *const hos_module_name[] = {%s};" % ", ".join('"%s"' % m["name"] for m in mods),
|
|
"const uint64_t hos_entry_nninitStartup = %#xULL;" % ex.get("nninitStartup", 0),
|
|
"const uint64_t hos_entry_nnMain = %#xULL;" % ex.get("nnMain", 0),
|
|
"const uint64_t hos_program_id = %#xULL; /* (main.npdm's ACI0) */" % program_id,
|
|
'const char hos_main_build_id[] = "%s";' % main_build]
|
|
for sym in ("_init_libc0", "_init_libc1", "_init_libc2", "nnosInitialize", "nninitInitializeSdkModule",
|
|
"nvnBootstrapLoaderInternal"):
|
|
lines.append("const uint64_t hos_entry_%s = %#xULL;" % (sym.lstrip("_"), ex.get(sym, 0)))
|
|
start_hook, exit_hook = thread_hooks(img, meta)
|
|
lines.append("const uint64_t hos_thread_start_hook = %#xULL, hos_thread_exit_hook = %#xULL;"
|
|
% (start_hook, exit_hook))
|
|
# (Triforce) for the mods' cheat files (host/mod_cheats.c): where the module main begins, which a cheat's addresses
|
|
# count from, and each module's code, which a cheat must not write (it is compiled ahead of time)
|
|
code = [(m["segments"][0]["addr"], m["segments"][0]["addr"] + m["segments"][0]["size"]) for m in meta["modules"]]
|
|
lines += ["const uint64_t hos_main_base = %#xULL; /* (the module main) */"
|
|
% next((m["base"] for m in meta["modules"] if m["name"] == "main"), meta["base"]),
|
|
"const int hos_code_count = %d;" % len(code),
|
|
"const uint64_t hos_code[][2] = {%s}; /* (each module's .text: from, to) */"
|
|
% ", ".join("{%#xULL, %#xULL}" % span for span in code)]
|
|
text = "\n".join(lines) + "\n"
|
|
path = os.path.join(gen, "hos_image.c")
|
|
if not os.path.exists(path) or open(path).read() != text:
|
|
open(path, "w", newline="\n").write(text)
|
|
return start_hook, exit_hook
|