diff --git a/app/task/Okww/AutoProxy.py b/app/task/Okww/AutoProxy.py index 6d23c5bc..aa6dfc66 100644 --- a/app/task/Okww/AutoProxy.py +++ b/app/task/Okww/AutoProxy.py @@ -211,8 +211,13 @@ async def set_okww(self) -> None: mas_config_dir = self._okww_mas_config_dir() if self.script_config.get("Script", "ConfigPathMode") == "Folder": + tmp_dst = self.script_config_path.with_name( + self.script_config_path.name + ".tmp" + ) + shutil.rmtree(tmp_dst, ignore_errors=True) + shutil.copytree(mas_config_dir, tmp_dst, dirs_exist_ok=True) shutil.rmtree(self.script_config_path, ignore_errors=True) - shutil.copytree(mas_config_dir, self.script_config_path, dirs_exist_ok=True) + tmp_dst.rename(self.script_config_path) elif self.script_config.get("Script", "ConfigPathMode") == "File": shutil.copy( mas_config_dir / self.script_config_path.name, diff --git a/app/task/Okww/manager.py b/app/task/Okww/manager.py index 9e662e23..ba6b7ca5 100644 --- a/app/task/Okww/manager.py +++ b/app/task/Okww/manager.py @@ -51,6 +51,8 @@ def __init__(self, script_info: ScriptItem): self.task_info = script_info.task_info self.script_info = script_info self.check_result = "-" + self.temp_path: Path | None = None + self.script_config_path: Path | None = None async def check(self) -> str: if self.task_info.mode not in METHOD_BOOK: @@ -120,7 +122,6 @@ async def prepare(self): if self.task_info.mode == "AutoProxy": self.script_config_path = Path(self.script_config.get("Script", "ConfigPath")) self.temp_path = Path.cwd() / f"data/{self.script_info.script_id}/Temp" - shutil.rmtree(self.temp_path, ignore_errors=True) self.temp_path.mkdir(parents=True, exist_ok=True) if self.script_config_path.exists(): if self.script_config.get("Script", "ConfigPathMode") == "Folder": @@ -170,13 +171,18 @@ async def final_task(self): return if self.task_info.mode == "AutoProxy": - if hasattr(self, "temp_path") and self.temp_path.exists(): + if self.temp_path and self.temp_path.exists(): if self.script_config.get("Script", "ConfigPathMode") == "Folder": logger.info(f"复原 OK-WW 脚本配置文件: {self.temp_path}") - shutil.rmtree(self.script_config_path, ignore_errors=True) + tmp_dst = self.script_config_path.with_name( + self.script_config_path.name + ".tmp" + ) + shutil.rmtree(tmp_dst, ignore_errors=True) shutil.copytree( - self.temp_path, self.script_config_path, dirs_exist_ok=True + self.temp_path, tmp_dst, dirs_exist_ok=True ) + shutil.rmtree(self.script_config_path, ignore_errors=True) + tmp_dst.rename(self.script_config_path) elif ( self.script_config.get("Script", "ConfigPathMode") == "File" and (self.temp_path / "config.temp").exists() @@ -192,36 +198,50 @@ async def final_task(self): if script_cfg.is_locked: await script_cfg.unlock() - if self.task_info.mode == "AutoProxy" and hasattr(self, "user_config"): + if self.task_info.mode == "AutoProxy": await script_cfg.UserData.load(await self.user_config.toDict()) - self.script_info.status = "完成" finally: if script_cfg.is_locked: with suppress(Exception): await script_cfg.unlock() + self.script_info.status = "完成" + async def on_crash(self, e: Exception): self.script_info.status = "异常" logger.exception(f"OK-WW任务出现异常: {e}") script_uid = uuid.UUID(self.script_info.script_id) if ( self.task_info.mode == "AutoProxy" - and hasattr(self, "script_config") - and hasattr(self, "temp_path") + and self.temp_path is not None and self.temp_path.exists() ): if self.script_config.get("Script", "ConfigPathMode") == "Folder": - shutil.rmtree(self.script_config_path, ignore_errors=True) + tmp_dst = self.script_config_path.with_name( + self.script_config_path.name + ".tmp" + ) + shutil.rmtree(tmp_dst, ignore_errors=True) shutil.copytree( - self.temp_path, self.script_config_path, dirs_exist_ok=True + self.temp_path, tmp_dst, dirs_exist_ok=True ) + shutil.rmtree(self.script_config_path, ignore_errors=True) + tmp_dst.rename(self.script_config_path) elif ( self.script_config.get("Script", "ConfigPathMode") == "File" and (self.temp_path / "config.temp").exists() ): shutil.copy(self.temp_path / "config.temp", self.script_config_path) shutil.rmtree(self.temp_path, ignore_errors=True) + + try: + if self.task_info.mode == "AutoProxy": + await Config.ScriptConfig[script_uid].UserData.load( + await self.user_config.toDict() + ) + except Exception: + logger.exception("on_crash 写回 UserConfig 失败,放弃本次状态变更") + script_cfg = Config.ScriptConfig[script_uid] if script_cfg.is_locked: with suppress(Exception): diff --git a/res/version.json b/res/version.json index 341e576c..0bf0ada5 100644 --- a/res/version.json +++ b/res/version.json @@ -8,7 +8,8 @@ "okww专项 重构游戏管理项", "okww专项 完善用户标签tag", "okww专项 优化用户配置页面的样式", - "okww专项 调度台展示游戏配置与启动流程日志" + "okww专项 调度台展示游戏配置与启动流程日志", + "okww专项 重构配置优化" ], "修复BUG": [ "修复静默模式未对M9A本体生效的问题",