-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
804 lines (709 loc) · 40.8 KB
/
Copy pathapp.py
File metadata and controls
804 lines (709 loc) · 40.8 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
# app.py
import subprocess
import os
import tkinter as tk
from tkinter import messagebox, filedialog
import queue
import threading
import traceback
import logging
import time
import platform
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
# 导入 UI 层 (这里先假设 UI 类已定义,后面再实现)
from ui import main_window #, config_dialogs, rtp_dialog, dict_editor # 等
# 导入 Core 层
from core import config as cfg # 重命名避免与 tkinter.config 冲突
from core.tasks import (
initialize, rename, export, json_creation,
dict_generation, translate, json_release, import_task,
easy_mode_flow, apply_base_dictionary
)
from core.utils import (text_processing, dictionary_manager)
from ui.dict_editor import DictEditorWindow
from core.utils.file_system import get_executable_dir, ensure_dir_exists # 导入路径辅助函数
from core.utils.engine_detection import detect_game_engine
from core.utils import windows_notifications
log = logging.getLogger(__name__) # 获取 logger 实例
CONTINUOUS_TASK_NAMES = {
'initialize',
'export',
'rename',
'create_json',
'generate_dictionary',
'translate',
'release_json',
'import',
'easy_flow',
}
FAILURE_STATUS_KEYWORDS = ("失败", "中止", "错误")
class TaskMessageProxy:
"""Forward task messages while tracking whether the task reported a problem."""
def __init__(self, target_queue):
self.target_queue = target_queue
self.has_problem = False
self._lock = threading.Lock()
def put(self, message):
with self._lock:
self._track_message(message)
self.target_queue.put(message)
def _track_message(self, message):
if not isinstance(message, tuple) or len(message) < 2:
return
msg_type, content = message[0], message[1]
if msg_type == "error":
self.has_problem = True
elif msg_type == "status" and isinstance(content, str):
if any(keyword in content for keyword in FAILURE_STATUS_KEYWORDS):
self.has_problem = True
class RPGTranslatorApp:
"""主应用程序类,负责协调 UI 和核心逻辑。"""
def __init__(self, root):
"""
初始化应用程序。
Args:
root (tk.Tk): Tkinter 的根窗口。
"""
self.root = root
self.root.protocol("WM_DELETE_WINDOW", self._on_close) # 绑定关闭事件
self.executable_dir = get_executable_dir()
self.works_dir = os.path.join(self.executable_dir, "Works")
self.config_file_path = os.path.join(self.executable_dir, "app_config.json") # 定义路径
ensure_dir_exists(self.works_dir)
self.config_manager = cfg.ConfigManager(self.config_file_path) # 实例化管理器
self.config = self.config_manager.load_config() # 加载配置
dictionary_manager.ensure_base_dictionaries()
if not os.path.exists(self.config_file_path):
try:
self.config_manager.save_config(self.config)
except Exception as e:
log.exception(f"创建默认配置文件失败: {e}")
# --- 应用状态 ---
self.game_path = tk.StringVar()
self.is_processing = False # 标记是否有后台任务在运行
self.current_task_thread = None # 引用当前运行的任务线程 (可选)
self._stop_requested = False # 用于请求停止当前任务 (可选)
# --- 后台任务处理 ---
self.message_queue = queue.Queue()
self.thread_pool = ThreadPoolExecutor(max_workers=1) # 主任务通常只跑一个
# 新增:用于存储任务ID和对应编辑器实例的映射
self._editor_callbacks_registry = {}
self.root.after(100, self._process_messages) # 启动消息循环
# --- 初始化 UI ---
# 将 self (App实例) 传递给 MainWindow,以便 UI 调用 App 的方法
self.main_window = main_window.MainWindow(self.root, self, self.config)
# 根据加载的配置设置初始模式和窗口大小
initial_mode = self.config.get('selected_mode', 'easy')
self.main_window.switch_to_mode(initial_mode)
log.info("WindyTranslator 应用程序已初始化。")
self.log_message("程序已启动,请选择游戏目录", "normal")
# --- UI 调用接口 ---
def browse_game_path(self):
"""弹出目录选择对话框,更新游戏路径。"""
path = filedialog.askdirectory(title="选择游戏目录", parent=self.root)
if path:
detected = detect_game_engine(path)
if detected:
self.game_path.set(path)
self.log_message(f"已选择游戏目录: {path} ({detected.engine})")
self.update_status("游戏目录已选择,可以开始操作。")
else:
messagebox.showerror(
"路径无效",
"选择的目录不是有效的受支持游戏目录(未找到可识别的游戏数据文件)。",
parent=self.root,
)
self.log_message("选择了无效的游戏目录。", "error")
def get_game_path(self):
"""获取当前游戏路径。"""
return self.game_path.get()
def start_task(self, task_name, mode='pro', game_path=None, task_id_for_callback=None, task_payload=None):
"""
根据任务名称启动相应的后台任务。
Args:
task_name (str): 任务的唯一标识符 (例如 'initialize', 'translate', 'easy_flow')。
mode (str): 当前的操作模式 ('easy' 或 'pro'),用于更新 UI 状态。
game_path (str, optional): 从外部(如DictEditor)传递的游戏路径。
如果为 None,则使用 self.get_game_path()。
task_id_for_callback (str, optional): 用于回调的唯一任务 ID,通常用于编辑器实例。
task_payload (object, optional): 独立工具任务需要的结构化参数。
"""
if self.is_processing:
self.log_message("请等待当前操作完成。", "error")
parent_window_for_msg = self.root
# 如果此任务是由编辑器发起的,错误提示应相对于编辑器
if task_id_for_callback and task_id_for_callback in self._editor_callbacks_registry:
editor = self._editor_callbacks_registry.get(task_id_for_callback)
if editor and editor.winfo_exists():
parent_window_for_msg = editor
messagebox.showwarning("操作繁忙", "请等待当前操作完成后再试。", parent=parent_window_for_msg)
# 如果是从编辑器调用的,并且操作被阻止,需要通知编辑器(可选,但更好)
if task_id_for_callback and task_id_for_callback in self._editor_callbacks_registry:
editor = self._editor_callbacks_registry.pop(task_id_for_callback) # 移除,因为任务未启动
if editor and editor.winfo_exists():
editor.handle_apply_base_dict_result(False, "操作繁忙,任务未启动。")
return
# --- 修改:优先使用传入的 game_path ---
current_game_path = game_path if game_path is not None else self.get_game_path()
# --- 修改:检查路径时使用 current_game_path ---
if not current_game_path and task_name not in [
'configure_gemini', 'configure_deepseek', 'select_rtp',
# 'open_base_dict_editor' 是直接方法调用,不在这里处理
]:
if not self._check_game_path_set(current_game_path): return # <--- 传递 current_game_path
# --- 准备任务参数 ---
task_func = None
task_args = []
task_kwargs = {}
# 从配置中获取所需参数
pro_config = self.config.get('pro_mode_settings', {}) # 专业模式的独立配置
rtp_options = pro_config.get('rtp_options', {'2000': True, '2000en': False, '2003': False, '2003steam': False, '2003zh_tw': False})
export_source_encoding = pro_config.get('export_source_encoding', 'auto')
import_encoding = pro_config.get('import_encoding', '936')
export_scope = pro_config.get('export_scope', {})
world_dict_config = self.config.get('world_dict_config', {})
translate_config = self.config.get('translate_config', {})
# 根据 task_name 选择任务函数和参数
if task_name == 'initialize':
task_func = initialize.run_initialize
task_args = [current_game_path, rtp_options, export_source_encoding, self.message_queue]
elif task_name == 'rename':
task_func = rename.run_rename
task_args = [current_game_path, self.message_queue]
elif task_name == 'export':
task_func = export.run_export
task_args = [current_game_path, export_source_encoding, export_scope, self.message_queue]
elif task_name == 'create_json':
task_func = json_creation.run_create_json
task_args = [current_game_path, self.works_dir, self.message_queue]
elif task_name == 'generate_dictionary':
task_func = dict_generation.run_generate_dictionary
task_args = [current_game_path, self.works_dir, world_dict_config, self.message_queue]
elif task_name == 'translate':
task_func = translate.run_translate
task_args = [current_game_path, self.works_dir, translate_config, world_dict_config, self.message_queue]
elif task_name == 'release_json':
json_files = self._find_translated_json_files(current_game_path) # <--- 传递 current_game_path
if not json_files:
messagebox.showerror("错误", f"在 Works/{self._get_work_subfolder(current_game_path)}/translated 目录下未找到翻译后的 JSON 文件。", parent=self.root)
return
selected_json_path = None
if len(json_files) == 1:
selected_json_path = json_files[0]
else:
selected_filename = self.main_window.show_file_selection_dialog(
"选择翻译文件",
"请选择要导入的翻译 JSON 文件:",
[os.path.basename(p) for p in json_files]
)
if selected_filename:
selected_json_path = os.path.join(self._get_translated_dir(current_game_path), selected_filename) # <--- 传递 current_game_path
else:
self.log_message("取消选择翻译文件。", "warning")
return
if selected_json_path:
task_func = json_release.run_release_json
task_args = [current_game_path, self.works_dir, selected_json_path, self.message_queue]
else:
return
elif task_name == 'import':
task_func = import_task.run_import
task_args = [current_game_path, export_source_encoding, import_encoding, self.message_queue]
elif task_name == 'apply_wolf_fonts':
from core.engines import wolf
task_func = wolf.apply_font_revision
task_args = [current_game_path, task_payload, self.message_queue]
elif task_name == 'easy_flow':
# 轻松模式需要检查 API Key 是否配置
if not world_dict_config.get("api_key") or not translate_config.get("api_key"):
messagebox.showerror("配置缺失", "请先在 字典API 和 翻译API 配置中填写 API Key。", parent=self.root)
return
task_func = easy_mode_flow.run_easy_flow
task_args = [
current_game_path, self.works_dir,
rtp_options, export_source_encoding, import_encoding, export_scope,
world_dict_config, translate_config,
self.message_queue
]
# --- 新增:手动应用基础字典任务 ---
elif task_name == 'apply_base_dictionary_manual':
task_func = apply_base_dictionary.run_apply_base_dictionary
# task_id_for_callback 将通过 kwargs 传递给任务函数
if task_id_for_callback:
task_kwargs['task_id_for_callback'] = task_id_for_callback
task_args = [current_game_path, self.works_dir, world_dict_config.copy(), self.message_queue]
elif task_name == 'start_game':
self._start_game() # 直接调用内部方法,不需后台线程
return
elif task_name == 'edit_dictionary':
self._open_dict_editor() # 直接调用内部方法
return
elif task_name == 'configure_gemini':
self._open_gemini_config() # 直接调用内部方法
return
elif task_name == 'configure_deepseek':
self._open_deepseek_config() # 直接调用内部方法
return
elif task_name == 'select_rtp':
self._open_rtp_selection() # 直接调用内部方法
return
else:
self.log_message(f"未知的任务名称: {task_name}", "error")
messagebox.showerror("错误", f"无法识别的操作: {task_name}", parent=self.root)
return
if task_func:
font_panel = None
if task_name == 'apply_wolf_fonts' and hasattr(self.main_window, "font_panel"):
font_panel = self.main_window.font_panel
font_panel.begin_apply()
try:
self._run_task_in_thread(task_func, task_args, task_kwargs, task_name, mode)
except Exception:
if font_panel:
font_panel.finish_apply()
raise
def save_pro_mode_settings(self, settings):
"""保存专业模式的设置到配置中。由 ProModePanel 调用。"""
if 'pro_mode_settings' not in self.config:
self.config['pro_mode_settings'] = {}
self.config['pro_mode_settings'].update(settings)
self.save_config() # 保存到文件
def save_config(self):
"""保存当前应用配置。"""
try:
# 更新当前选择的模式
self.config['selected_mode'] = self.main_window.get_current_mode()
self.config_manager.save_config(self.config)
self.log_message("配置已保存。", "success")
except Exception as e:
log.exception("保存配置失败。")
self.log_message(f"保存配置失败: {e}", "error")
messagebox.showerror("保存失败", f"无法保存配置文件。\n错误: {e}", parent=self.root)
def completion_notifications_enabled(self):
"""返回是否启用任务完成提醒。"""
return bool(self.config.get('enable_completion_notification', False))
def log_message(self, message, level="normal"):
"""将消息记录到 UI 日志区域。"""
if hasattr(self, 'main_window') and self.main_window:
self.main_window.add_log(message, level)
else:
print(f"[{level.upper()}] {message}") # UI 未就绪时的备用方案
def update_status(self, message):
"""更新 UI 状态栏文本。"""
if hasattr(self, 'main_window') and self.main_window:
self.main_window.update_status(message)
def update_easy_mode_status(self, message):
"""更新轻松模式状态标签。"""
if hasattr(self, 'main_window') and self.main_window:
self.main_window.update_easy_status(message)
def update_easy_mode_progress(self, value):
"""更新轻松模式进度条。"""
if hasattr(self, 'main_window') and self.main_window:
self.main_window.update_easy_progress(value)
def set_processing_state(self, processing):
"""设置应用的繁忙状态,并更新 UI 控件的可用性。"""
self.is_processing = processing
if hasattr(self, 'main_window') and self.main_window:
# MainWindow 需要实现 disable_controls/enable_controls 方法
self.main_window.set_controls_enabled(not processing)
# --- 内部方法 ---
def _get_work_subfolder(self, game_path_override=None):
"""获取当前游戏对应的 Works 子目录名。"""
current_game_path = game_path_override if game_path_override is not None else self.get_game_path()
if not current_game_path: return None
game_folder_name = text_processing.sanitize_filename(os.path.basename(current_game_path))
return game_folder_name or "UntitledGame"
def _get_translated_dir(self, game_path_override=None):
"""获取当前游戏翻译文件存放目录的完整路径。"""
subfolder = self._get_work_subfolder(game_path_override)
if not subfolder: return None
return os.path.join(self.works_dir, subfolder, "translated")
def _get_fallback_csv_path(self, game_path_override=None):
"""获取当前游戏 fallback_corrections.csv 的完整路径。"""
translated_dir = self._get_translated_dir(game_path_override)
if not translated_dir: return None
return os.path.join(translated_dir, "fallback_corrections.csv")
def _get_translated_json_path(self, game_path_override=None):
"""获取当前游戏 translation_translated.json 的完整路径。"""
translated_dir = self._get_translated_dir(game_path_override)
if not translated_dir: return None
return os.path.join(translated_dir, "translation_translated.json")
def get_tools_context_path(self, relative_path):
"""Resolve a tool input beneath the current Work directory."""
subfolder = self._get_work_subfolder()
if not subfolder or not relative_path:
return ""
return os.path.join(self.works_dir, subfolder, *Path(relative_path).parts)
def _find_translated_json_files(self, game_path_override=None):
"""查找当前游戏已翻译的 JSON 文件列表。"""
translated_dir = self._get_translated_dir(game_path_override)
if not translated_dir or not os.path.isdir(translated_dir):
return []
try:
return [
os.path.join(translated_dir, f)
for f in os.listdir(translated_dir)
if f.lower().endswith('.json')
]
except OSError as e:
log.error(f"无法读取翻译目录 {translated_dir}: {e}")
return []
def _check_game_path_set(self, path_to_check=None):
"""检查游戏路径是否已设置,如果未设置则显示错误。"""
current_path = path_to_check if path_to_check is not None else self.get_game_path()
if not current_path:
self.log_message("请先选择有效的游戏目录。", "error")
messagebox.showerror("错误", "请先选择一个有效的游戏目录。", parent=self.root)
return False
return True
def _run_task_in_thread(self, task_func, args, kwargs, task_name="后台任务", mode='pro'):
"""在后台线程中运行给定的任务函数。"""
self.set_processing_state(True)
self.update_status(f"正在执行: {task_name}...")
self._stop_requested = False # 重置停止标志
task_message_proxy = TaskMessageProxy(self.message_queue)
tracked_args = self._replace_message_queue_arg(args, task_message_proxy)
# 从 kwargs 中提取 task_id_for_callback,以便在异常时使用
# 实际的 task_id 会通过 kwargs 传递给 task_func
task_id_from_kwargs_for_exception_handling = kwargs.get('task_id_for_callback')
def wrapper():
task_start_time = time.time()
task_succeeded = False
try:
task_result = task_func(*tracked_args, **kwargs)
task_succeeded = bool(task_result)
# 注意:任务成功完成的消息由任务自身通过队列发送 ("success", ...)
except Exception as e:
task_message_proxy.has_problem = True
# 捕获任务函数本身抛出的未处理异常(理论上不应发生)
log.exception(f"任务 '{task_name}' 执行期间发生未捕获的严重错误。")
# 发送错误消息到队列
self.message_queue.put(("error", f"任务 '{task_name}' 失败: {traceback.format_exc()}")) # 发送完整 traceback
self.message_queue.put(("status", f"{task_name} 执行失败"))
# --- 如果任务因异常而终止,并且它有关联的回调ID,则尝试通知编辑器 ---
if task_id_from_kwargs_for_exception_handling and task_id_from_kwargs_for_exception_handling in self._editor_callbacks_registry:
editor_to_notify = self._editor_callbacks_registry.pop(task_id_from_kwargs_for_exception_handling, None)
if editor_to_notify and editor_to_notify.winfo_exists():
error_message_for_callback = f"任务 '{task_name}' 执行时发生意外错误: {e}"
log.info(f"任务 {task_id_from_kwargs_for_exception_handling} (异常结束) 将通知编辑器。消息: {error_message_for_callback}")
self.root.after(0, lambda w=editor_to_notify, msg=error_message_for_callback: w.handle_apply_base_dict_result(False, msg))
elif editor_to_notify: # 编辑器存在但窗口已关闭
log.info(f"任务 {task_id_from_kwargs_for_exception_handling} (异常结束) 的编辑器回调被跳过,因为窗口已关闭。")
finally:
elapsed = time.time() - task_start_time
log.info(f"任务 '{task_name}' (ID: {task_id_from_kwargs_for_exception_handling if task_id_from_kwargs_for_exception_handling else 'N/A'}) 线程执行完毕,耗时: {elapsed:.2f} 秒。")
if task_name == 'apply_wolf_fonts' and hasattr(self.main_window, "font_panel"):
self.root.after(0, self.main_window.font_panel.finish_apply)
# 确保发送 'done' 信号,即使任务内部忘记发送或出错中断
# 但为了避免重复发送,最好还是依赖任务自身发送
# self.message_queue.put(("done", None))
# 在主线程中更新状态
self.root.after(0, lambda: self.set_processing_state(False))
auto_import_pending = task_name == 'release_json' and task_succeeded and self._should_auto_import_after_release()
if self._should_emit_completion_notification(task_name, mode, task_succeeded) and not auto_import_pending:
self.root.after(
50,
lambda problem=task_message_proxy.has_problem: self._maybe_send_completion_notification(problem)
)
if auto_import_pending:
release_game_path = args[0] if args else None
self.root.after(100, lambda gp=release_game_path, m=mode: self._auto_import_after_release(gp, m))
# 移除对线程的引用
self.current_task_thread = None
# 启动线程
self.current_task_thread = threading.Thread(target=wrapper, daemon=True)
self.current_task_thread.start()
def _replace_message_queue_arg(self, args, replacement_queue):
"""Replace the task's message queue argument with a tracking proxy."""
return [
replacement_queue if arg is self.message_queue else arg
for arg in args
]
def _is_continuous_task(self, task_name, mode):
if task_name == 'easy_flow':
return mode == 'easy'
return mode == 'pro' and task_name in CONTINUOUS_TASK_NAMES
def _should_emit_completion_notification(self, task_name, mode, task_succeeded):
"""判断某个任务结束后是否应考虑发送完成提醒。"""
if not self._is_continuous_task(task_name, mode):
return False
if task_name == 'release_json' and task_succeeded and self._should_auto_import_after_release():
return False
return True
def _app_window_is_active(self):
try:
if platform.system() == "Windows":
return self._windows_app_window_is_foreground()
return self.root.focus_displayof() is not None
except Exception:
log.debug("检查窗口活跃状态失败,按活跃处理。", exc_info=True)
return True
def _windows_app_window_is_foreground(self):
import ctypes
from ctypes import wintypes
user32 = ctypes.windll.user32
foreground_window = user32.GetForegroundWindow()
if not foreground_window:
return False
user32.GetWindowThreadProcessId.argtypes = [wintypes.HWND, ctypes.POINTER(wintypes.DWORD)]
user32.GetWindowThreadProcessId.restype = wintypes.DWORD
foreground_pid = wintypes.DWORD()
user32.GetWindowThreadProcessId(foreground_window, ctypes.byref(foreground_pid))
if foreground_pid.value == os.getpid():
return True
try:
if self.root.focus_displayof() is not None:
return True
root_window = self.root.winfo_id()
except tk.TclError:
return True
# Tk may expose a child HWND via winfo_id(); compare top-level ancestors too.
ga_root = 2
user32.GetAncestor.argtypes = [wintypes.HWND, wintypes.UINT]
user32.GetAncestor.restype = wintypes.HWND
user32.IsChild.argtypes = [wintypes.HWND, wintypes.HWND]
user32.IsChild.restype = wintypes.BOOL
root_top = user32.GetAncestor(root_window, ga_root) or root_window
foreground_top = user32.GetAncestor(foreground_window, ga_root) or foreground_window
if foreground_window in (root_window, root_top):
return True
if foreground_top == root_top:
return True
return bool(user32.IsChild(root_top, foreground_window))
def _maybe_send_completion_notification(self, problem=False):
"""Notify only when enabled and the app window is not active."""
if not self.completion_notifications_enabled():
log.debug("完成提醒未发送:配置未启用。")
return
if self._app_window_is_active():
log.info("完成提醒未发送:应用窗口当前处于活跃状态。")
return
sent = windows_notifications.send_task_notification(problem=problem)
if sent:
log.info("完成提醒已发送:%s。", "当前任务出现问题" if problem else "当前任务已完成")
else:
log.warning("完成提醒未发送:Windows 任务中心提醒调用失败,请检查 winsdk/pywin32、通知权限或专注助手设置。")
def _should_auto_import_after_release(self):
"""检查第六步完成后是否应自动执行第七步。"""
pro_config = self.config.get('pro_mode_settings', {})
return bool(pro_config.get('auto_import_after_release', False))
def _auto_import_after_release(self, game_path, mode='pro'):
"""第六步释放成功后自动启动第七步导入。"""
if self.is_processing:
self.root.after(100, lambda gp=game_path, m=mode: self._auto_import_after_release(gp, m))
return
self.log_message("释放 JSON 完成,已启用自动导入,开始执行第 7 步。", "normal")
self.start_task('import', mode=mode, game_path=game_path)
def _process_messages(self):
"""处理来自后台任务的消息队列。"""
try:
while True: # 处理队列中的所有当前消息
message = self.message_queue.get_nowait()
msg_type, content = message
task_id_from_done_signal = None
callback_success = False
callback_message = ""
if msg_type == "done":
# 检查 content 是否是 (task_id, (success_bool, message_str)) 的格式
if isinstance(content, tuple) and len(content) == 2 and isinstance(content[0], str):
potential_task_id = content[0]
if isinstance(content[1], tuple) and len(content[1]) == 2 and isinstance(content[1][0], bool):
task_id_from_done_signal = potential_task_id
callback_success, callback_message = content[1]
log.info(f"收到带回调ID '{task_id_from_done_signal}' 的 'done' 信号。成功: {callback_success}, 消息: '{callback_message[:100]}...'")
else:
log.debug(f"收到普通 'done' 信号(content[0]是字符串但内容格式不符回调): {content}")
else:
log.debug(f"收到普通 'done' 信号,内容: {content}")
# 现在 task_id_from_done_signal 要么是 None,要么是从 "done" 消息中赋的值
if task_id_from_done_signal and task_id_from_done_signal in self._editor_callbacks_registry:
editor_to_notify = self._editor_callbacks_registry.pop(task_id_from_done_signal, None)
if editor_to_notify and editor_to_notify.winfo_exists():
log.info(f"为任务 {task_id_from_done_signal} 执行编辑器回调。")
self.root.after(0, lambda w=editor_to_notify, s=callback_success, m=callback_message: w.handle_apply_base_dict_result(s, m))
elif editor_to_notify:
log.info(f"任务 {task_id_from_done_signal} 的编辑器回调被跳过,因为窗口已关闭。")
# 注意:如果 "done" 信号是用于回调的,我们可能不希望它再触发下面的通用 "done" 处理逻辑
# 所以,这里在处理完回调后,可以考虑跳过后续的 msg_type == "done" 判断
# 或者,确保回调的 "done" 信号不会再被后续的通用 "done" 处理。
# 当前的结构,如果 task_id_from_done_signal 被赋值了,后续的 elif msg_type == "done" 不会执行。
elif msg_type == "log":
level, text = content
self.log_message(text, level)
elif msg_type == "status":
self.update_status(content)
elif msg_type == "success":
self.log_message(content, "success") # 在日志中也显示成功信息
# 可以在这里加一个短暂的成功状态显示,然后恢复默认
# self.update_status(content)
# self.root.after(3000, lambda: self.update_status("就绪"))
elif msg_type == "error":
self.log_message(content, "error")
# 可以在状态栏显示错误提示
# self.update_status("操作出错,详情请查看日志")
elif msg_type == "progress": # 特别为轻松模式
self.update_easy_mode_progress(content)
elif msg_type == "easy_status": # 特别为轻松模式
self.update_easy_mode_status(content)
elif msg_type == "font_revision_applied":
if hasattr(self.main_window, "font_panel"):
self.main_window.font_panel.refresh()
elif msg_type == "wolf_translation_imported":
if hasattr(self.main_window, "font_panel"):
panel = self.main_window.font_panel
if os.path.normcase(panel.game_path) == os.path.normcase(os.path.abspath(content)):
panel.refresh()
elif msg_type == "wolf_initialized":
if hasattr(self.main_window, "refresh_game_context"):
self.main_window.refresh_game_context()
elif msg_type == "done":
# 任务完成信号,由任务内部发送
# App 层主要用它来判断是否可以启动新任务
# self.set_processing_state(False) # 移到线程 wrapper 的 finally 中处理
self.log_message("后台任务处理完成。", "normal")
# 如果是轻松模式结束,可以显示最终状态
current_mode = self.main_window.get_current_mode()
if current_mode == 'easy' and not self.is_processing: # 确保是 easy 模式且真的结束了
# 检查最后的状态是否包含错误
last_status = self.main_window.get_status() # MainWindow 需要提供方法获取当前状态
if "失败" in last_status or "中止" in last_status or "错误" in last_status:
self.update_easy_mode_status("轻松模式执行完毕(有错误)。")
else:
self.update_easy_mode_status("轻松模式执行成功!")
self.message_queue.task_done() # 标记消息处理完成
except queue.Empty:
# 队列为空,是正常情况
pass
except Exception as e:
# 处理消息循环本身发生的错误
log.exception(f"处理消息队列时出错: {e}")
finally:
# 无论如何,100ms 后再次检查队列
self.root.after(100, self._process_messages)
def start_task_for_editor_callback(self, task_name, game_path, editor_instance):
"""
启动一个后台任务,并在任务完成后通过回调通知指定的编辑器实例。
"""
if self.is_processing:
if editor_instance and editor_instance.winfo_exists():
messagebox.showwarning("操作繁忙", "请等待当前操作完成后再试。", parent=editor_instance)
# 直接调用编辑器的回调,告知操作未执行
editor_instance.handle_apply_base_dict_result(False, "另一个任务正在运行,操作未执行。")
else:
messagebox.showwarning("操作繁忙", "请等待当前操作完成后再试。", parent=self.root)
return
# 为这个任务和编辑器实例的组合生成一个唯一的ID
# 使用时间戳确保ID的唯一性,以防编辑器快速关闭再打开
unique_task_id_for_callback = f"{task_name}_{id(editor_instance)}_{time.time_ns()}"
# 注册回调:存储 task_id 和 editor_instance 的映射
self._editor_callbacks_registry[unique_task_id_for_callback] = editor_instance
log.info(f"已为任务 '{task_name}' (编辑器: {id(editor_instance)}) 注册回调, ID: {unique_task_id_for_callback}")
# 调用通用的 start_task 方法,并传递这个 unique_id
self.start_task(
task_name=task_name,
game_path=game_path, # 确保 game_path 被使用
task_id_for_callback=unique_task_id_for_callback # 传递生成的唯一ID
)
def _start_game(self, game_path_to_start=None):
"""启动游戏。"""
current_game_path = game_path_to_start if game_path_to_start is not None else self.get_game_path()
if not self._check_game_path_set(current_game_path): return
player_exe = os.path.join(current_game_path, "Player.exe") # EasyRPG Player
rpg_rt_exe = os.path.join(current_game_path, "RPG_RT.exe") # 原版
vxace_exe = os.path.join(current_game_path, "Game.exe") # VX Ace / MV / MZ
exe_to_run = None
if os.path.exists(player_exe):
exe_to_run = player_exe
elif os.path.exists(vxace_exe):
exe_to_run = vxace_exe
elif os.path.exists(rpg_rt_exe):
exe_to_run = rpg_rt_exe
else:
messagebox.showerror("启动失败", f"未在游戏目录中找到 Player.exe、Game.exe 或 RPG_RT.exe。", parent=self.root)
self.log_message("无法启动游戏:未找到 Player.exe、Game.exe 或 RPG_RT.exe。", "error")
return
self.log_message(f"尝试启动游戏: {exe_to_run}")
try:
# 使用 subprocess.Popen 在后台启动,设置工作目录为游戏目录
subprocess.Popen([exe_to_run], cwd=current_game_path)
self.log_message("游戏已启动(在单独进程中)。", "success")
except Exception as e:
log.exception(f"启动游戏失败: {e}")
messagebox.showerror("启动失败", f"启动游戏时发生错误:\n{e}", parent=self.root)
self.log_message(f"启动游戏失败: {e}", "error")
def _open_dict_editor(self, game_path_to_edit=None):
"""打开世界观字典编辑器。"""
current_game_path = game_path_to_edit if game_path_to_edit is not None else self.get_game_path()
if not self._check_game_path_set(current_game_path): return
try:
DictEditorWindow(
parent=self.root,
app_controller=self,
works_dir=self.works_dir,
game_path=current_game_path, # <--- 使用 current_game_path
is_base_dict=False # 明确这是编辑游戏特定字典
)
self.log_message("世界观字典编辑器已打开。", "normal")
except Exception as e:
log.exception("打开字典编辑器时出错。")
messagebox.showerror("错误", f"无法打开字典编辑器:\n{e}", parent=self.root)
self.log_message(f"打开字典编辑器失败: {e}", "error")
def open_base_dict_editor(self, parent_for_editor=None): # 添加可选参数
"""打开基础字典编辑器。"""
try:
# 如果没有显式传递父窗口,则使用主窗口
actual_parent = parent_for_editor if parent_for_editor else self.root
editor = DictEditorWindow(
parent=actual_parent, # 使用实际的父窗口
app_controller=self,
works_dir=self.works_dir,
game_path=None,
is_base_dict=True
)
self.log_message("基础字典编辑器已打开。", "normal")
return editor # 返回创建的窗口实例
except Exception as e:
log.exception("打开基础字典编辑器时出错。")
messagebox.showerror("错误", f"无法打开基础字典编辑器:\n{e}", parent=actual_parent) # 使用 actual_parent
self.log_message(f"打开基础字典编辑器失败: {e}", "error")
return None # 打开失败返回 None
def _open_gemini_config(self):
"""打开 Gemini 配置窗口。"""
from ui.config_dialogs import WorldDictConfigWindow # 导入配置窗口类
WorldDictConfigWindow(self.root, self, self.config['world_dict_config'])
def _open_deepseek_config(self):
"""打开 DeepSeek 配置窗口。"""
from ui.config_dialogs import TranslateConfigWindow # 导入配置窗口类
TranslateConfigWindow(self.root, self, self.config['translate_config'])
def _open_rtp_selection(self):
"""打开 RTP 选择窗口。"""
from ui.rtp_dialog import RTPSelectionWindow # 导入 RTP 选择窗口类
# RTP 配置现在存在 config['pro_mode_settings']['rtp_options'] 中
pro_settings = self.config.setdefault('pro_mode_settings', {})
rtp_options = pro_settings.setdefault('rtp_options', {'2000': True, '2000en': False, '2003': False, '2003steam': False, '2003zh_tw': False})
# 传递配置给窗口,窗口修改后直接更新这个字典
RTPSelectionWindow(self.root, self, rtp_options)
# 更新专业模式面板上的按钮文本 (需要 MainWindow 提供方法)
self.main_window.update_rtp_button_text()
def _on_close(self):
"""应用程序关闭时的处理。"""
log.info("应用程序正在关闭...")
if self.is_processing:
if messagebox.askyesno("确认退出", "有后台任务正在运行,确定要强制退出吗?", parent=self.root):
log.warning("用户强制退出,后台任务可能未完成。")
# 可以尝试更优雅地停止线程,但 Popen 启动的外部进程无法直接停止
# self._stop_requested = True # 设置停止标志 (需要任务支持)
# self.thread_pool.shutdown(wait=False) # 不等待线程结束
if hasattr(self.main_window, "font_panel"):
self.main_window.font_panel.close()
self.root.destroy()
else:
return # 用户取消退出
else:
# 尝试保存最后的配置
self.save_config()
if hasattr(self.main_window, "font_panel"):
self.main_window.font_panel.close()
self.thread_pool.shutdown(wait=True) # 等待线程池关闭
self.root.destroy()