Files
2026-10-09 18:23:57 +08:00

59 lines
2.7 KiB
Python

# -*- coding: utf-8 -*-
"""用 javap 提取注册类里的注册名字符串,与 lang key 对比,找出真正缺失的 key。"""
import zipfile, json, os, io, sys, subprocess, tempfile, re
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
BASE = r'e:\MCStudioDownload\work\3027246179@qq.com\Cpp\AddOn\0aaaa4f2f16c46e8964025a5ab0ecc02\_sa_hanhua_out'
TMP = os.path.join(BASE, '_tmp', 'regjavap')
os.makedirs(TMP, exist_ok=True)
TARGETS = [
('sa_essentials-forge-1.20.1-1.0.0.jar', 'sa_combat', ['com/ogaba/sa_combat/registry/ModItems.class',
'com/ogaba/sa_combat/registry/ModBlocks.class',
'com/ogaba/sa_survival/registry/SASurvivalItems.class',
'com/ogaba/sa_survival/registry/SASurvivalBlocks.class']),
('sa_lib-forge-1.20.1-1.0.1.jar', 'sa_core', ['com/ogaba/sa_core/registry/ModItems.class',
'com/ogaba/sa_core/registry/ModBlocks.class']),
('sa_decor-forge-1.20.1-1.0.3.jar', 'sa_decor', ['com/ogaba/sa_decor/init/SADecorBlocks.class',
'com/ogaba/sa_decor/init/SADecorItems.class']),
]
name_re = re.compile(r'^[a-z][a-z0-9_]{2,}$')
for jn, ns, classes in TARGETS:
jp = os.path.join(BASE, jn)
if not os.path.exists(jp):
print('!! missing jar', jn)
continue
z = zipfile.ZipFile(jp)
names = z.namelist()
en = {}
for n in names:
if n == 'assets/%s/lang/en_us.json' % ns:
en = json.loads(z.read(n).decode('utf-8-sig'))
found = set()
present = []
for cls in classes:
if cls not in names:
print(' (no class %s)' % cls)
continue
present.append(cls)
out = os.path.join(TMP, jn.replace('.jar', '') + '__' + cls.replace('/', '_'))
with open(out, 'wb') as f:
f.write(z.read(cls))
r = subprocess.run(['javap', '-p', '-c', out], capture_output=True)
txt = r.stdout.decode('utf-8', 'ignore')
for m in re.finditer(r'// String (.+)$', txt, re.M):
s = m.group(1).strip().strip('"')
if name_re.match(s):
found.add(s)
print('=' * 70)
print('JAR', jn, 'ns', ns, 'classes', present)
print(' extracted names:', len(found))
# 这些名字中,lang 里 block./item. 都没有的
miss = sorted(s for s in found if ('block.%s.%s' % (ns, s)) not in en and ('item.%s.%s' % (ns, s)) not in en)
print(' 无对应 lang key 的名字 (%d):' % len(miss))
for s in miss:
print(' ', s)
z.close()