Files
d2r-mobile/dm/tools/mac_bundle.py
53e7c5bb9c Adopt community PRs: in-game Russian (#100), Mac packaging (#96), optional Mac VP8 movies (#97)
- Russian (ruRU), from #100 by @XpycT: a Русский column for all our UI text, a four-way language row
  (中文 / English / Español / Русский) in Options → Custom, Russian touch wording, map goal name plates,
  and first launch following a Russian device language. Voice: the Russian voice-over is used when your
  game files include it (chunk 16, present in a package built from complete game data), otherwise English.
  Item notes remain Chinese and Spanish only.
- Mac packaging, from #96 by @SwonDev: SW_MAC_ROMFS_COPY=1 bundles your own resources (private use only),
  SW_MAC_ICON_SOURCE builds an App icon from your own image, linked non-system dylibs are copied into the
  bundle and relinked, and the bundle is verified with a strict codesign check. Rebuilding into the same
  output directory still replaces the whole App.
- Mac movies, from #97 by @SwonDev: MOVIE_FFMPEG=1 swaps the duration-only movie backend for FFmpeg VP8
  decoding to NV12; the default build and iOS are unchanged. See dm/host/MOVIE.md.

Every change was ported into the internal tree and passed the host unit tests, a Russian UI regression
run on the iOS app on Mac, the Mac smoke test, a real-iPhone smoke test (3/3) and the full acceptance gate.

Exported from main 69614143f.

Co-authored-by: XpycT <XpycT.TOP@gmail.com>
Co-authored-by: SwonDev <35969131+SwonDev@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 13:13:20 +08:00

74 lines
3.3 KiB
Python

"""Relocate the host's linked dylib closure using Mach-O load commands and macOS tools."""
from pathlib import Path
import re
import shutil
import subprocess
import sys
def tool(*args):
return subprocess.check_output([str(a) for a in args], text=True)
def system(path):
return str(path).startswith(('/usr/lib/', '/System/Library/'))
def rpaths(binary, executable):
paths = re.findall(r'cmd LC_RPATH\s+cmdsize \d+\s+path (.*?) \(offset', tool('otool', '-l', binary))
return [Path(p.replace('@loader_path', str(binary.parent)).replace('@executable_path', str(executable.parent))) for p in paths]
def bundle(executable, origin=None):
executable = Path(executable).resolve()
frameworks = executable.parent.parent / 'Frameworks'
origin = Path(origin).resolve() if origin else executable
copied, names = {}, {}
def visit(source, target, inherited):
paths = rpaths(source, origin) + inherited
identity = tool('otool', '-D', source).splitlines()[1:]
dependencies = [line.strip().split(' (compatibility version', 1)[0]
for line in tool('otool', '-L', source).splitlines()[1:]]
for dep in dependencies:
if dep in identity or system(dep):
continue
expanded = dep.replace('@loader_path', str(source.parent)).replace('@executable_path', str(origin.parent))
candidates = [p / expanded[7:] for p in paths] if expanded.startswith('@rpath/') else [Path(expanded)]
resolved = next((p.resolve() for p in candidates if p.is_file()), None)
if resolved is None:
raise ValueError('Unresolved dependency: ' + dep)
if system(resolved):
replacement = str(resolved)
else:
if '.framework/' in str(resolved):
raise ValueError('Framework dependencies need a framework-aware packager: ' + dep)
name = resolved.name
if name in names and names[name] != resolved:
raise ValueError('Conflicting dylib basename: ' + name)
names[name] = resolved
replacement = '@executable_path/../Frameworks/' + name
if resolved not in copied:
frameworks.mkdir(exist_ok=True)
destination = frameworks / name
shutil.copy2(resolved, destination)
destination.chmod(destination.stat().st_mode | 0o200)
copied[resolved] = destination
visit(resolved, destination, paths)
tool('install_name_tool', '-id', '@rpath/' + name, destination)
tool('install_name_tool', '-change', dep, replacement, target)
original_paths = re.findall(r'cmd LC_RPATH\s+cmdsize \d+\s+path (.*?) \(offset', tool('otool', '-l', target))
for path in dict.fromkeys(original_paths):
tool('install_name_tool', '-delete_rpath', path, target)
visit(origin, executable, [])
for library in copied.values():
tool('codesign', '--force', '-s', '-', library)
if __name__ == '__main__':
try:
bundle(sys.argv[1], sys.argv[2] if len(sys.argv) > 2 else None)
except (ValueError, OSError, subprocess.CalledProcessError) as error:
sys.exit(str(error))