some worlds: some typing in `LocalRom` (#3090)

* some worlds: some typing in `LocalRom`

### `read_bytes`

It's not safe to return `bytearray` when we think it's `bytes`
```python
a = rom.read_bytes(8, 3)
hash(a)  # This won't crash, right?
```

### `write_bytes`

`Iterable[SupportsIndex]` is what's required for `bytearray.__setitem__(slice, values)`
We need to add `__len__` for the `len(values)` in this function.

* remove `object` inheritance
This commit is contained in:
Doug Hoskisson 2024-05-17 12:41:57 -07:00 committed by GitHub
parent 9ae7083bfc
commit 280b67f996
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 11 additions and 11 deletions

View File

@ -18,7 +18,7 @@ import subprocess
import threading import threading
import concurrent.futures import concurrent.futures
import bsdiff4 import bsdiff4
from typing import Optional, List from typing import Collection, Optional, List, SupportsIndex
from BaseClasses import CollectionState, Region, Location, MultiWorld from BaseClasses import CollectionState, Region, Location, MultiWorld
from Utils import local_path, user_path, int16_as_bytes, int32_as_bytes, snes_to_pc, is_frozen, parse_yaml, read_snes_rom from Utils import local_path, user_path, int16_as_bytes, int32_as_bytes, snes_to_pc, is_frozen, parse_yaml, read_snes_rom
@ -52,7 +52,7 @@ except:
enemizer_logger = logging.getLogger("Enemizer") enemizer_logger = logging.getLogger("Enemizer")
class LocalRom(object): class LocalRom:
def __init__(self, file, patch=True, vanillaRom=None, name=None, hash=None): def __init__(self, file, patch=True, vanillaRom=None, name=None, hash=None):
self.name = name self.name = name
@ -71,13 +71,13 @@ class LocalRom(object):
def read_byte(self, address: int) -> int: def read_byte(self, address: int) -> int:
return self.buffer[address] return self.buffer[address]
def read_bytes(self, startaddress: int, length: int) -> bytes: def read_bytes(self, startaddress: int, length: int) -> bytearray:
return self.buffer[startaddress:startaddress + length] return self.buffer[startaddress:startaddress + length]
def write_byte(self, address: int, value: int): def write_byte(self, address: int, value: int):
self.buffer[address] = value self.buffer[address] = value
def write_bytes(self, startaddress: int, values): def write_bytes(self, startaddress: int, values: Collection[SupportsIndex]) -> None:
self.buffer[startaddress:startaddress + len(values)] = values self.buffer[startaddress:startaddress + len(values)] = values
def encrypt_range(self, startaddress: int, length: int, key: bytes): def encrypt_range(self, startaddress: int, length: int, key: bytes):

View File

@ -434,7 +434,7 @@ level_music_ids = [
0x21, 0x21,
] ]
class LocalRom(object): class LocalRom:
def __init__(self, file, patch=True, vanillaRom=None, name=None, hash=None): def __init__(self, file, patch=True, vanillaRom=None, name=None, hash=None):
self.name = name self.name = name
@ -457,7 +457,7 @@ class LocalRom(object):
def read_byte(self, address: int) -> int: def read_byte(self, address: int) -> int:
return self.buffer[address] return self.buffer[address]
def read_bytes(self, startaddress: int, length: int) -> bytes: def read_bytes(self, startaddress: int, length: int) -> bytearray:
return self.buffer[startaddress:startaddress + length] return self.buffer[startaddress:startaddress + length]
def write_byte(self, address: int, value: int): def write_byte(self, address: int, value: int):

View File

@ -83,7 +83,7 @@ class LocalRom:
def read_byte(self, address: int) -> int: def read_byte(self, address: int) -> int:
return self.buffer[address] return self.buffer[address]
def read_bytes(self, startaddress: int, length: int) -> bytes: def read_bytes(self, startaddress: int, length: int) -> bytearray:
return self.buffer[startaddress:startaddress + length] return self.buffer[startaddress:startaddress + length]
def write_byte(self, address: int, value: int): def write_byte(self, address: int, value: int):

View File

@ -3,7 +3,7 @@ import os
import Utils import Utils
from worlds.Files import APDeltaPatch from worlds.Files import APDeltaPatch
from settings import get_settings from settings import get_settings
from typing import TYPE_CHECKING from typing import TYPE_CHECKING, Collection, SupportsIndex
from .Options import YoshiColors, BowserDoor, PlayerGoal, MinigameChecks from .Options import YoshiColors, BowserDoor, PlayerGoal, MinigameChecks
@ -396,7 +396,7 @@ location_table = {
0x30510B: [0x14B2, 4] 0x30510B: [0x14B2, 4]
} }
class LocalRom(object): class LocalRom:
def __init__(self, file: str) -> None: def __init__(self, file: str) -> None:
self.name = None self.name = None
@ -413,13 +413,13 @@ class LocalRom(object):
def read_byte(self, address: int) -> int: def read_byte(self, address: int) -> int:
return self.buffer[address] return self.buffer[address]
def read_bytes(self, startaddress: int, length: int) -> bytes: def read_bytes(self, startaddress: int, length: int) -> bytearray:
return self.buffer[startaddress:startaddress + length] return self.buffer[startaddress:startaddress + length]
def write_byte(self, address: int, value: int) -> None: def write_byte(self, address: int, value: int) -> None:
self.buffer[address] = value self.buffer[address] = value
def write_bytes(self, startaddress: int, values: bytearray) -> None: def write_bytes(self, startaddress: int, values: Collection[SupportsIndex]) -> None:
self.buffer[startaddress:startaddress + len(values)] = values self.buffer[startaddress:startaddress + len(values)] = values
def write_to_file(self, file: str) -> None: def write_to_file(self, file: str) -> None: