Просмотр исходного кода

Merge pull request #1998 from Ghostuel/feat/xinyu-mingxiao-scroll

feat: 适配新语明霄卷轴主题及共用阴阳寮入口掩码
huangrunheng 2 недель назад
Родитель
Сommit
d1881b827f

+ 1 - 0
assets/i18n/zh-CN.json

@@ -151,6 +151,7 @@
   "ap100_green_mark_help": "100体爬塔绿标位置",
   "enable_ap100_anti_detect": "100体爬塔防检测",
   "enable_ap100_anti_detect_help": "100体爬塔战斗过程是否随机点击或滑动",
+  "costume_theme_xinyu_mingxiao": "新语明霄",
   "costume_battle_15": "灵狐寄愿",
   "costume_battle_scene_default": "默认",
   "costume_battle_scene_1": "鎏金圣域",

+ 8 - 6
tasks/Component/Costume/config.py

@@ -39,6 +39,7 @@ class CarpBannerType(str, Enum):
 # 主题,就是庭院最右下角的展开按钮
 class ThemeType(str, Enum):
     COSTUME_THEME_DEFAULT = 'costume_theme_default'  # 伊始之卷
+    COSTUME_THEME_XINYU_MINGXIAO = 'costume_theme_xinyu_mingxiao'  # 新语明霄
 
 # 幕间,就是式神录这里
 class ShikigamiType(str, Enum):
@@ -103,9 +104,10 @@ class CostumeConfig(BaseModel):
             return RealmType.COSTUME_REALM_DEFAULT
         return v
 
-
-
-
-
-
-
+    @field_validator('costume_theme_type', mode='before')
+    @classmethod
+    def validate_theme(cls, v):
+        # Preserve selections saved by the earlier local numbered adaptation.
+        if v in ('costume_theme_1', 'costume_theme_2'):
+            return ThemeType.COSTUME_THEME_XINYU_MINGXIAO
+        return v

+ 12 - 0
tasks/Component/Costume/costume_base.py

@@ -13,6 +13,7 @@ from tasks.Component.Costume.config import (
     BattleType, BattleSceneType, CarpBannerType
 )
 from tasks.Component.Costume.assets import CostumeAssets
+from tasks.Component.CostumeTheme.theme import apply_theme
 from tasks.Component.CostumeBattle.assets import CostumeBattleAssets
 from tasks.Component.CostumeShikigami.assets import CostumeShikigamiAssets
 from tasks.Component.CostumeCarpBanner.assets import CostumeCarpBannerAssets
@@ -111,6 +112,17 @@ class CostumeBase:
         self.check_costume_battle(config.costume_battle_type)
         self.check_costume_battle_scene(config.costume_battle_scene_type)
         self.check_costume_shikigami(config.costume_shikigami_type)
