Example #1
0
from skytemple_files.common.types.file_types import FileType
from skytemple_files.common.util import get_files_from_rom_with_extension
from skytemple_files.compression_container.common_at.handler import CommonAtHandler
from skytemple_files.graphics.bgp.handler import BgpHandler
from skytemple_files.graphics.bgp.model import BGP_RES_WIDTH_IN_TILES

os.makedirs(os.path.join(os.path.dirname(__file__), 'dbg_output'), exist_ok=True)

base_dir = os.path.join(os.path.dirname(__file__), '..', '..', '..', '..', '..')

rom = NintendoDSRom.fromFile(os.path.join(base_dir, 'skyworkcopy.nds'))

for filename in get_files_from_rom_with_extension(rom, 'bgp'):
    print("Processing " + filename)

    bin_before = rom.getFileByName(filename)
    bgp = BgpHandler.deserialize(bin_before)

    bin_after = BgpHandler.serialize(bgp)

    bin_before_unpacked = CommonAtHandler.deserialize(bin_before).decompress()
    bin_after_unpacked = CommonAtHandler.deserialize(bin_after).decompress()

    assert bin_before_unpacked == bin_after_unpacked

    rom.setFileByName(filename, bin_after)


rom.saveToFile(os.path.join(base_dir, 'skyworkcopy_edit.nds'))
os.makedirs(os.path.join(os.path.dirname(__file__), 'dbg_output'), exist_ok=True)

base_dir = os.path.join(os.path.dirname(__file__), '..', '..', '..', '..', '..')

rom = NintendoDSRom.fromFile(os.path.join(base_dir, 'skyworkcopy.nds'))

for filename in get_files_from_rom_with_extension(rom, 'bgp'):
    print("Processing " + filename)
    filename_h = os.path.join(os.path.dirname(__file__), 'dbg_output', filename.replace('/', '_'))

    bin = rom.getFileByName(filename)
    with open(filename_h, 'wb') as f:
        tb = time()
        d = FileType.AT4PX.deserialize(bin).decompress()
        print(f"Decrompressing this takes {time() - tb}s.")
        f.write(d)

    bgp = BgpHandler.deserialize(bin)

    # Save as one big image
    bgp.to_pil().save(filename_h + '.png')

    # Save as tiles
    tile_dir = filename_h + '.tiles'
    os.makedirs(tile_dir, exist_ok=True)
    for i, img in enumerate(bgp.to_pil_tiled()):
        tile_x = i % BGP_RES_WIDTH_IN_TILES
        tile_y = math.floor(i / BGP_RES_WIDTH_IN_TILES)
        img.save(os.path.join(tile_dir, f'{tile_x}_{tile_y}.png'))