feat(core): 增强离线模式和日志记录功能

- 在主窗口中添加离线模式提示弹窗,用户可清晰了解离线模式切换情况。
- 更新离线模式管理器,优化补丁文件扫描和日志记录,确保无论调试模式下均能记录相关信息。
- 在下载管理器和补丁管理器中增强调试信息的记录,提升错误处理能力。
- 更新卸载处理程序,增加详细的日志记录,确保用户操作的透明性和可追溯性。
This commit is contained in:
hyb-oyqq
2025-08-07 16:10:11 +08:00
parent bf80c19fe1
commit 575116e45c
6 changed files with 333 additions and 38 deletions

View File

@@ -577,6 +577,7 @@ class DownloadManager:
"""处理下载队列中的下一个任务"""
if not self.download_queue:
# 所有下载任务都已完成,进行后检查
debug_mode = self.is_debug_mode()
if debug_mode:
logger.debug("DEBUG: 所有下载任务完成,进行后检查")
# 使用patch_detector进行安装后哈希比较

View File

@@ -62,8 +62,9 @@ class OfflineModeManager:
directory = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
debug_mode = self._is_debug_mode()
if debug_mode:
logger.debug(f"DEBUG: 扫描离线补丁文件,目录: {directory}")
# 无论是否为调试模式,都记录扫描操作
logger.info(f"扫描离线补丁文件,目录: {directory}")
# 要查找的补丁文件名
patch_files = ["vol.1.7z", "vol.2.7z", "vol.3.7z", "vol.4.7z", "after.7z"]
@@ -77,10 +78,19 @@ class OfflineModeManager:
if os.path.isfile(file_path):
patch_name = file.lower()
found_patches[patch_name] = file_path
# 无论是否为调试模式,都记录找到的补丁文件
logger.info(f"找到离线补丁文件: {patch_name} 路径: {file_path}")
if debug_mode:
logger.debug(f"DEBUG: 找到离线补丁文件: {patch_name} 路径: {file_path}")
self.offline_patches = found_patches
# 记录扫描结果
if found_patches:
logger.info(f"共找到 {len(found_patches)} 个离线补丁文件: {list(found_patches.keys())}")
else:
logger.info("未找到任何离线补丁文件")
return found_patches
def has_offline_patches(self):
@@ -113,6 +123,7 @@ class OfflineModeManager:
"\n未找到任何离线补丁文件,无法启用离线模式。\n\n请将补丁文件放置在软件所在目录后再尝试。\n",
QMessageBox.StandardButton.Ok
).exec()
logger.warning("尝试启用离线模式失败:未找到任何离线补丁文件")
return False
if debug_mode:
@@ -137,6 +148,8 @@ class OfflineModeManager:
ui_manager.online_mode_action.setChecked(not enabled)
ui_manager.offline_mode_action.setChecked(enabled)
# 无论是否为调试模式,都记录离线模式状态变化
logger.info(f"离线模式已{'启用' if enabled else '禁用'}")
if debug_mode:
logger.debug(f"DEBUG: 离线模式已{'启用' if enabled else '禁用'}")

View File

