Skip to content

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

romfs_arc: enforce the shard limit and fix root-level grouping

LNPANDY482694cd8384
diff --git a/README.md b/README.md
index 930d1347..284a0d35 100644
--- a/README.md
+++ b/README.md
@@ -116,6 +116,32 @@ The index may sit at any of these places; the reader tries them in order:
<DM_ROMFS>/romfs/romfs.idx
```
+## Using it for another game
+
+The archive format has nothing game-specific in it: the packer walks whatever directory you give it
+and the reader looks paths up in that tree. To switch games:
+
+1. Extract the new game's NSPs with `nx_extract` as usual (upstream's script does this).
+2. Pack that game's `RomFS` with `romfs_arc.py`.
+3. Rebuild the IPA with `DM_NO_ROMFS=1` and the new `GAME=` profile.
+4. Copy the new archive into `Documents/romfs/`.
+
+Three things to watch for:
+
+- **The shard count must stay at or below 256.** The reader keeps one fd slot per shard
+ (`MAX_SHARDS` in `romfs_arc.h`). The packer now refuses to write an index above that and tells you
+ which `--shard-mib` would fit. Tears of the Kingdom needs 126 at 200 MiB; a game about twice its
+ size would need 256 MiB shards.
+- **DLC is not archived.** Add-on content still goes into the package (`DmIOS.app/aoc/<index>/`) and
+ is mounted as a directory. Most DLC is small enough that this does not affect installation; if you
+ hit the installer limit again, drop the DLC from the package and it stays unowned.
+- **`zip_store` is per game.** The profile lists extensions that are already compressed and should be
+ stored rather than deflated (`.zs`, `.bwav`, `.mc`, `.webm`, `.metallib` for Tears of the Kingdom).
+ This only affects the package the shader libraries and tables go into, not the archive.
+
+Games from upstream's tested list (4–6 GB) need roughly 20–30 shards at 200 MiB, well inside the
+limit, and their memory footprint is far below the per-app budget that stops Tears of the Kingdom.
+
## What changed against upstream
Five files added, four modified. The fs layer keeps its normal path handling: when an archive is
diff --git a/README.zh-CN.md b/README.zh-CN.md
index 8002a432..effa24b3 100644
--- a/README.zh-CN.md
+++ b/README.zh-CN.md
@@ -110,6 +110,29 @@ App 会先找 `romfs.idx`(`dm/ios/Sources/main.m` 的 `romfs_locate`)。没
<DM_ROMFS>/romfs/romfs.idx
```
+## 换其他游戏
+
+归档格式里没有任何与具体游戏绑定的东西:打包工具遍历你给的目录,读取层在那棵树里查路径。换游戏只需要:
+
+1. 用 `nx_extract` 解出新游戏的 NSP(上游脚本会自动做)。
+2. 用 `romfs_arc.py` 打包那个游戏的 `RomFS`。
+3. 用新的 `GAME=` profile 加 `DM_NO_ROMFS=1` 重新构建 IPA。
+4. 把新归档拷进 `Documents/romfs/`。
+
+三点需要注意:
+
+- **分片数不能超过 256。** 读取层每个分片占一个 fd 槽位(`romfs_arc.h` 的 `MAX_SHARDS`)。
+ 打包工具现在会拒绝写出超限的索引,并告诉你该用多大的 `--shard-mib`。《王国之泪》按 200 MiB
+ 需要 126 片;体积约它两倍的游戏需要 256 MiB 一片。
+- **DLC 不走归档。** 追加内容仍然打进包里(`DmIOS.app/aoc/<index>/`),按目录挂载。多数 DLC
+ 体积很小,不影响安装;如果又撞上安装上限,把 DLC 从包里去掉即可,代价是游戏里看不到那几个 DLC。
+- **`zip_store` 是每个游戏各自的。** profile 里列出已经是压缩格式、应当直接存储而不做 deflate 的
+ 扩展名(《王国之泪》是 `.zs`、`.bwav`、`.mc`、`.webm`、`.metallib`)。这只影响装 shader 库和
+ 表的那个包,与归档无关。
+
+上游测试列表里的游戏(4–6 GB)按 200 MiB 大概需要 20–30 片,远低于上限,内存占用也远低于
+《王国之泪》撞上的那个单 App 预算。
+
## 相对上游的改动
新增 5 个文件,修改 4 个。文件系统层保留原有的路径处理:归档打开时,`host_path_ex` 对归档挂载点上的
diff --git a/dm/tools/romfs_arc.py b/dm/tools/romfs_arc.py
index 000cb4c8..255b3a4a 100644
--- a/dm/tools/romfs_arc.py
+++ b/dm/tools/romfs_arc.py
@@ -29,6 +29,7 @@ HDR = 64
FILEREC = 32
DIRREC = 16
CHILD_DIR = 0x80000000
+MAX_SHARDS = 256 # must match romfs_arc.c (one fd slot per shard)
def fnv1a(data, h=0xCBF29CE484222325):
@@ -62,16 +63,19 @@ def walk(root):
def plan_shards(files, shard_bytes):
- """Assign every file a shard. Files are grouped by top-level directory, keeping the walk order
- (path order) inside a group; a group is cut into shards of at most shard_bytes."""
+ """Assign every file to a shard. Files are grouped by their top-level directory so one shard
+ holds one kind of content, and each group is cut greedily into shards of at most shard_bytes.
+ A single file larger than shard_bytes gets a shard of its own (the cap is a packing target, not
+ a hard limit)."""
by_top = collections.OrderedDict()
for rel, size in files:
- top = rel.split("/", 1)[0]
+ parts = rel.split("/", 1)
+ top = parts[0] if len(parts) > 1 else "" # a file at the root has no group of its own
by_top.setdefault(top, []).append((rel, size))
- # Big groups first, so the shard count stays close to the size total / shard_bytes
+ # Big groups first, so the shard count stays close to size total / shard_bytes
order = sorted(by_top.items(), key=lambda kv: -sum(s for _, s in kv[1]))
assign, shard = {}, 0
- for top, items in order:
+ for _top, items in order:
cur = 0
for rel, size in items:
if cur and cur + size > shard_bytes:
@@ -79,7 +83,8 @@ def plan_shards(files, shard_bytes):
cur = 0
assign[rel] = (shard, cur)
cur += size
- shard += 1
+ if cur: # only consume a shard number when something was placed in it
+ shard += 1
return assign, shard
@@ -89,6 +94,13 @@ def pack(romfs, base, shard_mib):
total = sum(s for _, s in files)
print(f"pack: {len(files):,} files, {len(dirs):,} directories, {total / 2**30:.2f} GiB", flush=True)
assign, nshard = plan_shards(files, shard_bytes)
+ # The reader keeps one fd slot per shard (MAX_SHARDS in romfs_arc.h) and rejects an index above
+ # it. Fail here instead of producing an archive the app cannot open, and say how to fix it.
+ if nshard > MAX_SHARDS:
+ # Minimum shard size that fits the total into MAX_SHARDS, rounded up to a whole MiB
+ need_mib = max(1, (total + MAX_SHARDS - 1) // MAX_SHARDS // (1 << 20) + 1)
+ sys.exit(f"[RED] {nshard} shards needed, but the reader supports {MAX_SHARDS}. "
+ f"Raise --shard-mib to at least {need_mib}.")
print(f"pack: {nshard} shards of at most {shard_mib} MiB", flush=True)
# Shard files: written in path order within each shard