+        self.check_costume_theme(config.costume_theme_type)
+
+    def check_costume_theme(self, theme_type: ThemeType):
+        apply_theme(theme_type)
+        if theme_type != ThemeType.COSTUME_THEME_DEFAULT:
+            # Supplementary labels are available before the UI rebuilds its translations.
+            labels = I18n.load_additions().get('zh-CN', {})
+            theme_name = labels.get(theme_type.value)
+            if not theme_name:
+                theme_name = I18n.trans_zh_cn(theme_type)
+            logger.info(f'Switch menu theme {theme_type} ({theme_name})')
 
     def replace_img(self,
                     asset_before: str,

+ 17 - 0
tasks/Component/CostumeTheme/assets.py

@@ -0,0 +1,17 @@
+from module.atom.image import RuleImage
+from module.atom.click import RuleClick
+from module.atom.long_click import RuleLongClick
+from module.atom.swipe import RuleSwipe
+from module.atom.ocr import RuleOcr
+from module.atom.list import RuleList
+
+# This file was automatically generated by ./dev_tools/assets_extract.py.
+# Don't modify it manually.
+class CostumeThemeAssets: 
+
+
+	# Image Rule Assets
+	# 新语明霄:收起状态卷轴 
+	I_MAIN_SCROLL_CLOSE_2 = RuleImage(roi_front=(1190,632,48,58), roi_back=(1160,580,108,130), threshold=0.8, method="Template matching", file="./tasks/Component/CostumeTheme/theme2/theme2_main_scroll_close_2.png")
+
+

+ 35 - 0
tasks/Component/CostumeTheme/theme.py

@@ -0,0 +1,35 @@
+"""庭院菜单主题。原地更新共享规则,保持导航图持有的对象引用有效。"""
+from copy import deepcopy
+
+from tasks.Component.Costume.config import ThemeType
+from tasks.Component.CostumeTheme.assets import CostumeThemeAssets
+from tasks.GameUi.assets import GameUiAssets
+
+
+_THEME_NAMES = (
+    'I_MAIN_SCROLL_CLOSE',
+    'I_MAIN_GOTO_COLLECTION', 'I_MAIN_GOTO_TRAVEL',
+    'I_MAIN_GOTO_TEAM', 'I_MAIN_GOTO_GUILD', 'I_MAIN_GOTO_MALL',
+    'I_MAIN_GOTO_DAILY', 'I_MAIN_GOTO_FRIENDS', 'I_MAIN_GOTO_ONMYODO',
+    'I_MAIN_GOTO_SHIKIGAMI_RECORDS',
+)
+# Capture pristine defaults before any task can select a menu theme.
+_DEFAULT_RULES = {name: deepcopy(getattr(GameUiAssets, name)) for name in _THEME_NAMES}
+_THEME_RULES = {
+    ThemeType.COSTUME_THEME_DEFAULT: _DEFAULT_RULES,
+    ThemeType.COSTUME_THEME_XINYU_MINGXIAO: {
+        # Entry icons use the shared GameUi defaults, not theme-specific copies.
+        **_DEFAULT_RULES,
+        'I_MAIN_SCROLL_CLOSE': CostumeThemeAssets.I_MAIN_SCROLL_CLOSE_2,
+    },
+}
+
+
+def apply_theme(theme_type: ThemeType) -> None:
+    rules = _THEME_RULES[theme_type]
+    for name, source in rules.items():
+        target = getattr(GameUiAssets, name)
+        # Reset cached image/name/features and match state as well as file/ROI.
+        # Do NOT replace the object: page transitions retain references to it.
+        target.__dict__.clear()
+        target.__dict__.update(deepcopy(source.__dict__))

+ 11 - 0
tasks/Component/CostumeTheme/theme2/image.json

@@ -0,0 +1,11 @@
+[
+  {
+    "itemName": "main_scroll_close_2",
+    "imageName": "theme2_main_scroll_close_2.png",
+    "roiFront": "1190,632,48,58",
+    "roiBack": "1160,580,108,130",
+    "method": "Template matching",
+    "threshold": 0.8,
+    "description": "新语明霄:收起状态卷轴"
+  }
+]

BIN
tasks/Component/CostumeTheme/theme2/theme2_main_scroll_close_2.png


+ 1 - 1
tasks/GameUi/assets.py

@@ -126,7 +126,7 @@ class GameUiAssets:
 	# 庭院前往商店 
 	I_MAIN_GOTO_MALL = RuleImage(roi_front=(664,661,40,18), roi_back=(601,613,163,78), threshold=0.7, method="Template matching", file="./tasks/GameUi/page/page_main_goto_mall.png")
 	# 庭院前往阴阳寮 
-	I_MAIN_GOTO_GUILD = RuleImage(roi_front=(495,590,50,22), roi_back=(495,590,120,100), threshold=0.7, method="Template matching", file="./tasks/GameUi/page/page_main_goto_guild.png")
+	I_MAIN_GOTO_GUILD = RuleImage(roi_front=(495,590,50,22), roi_back=(495,590,120,100), threshold=0.7, method="Masked template matching", file="./tasks/GameUi/page/page_main_goto_guild.png")
 	# 庭院前往组队 
 	I_MAIN_GOTO_TEAM = RuleImage(roi_front=(395,590,38,48), roi_back=(395,590,120,100), threshold=0.7, method="Template matching", file="./tasks/GameUi/page/page_main_goto_team.png")
 	# 庭院前往图鉴 

+ 1 - 1
tasks/GameUi/page/image_main.json

@@ -85,7 +85,7 @@
     "imageName": "page_main_goto_guild.png",
     "roiFront": "495,590,50,22",
     "roiBack": "495,590,120,100",
-    "method": "Template matching",
+    "method": "Masked template matching",
     "threshold": 0.7,
     "description": "庭院前往阴阳寮"
   },

BIN
tasks/GameUi/page/page_main_goto_guild_mask.png


+ 109 - 0
tests/tasks/Component/CostumeTheme/test_theme.py

