looper/assets/update_assets.py

112 lines
4.5 KiB
Python
Raw Normal View History

#!/usr/bin/python3
import os
import sys
import subprocess
import os.path as path
import shutil
2024-04-09 10:15:05 -07:00
from shutil import which
from glob import glob
olddir = os.curdir
print("Entering assets directory to begin asset conversion...")
os.chdir(path.realpath(path.dirname(__file__)))
ASSETS = []
2024-04-09 10:15:05 -07:00
WARNINGS: dict[str, bool] = {}
def warn(id: str, message: str, WARNINGS = WARNINGS) -> None:
warned_already: bool = False
try:
warned_already = WARNINGS[id]
except KeyError:
pass
if not warned_already:
print("WARNING: %s" % message)
def run_btcc(args: list[str], outpath: str):
with open(outpath + ".h", "wt+") as f:
actual_args = ["./btcc"]
for arg in args:
actual_args += [arg]
actual_args += [outpath]
subprocess.call(actual_args, stdout=f)
2024-09-28 10:31:06 -07:00
def add_basic(input: str, output: str):
global ASSETS
ASSETS += [output + ".h"]
run_btcc(["-nocompress", input], output)
2024-09-28 10:31:06 -07:00
def add_base85(input: str, output: str):
global ASSETS
ASSETS += [output + ".h"]
run_btcc(["-base85", input], output)
def add_font(input: str, output: str|None = None):
if output == None:
FONT=path.basename(input).removesuffix(".ttf").lower().replace('-', '_')
else:
FONT=output
print("Adding font '%s' from file '%s'" % (FONT, path.abspath(input)))
add_base85(input, FONT)
def add_graphic(input: str):
GRAPHIC=path.basename(input).removesuffix(".png").lower().replace('-', '_')
print("Adding graphic '%s' from file '%s'" % (GRAPHIC, input))
add_base85(input, GRAPHIC)
def add_license(input: str, output: str):
LICENSE = output + "_license"
print("Adding license '%s' (C identifier: '%s') from file '%s'" % (output, LICENSE, input))
add_basic(input, LICENSE)
def compile_program(file: str, output: str):
print("Compiling file '%s' to '%s'" % (path.abspath(file), path.abspath(output)))
2024-10-21 13:30:18 -07:00
subprocess.call(["c++", file, "-o", output])
2024-03-26 18:39:02 -07:00
def add_css(file: str, output: str):
CSS_FILE = output + ".h"
print("Adding CSS file '%s' (C identifier '%s') from file '%s'" % (file, output, CSS_FILE))
add_basic(file, output)
2024-04-09 10:15:05 -07:00
def add_dbus(file: str, output_basename: str, adaptor: str|None = None, proxy: str|None = None, WARNINGS = WARNINGS):
if adaptor == None:
adaptor = output_basename + '_adaptor.hpp'
if proxy == None:
proxy = output_basename + '_proxy.hpp'
if which('sdbus-c++-xml2cpp') is not None:
subprocess.call(['sdbus-c++-xml2cpp', file, '--adaptor=' + adaptor, '--proxy=' + proxy])
else:
2024-08-08 13:12:37 -07:00
warn("dbus-nogen", "Not generating DBus API stubs.")
compile_program("../backends/ui/imgui/imgui/misc/fonts/binary_to_compressed_c.cpp", "btcc")
for i in glob("Noto_Sans/*.ttf"):
add_font(i)
for i in glob("Noto_Sans_JP/*.ttf"):
add_font(i)
2024-05-02 14:52:11 -07:00
add_font("ForkAwesome/fonts/forkawesome-webfont.ttf", "forkawesome")
add_graphic("icon.png")
add_license("Noto_Sans/OFL.txt", "notosans")
add_license("Noto_Sans_JP/OFL.txt", "notosansjp")
2024-03-26 18:39:02 -07:00
add_license("../LICENSE.MIT", "looper_mit")
add_license("../LICENSE.GPL", "looper_gpl")
add_license("../COPYING.md", "looper")
add_license("../subprojects/jsoncpp/LICENSE", "jsoncpp")
add_license("../subprojects/SDL-Mixer-X/COPYING.txt", "sdl_mixer_x")
add_license("../backends/ui/imgui/imgui-filebrowser/LICENSE", "imgui_filebrowser")
add_license("../backends/ui/imgui/imgui/LICENSE.txt", "imgui")
2024-03-26 18:39:02 -07:00
add_license("licenses/lgpl-3.0.txt", "lgpl_3_0")
add_license("licenses/lgpl-2.1.txt", "lgpl_2_1")
add_license("licenses/lgpl-2.0.txt", "lgpl_2_0")
add_license("licenses/ForkAwesome.txt", "forkawesome")
add_license("licenses/cli11.txt", "cli11")
add_license("licenses/TomlPlusPlus.txt", "tomlplusplus")
add_license("../backends/ui/imgui/IconFontCppHeaders/licence.txt", "icnfntcpphdrs")
2024-03-26 18:39:02 -07:00
add_css("gtk-frontend.css", "gtk_frontend_css")
2024-04-09 10:15:05 -07:00
add_dbus('app.dbus.xml', 'dbus_stub')
add_dbus('mpris.dbus.xml', 'mpris_stub')
2024-09-28 10:31:06 -07:00
def finalize(output: str):
global ASSETS
print("Writing a header including all previous asset headers and writing it to '%s'..." % output)
with open(output, "wt+") as f:
f.write("#pragma once\n")
for asset in ASSETS:
print("Adding include for '%s'..." % (asset))
f.write("#include \"%s\"\n" % asset)
ASSETS = []
finalize("assets.h")
2024-09-28 10:31:06 -07:00
for file in os.listdir("testdata"):
fpath = path.join(os.curdir, "testdata", path.basename(file))
add_basic(fpath, "testdata_%s_%s" % (path.splitext(path.basename(file))[0], path.splitext(file)[1].removeprefix(".")))
finalize("test_assets.h")
os.remove("btcc")
print("Returning to previous directory, now that we're done!")
2024-08-08 13:12:37 -07:00
os.chdir(olddir)