openwf/Tools/generate_region_name_mapping.py

121 lines
4.0 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
生成星图节点中文名称映射
从 ExportRegions.json 提取节点名称,在 dict.zh.json 中查找对应的中文翻译
"""
import json
import sys
from pathlib import Path
def main():
# 设置路径
script_dir = Path(__file__).parent
project_root = script_dir.parent
export_dir = project_root / "warframe-public-export-plus"
regions_file = export_dir / "ExportRegions.json"
dict_file = export_dir / "dict.zh.json"
output_file = project_root / "Scripts" / "RegionNameMapping.pluto"
# 检查文件是否存在
if not regions_file.exists():
print(f"❌ 错误:找不到 {regions_file}")
sys.exit(1)
if not dict_file.exists():
print(f"❌ 错误:找不到 {dict_file}")
sys.exit(1)
print("📖 正在读取 ExportRegions.json...")
try:
with open(regions_file, 'r', encoding='utf-8') as f:
regions = json.load(f)
except Exception as e:
print(f"❌ 读取 ExportRegions.json 失败: {e}")
sys.exit(1)
print("📖 正在读取 dict.zh.json...")
try:
with open(dict_file, 'r', encoding='utf-8') as f:
zh_dict = json.load(f)
except Exception as e:
print(f"❌ 读取 dict.zh.json 失败: {e}")
sys.exit(1)
# 构建映射
print("🔄 正在构建节点名称映射...")
node_mapping = {}
missing_translations = []
for node_id, node_data in regions.items():
if isinstance(node_data, dict) and 'name' in node_data:
name_path = node_data['name']
# 在中文字典中查找
if name_path in zh_dict:
cn_name = zh_dict[name_path]
node_mapping[cn_name] = node_id
# 同时记录节点的系统名称
if 'systemName' in node_data and node_data['systemName'] in zh_dict:
system_cn = zh_dict[node_data['systemName']]
# 创建 "星系-节点" 格式的键
full_name = f"{system_cn}-{cn_name}"
node_mapping[full_name] = node_id
else:
missing_translations.append((node_id, name_path))
# 输出统计
print(f"\n✅ 成功映射 {len(node_mapping)} 个节点名称")
if missing_translations:
print(f"⚠️ {len(missing_translations)} 个节点缺少中文翻译")
# 生成 Pluto 文件
print(f"\n📝 正在生成 {output_file.name}...")
lines = [
"-- 星图节点中文名称映射\n",
"-- 自动生成,请勿手动编辑\n",
"-- 生成工具: Tools/generate_region_name_mapping.py\n",
"\n",
"return {\n"
]
# 排序并写入
sorted_names = sorted(node_mapping.keys())
for i, name in enumerate(sorted_names):
node_id = node_mapping[name]
# 转义特殊字符
k = str(name).replace('\\', '\\\\').replace('"', '\\"').replace('\n', ' ').replace('\r', '')
v = str(node_id).replace('\\', '\\\\').replace('"', '\\"')
# 最后一项不加逗号
comma = "," if i < len(sorted_names) - 1 else ""
lines.append(f' ["{k}"] = "{v}"{comma}\n')
lines.append("}\n")
# 写入文件
try:
with open(output_file, 'w', encoding='utf-8') as f:
f.writelines(lines)
print(f"✅ 已生成: {output_file}")
print(f"📊 总计 {len(node_mapping)} 个节点映射")
except Exception as e:
print(f"❌ 写入文件失败: {e}")
sys.exit(1)
# 显示缺失翻译(可选)
if missing_translations and len(missing_translations) <= 20:
print("\n⚠️ 缺少中文翻译的节点:")
for node_id, path in missing_translations[:10]:
print(f" {node_id}: {path}")
if len(missing_translations) > 10:
print(f" ... 还有 {len(missing_translations) - 10}")
print("\n✨ 完成!")
if __name__ == '__main__':
main()