verify_runtime.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. """Run with the game's Python, from the repository root."""
  2. import ast
  3. import hashlib
  4. import importlib.util
  5. import json
  6. import os
  7. from pathlib import Path
  8. os.environ.setdefault('SDL_VIDEODRIVER', 'dummy')
  9. os.environ.setdefault('SDL_AUDIODRIVER', 'dummy')
  10. ROOT = Path(__file__).resolve().parents[2]
  11. os.chdir(str(ROOT))
  12. import sys
  13. sys.path.insert(0, str(ROOT))
  14. import pygame
  15. from PIL import Image
  16. HERE = Path(__file__).resolve().parent
  17. checks = []
  18. def verify_assets():
  19. manifest = json.loads((HERE / 'asset_manifest.json').read_text(encoding='utf-8'))
  20. expected_size = tuple(manifest['canvas'])
  21. baseline = manifest['foot_baseline']
  22. hero = json.loads((ROOT / 'game/hero/DeepseekBlueWhale.json').read_text(encoding='utf-8'))
  23. data = json.loads((ROOT / ('game/frame/character/' + hero['character'] + '.json')).read_text(encoding='utf-8'))
  24. paths = []
  25. for index, direction in enumerate(('R', 'U', 'L', 'D')):
  26. for state, count in [('STAND_' + direction, 1), (direction, 6)]:
  27. entry = data[state]
  28. assert entry['Len'] == count
  29. cloth = entry['Cloth']
  30. assert len(cloth['IMG']) == len(cloth['CX']) == len(cloth['CY']) == count
  31. hashes = []
  32. for filename in cloth['IMG']:
  33. path = ROOT / 'res/img/cloth' / filename
  34. assert path.exists(), path
  35. image = Image.open(path)
  36. assert image.size == expected_size and image.mode == 'RGBA'
  37. assert image.getchannel('A').getextrema() == (0, 255)
  38. assert image.getbbox()[3] <= baseline
  39. if manifest.get('version') == 2:
  40. assert image.getbbox()[3] - image.getbbox()[1] >= 87
  41. assert entry['Cx'] == -64 and entry['Cy'] == -93
  42. hashes.append(hashlib.sha256(image.tobytes()).hexdigest())
  43. paths.append(path)
  44. assert len(set(hashes)) == count, ('Repeated walking image', state)
  45. assert len(set(paths)) == 28
  46. if manifest.get('version') == 2:
  47. from PIL import ImageOps
  48. for motion, count in [('stand', 1), ('walk', 6)]:
  49. for i in range(count):
  50. right = Image.open(ROOT / f'res/img/cloth/deepseek_bluewhale_{motion}_0_{i}.png')
  51. left = Image.open(ROOT / f'res/img/cloth/deepseek_bluewhale_{motion}_2_{i}.png')
  52. assert left.tobytes() == ImageOps.mirror(right).tobytes()
  53. checks.append('V2 art >=87 px high, no clipping, exact side-view mirrors and updated anchor offsets')
  54. assert (ROOT / 'res/img/ui/game' / (hero['icon_img'] + '.png')).exists()
  55. menu = ast.parse((ROOT / 'test_menu.py').read_text(encoding='utf-8-sig'))
  56. name_func = next(node for node in menu.body if isinstance(node, ast.FunctionDef) and node.name == 'show_hero_name')
  57. namespace = {}
  58. exec(compile(ast.Module(body=[name_func], type_ignores=[]), 'menu-name-check', 'exec'), namespace)
  59. assert '蓝色大肥鱼' in namespace['show_hero_name']('DeepseekBlueWhale')
  60. checks.append('28 distinct transparent sprites, 4 standing + 24 walking, hero/menu/icon registrations')
  61. def verify_real_game():
  62. expected_size = tuple(json.loads((HERE / 'asset_manifest.json').read_text(encoding='utf-8'))['canvas'])
  63. from client_local.const import color as C
  64. from client_local.frame import character
  65. from client_local.file_loader import fileLoader
  66. # New unique asset names must work through the real archive-aware loader.
  67. pygame.init()
  68. pygame.display.set_mode((800, 600))
  69. data = fileLoader.load_json('game/frame/character/CharacterDeepseekBlueWhale.json')
  70. for color in (C.CHARACTER_RED, C.CHARACTER_SKYBLUE):
  71. loaded = character.get_character(data, color, {})
  72. assert len(loaded) == 9
  73. assert len(loaded['LOSE']['Cloth']) == 1
  74. for direction in ('R', 'U', 'L', 'D'):
  75. assert len(loaded[direction]['Cloth']) == 6
  76. assert len(loaded['STAND_' + direction]['Cloth']) == 1
  77. for frame in loaded[direction]['Cloth']:
  78. assert frame.image.get_size() == expected_size
  79. # No recolor mask: navy/cyan identity is unaffected by player color.
  80. original = pygame.image.load(str(ROOT / 'res/img/cloth/deepseek_bluewhale_walk_3_0.png')).convert_alpha()
  81. assert pygame.image.tobytes(original, 'RGBA') == pygame.image.tobytes(loaded['D']['Cloth'][0].image, 'RGBA')
  82. checks.append('Real frame loader, all 8 states, no missing-texture fallback, fixed palette across player colors')
  83. spec = importlib.util.spec_from_file_location('deepseek_trial', str(ROOT / '试玩蓝色大肥鱼.py'))
  84. module = importlib.util.module_from_spec(spec)
  85. spec.loader.exec_module(module)
  86. class SmokeControl(module.BlueWhaleControl):
  87. def update(self):
  88. assert self.me.hero_name == 'DeepseekBlueWhale'
  89. assert self.current_level is not None
  90. original = (ROOT / 'config.json').read_bytes()
  91. # Exercise the engine's actual frame switching and Player.draw.
  92. screen = pygame.Surface((800, 600))
  93. for direction in ('R', 'U', 'L', 'D'):
  94. self.me.orientation = direction
  95. for walking in (False, True):
  96. self.me.walking = walking
  97. self.me.character_frame_trigger = True
  98. for index in range(6):
  99. self.me.update_frame(pygame.time.get_ticks() + 1000 + index * 120)
  100. self.me.draw(screen)
  101. self.me.walking = False
  102. self.me.orientation = 'D'
  103. self.me.character_frame_trigger = True
  104. self.current_level.update(self.screen)
  105. self.me.rooted = 0
  106. positions = []
  107. for direction in ('R', 'U', 'L', 'D'):
  108. self.me.set_motion(direction)
  109. for index in range(8):
  110. pygame.time.delay(20)
  111. self.current_level.update(self.screen)
  112. positions.append((self.me.x_pos, self.me.y_pos))
  113. assert len(set(positions)) > 1, 'Player did not move in the live Level update'
  114. self.me.set_motion()
  115. self.current_level.update(self.screen)
  116. checks.append('Live Level.update movement with the new character')
  117. bombs_before = len(self.current_level.bomb_instances)
  118. remaining_before = self.me.remain_bombs
  119. self.me.set_bomb()
  120. assert len(self.current_level.bomb_instances) == bombs_before + 1
  121. assert self.me.remain_bombs == remaining_before - 1
  122. from client_local.sprite.player import PlayerState
  123. self.me.state = PlayerState.LOSE
  124. self.me.update_frame_dead(pygame.time.get_ticks() + 2000)
  125. self.me.draw(screen)
  126. self.me.state = PlayerState.NORMAL
  127. self.me.character_frame_trigger = True
  128. self.me.update_frame(pygame.time.get_ticks() + 2100)
  129. checks.append('Live bomb placement and a valid LOSE-state sprite fallback')
  130. pygame.image.save(pygame.display.get_surface(), str(HERE / 'in_game.png'))
  131. assert (ROOT / 'config.json').read_bytes() == original
  132. checks.append('Real Hero/Level initialization, standing/walking frame updates and draw in all four directions')
  133. SmokeControl()
  134. pygame.quit()
  135. if __name__ == '__main__':
  136. before = hashlib.sha256((ROOT / 'config.json').read_bytes()).hexdigest()
  137. verify_assets()
  138. verify_real_game()
  139. assert hashlib.sha256((ROOT / 'config.json').read_bytes()).hexdigest() == before
  140. (HERE / 'runtime_verification.json').write_text(json.dumps({'ok': True, 'checks': checks,
  141. 'config_sha256_unchanged': before}, indent=2, ensure_ascii=False), encoding='utf-8')
  142. print(json.dumps({'ok': True, 'checks': checks}, ensure_ascii=False))