TH1/Tools/fix_art_meta.py
2026-04-17 20:42:42 +08:00

167 lines
5.3 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.

"""
批量修复新阵营美术资产的 Unity .meta 文件
问题:新阵营的 Standalone 平台覆盖未启用 (overridden: 0),导致使用默认的 2048 纹理尺寸
修复:将 Standalone 块改为 maxTextureSize: 512, textureFormat: 25, compressionQuality: 100, overridden: 1
"""
import os
import re
import sys
# 新阵营关键词列表(文件名中包含这些就是需要修复的)
NEW_FACTIONS = [
"Arabian", "Aztec", "Greek", "Incan", "Khmer",
"Malian", "Mayan", "Mogolian", "Sumerian"
]
# 注意British 既是旧阵营也是新阵营名,需要特殊处理
# TH1Grounds/TH1Ground_British 是旧的(已经正确),但如果有新的则按目录判断
# 需要扫描的目录
SCAN_DIRS = [
"TH1Grounds",
"TH1Forests",
"TH1Fruits",
"TH1Animals",
"TH1Mountains",
]
# 正确的 Standalone 平台设置块
CORRECT_STANDALONE = """ - serializedVersion: 3
buildTarget: Standalone
maxTextureSize: 512
resizeAlgorithm: 0
textureFormat: 25
textureCompression: 1
compressionQuality: 100
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 1
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0"""
def is_new_faction(filename):
"""判断文件名是否属于新阵营"""
for faction in NEW_FACTIONS:
if faction in filename:
return True
return False
def fix_meta_file(filepath):
"""修复单个 .meta 文件的 Standalone 平台设置"""
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
# 检查是否已经正确 (overridden: 1 在 Standalone 块中)
# 用正则匹配 Standalone 块
standalone_pattern = re.compile(
r'( - serializedVersion: \d+\n'
r' buildTarget: Standalone\n'
r'(?: \w+: .*\n)*)',
re.MULTILINE
)
match = standalone_pattern.search(content)
if not match:
print(f" [SKIP] 没有找到 Standalone 块: {filepath}")
return False
old_block = match.group(0)
# 检查是否已经正确
if 'overridden: 1' in old_block and 'maxTextureSize: 512' in old_block:
print(f" [OK] 已经正确: {os.path.basename(filepath)}")
return False
# 替换 Standalone 块
new_content = content[:match.start()] + CORRECT_STANDALONE + '\n' + content[match.end():]
with open(filepath, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f" [FIXED] {os.path.basename(filepath)}")
return True
def main():
# Script at TH1/TH1/Tools/, art at TH1/TH1/Unity/Assets/Resources/ArtResources/
script_dir = os.path.dirname(os.path.abspath(__file__)) # TH1/TH1/Tools
base_dir = os.path.join(script_dir, '..', 'Unity', 'Assets', 'Resources', 'ArtResources')
base_dir = os.path.normpath(base_dir)
if not os.path.isdir(base_dir):
print(f"错误:找不到 ArtResources 目录: {base_dir}")
sys.exit(1)
dry_run = '--dry-run' in sys.argv
if dry_run:
print("=== DRY RUN 模式 (不会实际修改文件) ===\n")
total_fixed = 0
total_skipped = 0
total_ok = 0
for scan_dir in SCAN_DIRS:
full_dir = os.path.join(base_dir, scan_dir)
if not os.path.isdir(full_dir):
print(f"\n[WARN] 目录不存在: {scan_dir}")
continue
print(f"\n--- {scan_dir} ---")
for fname in sorted(os.listdir(full_dir)):
if not fname.endswith('.png.meta'):
continue
if not is_new_faction(fname):
continue
filepath = os.path.join(full_dir, fname)
if dry_run:
# 只检查不修改
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
if 'overridden: 1' in content and 'buildTarget: Standalone' in content:
# 需要更精确地检查 Standalone 块中的 overridden
lines = content.split('\n')
in_standalone = False
is_correct = False
for line in lines:
if 'buildTarget: Standalone' in line:
in_standalone = True
elif in_standalone and 'buildTarget:' in line:
break
elif in_standalone and 'overridden: 1' in line:
is_correct = True
break
if is_correct:
print(f" [OK] {fname}")
total_ok += 1
else:
print(f" [NEED FIX] {fname}")
total_fixed += 1
else:
print(f" [NEED FIX] {fname}")
total_fixed += 1
else:
if fix_meta_file(filepath):
total_fixed += 1
else:
total_ok += 1
print(f"\n{'='*40}")
if dry_run:
print(f"需要修复: {total_fixed} 个文件")
print(f"已经正确: {total_ok} 个文件")
print(f"\n运行 'python fix_art_meta.py' (不带 --dry-run) 来执行修复")
else:
print(f"已修复: {total_fixed} 个文件")
print(f"已正确: {total_ok} 个文件")
if __name__ == '__main__':
main()