diff --git a/docs/BACKLOG.md b/docs/BACKLOG.md index d91f85d..f1de3c5 100644 --- a/docs/BACKLOG.md +++ b/docs/BACKLOG.md @@ -42,6 +42,17 @@ fusion_result 0 **风险**:低。纯路径/清单;不改判定逻辑。 +**状态(2026-11-14 云端)**:已实现,待实机验收。 + +- 根因:上游 `ebb6629`(调整图片路径)把图片挪到 `assets/images/<主题>/<语言>/`,`tasks/mirror/__init__.py` 没跟着改。 + 按名字找图那一层(`path_manager` 依次搜各主题/语言目录)没坏,只是列目录列空了。 +- 修法:`install_shop_lists()` 在 `mirror` / `all` 任务里按 AALC 的六个图片根(`dark|default` × `zh_cn|en|share`) + 合并去重,**原地** `extend` 这三个列表——`in_shop.py` 用 `from tasks.mirror import ...` 在导入时绑定了列表对象, + 重新赋值对它无效。上游清单已非空时不动。 +- 补丁只在任务进程里生效,上面那条 `python -c` 仍会打印 `0 0 0`。离线等价验证是 `tests/test_shop_lists.py` + (执行上游真实 `__init__.py` 得到 `0 0 0`,补丁后为 `5 19 31`)。实机看任务日志里这一行: + `[lc_mcp] patch: 镜牢商店清单按 assets/images/<主题>/<语言> 补齐: must_purchase=5, must_be_abandoned=19, fusion_result=31` + --- ## P0-2 镜牢走位被配置降级成「只按 ↑」 diff --git a/lc_mcp/aalc_patches.py b/lc_mcp/aalc_patches.py index 800355c..e24828e 100644 --- a/lc_mcp/aalc_patches.py +++ b/lc_mcp/aalc_patches.py @@ -603,3 +603,59 @@ def install_system_team_selection(log=print): mirror_mod.select_battle_team = select_battle_team log('[lc_mcp] patch: 镜牢按体系名选队') return original + + +# ---------------------------------------------------------------- 镜牢商店清单 + +# 上游 ebb6629 把图片挪到 assets/images/<主题>/<语言>/ 下, tasks/mirror/__init__.py 却仍按 +# ./assets/images/share/mirror/shop/... 列目录, 三个清单恒为空。2026-11-14 实测进店 51 秒, +# 打印三次 "开始购买必买项" 一件没买; 弃用饰品识别与公式合成也一起空转。 +# 按名字找图(mirror/shop/<清单>/<文件>)由 path_manager 依次搜各主题/语言目录, 那一层没坏。 +SHOP_LISTS = ('must_purchase', 'must_be_abandoned', 'fusion_result') +IMAGE_ROOTS = ('dark/zh_cn', 'dark/en', 'dark/share', 'default/zh_cn', 'default/en', 'default/share') + + +def shop_list_images(kind, images_dir=os.path.join('assets', 'images'), roots=IMAGE_ROOTS): + """各主题/语言目录下 mirror/shop/ 的图片, 按 AALC 图片名格式去重排序。""" + names = set() + for root in roots: + folder = os.path.join(images_dir, *root.split('/'), 'mirror', 'shop', kind) + try: + entries = os.listdir(folder) + except OSError: + continue + names.update(name for name in entries + if name.lower().endswith('.png') and os.path.isfile(os.path.join(folder, name))) + return ['mirror/shop/%s/%s' % (kind, name) for name in sorted(names)] + + +def install_shop_lists(log=print, images_dir=os.path.join('assets', 'images')): + """原地补齐 tasks.mirror 的三个商店清单; 上游清单已非空时不动。 + + in_shop 用 from tasks.mirror import ... 在导入时绑定列表对象, 所以必须原地 extend, + 给模块属性重新赋值对它无效。 + """ + try: + import tasks.mirror as mirror_pkg + except Exception as e: + log('[lc_mcp] 商店清单补丁跳过: 无法导入 tasks.mirror: %s' % e) + return {} + try: + from utils.path_manager import PathManager + roots = tuple(PathManager.DEFAULT_ACTIVE_PATHS) + except Exception: + roots = IMAGE_ROOTS + filled = {} + for kind in SHOP_LISTS: + current = getattr(mirror_pkg, kind, None) + if not isinstance(current, list): + log('[lc_mcp] 商店清单补丁跳过 %s: 上游没有这个列表' % kind) + continue + if current: + continue # 上游已修好或已补过 + current.extend(shop_list_images(kind, images_dir, roots)) + filled[kind] = len(current) + if filled: + log('[lc_mcp] patch: 镜牢商店清单按 assets/images/<主题>/<语言> 补齐: %s' + % ', '.join('%s=%d' % item for item in filled.items())) + return filled diff --git a/lc_mcp/aalc_task.py b/lc_mcp/aalc_task.py index dcd0cd1..a5b24a1 100644 --- a/lc_mcp/aalc_task.py +++ b/lc_mcp/aalc_task.py @@ -13,8 +13,8 @@ from . import killswitch, preflight from .aalc_engine import bootstrap from .aalc_patches import (WindowGuard, driver_click_tasks, headless_hdr_warning, install_default_battle_team, install_exact_team_match, - install_robust_set_foreground, install_system_team_selection, - mirror_progress, stop_driver_click) + install_robust_set_foreground, install_shop_lists, + install_system_team_selection, mirror_progress, stop_driver_click) from .game_lock import GameBusyError, exclusive_game # 任务 -> {参数名: AALC 配置项}; 参数只在本次运行内生效 @@ -127,6 +127,7 @@ def _run(task, params): # 镜牢: 编队是自定义名字(体系名), AALC 只会找 "编队#N"; 改成按体系名点选。 if task in ('mirror', 'all'): install_system_team_selection(log=log) + install_shop_lists(log=log) guard = WindowGuard(screen, cfg, log=log) guard.start() restore_path = os.environ.get(RESTORE_ENV) diff --git a/tests/test_shop_lists.py b/tests/test_shop_lists.py new file mode 100644 index 0000000..fe62539 --- /dev/null +++ b/tests/test_shop_lists.py @@ -0,0 +1,119 @@ +# -*- coding: utf-8 -*- +"""镜牢商店"必买 / 必卖 / 公式合成"清单。 + +2026-11-14 实测: 上游 tasks/mirror/__init__.py 仍按 ./assets/images/share/... 列目录, +图片早已挪到 assets/images/<主题>/<语言>/ 下, 三个清单恒为空, 进店 51 秒一件没买。 +""" +import os +import sys +import tempfile +import types +import unittest +from contextlib import contextmanager +from pathlib import Path +from unittest import mock + +REPO = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(REPO)) +from lc_mcp import aalc_patches # noqa: E402 + +AALC = REPO / 'vendor' / 'aalc' +IMAGES = AALC / 'assets' / 'images' + + +@contextmanager +def chdir(path): + old = os.getcwd() + os.chdir(path) + try: + yield + finally: + os.chdir(old) + + +def as_tasks_mirror(package): + """让 import tasks.mirror 拿到给定模块(父包也要在 sys.modules 里)。""" + parent = types.ModuleType('tasks') + parent.mirror = package + return mock.patch.dict(sys.modules, {'tasks': parent, 'tasks.mirror': package}) + + +def upstream_mirror_package(): + """在 AALC 目录下执行上游 tasks/mirror/__init__.py, 得到它真实生成的三个列表。""" + module = types.ModuleType('tasks.mirror') + source = (AALC / 'tasks' / 'mirror' / '__init__.py').read_text(encoding='utf-8') + with chdir(AALC): + exec(compile(source, str(AALC / 'tasks' / 'mirror' / '__init__.py'), 'exec'), module.__dict__) + return module + + +@unittest.skipUnless(IMAGES.is_dir(), 'vendor/aalc 子模块未检出') +class UpstreamShopListTest(unittest.TestCase): + + def test_upstream_lists_are_empty_and_patch_fills_them(self): + package = upstream_mirror_package() + self.assertEqual([len(getattr(package, k)) for k in aalc_patches.SHOP_LISTS], [0, 0, 0], + '上游已修好时可以删掉这个补丁') + # in_shop 在导入时绑定的就是这几个列表对象 + in_shop = types.SimpleNamespace(**{k: getattr(package, k) for k in aalc_patches.SHOP_LISTS}) + lines = [] + with as_tasks_mirror(package), chdir(AALC): + filled = aalc_patches.install_shop_lists(log=lines.append) + self.assertEqual(filled, {'must_purchase': 5, 'must_be_abandoned': 19, 'fusion_result': 31}) + self.assertEqual([len(getattr(in_shop, k)) for k in aalc_patches.SHOP_LISTS], [5, 19, 31]) + self.assertIn('mirror/shop/must_purchase/carmilla.png', in_shop.must_purchase) + self.assertIn('must_purchase=5', '\n'.join(lines)) + + def test_every_listed_name_resolves_under_some_image_root(self): + for kind in aalc_patches.SHOP_LISTS: + for name in aalc_patches.shop_list_images(kind, str(IMAGES)): + with self.subTest(name=name): + self.assertTrue(any((IMAGES / root / name).is_file() for root in aalc_patches.IMAGE_ROOTS)) + + def test_local_roots_match_aalc_path_manager(self): + source = (AALC / 'utils' / 'path_manager.py').read_text(encoding='utf-8') + for root in aalc_patches.IMAGE_ROOTS: + self.assertIn('"%s"' % root, source) + + +class ShopListImagesTest(unittest.TestCase): + + def setUp(self): + tmp = tempfile.TemporaryDirectory() + self.addCleanup(tmp.cleanup) + self.images = tmp.name + + def put(self, root, kind, name): + folder = os.path.join(self.images, *root.split('/'), 'mirror', 'shop', kind) + os.makedirs(folder, exist_ok=True) + open(os.path.join(folder, name), 'wb').close() + + def test_merges_themes_and_languages_without_duplicates(self): + self.put('default/share', 'must_purchase', 'b.png') + self.put('dark/share', 'must_purchase', 'b.png') + self.put('default/zh_cn', 'must_purchase', 'a.png') + self.put('default/share', 'must_purchase', 'notes.txt') + os.makedirs(os.path.join(self.images, 'default', 'share', 'mirror', 'shop', 'must_purchase', 'x.png')) + self.assertEqual(aalc_patches.shop_list_images('must_purchase', self.images), + ['mirror/shop/must_purchase/a.png', 'mirror/shop/must_purchase/b.png']) + + def test_missing_folders_give_an_empty_list(self): + self.assertEqual(aalc_patches.shop_list_images('fusion_result', self.images), []) + + def test_non_empty_upstream_list_is_left_alone(self): + self.put('default/share', 'must_purchase', 'a.png') + package = types.SimpleNamespace(must_purchase=['upstream.png'], must_be_abandoned=[], fusion_result=[]) + with as_tasks_mirror(package): + filled = aalc_patches.install_shop_lists(log=lambda m: None, images_dir=self.images) + self.assertEqual(package.must_purchase, ['upstream.png']) + self.assertEqual(filled, {'must_be_abandoned': 0, 'fusion_result': 0}) + + def test_import_failure_is_reported_not_raised(self): + lines = [] + with mock.patch.dict(sys.modules, {'tasks': types.ModuleType('tasks'), 'tasks.mirror': None}): + self.assertEqual(aalc_patches.install_shop_lists(log=lines.append), {}) + self.assertIn('商店清单补丁跳过', '\n'.join(lines)) + + +if __name__ == '__main__': + unittest.main()