2024-01-02 18:16:18 +01:00
|
|
|
from splat.segtypes.n64.segment import N64Segment
|
|
|
|
from splat.segtypes.n64.palette import iter_in_groups
|
|
|
|
from splat.util.color import unpack_color
|
|
|
|
from splat.util import options
|
2023-06-26 12:27:37 +02:00
|
|
|
import png # type: ignore
|
|
|
|
|
2021-07-16 11:28:37 +02:00
|
|
|
|
|
|
|
def parse_palette(data):
|
|
|
|
palette = []
|
|
|
|
|
|
|
|
for a, b in iter_in_groups(data, 2):
|
|
|
|
palette.append(unpack_color([a, b]))
|
|
|
|
|
|
|
|
return palette
|
|
|
|
|
2023-06-26 12:27:37 +02:00
|
|
|
|
2021-07-16 11:28:37 +02:00
|
|
|
class N64SegPm_charset_palettes(N64Segment):
|
|
|
|
require_unique_name = False
|
|
|
|
|
|
|
|
def scan(self, rom_bytes):
|
2023-06-26 12:27:37 +02:00
|
|
|
data = rom_bytes[self.rom_start : self.rom_end]
|
2021-07-16 11:28:37 +02:00
|
|
|
|
2024-01-16 06:13:54 +01:00
|
|
|
# pm_charset siblings
|
|
|
|
self.siblings = []
|
|
|
|
for s in self.parent.subsegments:
|
|
|
|
if s.type == "pm_charset":
|
|
|
|
if len(s.yaml) > 6:
|
|
|
|
charset_pal = s.yaml[6]
|
|
|
|
else:
|
|
|
|
charset_pal = s.name
|
|
|
|
if self.name == charset_pal:
|
|
|
|
self.siblings.append(s)
|
2021-07-16 11:28:37 +02:00
|
|
|
|
|
|
|
self.palettes = []
|
|
|
|
|
|
|
|
for i in range(0, self.size, 0x10):
|
|
|
|
palette = parse_palette(data[i : i + 0x10])
|
|
|
|
self.palettes.append(palette)
|
|
|
|
|
|
|
|
def split(self, rom_bytes):
|
2022-09-28 22:52:12 +02:00
|
|
|
fs_dir = options.opts.asset_path / self.dir / self.name / "palette"
|
2021-07-16 11:28:37 +02:00
|
|
|
fs_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
for i, palette in enumerate(self.palettes):
|
2024-01-16 06:13:54 +01:00
|
|
|
raster = self.siblings[0].rasters[0]
|
2021-07-16 11:28:37 +02:00
|
|
|
|
2024-01-16 06:13:54 +01:00
|
|
|
w = png.Writer(self.siblings[0].width, self.siblings[0].height, palette=palette)
|
2021-07-16 11:28:37 +02:00
|
|
|
with open(fs_dir / f"{i:02X}.png", "wb") as f:
|
|
|
|
w.write_array(f, raster)
|
|
|
|
|
|
|
|
def get_linker_entries(self):
|
2024-01-02 18:16:18 +01:00
|
|
|
from splat.segtypes.linker_entry import LinkerEntry
|
2021-07-16 11:28:37 +02:00
|
|
|
|
2022-09-28 22:52:12 +02:00
|
|
|
fs_dir = options.opts.asset_path / self.dir / self.name / "palette"
|
2021-07-16 11:28:37 +02:00
|
|
|
|
2023-06-26 12:27:37 +02:00
|
|
|
return [
|
|
|
|
LinkerEntry(
|
|
|
|
self,
|
|
|
|
[fs_dir / f"{i:02X}.png" for i in range(self.yaml[3])],
|
|
|
|
fs_dir.with_suffix(".dat"),
|
|
|
|
".data",
|
2023-11-10 03:48:23 +01:00
|
|
|
".data",
|
2023-06-26 12:27:37 +02:00
|
|
|
),
|
2021-07-16 11:28:37 +02:00
|
|
|
]
|