summaryrefslogtreecommitdiff
path: root/tools/version_config.py
blob: 3516073f0d4c0d7862633306218a3b064716ea0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Version-specific configuration for setup and assets extraction

# SPDX-FileCopyrightText: © 2024 ZeldaRET
# SPDX-License-Identifier: CC0-1.0

from __future__ import annotations

from collections import OrderedDict
import csv
import dataclasses
from pathlib import Path
from typing import Optional

import yaml

PROJECT_ROOT = Path(__file__).parent.parent


@dataclasses.dataclass
class VersionConfig:
    # Version name
    version: str
    checksums: list[str]
    # ROM offset to start of DMA table
    dmadata_start: int
    # "NTSC" (JP/EN), "PAL" (EN/DE/FR), or "CN" (JP/CN)
    text_lang: str
    # DMA segment information, in ROM order
    dmadata_segments: OrderedDict[str, SegmentInfo]
    # ROM pieces that are copied directly into the build with .incbin
    incbins: list[IncbinConfig]
    # Addresses of important variables needed for asset extraction
    variables: dict[str, int]
    # Assets to extract
    assets: list[AssetConfig]


@dataclasses.dataclass
class SegmentInfo:
    name: str
    vram: Optional[int]


@dataclasses.dataclass
class IncbinConfig:
    name: str
    segment: str
    vram: int
    size: int


@dataclasses.dataclass
class AssetConfig:
    name: str
    xml_path: Path
    start_offset: Optional[int]
    end_offset: Optional[int]


def load_dmadata_segments(version: str) -> OrderedDict[str, SegmentInfo]:
    segments = OrderedDict()
    with open(PROJECT_ROOT / f"baseroms/{version}/segments.csv", "r") as f:
        reader = csv.DictReader(f)
        for row in reader:
            name = row["Name"]
            vram = int(row["VRAM start"], 16) if row["VRAM start"] else None
            segments[name] = SegmentInfo(name, vram)
    return segments


def load_version_config(version: str) -> VersionConfig:
    with open(PROJECT_ROOT / f"baseroms/{version}/config.yml", "r") as f:
        config = yaml.load(f, Loader=yaml.Loader)

    incbins = []
    for incbin in config["incbins"]:
        incbins.append(
            IncbinConfig(
                incbin["name"], incbin["segment"], incbin["vram"], incbin["size"]
            )
        )

    assets = []
    for asset in config["assets"]:
        name = asset["name"]
        xml_path = Path(asset["xml_path"])
        start_offset = asset.get("start_offset", None)
        end_offset = asset.get("end_offset", None)
        assets.append(AssetConfig(name, xml_path, start_offset, end_offset))

    return VersionConfig(
        version=version,
        checksums=config.get("checksums", ["checksum"]),
        dmadata_start=config["dmadata_start"],
        text_lang=config["text_lang"],
        dmadata_segments=load_dmadata_segments(version),
        incbins=incbins,
        variables=config["variables"],
        assets=assets,
    )