69 lines
2.6 KiB
Python
69 lines
2.6 KiB
Python
import zipfile, os, re, shutil
|
|
|
|
base = r'e:\MCStudioDownload\work\3027246179@qq.com\Cpp\AddOn\309c2d420bc84ee986db9cdd070ff02a\_sa_hanhua_out'
|
|
jars = {
|
|
'essentials': base + r'\sa_essentials-forge-1.20.1-1.0.0.jar',
|
|
'lib': base + r'\sa_lib-forge-1.20.1-1.0.1.jar',
|
|
'decor': base + r'\sa_decor-forge-1.20.1-1.0.3.jar',
|
|
}
|
|
|
|
# 每个 jar 的 mods.toml 替换规则:(旧字符串, 新字符串) 或 (正则, 新, is_regex)
|
|
repls = {
|
|
'essentials': [
|
|
('displayName = "Survivors Arsenal: Essentials"',
|
|
'displayName = "幸存者军火库:基础包"'),
|
|
('description = "Combat, survival and essential gameplay content for Survivors Arsenal."',
|
|
'description = "幸存者军火库的战斗、生存与核心玩法内容。"'),
|
|
],
|
|
'lib': [
|
|
('displayName="Survivor\'s Arsenal: Lib"',
|
|
'displayName="幸存者军火库:核心"'),
|
|
(r"description\s*=\s*'''[\s\S]*?'''",
|
|
"description='''\n我们所有幸存者军火库模组的公共库。\n'''"),
|
|
],
|
|
'decor': [
|
|
('displayName="Survivor\'s Arsenal: Loot & Decor"',
|
|
'displayName="幸存者军火库:战利品与装饰"'),
|
|
('description="Decoration module for Survivor\'s Arsenal."',
|
|
'description="幸存者军火库的装饰模块。"'),
|
|
],
|
|
}
|
|
|
|
for name, jar in jars.items():
|
|
with zipfile.ZipFile(jar) as z:
|
|
infos = z.infolist()
|
|
data = {i.filename: z.read(i) for i in infos}
|
|
meta = 'META-INF/mods.toml'
|
|
text = data[meta].decode('utf-8')
|
|
orig = text
|
|
for old, new in repls[name]:
|
|
if text.find(old) != -1:
|
|
text = text.replace(old, new)
|
|
print(f'[{name}] replaced: {old[:60]!r}')
|
|
else:
|
|
print(f'[{name}] WARN not found: {old[:60]!r}')
|
|
if text == orig:
|
|
print(f'[{name}] NO CHANGES!')
|
|
continue
|
|
data[meta] = text.encode('utf-8')
|
|
|
|
# 重建 jar
|
|
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(f'[{name}] jar updated.')
|
|
|
|
# 验证
|
|
print('\n===== VERIFY =====')
|
|
for name, jar in jars.items():
|
|
with zipfile.ZipFile(jar) as z:
|
|
t = z.read('META-INF/mods.toml').decode('utf-8')
|
|
for line in t.splitlines():
|
|
if 'displayName' in line or 'description' in line:
|
|
print(f'[{name}] {line.strip()}')
|
|
# 检查 jar 完好
|
|
bad = z.testzip()
|
|
print(f'[{name}] entries={len(z.namelist())} testzip={bad}')
|