|
3 | 3 | from functools import cache |
4 | 4 |
|
5 | 5 | from mars_patcher.constants.game_data import character_widths |
| 6 | +from mars_patcher.convert_array import u16_to_u8 |
6 | 7 | from mars_patcher.mf.constants.game_data import file_screen_text_ptrs |
7 | | -from mars_patcher.mf.data import get_data_path |
8 | | -from mars_patcher.rom import Region, Rom |
| 8 | +from mars_patcher.mf.data import get_data_path as get_data_path_mf |
| 9 | +from mars_patcher.rom import Rom |
| 10 | +from mars_patcher.zm.data import get_data_path as get_data_path_zm |
9 | 11 |
|
10 | 12 | SPACE_CHAR = 0x40 |
11 | 13 | SPACE_TAG = 0x8000 |
@@ -56,13 +58,18 @@ class MessageType(Enum): |
56 | 58 |
|
57 | 59 |
|
58 | 60 | @cache |
59 | | -def get_char_map(region: Region) -> dict[str, int]: |
60 | | - path = get_data_path("char_map_mf.json") |
| 61 | +def get_char_map(rom: Rom) -> dict[str, int]: |
| 62 | + if rom.is_mf(): |
| 63 | + path = get_data_path_mf("char_map_mf.json") |
| 64 | + elif rom.is_zm(): |
| 65 | + path = get_data_path_zm("char_map_zm.json") |
| 66 | + else: |
| 67 | + raise ValueError(rom.game) |
61 | 68 | with open(path, encoding="utf-8") as f: |
62 | 69 | sections = json.load(f) |
63 | 70 | char_map: dict[str, int] = {} |
64 | 71 | for section in sections: |
65 | | - if region.name in section["regions"]: |
| 72 | + if rom.region.name in section["regions"]: |
66 | 73 | char_map.update(section["chars"]) |
67 | 74 | char_map["\n"] = NEWLINE |
68 | 75 | return char_map |
@@ -118,7 +125,7 @@ def encode_text( |
118 | 125 | max_width: int = MAX_LINE_WIDTH, |
119 | 126 | centered: bool = False, |
120 | 127 | ) -> bytes: |
121 | | - char_map = get_char_map(rom.region) |
| 128 | + char_map = get_char_map(rom) |
122 | 129 | char_widths_addr = character_widths(rom) |
123 | 130 | text: list[int] = [] |
124 | 131 | line_width = 0 |
@@ -237,11 +244,7 @@ def encode_text( |
237 | 244 |
|
238 | 245 | text.append(END) |
239 | 246 |
|
240 | | - text_bytes = bytearray() |
241 | | - for val in text: |
242 | | - text_bytes.append(val & 0xFF) |
243 | | - text_bytes.append(val >> 8) |
244 | | - return bytes(text_bytes) |
| 247 | + return u16_to_u8(text) |
245 | 248 |
|
246 | 249 |
|
247 | 250 | def write_seed_hash(rom: Rom, seed_hash: str) -> None: |
|
0 commit comments