Skip to content

Commit

Permalink
More mypy error fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fenhl committed Mar 27, 2024
1 parent db6d4ac commit 2127e85
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
11 changes: 5 additions & 6 deletions N64Patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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':
Expand Down
14 changes: 8 additions & 6 deletions OcarinaSongs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand All @@ -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]


Expand All @@ -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))

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions Patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2127e85

Please sign in to comment.