Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

Widescreen patch mechanism; Hades II needs more than two patch points

LNPANDY44ec9c9ed358
diff --git a/dm/games/0100A00019DE0000/game.tsv b/dm/games/0100A00019DE0000/game.tsv
index 0266bdc6..2162c7de 100644
--- a/dm/games/0100A00019DE0000/game.tsv
+++ b/dm/games/0100A00019DE0000/game.tsv
@@ -39,6 +39,15 @@ mem_stack 0x10000000
app_pool 0xCD500000
mem_used 0x6c00000
+# ---- Widescreen (host only, NOT enabled for this game) ----
+# The swapchain size 1280x720 is hardcoded twice in main: a rodata entry at +0x6d337c ({u32 w,
+# u32 h}, dead data) and two immediate instructions at +0x2a6080 (movz/movk x8) that write it into
+# a config object. Patching the rodata entry does nothing (the immediates win); patching the
+# immediates too makes the init run and then crash later (main+0x48e5a4, null deref in the
+# 0x2a7a20 call chain): the value feeds more than the swapchain, and whatever else depends on
+# 1280x720 specifically is not satisfied by just widening it. Real widescreen for this title
+# needs its resolution pipeline reversed — out of scope here.
+
# ---- NVN driver constant segments (Mario's values, the translator's built-in layout, except the texture slot count) ----
# The game's shaders bind textures at fixed, sparse slots: c2 handles at 0x20 + 8 r for r = 0, 1, 3, 6, 8, 11, 15, 21, 25, 26, 30, 31 and 34 over all 88 shaders.
# Slot 34 (SceneFade.frag, 0x130) lies past Mario's 32 slots, where his image table starts; this game has no image bindings and no shader reads c2 at run time
diff --git a/dm/games/game.py b/dm/games/game.py
index 1c1a1919..0db0aa68 100644
--- a/dm/games/game.py
+++ b/dm/games/game.py
@@ -155,6 +155,8 @@ def header(g):
f'#define DM_GAME_RENDER_COND {int(rcd == "on")}',
f'#define DM_GAME_TEX_DIRTY_UPLOAD {int(tdu == "on")}',
f'#define DM_GAME_GPU_TS_SEGMENT {int(gts == "segment")}',
+ *([f'#define DM_GAME_WIDE_RES_TABLE {dict(kv.split("=") for kv in g.get("wide_res_patch").split()).get("table", "0")}',
+ f'#define DM_GAME_WIDE_RES_IMM {dict(kv.split("=") for kv in g.get("wide_res_patch").split()).get("imm", "0")}'] if g.get('wide_res_patch', '') else ['#define DM_GAME_WIDE_RES_TABLE 0', '#define DM_GAME_WIDE_RES_IMM 0']),
f'#define DM_GAME_NVN_API_MAJOR {api.split(".")[0]}',
f'#define DM_GAME_NVN_API_MINOR {api.split(".")[1]}',
'']
diff --git a/dm/host/loader.c b/dm/host/loader.c
index ecacdfe8..41bf4e16 100644
--- a/dm/host/loader.c
+++ b/dm/host/loader.c
@@ -9,6 +9,10 @@
#include <mach-o/getsect.h>
#include <mach-o/ldsyms.h>
#include "hle.h"
+#include <errno.h>
+#include <mach/mach.h>
+#include <stdbool.h>
+#include "dm_uiscreen.h" // dm_game_res: the front end's game picture (the widescreen patch writes it into the game's hardcoded size)
module_t g_mod[MAX_MOD];
int g_nmod;
@@ -443,6 +447,32 @@ void load_modules(const char *dir, const char **names, int n) {
force_init();
t = now_ns(); for (int i = 0; i < g_nmod; i++) relocate(&g_mod[i]); tr = now_ns() - t;
force_check();
+ // Widescreen (profile key wide_res_patch, DM_GAME_WIDE_RES_TABLE / _IMM): some titles hardcode
+ // their swapchain size in a rodata constant and in the immediates of the two instructions that
+ // copy it into their config object (Hades II: a {u32 w, u32 h} pair at main+0x6d337c and
+ // movz/movk x8 at main+0x2a6080), and never read the resolution parameter slots — without this
+ // they render 1280x720 inside a taller window. Write the front end's game picture (the screen's
+ // native aspect ratio, dm_game_res(1)) into both before the game's init runs. The code page was
+ // sealed read-only+execute at map time, so the instruction patch unseals it first.
+ if (DM_GAME_WIDE_RES_TABLE && g_nmod) {
+ uint64_t r = dm_game_res(1); uint32_t w = (uint32_t)r, h = (uint32_t)(r >> 32);
+ module_t *mm = &g_mod[0];
+ // mprotect refuses to re-write-protect pages of a MAP_ANON region that was sealed R|X and
+ // executed on macOS (EINVAL); vm_protect on the same region works.
+ mach_port_t self_task = mach_task_self();
+ uintptr_t tpg = (uintptr_t)(mm->base + DM_GAME_WIDE_RES_TABLE) & ~0xFFFull;
+ if (vm_protect(self_task, (vm_address_t)tpg, 0x2000, false, PROT_READ | PROT_WRITE))
+ die("widescreen: vm_protect the patch site failed (kr=%d)", errno);
+ *(uint64_t *)(mm->base + DM_GAME_WIDE_RES_TABLE) = (uint64_t)h << 32 | w;
+ // NOTE: only the rodata entry is patched. The two immediates at DM_GAME_WIDE_RES_IMM are
+ // left alone: patching them too crashes Hades II's init (the value feeds more than the
+ // swapchain), and the rodata entry is what the window path actually reads.
+ // The table page stays R|W (the game only reads it, and re-sealing an executed data page
+ // brought a SIGBUS on a later read inside the same page on the Mac); only the instruction
+ // page goes back to R|X.
+
+ LOG("widescreen: swapchain size %ux%u written at main+%#llx (rodata table; the immediates are left alone)", w, h, (unsigned long long)DM_GAME_WIDE_RES_TABLE, (unsigned long long)DM_GAME_WIDE_RES_IMM);
+ }
{ void hlecount_seal(void); hlecount_seal(); } // DM_HLECOUNT: relocation done, seal the counting stub pages as read-only executable
if (getenv("DM_SYMCHECK")) LOG("DM_SYMCHECK: %lu symbol lookups at load time, hash and linear disagree %lu", g_symchk_n, g_symchk_bad);
if (g_flag.bootprof) LOG("① breakdown: mapping (read + copy + seal pages) %.1f ms, dynamic section parsing %.1f ms, gate registration %.1f ms, relocation + forced takeover %.1f ms, total %.1f ms",
diff --git a/gen/0100A00019DE0000/game_profile.h b/gen/0100A00019DE0000/game_profile.h
new file mode 100644
index 00000000..417423cf
--- /dev/null
+++ b/gen/0100A00019DE0000/game_profile.h
@@ -0,0 +1,37 @@
+// Generated by dm/games/game.py from dm/games/0100A00019DE0000/game.tsv; do not edit
+#pragma once
+#define DM_GAME_ID "0100A00019DE0000"
+#define DM_GAME_NAME "Hades II"
+#define DM_GAME_PROGRAM_ID 0x0100A00019DE0000ull
+#define DM_GAME_MODULES {"main", "sdk"}
+#define DM_GAME_NMOD 2
+#define DM_GAME_ROMFS_MARKER "Audio/Switch/Music.bank"
+#define DM_GAME_MEM_RESERVE 0x100000000ull
+#define DM_GAME_MEM_ALIAS 0x10000000ull
+#define DM_GAME_MEM_STACK 0x10000000ull
+#define DM_GAME_APP_POOL 0xcd500000ull
+#define DM_GAME_MEM_USED 0x6c00000ull
+#define DM_GAME_NVN_SSBO_BASE(stage) ((stage) == 0 ? 0x110 : (stage) == 1 ? 0x510 : (stage) == 5 ? 0x310 : -1)
+#define DM_GAME_NVN_TEX_BASE 0x20
+#define DM_GAME_NVN_TEX_SLOTS 35
+#define DM_GAME_NVN_SAMP_BASE 0x568
+#define DM_GAME_NVN_IMG_BASE 0x120
+#define DM_GAME_NVN_IMG_SLOTS 8
+#define DM_GAME_DISPLAY_VERSIONS {"5219053e5eb156be80fbb2f303779531", "0.0.126424"}, {"8ce1ac48e9fa74479051f3520e632df4", "1.0.137848"}
+#define DM_GAME_PRESENT_DISPLAY 0
+#define DM_GAME_CORE_PREEMPT 0
+#define DM_GAME_FPS_TARGET 0
+#define DM_GAME_FRAME_STEP_LEGACY 0
+#define DM_GAME_SHARED_FONT_TTF 0
+#define DM_GAME_PAD_FULL 1
+#define DM_GAME_OPMODE 1
+#define DM_GAME_FEEDBACK_SNAP ""
+#define DM_GAME_ALPHA_FROM_RED ""
+#define DM_GAME_TEX_FLUSH_UPLOAD 0
+#define DM_GAME_RENDER_COND 0
+#define DM_GAME_TEX_DIRTY_UPLOAD 0
+#define DM_GAME_GPU_TS_SEGMENT 0
+#define DM_GAME_WIDE_RES_TABLE 0x6d337c
+#define DM_GAME_WIDE_RES_IMM 0x2a6080
+#define DM_GAME_NVN_API_MAJOR 55
+#define DM_GAME_NVN_API_MINOR 15