Files
2026-10-09 18:08:05 +08:00

43 lines
1.5 KiB
Python

import zipfile, os, shutil
base = r'e:\MCStudioDownload\work\3027246179@qq.com\Cpp\AddOn\309c2d420bc84ee986db9cdd070ff02a\_sa_hanhua_out'
jar = base + r'\sa_essentials-forge-1.20.1-1.0.0.jar'
target = 'com/ogaba/sa_combat/client/event/SACombatClientForgeEvents.class'
old_ascii = b'Reloading...' # 11 字节
old_pat = b'\x01\x00\x0b' + old_ascii # CONSTANT_Utf8 tag + u2 length(11) + bytes
new_utf8 = '换弹中...'.encode('utf-8') # 12 字节 (E6 8D A2 E5 BC B9 E4 B8 AD 2E 2E 2E)
new_pat = b'\x01\x00\x0c' + new_utf8
print('new bytes:', new_utf8.hex())
with zipfile.ZipFile(jar) as z:
infos = z.infolist()
data = {i.filename: z.read(i) for i in infos}
b = data[target]
cnt = b.count(old_pat)
print('occurrences of utf8-const pattern:', cnt)
if cnt != 1:
raise SystemExit('ABORT: expected exactly 1 occurrence')
nb = b.replace(old_pat, new_pat)
print('class size: %d -> %d' % (len(b), len(nb)))
# 验证替换后不再含旧 ASCII(单独出现可能在其他字段;此处仅验证常量池模式消失)
print('old const gone:', nb.count(old_pat) == 0)
data[target] = nb
tmp = jar + '.tmp'
with zipfile.ZipFile(tmp, 'w', zipfile.ZIP_DEFLATED) as z:
for i in infos:
z.writestr(i, data[i.filename])
shutil.move(tmp, jar)
print('jar updated.')
# verify
with zipfile.ZipFile(jar) as z:
nb = z.read(target)
print('new const present:', nb.count(new_pat) == 1)
print('old ascii anywhere in class:', nb.count(old_ascii))
print('entries=', len(z.namelist()), 'testzip=', z.testzip())