@@ -0,0 +1,109 @@
+"""Offline regression tests; no device or game interaction."""
+import unittest
+from unittest.mock import patch
+from module.server.i18n import I18n
+import json
+from pathlib import Path
+from copy import deepcopy
+
+from tasks.Component.Costume.config import CostumeConfig, ThemeType
+from tasks.Component.Costume.costume_base import CostumeBase
+from tasks.Component.CostumeTheme.theme import apply_theme, _THEME_NAMES
+from tasks.GameUi.assets import GameUiAssets
+
+
+class ThemeTests(unittest.TestCase):
+    def setUp(self):
+        apply_theme(ThemeType.COSTUME_THEME_DEFAULT)
+
+    def tearDown(self):
+        apply_theme(ThemeType.COSTUME_THEME_DEFAULT)
+
+    def test_enum_schema_and_default(self):
+        self.assertEqual(CostumeConfig().costume_theme_type, ThemeType.COSTUME_THEME_DEFAULT)
+        self.assertEqual(CostumeConfig(costume_theme_type='costume_theme_xinyu_mingxiao').costume_theme_type,
+                         ThemeType.COSTUME_THEME_XINYU_MINGXIAO)
+        schema = CostumeConfig.model_json_schema()
+        self.assertIn('costume_theme_xinyu_mingxiao', schema['$defs']['ThemeType']['enum'])
+
+    def test_shared_references_are_updated_and_default_restores(self):
+        refs = {name: getattr(GameUiAssets, name) for name in _THEME_NAMES}
+        defaults = {name: (rule.file, tuple(rule.roi_front), rule.roi_back) for name, rule in refs.items()}
+        for rule in refs.values():
+            _ = rule.image  # Prime old image cache before changing theme.
+            _ = rule.name
+        CostumeBase().check_costume_theme(ThemeType.COSTUME_THEME_XINYU_MINGXIAO)
+        for name, rule in refs.items():
+            self.assertIs(rule, getattr(GameUiAssets, name))
+            if name == 'I_MAIN_SCROLL_CLOSE':
+                self.assertIn('/CostumeTheme/theme2/', rule.file)
+            else:
+                self.assertEqual((rule.file, tuple(rule.roi_front), rule.roi_back), defaults[name])
+            self.assertIsNone(rule._image)
+            self.assertNotIn('name', rule.__dict__)
+            self.assertEqual(rule.image.shape[:2], (rule.roi_front[3], rule.roi_front[2]))
+        CostumeBase().check_costume_theme(ThemeType.COSTUME_THEME_DEFAULT)
+        for name, rule in refs.items():
+            self.assertEqual((rule.file, tuple(rule.roi_front), rule.roi_back), defaults[name])
+            self.assertIsNone(rule._image)
+
+    def test_reapply_resets_match_position_without_mutating_source(self):
+        apply_theme(ThemeType.COSTUME_THEME_XINYU_MINGXIAO)
+        rule = GameUiAssets.I_MAIN_SCROLL_CLOSE
+        original = tuple(rule.roi_front)
+        rule.roi_front[0] = 0
+        apply_theme(ThemeType.COSTUME_THEME_XINYU_MINGXIAO)
+        self.assertEqual(tuple(rule.roi_front), original)
+
+    def test_only_scroll_has_theme_specific_material(self):
+        from tasks.Component.CostumeTheme.assets import CostumeThemeAssets
+        folder = Path(__file__).resolve().parents[4] / 'tasks/Component/CostumeTheme/theme2'
+        items = json.loads((folder / 'image.json').read_text(encoding='utf-8'))
+        self.assertEqual([item['itemName'] for item in items], ['main_scroll_close_2'])
+        self.assertEqual({p.name for p in folder.glob('*.png')},
+                         {'theme2_main_scroll_close_2.png'})
+        self.assertEqual({name for name in vars(CostumeThemeAssets) if name.startswith('I_')},
+                         {'I_MAIN_SCROLL_CLOSE_2'})
+
+    def test_previous_local_selections_are_preserved(self):
+        for value in ('costume_theme_1', 'costume_theme_2'):
+            config = CostumeConfig(costume_theme_type=value)
+            self.assertEqual(config.costume_theme_type, ThemeType.COSTUME_THEME_XINYU_MINGXIAO)
+            self.assertEqual(config.model_dump(mode='json')['costume_theme_type'],
+                             'costume_theme_xinyu_mingxiao')
+
+    def test_theme_log_uses_supplementary_label_before_ui_translation_update(self):
+        theme = ThemeType.COSTUME_THEME_XINYU_MINGXIAO
+        with patch.object(I18n, 'load_additions', return_value={
+                'zh-CN': {theme.value: '新语明霄'}}), \
+             patch.object(I18n, 'trans_zh_cn', return_value=theme.value) as fallback, \
+             patch('tasks.Component.Costume.costume_base.logger') as log:
+            CostumeBase().check_costume_theme(theme)
+        fallback.assert_not_called()
+        log.info.assert_called_once_with(f'Switch menu theme {theme} (新语明霄)')
+
+    def test_theme_log_falls_back_when_supplementary_label_is_missing(self):
+        theme = ThemeType.COSTUME_THEME_XINYU_MINGXIAO
+        with patch.object(I18n, 'load_additions', return_value={}), \
+             patch.object(I18n, 'trans_zh_cn', return_value='已有运行时翻译') as fallback, \
+             patch('tasks.Component.Costume.costume_base.logger') as log:
+            CostumeBase().check_costume_theme(theme)
+        fallback.assert_called_once_with(theme)
+        log.info.assert_called_once_with(f'Switch menu theme {theme} (已有运行时翻译)')
+
+    def test_invalid_theme_does_not_mutate_rules(self):
+        for selected in (ThemeType.COSTUME_THEME_DEFAULT, ThemeType.COSTUME_THEME_XINYU_MINGXIAO):
+            with self.subTest(selected=selected):
+                apply_theme(selected)
+                refs = {name: getattr(GameUiAssets, name) for name in _THEME_NAMES}
+                states = {name: deepcopy(rule.__dict__) for name, rule in refs.items()}
+                with self.assertRaises(KeyError):
+                    apply_theme('not_a_theme')
+                for name, ref in refs.items():
+                    with self.subTest(rule=name):
+                        self.assertIs(getattr(GameUiAssets, name), ref)
+                        self.assertEqual(ref.__dict__, states[name])
+
+
+if __name__ == '__main__':
+    unittest.main()

BIN
tests/tasks/GameUi/fixtures/guild_search_closed.png


BIN
tests/tasks/GameUi/fixtures/guild_search_open.png


+ 135 - 0
tests/tasks/GameUi/test_shared_guild_mask.py

@@ -0,0 +1,135 @@
+"""Offline tests for the shared courtyard guild entrance (no device required).
+
+Fixtures contain only the 120x100 guild search area at (495,590), cropped from
+user-provided new-theme open/closed screenshots after 1280x720 normalization.
+The original-template and altered-background cases are synthetic controls,
+not real screenshots of the default theme.
+"""
+import hashlib
+import json
+import unittest
+from copy import deepcopy
+from pathlib import Path
+
+import cv2
+import numpy as np
+
+from module.atom.image import RuleImage, set_roi_match_padding
+from tasks.GameUi.assets import GameUiAssets
+from tasks.Component.Costume.config import ThemeType
+from tasks.Component.CostumeTheme.theme import apply_theme
+
+ROOT = Path(__file__).resolve().parents[3]
+FIXTURES = Path(__file__).resolve().parent / 'fixtures'
+OTHER_ENTRANCES = ('COLLECTION', 'TRAVEL', 'TEAM', 'MALL', 'DAILY', 'FRIENDS',
+                   'ONMYODO', 'SHIKIGAMI_RECORDS')
+
+
+class SharedGuildMaskTests(unittest.TestCase):
+    def setUp(self):
+        apply_theme(ThemeType.COSTUME_THEME_DEFAULT)
+        set_roi_match_padding(0)
+
+    def tearDown(self):
+        apply_theme(ThemeType.COSTUME_THEME_DEFAULT)
+        set_roi_match_padding(0)
+
+    def rule(self, method='Masked template matching'):
+        source = GameUiAssets.I_MAIN_GOTO_GUILD
+        return RuleImage(tuple(source.roi_front), source.roi_back, method,
+                         source.threshold, source.file)
+
+    def frame(self, state):
+        crop = cv2.cvtColor(cv2.imread(str(FIXTURES / ('guild_search_' + state + '.png'))),
+                            cv2.COLOR_BGR2RGB)
+        frame = np.full((720, 1280, 3), 100, dtype=np.uint8)
+        frame[590:690, 495:615] = crop
+        return frame
+
+    def test_shared_source_and_mask_contract(self):
+        items = json.loads((ROOT / 'tasks/GameUi/page/image_main.json').read_text(encoding='utf-8'))
+        item = next(x for x in items if x['itemName'] == 'main_goto_guild')
+        rule = self.rule()
+        self.assertEqual(item['method'], 'Masked template matching')
+        self.assertEqual(GameUiAssets.I_MAIN_GOTO_GUILD.method, item['method'])
+        self.assertEqual(rule.threshold, 0.7)
+        self.assertEqual(rule.roi_back, (495, 590, 120, 100))
+        self.assertEqual(rule.mask.shape, rule.image.shape[:2])
+        self.assertEqual(set(np.unique(rule.mask)), {0, 255})
+        self.assertGreater(np.count_nonzero(rule.mask), 200)
+        self.assertLess(np.count_nonzero(rule.mask), rule.mask.size)
+        self.assertEqual(hashlib.sha256((ROOT / rule.file.removeprefix('./')).read_bytes()).hexdigest(),
+                         'b7cbc4f76c6eef129e955b9b3d89a376a721b9930e4295011e98363bdd80a5d9')
+
+    def test_new_theme_open_hits_at_guild_not_another_icon(self):
+        for pad in (0, 2):
+            with self.subTest(padding=pad):
+                set_roi_match_padding(pad)
+                rule = self.rule()
+                self.assertTrue(rule.match(self.frame('open')))
+                self.assertGreater(rule.last_score, 0.9)
+                self.assertEqual(rule.roi_front[:2], [556, 610])
+
+    def test_plain_matching_reproduces_reported_failure(self):
+        rule = self.rule('Template matching')
+        self.assertFalse(rule.match(self.frame('open')))
+        self.assertAlmostEqual(rule.last_score, 0.52842, places=4)
+
+    def test_closed_scroll_is_not_a_guild_entrance(self):
+        for pad in (0, 2):
+            with self.subTest(padding=pad):
+                set_roi_match_padding(pad)
+                rule = self.rule()
+                self.assertFalse(rule.match(self.frame('closed')))
+                self.assertLess(rule.last_score, rule.threshold)
+
+    def test_original_template_synthetic_control(self):
+        rule = self.rule()
+        frame = np.full((720, 1280, 3), 100, dtype=np.uint8)
+        frame[610:632, 556:606] = rule.image
+        self.assertTrue(rule.match(frame))
+        self.assertGreater(rule.last_score, 0.999)
+        self.assertEqual(rule.roi_front[:2], [556, 610])
+
+    def test_ignored_background_changes_do_not_change_result(self):
+        rng = np.random.default_rng(1862)
+        for variant in ('black', 'white', 'noise'):
+            with self.subTest(background=variant):
+                rule = self.rule()
+                icon = rule.image.copy()
+                background = (rng.integers(0, 256, icon.shape, dtype=np.uint8)
+                              if variant == 'noise' else
+                              np.full_like(icon, 0 if variant == 'black' else 255))
+                icon[rule.mask == 0] = background[rule.mask == 0]
+                frame = np.full((720, 1280, 3), 100, dtype=np.uint8)
+                frame[610:632, 556:606] = icon
+                self.assertTrue(rule.match(frame))
+                self.assertGreater(rule.last_score, 0.999)
+                self.assertEqual(rule.roi_front[:2], [556, 610])
+
+    def test_other_entrance_icons_do_not_match_guild(self):
+        for name in OTHER_ENTRANCES:
+            with self.subTest(icon=name):
+                rule = self.rule()
+                icon = deepcopy(getattr(GameUiAssets, 'I_MAIN_GOTO_' + name)).image
+                h, w = icon.shape[:2]
+                frame = np.full((720, 1280, 3), 100, dtype=np.uint8)
+                frame[610:610+h, 535:535+w] = icon
+                self.assertFalse(rule.match(frame))
+
+    def test_theme_switch_keeps_shared_mask_and_navigation_reference(self):
+        from tasks.GameUi.default_pages import page_main
+        ref = GameUiAssets.I_MAIN_GOTO_GUILD
+        transitions = [t for t in page_main.transitions if t.action is ref]
+        self.assertTrue(transitions)
+        for theme in (ThemeType.COSTUME_THEME_XINYU_MINGXIAO, ThemeType.COSTUME_THEME_DEFAULT):
+            apply_theme(theme)
+            self.assertIs(GameUiAssets.I_MAIN_GOTO_GUILD, ref)
+            self.assertEqual(ref.method, 'Masked template matching')
+            self.assertIsNotNone(ref.mask)
+            self.assertTrue(all(t.action is ref for t in transitions))
+            self.assertTrue(ref.match(self.frame('open')))
+
+
+if __name__ == '__main__':
+    unittest.main()