costume_base.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. from copy import deepcopy
  5. from module.atom.image import RuleImage
  6. from module.atom.gif import RuleGif
  7. from module.logger import logger
  8. from module.server.i18n import I18n
  9. from tasks.Component.Costume.config import (
  10. MainType, CostumeConfig, RealmType,
  11. ThemeType, ShikigamiType, SignType,
  12. BattleType, BattleSceneType, CarpBannerType
  13. )
  14. from tasks.Component.Costume.assets import CostumeAssets
  15. from tasks.Component.CostumeTheme.theme import apply_theme
  16. from tasks.Component.CostumeBattle.assets import CostumeBattleAssets
  17. from tasks.Component.CostumeShikigami.assets import CostumeShikigamiAssets
  18. from tasks.Component.CostumeCarpBanner.assets import CostumeCarpBannerAssets
  19. from tasks.Component.CostumeBattleScene.assets import CostumeBattleSceneAssets
  20. # 庭院皮肤
  21. # 主界面皮肤(使用字典推导式动态生成)
  22. main_costume_model = {
  23. getattr(MainType, f"COSTUME_MAIN_{i}"): {
  24. 'I_CHECK_MAIN': f'I_CHECK_MAIN_{i}',
  25. 'I_MAIN_GOTO_EXPLORATION': f'I_MAIN_GOTO_EXPLORATION_{i}',
  26. 'I_MAIN_GOTO_SUMMON': f'I_MAIN_GOTO_SUMMON_{i}',
  27. 'I_MAIN_GOTO_TOWN': f'I_MAIN_GOTO_TOWN_{i}',
  28. 'I_PET_HOUSE': f'I_PET_HOUSE_{i}'
  29. } for i in range(1, 17)
  30. }
  31. # 玉岚狐庭(issue #1824)
  32. main_costume_model[getattr(MainType, "COSTUME_MAIN_17")] = {
  33. 'I_CHECK_MAIN': ['I_CHECK_MAIN_17_A', 'I_CHECK_MAIN_17_B', 'I_CHECK_MAIN_17_C'],
  34. 'I_MAIN_GOTO_EXPLORATION': ['I_MAIN_GOTO_EXPLORATION_17_A', 'I_MAIN_GOTO_EXPLORATION_17_B', 'I_MAIN_GOTO_EXPLORATION_17_C'],
  35. 'I_MAIN_GOTO_SUMMON': ['I_MAIN_GOTO_SUMMON_17_A', 'I_MAIN_GOTO_SUMMON_17_B', 'I_MAIN_GOTO_SUMMON_17_C'],
  36. 'I_MAIN_GOTO_TOWN': ['I_MAIN_GOTO_TOWN_17_A', 'I_MAIN_GOTO_TOWN_17_B', 'I_MAIN_GOTO_TOWN_17_C'],
  37. 'I_PET_HOUSE': ['I_PET_HOUSE_17_A', 'I_PET_HOUSE_17_B', 'I_PET_HOUSE_17_C'],
  38. }
  39. main_costume_model[getattr(MainType, "COSTUME_MAIN_13")] = {
  40. 'I_CHECK_MAIN': ['I_CHECK_MAIN_13',],
  41. 'I_MAIN_GOTO_EXPLORATION': ['I_MAIN_GOTO_EXPLORATION_13', ],
  42. 'I_MAIN_GOTO_SUMMON': ['I_MAIN_GOTO_SUMMON_13',],
  43. 'I_MAIN_GOTO_TOWN': ['I_MAIN_GOTO_TOWN_13',],
  44. 'I_PET_HOUSE': ['I_PET_HOUSE_13',],
  45. }
  46. # 鲤鱼旗皮肤
  47. carpbanner_costume_model = {
  48. getattr(CarpBannerType, f"COSTUME_CARPBANNER_{i}"): {
  49. 'I_SHI_CARD': f'I_SHI_CARD_{i}',
  50. 'I_SHI_DEFENSE': f'I_SHI_DEFENSE_{i}',
  51. 'I_SHI_GROWN': f'I_SHI_GROWN_{i}',
  52. } for i in range(1, 4)
  53. }
  54. # 战斗主题
  55. battle_theme_model = {
  56. getattr(BattleType, f"COSTUME_BATTLE_{i}"): {
  57. 'I_LOCAL': f'I_LOCAL_{i}',
  58. 'I_EXIT': f'I_EXIT_{i}',
  59. 'I_FRIENDS': f'I_FRIENDS_{i}',
  60. 'I_BATTLE_INFO': f'I_BATTLE_INFO_{i}',
  61. # 以下资源并非所有主题都需要修改,未采集的资源将被跳过
  62. 'I_WIN': f'I_WIN_{i}', # 已知:8,12,13,14
  63. 'I_DE_WIN': f'I_DE_WIN_{i}', # 已知:8,12,13,14
  64. 'I_FALSE': f'I_FALSE_{i}' # 已知:8,12,13,14
  65. } for i in range(1, 16)
  66. }
  67. # 战斗场景皮肤。图片资源待补充,先保留空映射。
  68. battle_scene_model = {
  69. BattleSceneType.COSTUME_BATTLE_SCENE_1: {
  70. 'I_REWARD': 'I_REWARD_1',
  71. },
  72. }
  73. # 幕间主题
  74. shikigami_costume_model = {
  75. getattr(ShikigamiType, f"COSTUME_SHIKIGAMI_{i}"): {
  76. # GameUi 进出式神录
  77. 'I_CHECK_RECORDS': f'I_CHECK_RECORDS_{i}',
  78. 'I_RECORD_SOUL_BACK': f'I_RECORD_SOUL_BACK_{i}',
  79. # SwitchSoul 相关
  80. 'I_SOUL_PRESET': f'I_SOUL_PRESET_{i}',
  81. 'I_SOU_CHECK_IN': f'I_SOU_CHECK_IN_{i}',
  82. 'I_SOU_TEAM_PRESENT': f'I_SOU_TEAM_PRESENT_{i}',
  83. 'I_SOU_CLICK_PRESENT': f'I_SOU_CLICK_PRESENT_{i}',
  84. # SwitchSoul 分组相关 (1-7组)
  85. **{f'I_SOU_CHECK_GROUP_{g}': f'I_SOU_CHECK_GROUP_{g}_{i}' for g in range(1, 8)},
  86. # SwitchSoul 队伍相关 (1-4队)
  87. **{f'I_SOU_SWITCH_{t}': f'I_SOU_SWITCH_{t}_{i}' for t in range(1, 5)},
  88. # SoulsTidy 相关
  89. 'I_ST_SOULS': f'I_ST_SOULS_{i}',
  90. 'I_ST_REPLACE': f'I_ST_REPLACE_{i}',
  91. }
  92. for i in range(1, 14) # 目前支持 COSTUME_SHIKIGAMI_1 到 COSTUME_SHIKIGAMI_13
  93. }
  94. class CostumeBase:
  95. def check_costume(self, config: CostumeConfig=None):
  96. if config is None:
  97. config: CostumeConfig = self.config.model.global_game.costume_config
  98. current_task = self.get_task_name()
  99. logger.info(f'Current task: {current_task}')
  100. self.check_costume_main(config.costume_main_type)
  101. self.check_costume_carpbanner(config.costume_carpbanner_type)
  102. self.check_costume_battle(config.costume_battle_type)
  103. self.check_costume_battle_scene(config.costume_battle_scene_type)
  104. self.check_costume_shikigami(config.costume_shikigami_type)
  105. self.check_costume_theme(config.costume_theme_type)
  106. def check_costume_theme(self, theme_type: ThemeType):
  107. apply_theme(theme_type)
  108. if theme_type != ThemeType.COSTUME_THEME_DEFAULT:
  109. # Supplementary labels are available before the UI rebuilds its translations.
  110. labels = I18n.load_additions().get('zh-CN', {})
  111. theme_name = labels.get(theme_type.value)
  112. if not theme_name:
  113. theme_name = I18n.trans_zh_cn(theme_type)
  114. logger.info(f'Switch menu theme {theme_type} ({theme_name})')
  115. def replace_img(self,
  116. asset_before: str,
  117. asset_after: RuleImage,
  118. rp_roi_back: bool = True):
  119. if not hasattr(self, asset_before):
  120. return
  121. # setattr(self, asset_before, asset_after)
  122. asset_before_object: RuleImage = getattr(self, asset_before)
  123. RuleGif.detach_from(asset_before_object)
  124. asset_before_object.roi_front = list(asset_after.roi_front)
  125. if rp_roi_back:
  126. asset_before_object.roi_back = asset_after.roi_back
  127. asset_before_object.threshold = asset_after.threshold
  128. asset_before_object.method = asset_after.method
  129. asset_before_object.file = asset_after.file
  130. # https://github.com/runhey/OnmyojiAutoScript/issues/1972
  131. asset_before_object.refresh()
  132. def check_costume_main(self, main_type: MainType):
  133. if main_type == MainType.COSTUME_MAIN:
  134. return
  135. logger.info(f'Switch main costume to {main_type} ({I18n.trans_zh_cn(main_type)})')
  136. costume_assets = CostumeAssets()
  137. for key, value in main_costume_model[main_type].items():
  138. if isinstance(value, list):
  139. if not hasattr(self, key):
  140. continue
  141. rules: list[RuleImage] = [getattr(costume_assets, item) for item in value]
  142. RuleGif.attach_to(getattr(self, key), rules)
  143. else:
  144. assert_value: RuleImage = getattr(costume_assets, value)
  145. self.replace_img(key, assert_value)
  146. def check_costume_carpbanner(self, carpbanner_type: CarpBannerType):
  147. if carpbanner_type == CarpBannerType.COSTUME_CARPBANNER_DEFAULT:
  148. return
  149. logger.info(f'Switch carp banner theme {carpbanner_type} (override realm assets) ({I18n.trans_zh_cn(carpbanner_type)})')
  150. carpbanner_assets = CostumeCarpBannerAssets()
  151. model = carpbanner_costume_model.get(carpbanner_type, {})
  152. for key, value in model.items():
  153. if not hasattr(carpbanner_assets, value):
  154. logger.warning(f'Carp banner asset {value} not found, skip')
  155. continue
  156. assert_value: RuleImage = getattr(carpbanner_assets, value)
  157. # 执行替换(覆盖结界皮肤的同名key)
  158. self.replace_img(key, assert_value)
  159. def check_costume_battle(self, battle_type: BattleType):
  160. if battle_type == BattleType.COSTUME_BATTLE_DEFAULT:
  161. return
  162. logger.info(f'Switch battle theme {battle_type} ({I18n.trans_zh_cn(battle_type)})')
  163. costume_battle_assets = CostumeBattleAssets()
  164. for key, value in battle_theme_model[battle_type].items():
  165. if not hasattr(costume_battle_assets, value):
  166. # 尚未采集完成的资产,跳过
  167. continue
  168. assert_value: RuleImage = getattr(costume_battle_assets, value)
  169. # 绿标的坐标点范围不变
  170. if key == 'I_LOCAL':
  171. self.replace_img(key, assert_value, rp_roi_back=False)
  172. else:
  173. self.replace_img(key, assert_value)
  174. def check_costume_battle_scene(self, scene_type: BattleSceneType):
  175. # Scene skins only apply to selected tasks; never mutate the shared battle assets.
  176. for key, original in self.__dict__.pop('_battle_scene_original_assets', {}).items():
  177. setattr(self, key, original)
  178. if scene_type == BattleSceneType.COSTUME_BATTLE_SCENE_DEFAULT:
  179. return
  180. logger.info(f'Switch battle scene skin {scene_type} ({I18n.trans_zh_cn(scene_type)})')
  181. current_task = self.get_task_name()
  182. allowed_tasks = {
  183. 'Orochi',
  184. }
  185. if current_task not in allowed_tasks:
  186. return
  187. costume_assets = CostumeBattleSceneAssets()
  188. for key, value in battle_scene_model.get(scene_type, {}).items():
  189. if not hasattr(self, key) or not hasattr(costume_assets, value):
  190. # 尚未采集完成的资产,跳过
  191. continue
  192. assert_value: RuleImage = getattr(costume_assets, value)
  193. original = getattr(self, key)
  194. self.__dict__.setdefault('_battle_scene_original_assets', {})[key] = original
  195. setattr(self, key, deepcopy(original))
  196. self.replace_img(key, assert_value)
  197. def check_costume_shikigami(self, shikigami_type: ShikigamiType):
  198. if shikigami_type == ShikigamiType.COSTUME_SHIKIGAMI_DEFAULT:
  199. return
  200. logger.info(f'Switch shikigami theme {shikigami_type} ({I18n.trans_zh_cn(shikigami_type)})')
  201. shikigami_assets = CostumeShikigamiAssets()
  202. model = shikigami_costume_model.get(shikigami_type, {})
  203. for key, value in model.items():
  204. if not hasattr(shikigami_assets, value):
  205. # 尚未采集完成的资产,跳过
  206. continue
  207. assert_value: RuleImage = getattr(shikigami_assets, value)
  208. # 一般不需要固定 back ROI,如确有需要可在此为特例设置 rp_roi_back=False
  209. self.replace_img(key, assert_value)
  210. if __name__ == '__main__':
  211. c = CostumeBase()
  212. c.check_costume_main(MainType.COSTUME_MAIN_13)