# -*- coding: utf-8 -*- """让屏幕采集器在设置变化时重建。 原来的实现: def _ensure_capturer(self): if self._capturer is None: self._capturer = ScreenCapturer( max_width=..., quality=...) return self._capturer 问题:**缓存住了**。改了 screen_max_width / screen_quality 之后, 正在推流的话新设置永远不生效 —— 用户会以为"改了没用"。 修法:记住建它时用的参数,参数变了就重建。 顺带把 relay_saver 里那句 reset_capturer() 去掉(Actions 上没这个方法, 写了也是白写)。 """ from __future__ import annotations import sys from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") SERVER = ROOT / "windows" / "lanremote" / "server.py" FEATURES = ROOT / "windows" / "lanremote" / "features.py" OLD = ''' def _ensure_capturer(self) -> ScreenCapturer | None: if self._capturer is None: self._capturer = ScreenCapturer( max_width=int(self.store.get("screen_max_width", 480) or 480), quality=int(self.store.get("screen_quality", 55) or 55)) return self._capturer''' NEW = ''' def _ensure_capturer(self) -> ScreenCapturer | None: """拿到屏幕采集器。**设置变了要重建。** 以前只判断 self._capturer is None,于是改了分辨率/画质之后 正在推流的会话永远用旧设置 —— 用户会以为"改了没用"。 现在记住建它时用的参数,参数一变就重建。 """ width = int(self.store.get("screen_max_width", 480) or 480) quality = int(self.store.get("screen_quality", 55) or 55) signature = (width, quality) if self._capturer is not None and \\ getattr(self, "_capturer_signature", None) == signature: return self._capturer self._capturer = ScreenCapturer(max_width=width, quality=quality) self._capturer_signature = signature return self._capturer''' INIT_OLD = ''' self._capturer: ScreenCapturer | None = None''' INIT_NEW = ''' self._capturer: ScreenCapturer | None = None # 建采集器时用的 (分辨率, 画质)。变了就重建,不然改设置不生效。 self._capturer_signature = None''' FEAT_OLD = ''' # 正在推流的话要重建采集器,新设置才生效 try: self.actions.reset_capturer() except Exception: # noqa: BLE001 pass return note''' FEAT_NEW = ''' # 采集器那边会自己检测参数变化并重建(见 server._ensure_capturer), # 这里不用额外做什么。 return note''' def main() -> int: ok = True text = SERVER.read_text(encoding="utf-8") for old, new, tag in ((OLD, NEW, "_ensure_capturer 按参数重建"), (INIT_OLD, INIT_NEW, "__init__ 加签名字段")): if old not in text: print(f" [失败] {tag} —— 锚点没找到") ok = False continue text = text.replace(old, new, 1) print(f" server.py: {tag}") SERVER.write_text(text, encoding="utf-8") text = FEATURES.read_text(encoding="utf-8") if FEAT_OLD in text: text = text.replace(FEAT_OLD, FEAT_NEW, 1) FEATURES.write_text(text, encoding="utf-8") print(" features.py: 去掉无效的 reset_capturer 调用") else: print(" features.py: [跳过] 没找到那段(可能已改)") import ast for label, path in (("server.py", SERVER), ("features.py", FEATURES)): try: ast.parse(path.read_text(encoding="utf-8")) except SyntaxError as exc: print(f" {label} 语法错误: {exc}") ok = False return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())