vc-online/tools/online-icons.py

58 lines
2.5 KiB
Python

#!/usr/bin/env python3
# Puts the mobile game's pictures the page uses into <game dir>/online/icons, from the output of mobile-icons.py.
# Button pictures keep their name, weapons become w<type>.png, and the two buttons the game has no picture for
# are made from its own parts. Usage: python tools/online-icons.py <mobile icons dir> <game dir>
import json, os, re, struct, sys
import numpy as np
from PIL import Image
src, game = sys.argv[1], sys.argv[2]
out = f'{game}/online/icons'
os.makedirs(out, exist_ok=True)
menu = json.load(open(os.path.join(os.path.dirname(__file__), '../gamefiles/online/menu.json'), encoding='utf-8'))
hud = lambda name: Image.open(f'{src}/hud_{name}.png').convert('RGBA')
names = {b['icon'] for b in menu['touch']['buttons'] if 'icon' in b and 'parts' not in b} | {'circle', 'analognub'}
for name in names:
hud(name).save(f'{out}/{name}.png')
for w in menu['weapons']:
hud(w['icon']).save(f'{out}/w{w["type"]}.png')
for b in menu['touch']['buttons']:
if 'parts' not in b:
continue
base = hud(b['parts'][0])
mark = hud(b['parts'][1])
mark.thumbnail((base.width * 11 // 20, base.height * 11 // 20))
base.alpha_composite(mark, ((base.width - mark.width) // 2, (base.height - mark.height) // 2))
base.save(f'{out}/{b["icon"]}.png')
# The station logos are in the PC game's menu textures: DXT3, 128 x 128.
def c565(c):
return np.array([((c >> 11) & 31) * 255 // 31, ((c >> 5) & 63) * 255 // 63, (c & 31) * 255 // 31])
def dxt3(d, w, h):
img = np.zeros((h, w, 4), np.uint8)
at = 0
for y in range(0, h, 4):
for x in range(0, w, 4):
alpha = int.from_bytes(d[at:at + 8], 'little')
c0, c1, bits = struct.unpack('<HHI', d[at + 8:at + 16])
p = [c565(c0), c565(c1)]
p += [(2 * p[0] + p[1]) // 3, (p[0] + 2 * p[1]) // 3]
for i in range(16):
img[y + i // 4, x + i % 4, :3] = p[(bits >> (2 * i)) & 3]
img[y + i // 4, x + i % 4, 3] = ((alpha >> (4 * i)) & 15) * 17
at += 16
return Image.fromarray(img, 'RGBA')
txd = open(f'{game}/models/fronten2.txd', 'rb').read()
stations = {r['icon']: r['station'] for r in menu['radio'] if 'icon' in r}
for m in re.finditer(rb'\x15\x00\x00\x00', txd):
s = m.start() + 24
name = txd[s + 8:s + 40].split(b'\0')[0].decode('latin1')
if name in stations:
dxt3(txd[s + 92:s + 92 + 128 * 128], 128, 128).save(f'{out}/r{stations[name]}.png')
print(len(os.listdir(out)), 'pictures')