@@ -70,13 +70,21 @@ class PatchDetector:
"""
debug_mode = self._is_debug_mode()
if debug_mode:
logger.debug(f"DEBUG: 检查 {game_version} 是否已安装补丁,目录: {game_dir}")
if game_version not in self.game_info:
if debug_mode:
logger.debug(f"DEBUG: {game_version} 不在支持的游戏列表中,跳过检查")
return False
# 获取可能的补丁文件路径
install_path_base = os.path.basename(self.game_info[game_version]["install_path"])
patch_file_path = os.path.join(game_dir, install_path_base)
if debug_mode:
logger.debug(f"DEBUG: 基础补丁文件路径: {patch_file_path}")
# 尝试查找补丁文件,支持不同大小写
patch_files_to_check = [
patch_file_path,
@@ -86,18 +94,27 @@ class PatchDetector:
patch_file_path.replace("_", "-"),
]
if debug_mode:
logger.debug(f"DEBUG: 将检查以下补丁文件路径: {patch_files_to_check}")
# 查找补丁文件
for patch_path in patch_files_to_check:
if os.path.exists(patch_path):
if debug_mode:
logger.debug(f"找到补丁文件: {patch_path}")
logger.debug(f"DEBUG: 找到补丁文件: {patch_path}")
logger.debug(f"DEBUG: {game_version} 已安装补丁")
return True
# 检查是否存在被禁用的补丁文件(带.fain后缀
disabled_path = f"{patch_path}.fain"
if os.path.exists(disabled_path):
if debug_mode:
logger.debug(f"找到被禁用的补丁文件: {disabled_path}")
logger.debug(f"DEBUG: 找到被禁用的补丁文件: {disabled_path}")
logger.debug(f"DEBUG: {game_version} 已安装补丁(但被禁用)")
return True
if debug_mode:
logger.debug(f"DEBUG: 未找到补丁文件,继续检查补丁文件夹")
# 检查是否有补丁文件夹
patch_folders_to_check = [
@@ -106,28 +123,46 @@ class PatchDetector:
os.path.join(game_dir, "PATCH"),
]
if debug_mode:
logger.debug(f"DEBUG: 将检查以下补丁文件夹: {patch_folders_to_check}")
for patch_folder in patch_folders_to_check:
if os.path.exists(patch_folder):
if debug_mode:
logger.debug(f"找到补丁文件夹: {patch_folder}")
logger.debug(f"DEBUG: 找到补丁文件夹: {patch_folder}")
logger.debug(f"DEBUG: {game_version} 已安装补丁")
return True
if debug_mode:
logger.debug(f"DEBUG: 未找到补丁文件夹继续检查game/patch文件夹")
# 检查game/patch文件夹
game_folders = ["game", "Game", "GAME"]
patch_folders = ["patch", "Patch", "PATCH"]
if debug_mode:
logger.debug(f"DEBUG: 将检查以下game/patch组合: {[(g, p) for g in game_folders for p in patch_folders]}")
for game_folder in game_folders:
for patch_folder in patch_folders:
game_patch_folder = os.path.join(game_dir, game_folder, patch_folder)
if os.path.exists(game_patch_folder):
if debug_mode:
logger.debug(f"找到game/patch文件夹: {game_patch_folder}")
logger.debug(f"DEBUG: 找到game/patch文件夹: {game_patch_folder}")
logger.debug(f"DEBUG: {game_version} 已安装补丁")
return True
if debug_mode:
logger.debug(f"DEBUG: 未找到game/patch文件夹继续检查配置文件和脚本文件")
# 检查配置文件
config_files = ["config.json", "Config.json", "CONFIG.JSON"]
script_files = ["scripts.json", "Scripts.json", "SCRIPTS.JSON"]
if debug_mode:
logger.debug(f"DEBUG: 将在game文件夹中检查以下配置文件: {config_files}")
logger.debug(f"DEBUG: 将在game文件夹中检查以下脚本文件: {script_files}")
for game_folder in game_folders:
game_path = os.path.join(game_dir, game_folder)
if os.path.exists(game_path):
@@ -136,7 +171,8 @@ class PatchDetector:
config_path = os.path.join(game_path, config_file)
if os.path.exists(config_path):
if debug_mode:
logger.debug(f"找到配置文件: {config_path}")
logger.debug(f"DEBUG: 找到配置文件: {config_path}")
logger.debug(f"DEBUG: {game_version} 已安装补丁")
return True
# 检查脚本文件
@@ -144,12 +180,13 @@ class PatchDetector:
script_path = os.path.join(game_path, script_file)
if os.path.exists(script_path):
if debug_mode:
logger.debug(f"找到脚本文件: {script_path}")
logger.debug(f"DEBUG: 找到脚本文件: {script_path}")
logger.debug(f"DEBUG: {game_version} 已安装补丁")
return True
# 没有找到补丁文件或文件夹
if debug_mode:
logger.debug(f"{game_version}{game_dir} 中没有安装补丁")
logger.debug(f"DEBUG: {game_version}{game_dir} 中没有安装补丁")
return False
def check_patch_disabled(self, game_dir, game_version):

View File

@@ -86,17 +86,24 @@ class PatchManager:
debug_mode = self._is_debug_mode()
if debug_mode:
self.logger.debug(f"开始卸载 {game_version} 补丁,目录: {game_dir}")
self.logger.debug(f"DEBUG: 开始卸载 {game_version} 补丁,目录: {game_dir}")
self.logger.info(f"开始卸载 {game_version} 补丁,目录: {game_dir}")
if game_version not in self.game_info:
error_msg = f"无法识别游戏版本: {game_version}"
if debug_mode:
self.logger.debug(f"DEBUG: 卸载失败 - {error_msg}")
self.logger.error(f"卸载失败 - {error_msg}")
if not silent:
QMessageBox.critical(
None,
f"错误 - {self.app_name}",
f"\n无法识别游戏版本: {game_version}\n",
f"\n{error_msg}\n",
QMessageBox.StandardButton.Ok,
)
return False if not silent else {"success": False, "message": f"无法识别游戏版本: {game_version}", "files_removed": 0}
return False if not silent else {"success": False, "message": error_msg, "files_removed": 0}
try:
files_removed = 0
@@ -105,6 +112,9 @@ class PatchManager:
install_path_base = os.path.basename(self.game_info[game_version]["install_path"])
patch_file_path = os.path.join(game_dir, install_path_base)
if debug_mode:
self.logger.debug(f"DEBUG: 基础补丁文件路径: {patch_file_path}")
# 尝试查找补丁文件,支持不同大小写
patch_files_to_check = [
patch_file_path,
@@ -115,7 +125,7 @@ class PatchManager:
]
if debug_mode:
self.logger.debug(f"查找以下可能的补丁文件路径: {patch_files_to_check}")
self.logger.debug(f"DEBUG: 查找以下可能的补丁文件路径: {patch_files_to_check}")
# 查找并删除补丁文件,包括启用和禁用的
patch_file_found = False
@@ -123,44 +133,68 @@ class PatchManager:
# 检查常规补丁文件
if os.path.exists(patch_path):
patch_file_found = True
if debug_mode:
self.logger.debug(f"DEBUG: 找到补丁文件: {patch_path},准备删除")
self.logger.info(f"删除补丁文件: {patch_path}")
os.remove(patch_path)
files_removed += 1
if debug_mode:
self.logger.debug(f"已删除补丁文件: {patch_path}")
self.logger.debug(f"DEBUG: 已删除补丁文件: {patch_path}")
# 检查被禁用的补丁文件(带.fain后缀
disabled_path = f"{patch_path}.fain"
if os.path.exists(disabled_path):
patch_file_found = True
if debug_mode:
self.logger.debug(f"DEBUG: 找到被禁用的补丁文件: {disabled_path},准备删除")
self.logger.info(f"删除被禁用的补丁文件: {disabled_path}")
os.remove(disabled_path)
files_removed += 1
if debug_mode:
self.logger.debug(f"已删除被禁用的补丁文件: {disabled_path}")
self.logger.debug(f"DEBUG: 已删除被禁用的补丁文件: {disabled_path}")
if not patch_file_found and debug_mode:
self.logger.debug(f"未找到补丁文件,检查了以下路径: {patch_files_to_check}")
self.logger.debug(f"也检查了禁用的补丁文件(.fain后缀")
if not patch_file_found:
if debug_mode:
self.logger.debug(f"DEBUG: 未找到补丁文件,检查了以下路径: {patch_files_to_check}")
self.logger.debug(f"DEBUG: 也检查了禁用的补丁文件(.fain后缀")
self.logger.warning(f"未找到 {game_version} 的补丁文件")
# 检查是否有额外的签名文件 (.sig)
if game_version == "NEKOPARA After":
if debug_mode:
self.logger.debug(f"DEBUG: {game_version} 需要检查额外的签名文件")
for patch_path in patch_files_to_check:
# 检查常规签名文件
sig_file_path = f"{patch_path}.sig"
if os.path.exists(sig_file_path):
if debug_mode:
self.logger.debug(f"DEBUG: 找到签名文件: {sig_file_path},准备删除")
self.logger.info(f"删除签名文件: {sig_file_path}")
os.remove(sig_file_path)
files_removed += 1
if debug_mode:
self.logger.debug(f"已删除签名文件: {sig_file_path}")
self.logger.debug(f"DEBUG: 已删除签名文件: {sig_file_path}")
# 检查被禁用补丁的签名文件
disabled_sig_path = f"{patch_path}.fain.sig"
if os.path.exists(disabled_sig_path):
if debug_mode:
self.logger.debug(f"DEBUG: 找到被禁用补丁的签名文件: {disabled_sig_path},准备删除")
self.logger.info(f"删除被禁用补丁的签名文件: {disabled_sig_path}")
os.remove(disabled_sig_path)
files_removed += 1
if debug_mode:
self.logger.debug(f"已删除被禁用补丁的签名文件: {disabled_sig_path}")
self.logger.debug(f"DEBUG: 已删除被禁用补丁的签名文件: {disabled_sig_path}")
# 删除patch文件夹
if debug_mode:
self.logger.debug(f"DEBUG: 检查并删除patch文件夹")
patch_folders_to_check = [
os.path.join(game_dir, "patch"),
os.path.join(game_dir, "Patch"),
@@ -169,12 +203,20 @@ class PatchManager:
for patch_folder in patch_folders_to_check:
if os.path.exists(patch_folder):
if debug_mode:
self.logger.debug(f"DEBUG: 找到补丁文件夹: {patch_folder},准备删除")
self.logger.info(f"删除补丁文件夹: {patch_folder}")
import shutil
shutil.rmtree(patch_folder)
files_removed += 1
if debug_mode:
self.logger.debug(f"已删除补丁文件夹: {patch_folder}")
self.logger.debug(f"DEBUG: 已删除补丁文件夹: {patch_folder}")
# 删除game/patch文件夹
if debug_mode:
self.logger.debug(f"DEBUG: 检查并删除game/patch文件夹")
game_folders = ["game", "Game", "GAME"]
patch_folders = ["patch", "Patch", "PATCH"]
@@ -182,12 +224,20 @@ class PatchManager:
for patch_folder in patch_folders:
game_patch_folder = os.path.join(game_dir, game_folder, patch_folder)
if os.path.exists(game_patch_folder):
if debug_mode:
self.logger.debug(f"DEBUG: 找到game/patch文件夹: {game_patch_folder},准备删除")
self.logger.info(f"删除game/patch文件夹: {game_patch_folder}")
import shutil
shutil.rmtree(game_patch_folder)
files_removed += 1
if debug_mode:
self.logger.debug(f"已删除game/patch文件夹: {game_patch_folder}")
self.logger.debug(f"DEBUG: 已删除game/patch文件夹: {game_patch_folder}")
# 删除配置文件
if debug_mode:
self.logger.debug(f"DEBUG: 检查并删除配置文件和脚本文件")
config_files = ["config.json", "Config.json", "CONFIG.JSON"]
script_files = ["scripts.json", "Scripts.json", "SCRIPTS.JSON"]
@@ -198,55 +248,82 @@ class PatchManager:
for config_file in config_files:
config_path = os.path.join(game_path, config_file)
if os.path.exists(config_path):
if debug_mode:
self.logger.debug(f"DEBUG: 找到配置文件: {config_path},准备删除")
self.logger.info(f"删除配置文件: {config_path}")
os.remove(config_path)
files_removed += 1
if debug_mode:
self.logger.debug(f"已删除配置文件: {config_path}")
self.logger.debug(f"DEBUG: 已删除配置文件: {config_path}")
# 删除脚本文件
for script_file in script_files:
script_path = os.path.join(game_path, script_file)
if os.path.exists(script_path):
if debug_mode:
self.logger.debug(f"DEBUG: 找到脚本文件: {script_path},准备删除")
self.logger.info(f"删除脚本文件: {script_path}")
os.remove(script_path)
files_removed += 1
if debug_mode:
self.logger.debug(f"已删除脚本文件: {script_path}")
self.logger.debug(f"DEBUG: 已删除脚本文件: {script_path}")
# 更新安装状态
self.installed_status[game_version] = False
if debug_mode:
self.logger.debug(f"DEBUG: 已更新 {game_version} 的安装状态为未安装")
# 在非静默模式且非批量卸载模式下显示卸载成功消息
if not silent and game_version != "all":
# 显示卸载成功消息
if files_removed > 0:
success_msg = f"\n{game_version} 补丁卸载成功!\n共删除 {files_removed} 个文件/文件夹。\n"
if debug_mode:
self.logger.debug(f"DEBUG: 显示卸载成功消息: {success_msg}")
QMessageBox.information(
None,
f"卸载完成 - {self.app_name}",
f"\n{game_version} 补丁卸载成功!\n共删除 {files_removed} 个文件/文件夹。\n",
success_msg,
QMessageBox.StandardButton.Ok,
)
else:
warning_msg = f"\n未找到 {game_version} 的补丁文件,可能未安装补丁或已被移除。\n"
if debug_mode:
self.logger.debug(f"DEBUG: 显示警告消息: {warning_msg}")
QMessageBox.warning(
None,
f"警告 - {self.app_name}",
f"\n未找到 {game_version} 的补丁文件,可能未安装补丁或已被移除。\n",
warning_msg,
QMessageBox.StandardButton.Ok,
)
# 卸载成功
if debug_mode:
self.logger.debug(f"DEBUG: {game_version} 卸载完成,共删除 {files_removed} 个文件/文件夹")
self.logger.info(f"{game_version} 卸载完成,共删除 {files_removed} 个文件/文件夹")
if silent:
return {"success": True, "message": f"{game_version} 补丁卸载成功", "files_removed": files_removed}
return True
except Exception as e:
error_message = f"卸载 {game_version} 补丁时出错:{str(e)}"
if debug_mode:
self.logger.debug(f"DEBUG: {error_message}")
import traceback
self.logger.debug(f"DEBUG: 错误详情:\n{traceback.format_exc()}")
self.logger.error(error_message)
# 在非静默模式且非批量卸载模式下显示卸载失败消息
if not silent and game_version != "all":
# 显示卸载失败消息
error_message = f"\n卸载 {game_version} 补丁时出错:\n\n{str(e)}\n"
if debug_mode:
self.logger.debug(f"卸载错误 - {str(e)}")
import traceback
self.logger.debug(f"错误详情:\n{traceback.format_exc()}")
self.logger.debug(f"DEBUG: 显示卸载失败消息")
QMessageBox.critical(
None,
@@ -274,16 +351,38 @@ class PatchManager:
debug_mode = self._is_debug_mode()
results = []
if debug_mode:
self.logger.debug(f"DEBUG: 开始批量卸载补丁,游戏数量: {len(game_dirs)}")
self.logger.debug(f"DEBUG: 要卸载的游戏: {list(game_dirs.keys())}")
self.logger.info(f"开始批量卸载补丁,游戏数量: {len(game_dirs)}")
self.logger.info(f"要卸载的游戏: {list(game_dirs.keys())}")
for version, path in game_dirs.items():
if debug_mode:
self.logger.debug(f"DEBUG: 处理游戏 {version},路径: {path}")
self.logger.info(f"开始卸载 {version} 的补丁")
try:
# 在批量模式下使用静默卸载
if debug_mode:
self.logger.debug(f"DEBUG: 使用静默模式卸载 {version}")
result = self.uninstall_patch(path, version, silent=True)
if isinstance(result, dict): # 使用了静默模式
if result["success"]:
success_count += 1
if debug_mode:
self.logger.debug(f"DEBUG: {version} 卸载成功,删除了 {result['files_removed']} 个文件/文件夹")
self.logger.info(f"{version} 卸载成功,删除了 {result['files_removed']} 个文件/文件夹")
else:
fail_count += 1
if debug_mode:
self.logger.debug(f"DEBUG: {version} 卸载失败,原因: {result['message']}")
self.logger.warning(f"{version} 卸载失败,原因: {result['message']}")
results.append({
"version": version,
"success": result["success"],
@@ -293,8 +392,15 @@ class PatchManager:
else: # 兼容旧代码,不应该执行到这里
if result:
success_count += 1
if debug_mode:
self.logger.debug(f"DEBUG: {version} 卸载成功(旧格式)")
self.logger.info(f"{version} 卸载成功(旧格式)")
else:
fail_count += 1
if debug_mode:
self.logger.debug(f"DEBUG: {version} 卸载失败(旧格式)")
self.logger.warning(f"{version} 卸载失败(旧格式)")
results.append({
"version": version,
"success": result,
@@ -304,7 +410,12 @@ class PatchManager:
except Exception as e:
if debug_mode:
self.logger.debug(f"卸载 {version} 时出错: {str(e)}")
self.logger.debug(f"DEBUG: 卸载 {version} 时出错: {str(e)}")
import traceback
self.logger.debug(f"DEBUG: 错误详情:\n{traceback.format_exc()}")
self.logger.error(f"卸载 {version} 时出错: {str(e)}")
fail_count += 1
results.append({
"version": version,
@@ -312,7 +423,12 @@ class PatchManager:
"message": f"卸载出错: {str(e)}",
"files_removed": 0
})
if debug_mode:
self.logger.debug(f"DEBUG: 批量卸载完成,成功: {success_count},失败: {fail_count}")
self.logger.info(f"批量卸载完成,成功: {success_count},失败: {fail_count}")
return success_count, fail_count, results
def show_uninstall_result(self, success_count, fail_count, results=None):
@@ -323,6 +439,11 @@ class PatchManager:
fail_count: 卸载失败的数量
results: 详细结果列表,如果提供,会显示更详细的信息
"""
debug_mode = self._is_debug_mode()
if debug_mode:
self.logger.debug(f"DEBUG: 显示卸载结果,成功: {success_count},失败: {fail_count}")
result_text = f"\n批量卸载完成!\n成功: {success_count}\n失败: {fail_count}\n"
# 如果有详细结果,添加到消息中
@@ -330,11 +451,24 @@ class PatchManager:
success_list = [r["version"] for r in results if r["success"]]
fail_list = [r["version"] for r in results if not r["success"]]
if debug_mode:
self.logger.debug(f"DEBUG: 成功卸载的游戏: {success_list}")
self.logger.debug(f"DEBUG: 卸载失败的游戏: {fail_list}")
if success_list:
result_text += f"\n【成功卸载】:\n{chr(10).join(success_list)}\n"
if fail_list:
result_text += f"\n【卸载失败】:\n{chr(10).join(fail_list)}\n"
# 记录更详细的失败原因
if debug_mode:
for r in results:
if not r["success"]:
self.logger.debug(f"DEBUG: {r['version']} 卸载失败原因: {r['message']}")
if debug_mode:
self.logger.debug(f"DEBUG: 显示卸载结果对话框")
QMessageBox.information(
None,