diff --git a/N64Patch.py b/N64Patch.py index 41622ca7d..6d4953cf4 100644 --- a/N64Patch.py +++ b/N64Patch.py @@ -171,12 +171,11 @@ def create_patch_file(rom: Rom, file: str, xor_range: tuple[int, int] = (0x00B8A xor_address = write_block(rom, xor_address, xor_range, block_start, data, patch_data) # compress the patch file - patch_data = bytes(patch_data.buffer) - patch_data = zlib.compress(patch_data) + compressed_patch_data = zlib.compress(patch_data.buffer) # save the patch file with open(file, 'wb') as outfile: - outfile.write(patch_data) + outfile.write(compressed_patch_data) # This will apply a patch file to a source rom to generate a patched rom. @@ -188,13 +187,13 @@ def apply_patch_file(rom: Rom, settings: Settings, sub_file: Optional[str] = Non with zipfile.ZipFile(file, 'r') as patch_archive: try: with patch_archive.open(sub_file, 'r') as stream: - patch_data = stream.read() + compressed_patch_data = stream.read() except KeyError as ex: raise FileNotFoundError('Patch file missing from archive. Invalid Player ID.') else: with open(file, 'rb') as stream: - patch_data = stream.read() - patch_data = BigStream(bytearray(zlib.decompress(patch_data))) + compressed_patch_data = stream.read() + patch_data = BigStream(bytearray(zlib.decompress(compressed_patch_data))) # make sure the header is correct if patch_data.read_bytes(length=4) != b'ZPFv': diff --git a/OcarinaSongs.py b/OcarinaSongs.py index c8b6c0d5f..cf8a52552 100644 --- a/OcarinaSongs.py +++ b/OcarinaSongs.py @@ -18,6 +18,8 @@ ActivationTransform: TypeAlias = "Callable[[list[int]], list[int]]" PlaybackTransform: TypeAlias = "Callable[[list[dict[str, int]]], list[dict[str, int]]]" +P = TypeVar('P', list[int], list[dict[str, int]]) +T = TypeVar('T', ActivationTransform, PlaybackTransform) PLAYBACK_START: int = 0xB781DC PLAYBACK_LENGTH: int = 0xA0 @@ -136,7 +138,7 @@ def copy_playback_info(playback: list[dict[str, int]], piece: list[int]): return [{'note': n, 'volume': p['volume'], 'duration': p['duration']} for (p, n) in zip(playback, piece)] -def identity(x: list[int | dict[str, int]]) -> list[int | dict[str, int]]: +def identity(x: P) -> P: return x @@ -148,7 +150,7 @@ def invert_piece(piece: list[int]) -> list[int]: return [4 - note for note in piece] -def reverse_piece(piece: list[int | dict[str, int]]) -> list[int | dict[str, int]]: +def reverse_piece(piece: P) -> P: return piece[::-1] @@ -162,7 +164,6 @@ def transpose(piece: list[int]) -> list[int]: return transpose -T = TypeVar('T', ActivationTransform, PlaybackTransform) def compose(f: T, g: T) -> T: return lambda x: f(g(x)) @@ -188,6 +189,7 @@ def __init__(self, rand_song: bool = True, piece_size: int = 3, extra_position: self.activation_data: list[int] = [] self.playback_data: list[int] = [] self.total_duration: int = 0 + self.difficulty: int = -1 if activation: self.length = len(activation) @@ -341,8 +343,8 @@ def get_random_song() -> Song: rand_song = random.choices([True, False], [1, 9])[0] piece_size = random.choices([3, 4], [5, 2])[0] extra_position = random.choices(['none', 'start', 'middle', 'end'], [12, 1, 1, 1])[0] - activation_transform = identity - playback_transform = identity + activation_transform: ActivationTransform = identity + playback_transform: PlaybackTransform = identity weight_damage = 0 should_transpose = random.choices([True, False], [1, 4])[0] starting_range = range(0, 5) @@ -403,7 +405,7 @@ def generate_song_list(world: World, frog: bool, warp: bool) -> dict[str, Song]: for name2, song2 in fixed_songs.items(): if name1 != name2 and subsong(song1, song2): raise ValueError(f'{name2} is unplayable because it contains {name1}') - random_songs = [] + random_songs: list[Song] = [] for _ in range(12 - len(fixed_songs)): for _ in range(1000): diff --git a/Patches.py b/Patches.py index 89bbf0a92..ebc875b44 100644 --- a/Patches.py +++ b/Patches.py @@ -95,8 +95,8 @@ def patch_rom(spoiler: Spoiler, world: World, rom: Rom) -> Rom: extended_objects_start = start_address = rom.dma.free_space() for name, zobj_path, object_id in zobj_imports: - with open(zobj_path, 'rb') as stream: - obj_data = stream.read() + with open(zobj_path, 'rb') as zobj_stream: + obj_data = zobj_stream.read() rom.write_bytes(start_address, obj_data) # Add it to the extended object table end_address = ((start_address + len(obj_data) + 0x0F) >> 4) << 4