1
0
Fork 0
mirror of https://github.com/zeldaret/oot.git synced 2024-11-10 19:20:13 +00:00
oot/tools/dmadata.py
cadmic d674dad3da
Match compression for gc-eu-mq (#1704)
* Improve compression

* Format

* Typo

* Use Python assignment expression in tools/dmadata.py

Co-authored-by: Dragorn421 <Dragorn421@users.noreply.github.com>

* Review decompress_baserom.py

* Replace DEFINE_DEBUG_SCENE with CPP defines

* Pass is_zlib_compressed instead of version

* Reword NOLOAD comment in write_compress_ranges

* Remove redundant comment

---------

Co-authored-by: Dragorn421 <Dragorn421@users.noreply.github.com>
2024-02-03 23:59:19 +01:00

75 lines
1.6 KiB
Python

# SPDX-FileCopyrightText: © 2024 ZeldaRET
# SPDX-License-Identifier: CC0-1.0
from __future__ import annotations
import dataclasses
import struct
STRUCT_IIII = struct.Struct(">IIII")
@dataclasses.dataclass
class DmaEntry:
"""
A Python counterpart to the dmadata entry struct:
```c
typedef struct {
/* 0x00 */ uintptr_t vromStart;
/* 0x04 */ uintptr_t vromEnd;
/* 0x08 */ uintptr_t romStart;
/* 0x0C */ uintptr_t romEnd;
} DmaEntry;
```
"""
vrom_start: int
vrom_end: int
rom_start: int
rom_end: int
def __repr__(self):
return (
"DmaEntry("
f"vrom_start=0x{self.vrom_start:08X}, "
f"vrom_end=0x{self.vrom_end:08X}, "
f"rom_start=0x{self.rom_start:08X}, "
f"rom_end=0x{self.rom_end:08X}"
")"
)
SIZE_BYTES = STRUCT_IIII.size
def to_bin(self, data: memoryview):
STRUCT_IIII.pack_into(
data,
0,
self.vrom_start,
self.vrom_end,
self.rom_start,
self.rom_end,
)
@staticmethod
def from_bin(data: memoryview):
return DmaEntry(*STRUCT_IIII.unpack_from(data))
def is_compressed(self) -> bool:
return self.rom_end != 0
DMA_ENTRY_END = DmaEntry(0, 0, 0, 0)
def read_dmadata(rom_data: memoryview, start_offset: int) -> list[DmaEntry]:
result = []
offset = start_offset
while (
entry := DmaEntry.from_bin(rom_data[offset : offset + DmaEntry.SIZE_BYTES])
) != DMA_ENTRY_END:
result.append(entry)
offset += DmaEntry.SIZE_BYTES
return result