-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinsert_pak.py
More file actions
36 lines (30 loc) · 1.04 KB
/
insert_pak.py
File metadata and controls
36 lines (30 loc) · 1.04 KB
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
# Insert new .png image into existing .pak file found in data/ui
#
# Usage:
# python insert_pak.py <pak_file_ref> <pak_file> <png_file>
#
# Example:
# python insert_pak.py folder_name.pak folder_name_out.pak folder_name.png
# Creates folder_name_out.pak by taking folder_name.pak and inserting
# folder_name.png into it.
from argparse import ArgumentParser
from pathlib import Path
from PIL import Image
import struct
parser = ArgumentParser()
parser.add_argument('pak_file_ref', type=Path)
parser.add_argument('pak_file', type=Path)
parser.add_argument('png_file', type=Path)
args = parser.parse_args()
pak_file_ref: Path = args.pak_file_ref
pak_file: Path = args.pak_file
png_file: Path = args.png_file
bmp = Image.open(png_file).convert('RGBA')
# Read data from old .pak file
with open(pak_file_ref, 'rb') as pak_f:
tex_addr, = struct.unpack_from('<I', pak_f.peek(4))
data = pak_f.read(tex_addr)
# Build new .pak using old data and new image
with open(pak_file, 'wb') as pak_f:
pak_f.write(data)
pak_f.write(bmp.tobytes('raw', 'BGRA'))