Files
assets/tools/branding/fancymenu.py
T

86 lines
4.0 KiB
Python

"""Writes the FancyMenu configuration of the Create RP title screen into a game's config folder.
python tools/branding/fancymenu.py <config dir> <1.21.1.jar> [pack version]
<config dir> is the client pack's config/ or a profile's config/. The version is read from pack.toml next to the
config dir when it is not given.
Layout: the key art as the background, our logo instead of Minecraft's, one wide button that joins the server,
single player and multiplayer, then language, options and an icon-only quit button in one row. Minecraft's own
lines in the bottom left corner are replaced with the pack version. The layout file itself is
branding/menu/title_screen_layout.txt; this script copies it with the pictures, draws the version label and
switches the window title, the window icon and the title-screen customisation on.
"""
import re
import shutil
import sys
import zipfile
from pathlib import Path
import numpy as np
from PIL import Image
ROOT = Path(__file__).resolve().parent.parent.parent
sys.path.insert(0, str(ROOT / 'tools' / 'signs'))
from render import Font # noqa: E402 (the pixel font reader made for the building signs)
MENU, ICONS = ROOT / 'branding' / 'menu', ROOT / 'branding' / 'icons'
WINDOW_TITLE = 'Create RP'
def option(text: str, key: str, value: str) -> str:
pattern = re.compile(r"^(\w:" + re.escape(key) + r" = ')[^']*(';)", re.M)
if not pattern.search(text):
raise SystemExit('FancyMenu option not found: ' + key)
return pattern.sub(lambda m: m.group(1) + value + m.group(2), text)
def label(font: Font, text: str) -> Image.Image:
"""A line of text exactly as the game draws it: white, with the dark shadow one pixel down and right."""
mask = font.line(text)
rows = np.nonzero(mask.any(axis=1))[0]
mask = mask[rows.min():rows.max() + 1]
h, w = mask.shape
px = np.zeros((h + 1, w + 1, 4), np.uint8)
px[1:, 1:][mask] = (63, 63, 63, 255)
px[:-1, :-1][mask] = (255, 255, 255, 255)
return Image.fromarray(px, 'RGBA')
def main() -> None:
config = Path(sys.argv[1])
fm = config / 'fancymenu'
if len(sys.argv) > 3:
version = sys.argv[3]
else:
version = re.search(r'^version = "([^"]+)"', (config.parent / 'pack.toml').read_text(encoding='utf-8'), re.M).group(1)
with zipfile.ZipFile(sys.argv[2]) as jar:
version_label = label(Font(jar), f'{WINDOW_TITLE} {version}')
(fm / 'assets').mkdir(parents=True, exist_ok=True)
(fm / 'customization').mkdir(parents=True, exist_ok=True)
shutil.copyfile(MENU / 'logo.png', fm / 'assets' / 'logo.png')
shutil.copyfile(MENU / 'background.png', fm / 'assets' / 'background.png')
shutil.copyfile(MENU / 'quit.png', fm / 'assets' / 'quit.png')
version_label.save(fm / 'assets' / 'version.png')
for icon in ('favicon-16x16.png', 'favicon-32x32.png'):
shutil.copyfile(ICONS / icon, fm / 'assets' / icon)
layout = (MENU / 'title_screen_layout.txt').read_text(encoding='utf-8')
layout = layout.replace('{version_width}', str(version_label.width)).replace('{version_height}', str(version_label.height))
(fm / 'customization' / 'title_screen_layout.txt').write_text(layout, encoding='utf-8', newline='\n')
(fm / 'customizablemenus.txt').write_text(
'type = customizablemenus\n\nnet.minecraft.client.gui.screens.TitleScreen {\n}\n\n', encoding='utf-8')
options = (fm / 'options.txt').read_text(encoding='utf-8')
options = option(options, 'custom_window_title', WINDOW_TITLE)
options = option(options, 'show_custom_window_icon', 'true')
options = option(options, 'custom_window_icon_16', '/config/fancymenu/assets/favicon-16x16.png')
options = option(options, 'custom_window_icon_32', '/config/fancymenu/assets/favicon-32x32.png')
options = option(options, 'custom_window_icon_macos', '')
options = option(options, 'global_background_panorama', '')
(fm / 'options.txt').write_text(options, encoding='utf-8')
print('title screen installed into', fm, '| version label:', version, version_label.size)
if __name__ == '__main__':
main()