Files
d2r-mobile/dm/verify/nsoimg.py
T
shenleiandClaude Opus 5.5 4d91f80bae Android port (Adreno): Vulkan backend, GameActivity app, touch HUD, lifecycle, minimap quest goals
- host/android: Vulkan replay backend (nvn_vk.c) with pipeline/descriptor caching, ADPF,
  AChoreographer present link; Linux compat layer; Makefile.android (headless dmhost + APK lib)
- android/app: GameActivity shell without Gradle (build_apk.sh), AAudio output, touch HUD port
  (touch_app.c + TouchOverlay.java, three layers: body / joystick / minimap goals),
  lifecycle save-on-background like iOS, art_from_ipa.py for touch / HUD / mapgoal art
- touch: shared touch controls moved from ios/Sources to dm/touch
- patch_nso.py Android ruleset, dmsh_vk.py shader pack, DM_TOUCH_PRODUCT defaults
- hle_audio: AAudio resume waits through STARTING and reopens once on failure
- .gitignore: local resources/ (original game data) never committed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 14:55:05 +09:00

99 lines
4.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# NSO → 内存映像(与 patch_nso.py 同一解法);验证工具共用。
# NSO → memory image (same decoding as patch_nso.py); shared by the verification tools.
import struct
import lz4.block
def load(path):
d = open(path, 'rb').read()
assert d[:4] == b'NSO0', path
flags = struct.unpack_from('<I', d, 0xc)[0]
segs = [struct.unpack_from('<III', d, 0x10 + i * 0x10) for i in range(3)]
bss = struct.unpack_from('<I', d, 0x3c)[0]
csz = struct.unpack_from('<III', d, 0x60)
img = bytearray(segs[2][1] + segs[2][2] + bss)
for i, (fo, mo, sz) in enumerate(segs):
raw = d[fo:fo + csz[i]]
if flags >> i & 1: raw = lz4.block.decompress(raw, uncompressed_size=sz)
img[mo:mo + sz] = raw
return bytes(img)
def header(out_dir, mod):
h = {}
for line in open(f'{out_dir}/{mod}.fixups.tsv'):
if not line.startswith('#'): break
k, v = line[1:].split()
h[k] = v if k == 'ABI' else int(v, 0) # 0x 开头为偏移,十进制为槽号;ABI 为名字 | A 0x prefix means an offset, decimal means a slot number; ABI is a name
h.setdefault('ABI', 'apple')
return h
def slot_base(ins, i, hdr):
"""ins[i:] 是否为槽块基址前缀(patch_nso.emit_slot_base)→ (基址寄存器, 指令条数),否则 None。
apple:mrs xT, tpidrro_el0;linux-bionic:mrs xT, tpidr_el0 / ldr xT, [xT, #TP_SLOT_OFF]。
Whether ins[i:] is a slot block base prefix (patch_nso.emit_slot_base) → (base register, instruction count), else None.
apple: mrs xT, tpidrro_el0; linux-bionic: mrs xT, tpidr_el0 / ldr xT, [xT, #TP_SLOT_OFF]."""
if i >= len(ins) or ins[i].mnemonic != 'mrs': return None
T = ins[i].operands[0].reg
if hdr['ABI'] == 'apple':
return (T, 1) if ins[i].op_str.endswith('tpidrro_el0') else None
if not ins[i].op_str.endswith('tpidr_el0') or i + 1 >= len(ins): return None
l = ins[i + 1]
if l.mnemonic != 'ldr' or l.operands[0].reg != T or l.operands[1].mem.base != T or l.operands[1].mem.disp != hdr['TP_SLOT_OFF']: return None
return (T, 2)
def slot_base_words(hdr, reg):
"""槽块基址前缀的机器字(与 patch_nso.emit_slot_base 同)。 | Machine words of the slot block base prefix (same as patch_nso.emit_slot_base)."""
if hdr['ABI'] == 'apple': return [0xD53BD060 | reg]
return [0xD53BD040 | reg, 0xF9400000 | (hdr['TP_SLOT_OFF'] // 8 << 10) | (reg << 5) | reg]
def mod0(img):
m0 = struct.unpack_from('<I', img, 4)[0]
assert img[m0:m0 + 4] == b'MOD0'
return m0
def func_starts(img, text_end):
"""eh_frame_hdr 里的函数起点(与 patch_nso.py 同法)。 | Function start addresses from eh_frame_hdr (same method as patch_nso.py)."""
m0 = mod0(img)
ehs = m0 + struct.unpack_from('<i', img, m0 + 16)[0]
def rd(o, enc):
fmt, app = enc & 0xF, enc & 0x70
v, sz = {0x3: (struct.unpack_from('<I', img, o)[0], 4), 0xB: (struct.unpack_from('<i', img, o)[0], 4),
0x4: (struct.unpack_from('<Q', img, o)[0], 8)}[fmt]
if app == 0x10: v += o
if app == 0x30: v += ehs
return v, sz
_, penc, cenc, tenc = img[ehs:ehs + 4]
o = ehs + 4
_, s = rd(o, penc); o += s
nfde, s = rd(o, cenc); o += s
starts = set()
for _ in range(nfde):
a, s = rd(o, tenc); o += s
_, s = rd(o, tenc); o += s
starts.add(a)
return sorted(x for x in starts if x < text_end)
def relocs(img):
"""[(偏移, 类型, 符号名或 None, 符号值或 None, addend)]:RELA + JMPREL 全部条目。符号值 None = 未定义(导入)。 | [(offset, type, symbol name or None, symbol value or None, addend)]: all RELA + JMPREL entries. Symbol value None = undefined (import)."""
m0 = mod0(img)
dyn = m0 + struct.unpack_from('<i', img, m0 + 4)[0]
d = {}
o = dyn
while True:
tag, val = struct.unpack_from('<qQ', img, o); o += 16
if tag == 0: break
d[tag] = val
def cstr(p):
e = img.index(b'\0', p); return img[p:e].decode('latin1')
out = []
for tab, sz in ((d.get(7), d.get(8, 0)), (d.get(23), d.get(2, 0))):
if not tab: continue
for i in range(sz // 24):
off, info, add = struct.unpack_from('<QQq', img, tab + 24 * i)
typ, si = info & 0xFFFFFFFF, info >> 32
name = val = None
if si:
nm, _, _, shn, sv, _ = struct.unpack_from('<IBBHQQ', img, d[6] + 24 * si)
name = cstr(d[5] + nm); val = sv if shn else None
out.append((off, typ, name, val, add))
return out