diff --git a/config/config.json b/config/config.json index 9fa2b7c..2feb0df 100644 --- a/config/config.json +++ b/config/config.json @@ -194,12 +194,12 @@ "monitor_critical_mb": 200.0 }, "debug_captures": { - "enabled": false, + "enabled": true, "keep_days": 1, "max_files": 1000, "auto_cleanup": true, "async_save": false, - "log_on_complete": false + "log_on_complete": true }, "fudai_detail_template_threshold": 0.52, "switch_room_wait_after_stage_diag_seconds": 7.5, diff --git a/douyin/core/path_config.py b/douyin/core/path_config.py index 5d64225..ba675ec 100644 --- a/douyin/core/path_config.py +++ b/douyin/core/path_config.py @@ -184,7 +184,8 @@ def find_executable(cmd: str) -> Optional[str]: return first else: # 类 Unix:尝试 command -v 再 which - proc = subprocess.run(["/bin/sh", "-c", f"command -v {cmd} || which {cmd}"], capture_output=True, text=True) + import shlex + proc = subprocess.run(["/bin/sh", "-c", f"command -v {shlex.quote(cmd)} || which {shlex.quote(cmd)}"], capture_output=True, text=True) if proc.returncode == 0 and proc.stdout: first = proc.stdout.splitlines()[0].strip() if first: diff --git a/douyin/core/underlying_operations.py b/douyin/core/underlying_operations.py index feb3868..d93a974 100644 --- a/douyin/core/underlying_operations.py +++ b/douyin/core/underlying_operations.py @@ -926,8 +926,8 @@ class underlying_operations: if not ime: return False # 1) 优先走 ime(某些 ROM 会失败) - self._run_adb_shell(device_id, f"ime enable {ime}", timeout_seconds=2.5) - if self._run_adb_shell(device_id, f"ime set {ime}", timeout_seconds=2.5): + self._run_adb_shell(device_id, f"ime enable {shlex.quote(ime)}", timeout_seconds=2.5) + if self._run_adb_shell(device_id, f"ime set {shlex.quote(ime)}", timeout_seconds=2.5): return True # 2) 退化:尝试 settings put(通常也会被 ROM 限制,但试一下无害) diff --git a/douyin/core/underlying_operations_pkg/device_select.py b/douyin/core/underlying_operations_pkg/device_select.py index b8907ae..69aee1e 100644 --- a/douyin/core/underlying_operations_pkg/device_select.py +++ b/douyin/core/underlying_operations_pkg/device_select.py @@ -57,10 +57,18 @@ def select_device(): print("检测到多台设备且当前不是交互式终端,请通过 --device-id 明确指定设备。") return False num = input() - num = int(num) + try: + num = int(num) + except ValueError: + print("请输入有效数字:") + return False while not num in dictdevice.keys(): print("输入不正确,请重新输入:") num = input() - num = int(num) + try: + num = int(num) + except ValueError: + print("请输入有效数字:") + return False return dictdevice[num] diff --git a/douyin/core/underlying_operations_pkg/screenshot_direct.py b/douyin/core/underlying_operations_pkg/screenshot_direct.py index d233ef6..c7229e6 100644 --- a/douyin/core/underlying_operations_pkg/screenshot_direct.py +++ b/douyin/core/underlying_operations_pkg/screenshot_direct.py @@ -128,14 +128,26 @@ def capture_screenshot_direct( return False # 优先走 exec-out - execout_timeout_sec = float(os.environ.get("ADB_EXECOUT_TIMEOUT_SEC", "10")) - execout_warn_after_sec = float( - os.environ.get("ADB_EXECOUT_WARN_AFTER_SEC", "3") - ) - execout_heartbeat_interval_sec = float( - os.environ.get("ADB_EXECOUT_HEARTBEAT_INTERVAL_SEC", "5") - ) - execout_heartbeat_max = int(os.environ.get("ADB_EXECOUT_HEARTBEAT_MAX", "3")) + try: + execout_timeout_sec = float(os.environ.get("ADB_EXECOUT_TIMEOUT_SEC", "10")) + except ValueError: + execout_timeout_sec = 10.0 + try: + execout_warn_after_sec = float( + os.environ.get("ADB_EXECOUT_WARN_AFTER_SEC", "3") + ) + except ValueError: + execout_warn_after_sec = 3.0 + try: + execout_heartbeat_interval_sec = float( + os.environ.get("ADB_EXECOUT_HEARTBEAT_INTERVAL_SEC", "5") + ) + except ValueError: + execout_heartbeat_interval_sec = 5.0 + try: + execout_heartbeat_max = int(os.environ.get("ADB_EXECOUT_HEARTBEAT_MAX", "3")) + except ValueError: + execout_heartbeat_max = 3 for attempt in range(2): try: diff --git a/douyin/fudai/core/fudai_detail_orchestrator_pkg/read_once.py b/douyin/fudai/core/fudai_detail_orchestrator_pkg/read_once.py index c9e243d..384e951 100644 --- a/douyin/fudai/core/fudai_detail_orchestrator_pkg/read_once.py +++ b/douyin/fudai/core/fudai_detail_orchestrator_pkg/read_once.py @@ -255,22 +255,18 @@ def read_fudai_detail_once(analyser: Any) -> Tuple[str, Tuple, dict]: pass # 使用统一的截图路径配置 - try: - from douyin.core import path_config - - pic_path = path_config.get_screenshot_path() - except Exception: - # 回退到旧路径(向后兼容) - from douyin.core import path_config - - pic_path = path_config.get_screenshot_path() + from douyin.core import path_config + pic_path = path_config.get_screenshot_path() pic_rgb = None try: if os.path.exists(pic_path): _img0 = time.time() pic = Image.open(pic_path) - pic.load() - pic_rgb = pic.convert("RGB") + try: + pic.load() + pic_rgb = pic.convert("RGB") + finally: + pic.close() image_load_ms = round((time.time() - _img0) * 1000, 1) _log_perf("after_image_load") # 移除:避免过多性能日志 except Exception: diff --git a/douyin/fudai/douyin_fudai_finder_pkg/detail_verifier.py b/douyin/fudai/douyin_fudai_finder_pkg/detail_verifier.py index 7c2db44..ff9890a 100644 --- a/douyin/fudai/douyin_fudai_finder_pkg/detail_verifier.py +++ b/douyin/fudai/douyin_fudai_finder_pkg/detail_verifier.py @@ -79,8 +79,11 @@ class FudaiDetailVerifier: pic_path = path_config.get_screenshot_path() if os.path.exists(pic_path): pic = Image.open(pic_path) - pic.load() - pic_rgb = pic.convert("RGB") + try: + pic.load() + pic_rgb = pic.convert("RGB") + finally: + pic.close() width, height = pic_rgb.size rx = int( diff --git a/douyin/fudai/douyin_fudai_finder_pkg/detector.py b/douyin/fudai/douyin_fudai_finder_pkg/detector.py index 2f96478..c2889d4 100644 --- a/douyin/fudai/douyin_fudai_finder_pkg/detector.py +++ b/douyin/fudai/douyin_fudai_finder_pkg/detector.py @@ -452,8 +452,6 @@ class FudaiDetector: pass return detected_x - if found_x is not None: - break # 找到后退出offset_y循环 except Exception: pass diff --git a/douyin/fudai/douyin_fudai_finder_pkg/image_acquirer.py b/douyin/fudai/douyin_fudai_finder_pkg/image_acquirer.py index 7a81d49..79ff531 100644 --- a/douyin/fudai/douyin_fudai_finder_pkg/image_acquirer.py +++ b/douyin/fudai/douyin_fudai_finder_pkg/image_acquirer.py @@ -62,18 +62,21 @@ class FudaiImageAcquirer: # 读取截图 try: pic = Image.open(self.screenshot_path) - pic.load() - elapsed = (time.time() - start_time) * 1000 try: - self.analyser._log_kv( - "[福袋查找] 图像获取", - "成功", - elapsed_ms=round(elapsed, 2), - size=f"{pic.size[0]}x{pic.size[1]}", - ) - except Exception: - pass - return pic.convert("RGBA") + pic.load() + elapsed = (time.time() - start_time) * 1000 + try: + self.analyser._log_kv( + "[福袋查找] 图像获取", + "成功", + elapsed_ms=round(elapsed, 2), + size=f"{pic.size[0]}x{pic.size[1]}", + ) + except Exception: + pass + return pic.convert("RGBA") + finally: + pic.close() except Exception: return None except Exception as e: diff --git a/douyin/fudai/douyin_fudai_finder_pkg/optimized_entry.py b/douyin/fudai/douyin_fudai_finder_pkg/optimized_entry.py index 06893ba..8f1dace 100644 --- a/douyin/fudai/douyin_fudai_finder_pkg/optimized_entry.py +++ b/douyin/fudai/douyin_fudai_finder_pkg/optimized_entry.py @@ -39,7 +39,7 @@ def check_have_fudai_with_ready_screenshot_optimized( screenshot_ready: 是否已有可用截图 返回: - 福袋X坐标(1080基准),未找到返回False + 福袋X坐标(1080基准),未找到返回 0 """ # 只检测,不点击(避免与 find_and_open_fudai_detail 重复点击) try: @@ -47,13 +47,13 @@ def check_have_fudai_with_ready_screenshot_optimized( pic = acquirer.acquire_screenshot(force_refresh=not screenshot_ready) if pic is None: - return False + return 0 detector = FudaiDetector(analyser) detected_x, _detect_method = detector.detect(pic) if detected_x is not None: return int(detected_x) - return False + return 0 except Exception: return False