-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path_handleVariantNames.py
186 lines (162 loc) · 6.84 KB
/
_handleVariantNames.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python
# -*- coding: utf-8 -*
import os
import re
import json5
from urllib import request
starsector_comment_line = re.compile(r'\s*#')
variant_display_name_re = re.compile(r'"displayName"\s*:\s*"(.*?)"')
def getSSjsonString(file_path):
# Alex将 # 用在json文件中作为注释...
json_string = ''
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
lines = f.readlines()
for line in lines:
if starsector_comment_line.search(line):
continue
json_string += line
return json_string
# 在这里更改抓取装配文件夹的目录
source_variants_path = "original/data/variants"
# 在这里更改更新目标装配文件夹的目录
target_variants_path = "localization/data/variants"
# 装配映射的文件
map_file_path = "json.key.map/variant_name_map.json"
root_folder = os.path.dirname(os.path.realpath(__file__))
source_variant_folder = os.path.join(root_folder, source_variants_path)
target_variant_folder = os.path.join(root_folder, target_variants_path)
variant_map_file = os.path.join(root_folder, map_file_path)
def chooseAction():
print("此脚本用于从指定目录中获取所有去除重复的装配文件名或者根据这些装配名的映射更新指定目录的装配名...")
action = input(
"输入1来根据指定源目录更新映射文件\n输入2来根据映射文件更新指定目标目录\n输入3远程下载装配映射文件(文件来源于汉化项目中,注意!会覆盖本地文件)\n输入其他字符或回车自动退出\n")
if action == '1':
if updateMap():
chooseAction()
elif action == '2':
if updateTargetVariants():
chooseAction()
elif action == '3':
if downloadVariantNameMap():
chooseAction()
else:
exit()
return
def updateMap():
print("-"*50)
print("当前处理目录为:" + source_variants_path)
print("保存映射文件为:" + map_file_path)
variant_name_list = list()
processed = 0
processed_not_duplicated = 0
processed_files = 0
for root, sub_folders, filenames in os.walk(source_variant_folder):
for filename in filenames:
processed_files += 1
file_path = os.path.join(root, filename)
ignored, ext = os.path.splitext(file_path)
if not ext == '.variant':
continue
with open(file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
display_match = variant_display_name_re.search(line)
if display_match:
name = display_match.group(1)
if name:
processed += 1
if name and not variant_name_list.__contains__(name):
processed_not_duplicated += 1
variant_name_list.append(name)
print("\r已搜索{}个装配文件(搜索文件总和:{}),已检索{}个独特装配名...".format(
processed, processed + processed_files, processed_not_duplicated), end=" ")
print()
variant_name_list.sort()
for name in variant_name_list:
print(name)
print("开始更新映射...")
variant_map_json = json5.load(
open(variant_map_file, 'r', encoding='utf-8'))
for name in variant_name_list:
if not variant_map_json.__contains__(name):
variant_map_json[name] = name
print(variant_map_json)
json5.dump(variant_map_json, open(
variant_map_file, 'w+', encoding='utf-8'), indent=4, quote_keys=True, trailing_commas=False, sort_keys=True, ensure_ascii=False)
print("已完成更新")
print("-"*50)
return True
def updateTargetVariants():
print("-"*50)
print("当前处理目录为:" + target_variants_path)
print("读取映射文件为:" + map_file_path)
print("\n读取映射文件中")
variant_map_json = dict(json5.load(
open(variant_map_file, 'r', encoding='utf-8')))
variant_map_values = list(variant_map_json.values())
print("读取完毕")
print("\n开始替换...")
no_match_variant_dict = dict()
processed = 0
processed_not_variant = 0
processed_files = 0
for root, sub_folders, filenames in os.walk(target_variant_folder):
for filename in filenames:
processed_files += 1
file_path = os.path.join(root, filename)
ignored, ext = os.path.splitext(file_path)
if not ext == '.variant':
processed_not_variant += 1
continue
new_lines = list()
line_changed = False
with open(file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
if line_changed:
new_lines.append(line)
else:
display_match = variant_display_name_re.search(line)
if display_match:
name = display_match.group(1)
if name:
if variant_map_json.__contains__(name):
processed += 1
line = line.replace(
name, variant_map_json[name])
line_changed = True
elif not variant_map_values.__contains__(name):
no_match_variant_dict[file_path] = name
new_lines.append(line)
if line_changed:
with open(file_path, 'w', encoding='utf-8') as f:
f.writelines(new_lines)
print("\r已替换{}个装配文件(搜索文件总和:{})...".format(
processed, processed_files), end=" ")
print()
if no_match_variant_dict:
print("以下装配文件的装配名没有对应翻译:")
for (key, value) in no_match_variant_dict.items():
print(key + " : " + value)
print("已完成替换")
print("-"*50)
return True
def downloadVariantNameMap():
print("-"*50)
print("下载中...")
try:
with request.urlopen('https://raw.githubusercontent.com/TruthOriginem/Starsector-096-Localization/main/variant_name_map.json', timeout=1.0) as rf:
data = rf.read()
print("下载成功!")
if data:
data = data.decode('utf-8')
with open(variant_map_file, 'w+', encoding='utf-8') as f:
f.write(data)
print("保存成功!")
except:
print("下载出现错误!请检查你的网络连接!")
print("-"*50)
return True
if __name__ == '__main__':
chooseAction()
# updateMap()