-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
213 lines (149 loc) · 6.06 KB
/
main.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import os
import asyncio
import ujson as json
from menu_tools import MenuTools
from pathlib import Path
from tqdm import tqdm
from utils import logger, get_ecx_path, has_japanese
from utils.arg_require import ArgRequire, ArgRequireOption
__version__ = "0.6.8"
ag = ArgRequire(ArgRequireOption(save=True, save_path="config.ini"))
last_game_path = None
async def connect_openai_servers():
from configparser import ConfigParser
from core import JPTranslator, OpenAiServer
server_list_config = ConfigParser()
server_list_config.read("server-list.ini", encoding="utf-8")
tg = JPTranslator()
for config in server_list_config._sections.values():
if config.get("enable", "").lower() in ["false", "no", "n", "0"]:
continue
logger.info(f"连接API服务器[{config['server_name']}]: {config['api_url']}")
if await tg.connect_server(OpenAiServer(**config)):
logger.info(f"服务器 [{config['server_name']}] 连接成功")
else:
logger.error(f"服务器 [{config['server_name']}] 连接失败")
await tg.servers_load_default_model()
tg.start_server()
if len(tg.servers) == 0:
logger.error("没有可用的API服务器")
logger.error("请检查配置文件 server-list.ini 是否正确配置")
logger.error(
"如果没有服务器, 请到 https://pan.baidu.com/s/15nk8-pUzDeXW_jgFFvM9Pw?pwd=2z55 下载"
)
return
return tg
@ag.apply("请拖入游戏目录")
def unity_game(game_path: Path):
from core.UnityExtractor.TextFinder import TextFinder
global last_game_path
utf = TextFinder(game_path)
utf.dump_prepare_text()
last_game_path = utf.game_path
async def run_translate_async(game_path: Path):
tg = await connect_openai_servers()
tg.set_cache_path(game_path)
text_list = tg.read_prepare_text()
await tg.translate(text_list)
# await tg.unload_model()
logger.info("翻译完成")
@ag.apply("请拖入游戏目录")
def run_translate(game_path: Path):
asyncio.run(run_translate_async(game_path))
@ag.apply("请拖入游戏目录")
def run_write_unity_file(game_path: Path):
from core.UnityExtractor.WriteMonoBehaviour import WriteMonoBehaviour
wmb = WriteMonoBehaviour(game_path)
wmb.write_cache_to_file()
@ag.apply("请拖入游戏目录")
def run_replace_font(game_path: Path):
import dumb_menu
from core.UnityExtractor.ReplaceFont import replace_unity_font
# fonts = [f for f in Path("./font").iterdir() if f.is_file() and f.suffix in [".ttf", ".otf"]]
# options = [f"{i+1}. {f.name}" for i, f in enumerate(fonts)]
# index = dumb_menu.get_menu_choice(options, True)
# font_path = fonts[index]
# replace_unity_font(game_path, fonts[index])
# ./font/unifont-all.ttf
# replace_unity_font(game_path, Path("./font/unifont-all.ttf"))
from core.UnityExtractor.TextFinder import TextFinder
# custom_font_path = Path("./font/NotoSansSC-Regular.otf")
# if not custom_font_path.exists():
# logger.error("字体文件不存在")
# return
utf = TextFinder(game_path)
utf.replace_font(
)
async def run_translate_json_async(json_path: Path):
pending_file = json_path.with_stem(json_path.stem + "_Translated_Cache")
tran_cache = json_path.with_stem(json_path.stem + "_Translated")
if not pending_file.exists():
with json_path.open("r", encoding="utf-8") as f:
data = json.load(f)
if tran_cache.exists():
with tran_cache.open("r", encoding="utf-8") as f:
cache_data = json.load(f)
else:
cache_data = {}
pending_data = {}
for key, value in tqdm(data.items(), desc="生成待翻译文件"):
if has_japanese(key):
pending_data[key] = cache_data.get(key, "")
else:
pending_data[key] = value
with pending_file.open("w", encoding="utf-8") as f:
json.dump(pending_data, f, ensure_ascii=False, indent=4)
tg = await connect_openai_servers()
tg.cache_path = tran_cache.parent
text_list = tg.read_prepare_text(pending_file)
await tg.translate(
text_list, target_out_file=pending_file, tran_cache_file=tran_cache
)
logger.info("翻译完成")
@ag.apply("请拖入其他需要翻译的Json文件")
def run_translate_json(json_path: Path):
asyncio.run(run_translate_json_async(json_path))
@ag.apply("请拖入游戏目录")
def test(game_path: Path):
import dumb_menu
from core.UnityExtractor.AssetsTools.SDF_Font.make_sdf_font import make_sdf_font
assets = [
f for f in Path("./font").iterdir() if f.is_file() and f.suffix in [".asset"]
]
# options = [f"{i+1}. {f.name}" for i, f in enumerate(assets)]
# index = dumb_menu.get_menu_choice(options, True)
index = 1
asset_path = assets[index]
make_sdf_font(asset_path)
async def run_api_server():
from server import run_server
from threading import Thread
t = Thread(
target=run_server, daemon=True
)
t.start()
t.join()
def run_api_server_async():
asyncio.run(run_api_server())
def run():
MenuTools(
title=f"--- 简简单单翻译一下 v{__version__} ---",
options={
# test: "测试",
unity_game: "1. 提取游戏文本资源 (先选这个, 生成需要翻译的文本)",
run_translate: "2. 使用AI翻提取前的文本",
run_write_unity_file: "3. 替换游戏内文本 (翻译完成后, 选这个)",
run_replace_font: "4. 替换游戏内字体 (出现口口或者识别不出中文的情况, 选这个)",
run_translate_json: "额外功能: 翻译其他工具导出的Json文件",
run_api_server_async: "启动API服务器",
},
args={
run_translate: {"game_path": last_game_path},
run_write_unity_file: {"game_path": last_game_path},
run_replace_font: {"game_path": last_game_path},
},
).show()
os.system("pause")
run()
if __name__ == "__main__":
run()