-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f82ad2
commit 035c65e
Showing
4 changed files
with
245 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
from __future__ import annotations | ||
|
||
import collections | ||
import dataclasses | ||
import io | ||
import typing | ||
from pathlib import Path | ||
|
||
import construct | ||
|
||
from retro_data_structures.gc_disc import GcDisc | ||
|
||
|
||
@dataclasses.dataclass | ||
class FileEntry: | ||
offset: int | ||
size: int | ||
|
||
|
||
FileTree: typing.TypeAlias = dict[str, typing.Union[FileEntry, "FileTree"]] | ||
|
||
|
||
class GameDisc: | ||
_file_path: Path | ||
_raw: construct.Container | ||
_file_tree: FileTree | ||
|
||
def __init__(self, file_path: Path, raw: construct.Container, file_tree: FileTree): | ||
self._file_path = file_path | ||
self._raw = raw | ||
self._file_tree = file_tree | ||
|
||
@classmethod | ||
def parse(cls, file_path: Path) -> GameDisc: | ||
with file_path.open("rb") as source: | ||
data = GcDisc.parse_stream(source) | ||
|
||
file_tree: dict = {} | ||
current_dir = file_tree | ||
|
||
end_folder = collections.defaultdict(list) | ||
|
||
names_stream = io.BytesIO(data.fst.names) | ||
for i, file in enumerate(data.fst.file_entries): | ||
if i == 0: | ||
continue | ||
|
||
if i in end_folder: | ||
current_dir = end_folder.pop(i)[0] | ||
|
||
names_stream.seek(file.file_name) | ||
name = construct.CString("ascii").parse_stream(names_stream) | ||
if file.is_directory: | ||
new_dir = {} | ||
end_folder[file.param].append(current_dir) | ||
current_dir[name] = new_dir | ||
current_dir = new_dir | ||
else: | ||
current_dir[name] = FileEntry( | ||
offset=file.offset, | ||
size=file.param, | ||
) | ||
|
||
return GameDisc(file_path, data, file_tree) | ||
|
||
def _get_file_entry(self, name: str) -> FileEntry: | ||
file_entry = self._file_tree | ||
for segment in name.split("/"): | ||
file_entry = file_entry[segment] | ||
|
||
if isinstance(file_entry, FileEntry): | ||
return file_entry | ||
else: | ||
raise OSError(f"{name} is a directory") | ||
|
||
def files(self) -> list[str]: | ||
result = [] | ||
|
||
def recurse(parent: str, tree: FileTree) -> None: | ||
for key, item in tree.items(): | ||
name = f"{parent}/{key}" if parent else key | ||
|
||
if isinstance(item, FileEntry): | ||
result.append(name) | ||
else: | ||
recurse(name, item) | ||
|
||
recurse("", self._file_tree) | ||
return result | ||
|
||
def open_binary(self, name: str) -> typing.BinaryIO: | ||
entry = self._get_file_entry(name) | ||
file = self._file_path.open("rb") | ||
file.seek(entry.offset) | ||
return file | ||
|
||
def read_binary(self, name: str) -> bytes: | ||
entry = self._get_file_entry(name) | ||
with self._file_path.open("rb") as file: | ||
file.seek(entry.offset) | ||
return file.read(entry.size) | ||
|
||
|
||
def main(): | ||
# file = r"F:\Hauzer\GameCube\Metroid Prime 2 - Echoes (USA).iso" | ||
file = r"F:\Hauzer\GameCube\Kirby Air Ride.iso" | ||
file = r"F:\Hauzer\Wii\Metroid Prime 3 - Corruption [RM3E01]\Metroid Prime 3.iso" | ||
|
||
GameDisc.parse(Path(file)) | ||
|
||
# data = GcDisc.parse_file(file) | ||
# print(data) | ||
# | ||
# names_stream = io.BytesIO(data.fst.names) | ||
# for file in data.fst.file_entries: | ||
# names_stream.seek(file.file_name) | ||
# print(file.is_directory, file.param, construct.CString("ascii").parse_stream(names_stream)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from __future__ import annotations | ||
|
||
import construct | ||
|
||
# boot.bin | ||
DiscHeader = construct.Struct( | ||
game_code=construct.Bytes(4), | ||
maker_code=construct.Bytes(2), | ||
disc_id=construct.Int8ub, # for multi-disc games | ||
version=construct.Int8ub, | ||
audio_streaming=construct.Int8ub, | ||
stream_buffer_size=construct.Int8ub, | ||
_unused_a=construct.Const(b"\x00" * 14), | ||
_wii_magic_word=construct.Const(0, construct.Int32ub), # 0x5D1C9EA3 | ||
_gc_magic_word=construct.Const(0xC2339F3D, construct.Int32ub), | ||
game_name=construct.PaddedString(0x3E0, "utf8"), | ||
debug_monitor_offset=construct.Int32ub, | ||
debug_monitor_load_address=construct.Int32ub, | ||
_unused_b=construct.Const(b"\x00" * 24), | ||
main_executable_offset=construct.Int32ub, | ||
fst_offset=construct.Int32ub, | ||
fst_size=construct.Int32ub, | ||
fst_maximum_size=construct.Int32ub, | ||
user_position=construct.Int32ub, | ||
user_length=construct.Int32ub, | ||
unknown=construct.Int32ub, | ||
_unused_c=construct.Const(b"\x00" * 4), # construct.Bytes(0x4), | ||
) | ||
assert DiscHeader.sizeof() == 0x0440 | ||
|
||
DiscHeaderInformation = construct.Struct( | ||
debug_monitor_size=construct.Int32ub, | ||
simulated_memory_size=construct.Int32ub, | ||
argument_offset=construct.Int32ub, | ||
debug_flag=construct.Int32ub, | ||
track_address=construct.Int32ub, | ||
track_size=construct.Int32ub, | ||
country_code=construct.Int32ub, | ||
unknown=construct.Int32ub, | ||
padding=construct.Bytes(8160), | ||
) | ||
assert DiscHeaderInformation.sizeof() == 0x2000 | ||
|
||
AppLoader = construct.Struct( | ||
date=construct.Aligned(16, construct.Bytes(10)), | ||
entry_point=construct.Hex(construct.Int32ub), | ||
_size=construct.Rebuild(construct.Int32ub, construct.len_(construct.this.code)), | ||
trailer_size=construct.Int32ub, | ||
code=construct.Bytes(construct.this._size), | ||
) | ||
|
||
FileEntry = construct.Struct( | ||
is_directory=construct.Flag, | ||
file_name=construct.Int24ub, | ||
offset=construct.Int32ub, | ||
param=construct.Int32ub, | ||
) | ||
RootFileEntry = construct.Struct( | ||
is_directory=construct.Const(True, construct.Flag), | ||
file_name=construct.Const(0, construct.Int24ub), | ||
_offset=construct.Const(0, construct.Int32ub), | ||
num_entries=construct.Int32ub, | ||
) | ||
|
||
GcDisc = construct.Struct( | ||
header=DiscHeader, | ||
header_information=DiscHeaderInformation, | ||
app_loader=AppLoader, | ||
root_offset=construct.Tell, | ||
_fst_seek=construct.Seek(construct.this.header.fst_offset), | ||
fst=construct.FixedSized( | ||
construct.this.header.fst_size, | ||
construct.Struct( | ||
root_entry=construct.Peek(RootFileEntry), | ||
file_entries=FileEntry[construct.this.root_entry.num_entries], | ||
names=construct.GreedyBytes, | ||
), | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters