Files
LihonT/scripts/make_ltp.py
cheb0chik 1d5a9a1fad LihonT 2 Luna
Ghost mode for stories and chats, several photos in one comment, .ltp plugins (rights, defender, rollback, downloads
only with consent), custom chat bubbles with a text colour of your choice, the lihont-ltp SDK and the plugin docs,
streak flames as widgets with a keeper, avatar shapes, Material 3 surfaces.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-01 18:19:27 +03:00

92 lines
4.1 KiB
Python

#!/usr/bin/env python3
"""Packs plugin folders (plugins/<id>/plugin.json, an optional icon.png, optional files in assets/) into .ltp files.
An .ltp is a plain zip with plugin.json, optionally icon.png, and optionally files of assets/ (pictures and sounds
the plugin's rules point at); the runtime refuses anything else. This script does the same light checks the runtime
does (so a mistake shows up here and not on the phone) and writes a file named after the plugin id.
python scripts/make_ltp.py packs every folder of plugins/ into ComplectAPK/plugins/
python scripts/make_ltp.py plugins/my.plugin packs one folder
python scripts/make_ltp.py -o some/folder ... writes elsewhere
"""
from __future__ import annotations
import argparse
import json
import re
import sys
import zipfile
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
ID = re.compile(r"[a-z0-9][a-z0-9._-]{2,63}")
PNG = b"\x89PNG\r\n\x1a\n"
MAX_FILE = 1024 * 1024
MAX_ICON = 64 * 1024
MAX_ASSET = 400 * 1024
MAX_ASSETS = 8
ASSET = re.compile(r"[a-z0-9][a-z0-9._-]{0,39}")
MAGIC = {
".png": lambda d: d.startswith(PNG),
".gif": lambda d: d.startswith(b"GIF8"),
".webp": lambda d: d[:4] == b"RIFF" and d[8:12] == b"WEBP",
".ogg": lambda d: d.startswith(b"OggS"),
".mp3": lambda d: d.startswith(b"ID3") or (len(d) > 2 and d[0] == 0xFF and d[1] & 0xE0 == 0xE0),
}
def pack(folder: Path, out_dir: Path) -> Path:
manifest = folder / "plugin.json"
if not manifest.is_file():
raise SystemExit("%s: no plugin.json" % folder)
raw = manifest.read_bytes()
try:
data = json.loads(raw.decode("utf-8"))
except (UnicodeDecodeError, ValueError) as error:
raise SystemExit("%s: plugin.json is not valid UTF-8 JSON: %s" % (folder, error))
plugin_id = data.get("id", "")
if not isinstance(plugin_id, str) or not ID.fullmatch(plugin_id):
raise SystemExit("%s: bad id %r" % (folder, plugin_id))
if folder.name != plugin_id:
raise SystemExit("%s: the folder name must be the plugin id (%s)" % (folder, plugin_id))
icon = folder / "icon.png"
out = out_dir / (plugin_id + ".ltp")
out_dir.mkdir(parents=True, exist_ok=True)
assets = sorted(p for p in (folder / "assets").glob("*") if p.is_file()) if (folder / "assets").is_dir() else []
if len(assets) > MAX_ASSETS:
raise SystemExit("%s: at most %d files in assets/" % (folder, MAX_ASSETS))
with zipfile.ZipFile(out, "w", zipfile.ZIP_DEFLATED) as archive:
archive.writestr("plugin.json", raw)
for asset in assets:
body = asset.read_bytes()
check = MAGIC.get(asset.suffix.lower())
if not ASSET.fullmatch(asset.name) or check is None or not check(body) or len(body) > MAX_ASSET:
raise SystemExit("%s: assets/%s is not an allowed file (png, gif, webp, ogg, mp3; up to 400 KB; "
"lower-case name)" % (folder, asset.name))
archive.writestr("assets/" + asset.name, body)
if icon.is_file():
picture = icon.read_bytes()
if not picture.startswith(PNG) or len(picture) > MAX_ICON:
raise SystemExit("%s: icon.png must be a PNG of at most 64 KB" % folder)
archive.writestr("icon.png", picture)
if out.stat().st_size > MAX_FILE:
raise SystemExit("%s: the file is over 1 MB" % out)
return out
def main(argv) -> int:
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("folders", nargs="*", help="plugin folders (default: every folder of plugins/)")
parser.add_argument("-o", "--out", default=str(ROOT / "ComplectAPK" / "plugins"))
args = parser.parse_args(argv)
folders = [Path(f) for f in args.folders] or sorted(p for p in (ROOT / "plugins").iterdir() if p.is_dir())
for folder in folders:
out = pack(folder, Path(args.out))
print("%s %d bytes" % (out, out.stat().st_size))
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))