Files
xczjhk/_sa_hanhua_out/_tmp/fix_reloading2.py
T
2026-10-04 21:07:49 +08:00

39 lines
1.3 KiB
Python

import zipfile, 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...' # 12 字节
old_pat = b'\x01\x00\x0c' + old_ascii
new_utf8 = '换弹中...'.encode('utf-8') # 12 字节,与旧串等长
assert len(new_utf8) == len(old_ascii), (len(new_utf8), len(old_ascii))
new_pat = b'\x01\x00\x0c' + new_utf8
print('new bytes:', new_utf8.hex(), 'len', len(new_utf8))
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:', cnt)
assert cnt == 1, 'ABORT'
nb = b.replace(old_pat, new_pat)
assert len(nb) == len(b)
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.')
with zipfile.ZipFile(jar) as z:
nb = z.read(target)
print('new const present:', nb.count(new_pat) == 1)
print('old ascii remains:', nb.count(old_ascii))
print('entries=', len(z.namelist()), 'testzip=', z.testzip())