Merge Droid-Deck main into fix/proot-performance

This commit is contained in:
maxjivi05 committed 2026-09-24 21:27:31 -04:00
commit 9f0c1fc797
50 files changed
+3126 -428

No files matched your search

+9
View File
@@ -2,6 +2,15 @@
set -euo pipefail
repo_root=$(CDPATH= cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)
if tree_status=$(git -C "${repo_root}" status --porcelain 2>/dev/null); then
if [[ -n "${tree_status}" ]]; then
export DROIDDECK_BUILD_TREE_STATE=dirty
else
export DROIDDECK_BUILD_TREE_STATE=clean
fi
else
export DROIDDECK_BUILD_TREE_STATE=unknown
fi
sdk_dir=${ANDROID_HOME:-${ANDROID_SDK_ROOT:-"${HOME}/Library/Android/sdk"}}
java_dir=${JAVA_HOME:-"/opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home"}
image_name=${DROIDDECK_BUILD_IMAGE:-droiddeck-local-cross:24.04-v2}
@@ -116,7 +116,7 @@ def ini_section(lines, name):
return (start, len(lines)) if start is not None else None
def patch_ini_section(path, section, body, what, needle="SDL-", require=None):
def patch_ini_section(path, section, body, what, needle="SDL-", require=None, done="Player 1 on the controller"):
"""Add body's lines to [section] of an ini the emulator wrote itself, unless the section
already binds a controller (needle). Missing file: nothing - the emulator writes its own
defaults on first run and this runs again."""
@@ -144,7 +144,7 @@ def patch_ini_section(path, section, body, what, needle="SDL-", require=None):
with open(path + ".tmp", "w", encoding="utf-8") as f:
f.write("\n".join(lines) + "\n")
os.replace(path + ".tmp", path)
print(f"bannerlator-pad-defaults: {what}: Player 1 on the controller ({path})")
print(f"bannerlator-pad-defaults: {what}: {done} ({path})")
# DuckStation: SDL-<n>/<name>, axes signed (src/util/sdl_input_source.cpp); Pad1 keys from
@@ -180,9 +180,131 @@ SmallMotor = SDL-0/SmallMotor
"""
def ini_get(path, section, key):
try:
with open(path, encoding="utf-8", errors="replace") as f:
lines = f.read().splitlines()
except OSError:
return None
span = ini_section(lines, section)
if not span:
return None
for l in lines[span[0]:span[1]]:
if "=" in l and l.split("=", 1)[0].strip() == key:
return l.split("=", 1)[1].strip()
return None
def ini_set(path, section, key, value):
"""Sets one key (the first of that name) in [section], adding either as needed."""
with open(path, encoding="utf-8", errors="replace") as f:
lines = f.read().splitlines()
span = ini_section(lines, section)
if span is None:
lines += ["", f"[{section}]", f"{key} = {value}"]
else:
for i in range(span[0], span[1]):
if "=" in lines[i] and lines[i].split("=", 1)[0].strip() == key:
lines[i] = f"{key} = {value}"
break
else:
end = span[1]
while end > span[0] and not lines[end - 1].strip():
end -= 1
lines.insert(end, f"{key} = {value}")
with open(path + ".tmp", "w", encoding="utf-8") as f:
f.write("\n".join(lines) + "\n")
os.replace(path + ".tmp", path)
def drop_line(path, line):
with open(path, encoding="utf-8", errors="replace") as f:
lines = f.read().splitlines()
kept = [l for l in lines if l.replace(" ", "") != line.replace(" ", "")]
if len(kept) != len(lines):
with open(path + ".tmp", "w", encoding="utf-8") as f:
f.write("\n".join(kept) + "\n")
os.replace(path + ".tmp", path)
def system_folder(names, default):
for name in names:
for cand in (name, name.upper()):
d = os.path.join(ROMS, cand)
if os.path.isdir(d):
return d
return os.path.join(ROMS, default)
def bios_folder(base):
for name in ("bios", "BIOS", "firmware"):
d = os.path.join(base, name)
if os.path.isdir(d):
return d
d = os.path.join(base, "bios")
if os.path.isdir(ROMS):
try:
os.makedirs(d, exist_ok=True) # where the user is told to put their BIOS
except OSError:
pass
return d
# DuckStation (PS1) - set up like ARMSX2: users bring their own BIOS and games, in the ROMs folder.
# Front-end runs use -batch -bigpicture -fullscreen, so the guide button is the pause menu
# (Hotkeys OpenPauseMenu, src/core/hotkeys.cpp), not the analog toggle. Keys from
# src/core/settings.cpp: [BIOS] SearchDirectory, [GPU] Renderer ("Vulkan"), [Main]
# SetupWizardIncomplete (src/duckstation-qt/qthost.cpp).
DUCKSTATION_PAD1_GUIDE_FREE = "\n".join(l for l in DUCKSTATION_PAD1.splitlines() if not l.startswith("Analog ")) + "\n"
def duckstation():
for d in (os.path.join(CONFIG, "duckstation"), os.path.join(HOME, ".local/share/duckstation")):
patch_ini_section(os.path.join(d, "settings.ini"), "Pad1", DUCKSTATION_PAD1, "DuckStation")
ps1 = system_folder(("psx", "ps1"), "psx")
d = os.path.join(CONFIG, "duckstation")
ini = os.path.join(d, "settings.ini")
legacy = os.path.join(HOME, ".local/share/duckstation/settings.ini")
if not os.path.isfile(ini) and os.path.isfile(legacy):
ini = legacy
if not os.path.isfile(ini):
# A new player: everything DuckStation's first-time setup asks is answered here (it finds
# the BIOS in its folder by itself), so a game from the front end boots straight away.
bios = bios_folder(ps1)
seed(ini, "[Main]\nSetupWizardIncomplete = false\nInhibitScreensaver = false\n\n"
f"[BIOS]\nSearchDirectory = {bios}\n\n"
f"[GameList]\nRecursivePaths = {ps1}\n\n"
"[GPU]\nRenderer = Vulkan\n\n"
"[Hotkeys]\nOpenPauseMenu = Keyboard/Escape\nOpenPauseMenu = SDL-0/Guide\n\n"
"[Pad1]\n" + DUCKSTATION_PAD1_GUIDE_FREE, "DuckStation", "first-time setup, BIOS from " + bios)
return
patch_ini_section(ini, "Pad1", DUCKSTATION_PAD1_GUIDE_FREE, "DuckStation")
drop_line(ini, "Analog = SDL-0/Guide")
patch_ini_section(ini, "Hotkeys", "OpenPauseMenu = SDL-0/Guide\n", "DuckStation", needle="SDL-0/Guide",
done="the guide button opens the pause menu")
patch_ini_section(ini, "GameList", f"RecursivePaths = {ps1}\n", "DuckStation", needle=ROMS,
done="games listed from the ROMs folder")
# One set up by an earlier build still has its first-time setup pending, in the way of a game.
if ini_get(ini, "Main", "SetupWizardIncomplete") == "true":
ini_set(ini, "Main", "SetupWizardIncomplete", "false")
print("bannerlator-pad-defaults: DuckStation: first-time setup already done here")
# Keeping the screen awake goes through org.freedesktop.ScreenSaver, which nothing in the session
# provides (Android decides when the screen sleeps): DuckStation only warns it failed, every game.
if (ini_get(ini, "Main", "InhibitScreensaver") or "true") == "true":
ini_set(ini, "Main", "InhibitScreensaver", "false")
print("bannerlator-pad-defaults: DuckStation: no screensaver inhibit (no such service here)")
# Once, for a DuckStation set up before this: its own empty BIOS folder moves to the ROMs folder,
# and Automatic becomes Vulkan. A later choice in DuckStation's settings is kept.
marker = os.path.join(os.path.dirname(ini), ".droiddeck-setup")
if os.path.exists(marker):
return
current = ini_get(ini, "BIOS", "SearchDirectory") or "bios"
own = current if os.path.isabs(current) else os.path.join(os.path.dirname(ini), current)
if not (os.path.isdir(own) and os.listdir(own)):
ini_set(ini, "BIOS", "SearchDirectory", bios_folder(ps1))
print(f"bannerlator-pad-defaults: DuckStation: BIOS from {bios_folder(ps1)}")
if (ini_get(ini, "GPU", "Renderer") or "Automatic") == "Automatic":
ini_set(ini, "GPU", "Renderer", "Vulkan")
print("bannerlator-pad-defaults: DuckStation: renderer Vulkan")
open(marker, "w").close()
# PCSX2: FaceSouth/... names (pcsx2/Input/SDLInputSource.cpp), keys from SIO/Pad/PadDualshock2.cpp.
@@ -221,10 +343,7 @@ SmallMotor = SDL-0/SmallMotor
def pcsx2():
patch_ini_section(os.path.join(CONFIG, "PCSX2/inis/PCSX2.ini"), "Pad1", PCSX2_PAD1, "PCSX2",
require="SettingsVersion")
# ARMSX2 (PCSX2 with ARM64 recompilers): the same ini and binding names under its own folder
# (pcsx2/Pcsx2Config.cpp: $XDG_CONFIG_HOME/ARMSX2/inis/PCSX2.ini).
patch_ini_section(os.path.join(CONFIG, "ARMSX2/inis/PCSX2.ini"), "Pad1", PCSX2_PAD1, "ARMSX2",
require="SettingsVersion")
# Dolphin: SDL/<id>/<SDL gamepad name>; control names from InputCommon/.../SDL/SDLGamepad.h (Left Y+
@@ -287,6 +406,110 @@ Nunchuk/Stick/Right = `Left X+`
"""
# ARMSX2 - the PS2 emulator (PCSX2 with ARM64 recompilers; upstream PCSX2 interprets on ARM64). Its
# files are PCSX2's under $XDG_CONFIG_HOME/ARMSX2 (pcsx2/Pcsx2Config.cpp). The user supplies the BIOS
# and the games; they live in the ROMs folder (/root/ROMs). The guide button opens the pause menu
# (Hotkeys OpenPauseMenu) - the way out of a game started from the front end, which runs ARMSX2
# without its window (-batch -bigpicture) - so it is not also the analog toggle here.
ARMSX2_PAD1 = "\n".join(l for l in PCSX2_PAD1.splitlines() if not l.startswith("Analog ")) + "\n"
ROMS = "/root/ROMs"
def ps2_folder():
for name in ("ps2", "PS2"):
d = os.path.join(ROMS, name)
if os.path.isdir(d):
return d
return os.path.join(ROMS, "ps2")
def ps2_bios_folder():
base = ps2_folder()
for name in ("bios", "BIOS", "firmware"):
d = os.path.join(base, name)
if os.path.isdir(d):
return d
d = os.path.join(base, "bios")
if os.path.isdir(ROMS):
try:
os.makedirs(d, exist_ok=True) # where the user is told to put their BIOS
except OSError:
pass
return d
def armsx2():
root = os.path.join(CONFIG, "ARMSX2")
ini = os.path.join(root, "inis/PCSX2.ini")
old = os.path.join(CONFIG, "PCSX2")
if not os.path.isfile(ini) and os.path.isfile(os.path.join(old, "inis/PCSX2.ini")):
# Someone who played on PCSX2 here: the same settings, BIOS choice and memory cards (the
# saves), copied once. PCSX2's own folder is left as it was.
import shutil
for sub in ("inis", "memcards", "bios", "gamesettings", "cheats", "patches", "covers",
"inputprofiles", "textures"):
src = os.path.join(old, sub)
if os.path.isdir(src):
shutil.copytree(src, os.path.join(root, sub), dirs_exist_ok=True)
print(f"bannerlator-pad-defaults: ARMSX2: settings and memory cards copied from PCSX2 ({root})")
elif not os.path.isfile(ini):
# A new player: everything ARMSX2's first-time setup asks is answered here - the BIOS
# folder in the ROMs folder, the ps2 folder as its game list, the controller on Player 1 -
# and the BIOS file itself is picked below once one is there.
bios, games = ps2_bios_folder(), ps2_folder()
seed(ini, "[UI]\nSettingsVersion = 1\nSetupWizardIncomplete = false\n\n"
f"[Folders]\nBios = {bios}\n\n"
f"[GameList]\nRecursivePaths = {games}\n\n"
# Vulkan outright rather than Auto's pick: the renderer every other path here uses.
"[EmuCore/GS]\nRenderer = 14\n\n"
"[Hotkeys]\nOpenPauseMenu = Keyboard/Escape\nOpenPauseMenu = SDL-0/Guide\n\n"
"[Pad1]\n" + ARMSX2_PAD1, "ARMSX2", "first-time setup, BIOS from " + bios)
armsx2_bios(ini)
return
patch_ini_section(ini, "Pad1", ARMSX2_PAD1, "ARMSX2", require="SettingsVersion")
# The guide button is the pause menu here; PCSX2's profile (ours, copied over) had it as the
# analog toggle as well.
with open(ini, encoding="utf-8", errors="replace") as f:
lines = f.read().splitlines()
kept = [l for l in lines if l.replace(" ", "") != "Analog=SDL-0/Guide"]
if len(kept) != len(lines):
with open(ini + ".tmp", "w", encoding="utf-8") as f:
f.write("\n".join(kept) + "\n")
os.replace(ini + ".tmp", ini)
patch_ini_section(ini, "Hotkeys", "OpenPauseMenu = SDL-0/Guide\n", "ARMSX2", needle="SDL-0/Guide",
require="SettingsVersion", done="the guide button opens the pause menu")
patch_ini_section(ini, "GameList", f"RecursivePaths = {ps2_folder()}\n", "ARMSX2", needle=ROMS,
require="SettingsVersion", done="games listed from the ROMs folder")
if ini_get(ini, "UI", "SetupWizardIncomplete") == "true":
ini_set(ini, "UI", "SetupWizardIncomplete", "false")
print("bannerlator-pad-defaults: ARMSX2: first-time setup already done here")
armsx2_bios(ini)
def armsx2_bios(ini):
"""PCSX2 boots the one BIOS file named in [Filenames] BIOS (inside [Folders] Bios); its first-time
setup is what names it. With that skipped, the first dump in the folder is named here - a US one
first - while none is named or the named one is gone. A choice made in ARMSX2 is kept."""
folder = ini_get(ini, "Folders", "Bios") or ps2_bios_folder()
if not os.path.isabs(folder):
folder = os.path.join(os.path.dirname(os.path.dirname(ini)), folder)
current = ini_get(ini, "Filenames", "BIOS")
if current and os.path.isfile(os.path.join(folder, current)):
return
try:
# A PS2 BIOS dump is 4 MiB (a few are 8); the .nvm/.mec files beside it are not dumps.
dumps = sorted(f for f in os.listdir(folder)
if os.path.isfile(os.path.join(folder, f)) and 4 << 20 <= os.path.getsize(os.path.join(folder, f)) <= 8 << 20)
except OSError:
return
if not dumps:
return
us = [f for f in dumps if any(t in f.lower() for t in ("usa", "(us)", "(us,", "ntsc-u"))]
pick = (us or dumps)[0]
ini_set(ini, "Filenames", "BIOS", pick)
print(f"bannerlator-pad-defaults: ARMSX2: BIOS {pick}")
def dolphin():
d = os.path.join(CONFIG, "dolphin-emu")
seed(os.path.join(d, "GCPadNew.ini"), DOLPHIN_GC, "Dolphin (GameCube)")
@@ -454,7 +677,7 @@ def retroarch():
print(f"bannerlator-pad-defaults: RetroArch: joypad driver udev -> sdl2 ({cfg})")
for step in (rpcs3, duckstation, pcsx2, dolphin, cemu, melonds, ppsspp, retroarch):
for step in (rpcs3, duckstation, pcsx2, armsx2, dolphin, cemu, melonds, ppsspp, retroarch):
try:
step()
except Exception as e:
@@ -347,6 +347,22 @@ case $mode in
# moments later, so the seeder has only that gap to mark the redistributables as already run.
# It stamps each prefix it has covered and skips it thereafter, so the extra passes cost
# almost nothing.
# The drawer's "Stretch games to fill the screen" works live: the app writes its choice to
# ~/.droiddeck-fill, and each change goes to gamescope as GAMESCOPE_FORCE_WINDOWS_FULLSCREEN on
# the root window, which it watches. What gamescope was started with is where this begins.
(
last=${BL_GAMESCOPE_FORCE_FULLSCREEN:-1}
while :; do
sleep 1
want=$(cat "$HOME/.droiddeck-fill" 2>/dev/null) || continue
case $want in 0|1) ;; *) continue ;; esac
[ "$want" = "$last" ] && continue
if xprop -root -f GAMESCOPE_FORCE_WINDOWS_FULLSCREEN 32c -set GAMESCOPE_FORCE_WINDOWS_FULLSCREEN "$want" 2>/dev/null; then
echo "== stretch games to fill the screen: $want (live)"
last=$want
fi
done
) &
(
beat=0
for _ in $(seq 1 720); do
@@ -87,6 +87,31 @@ bl_game_cpus() {
}
"""
# Quake III engine games (id Tech 3) set their own resolution and switch the display to it. Under
# gamescope with the ARM64 Proton that switch is refused ("calling CDS: failed, bad mode") and the
# game never gets OpenGL; left at its own size in a window, it draws small in a corner. So these
# titles are told to stay windowed at the session's own size - unless the user's launch options
# already say something about the video mode, which are left as they are.
BL_IDTECH3_SETUP = """
bl_idtech3() {
BL_EXTRA_ARGS=()
case "${SteamAppId:-${STEAM_COMPAT_APP_ID:-}}" in
# Quake III Arena, Quake III: Team Arena, Return to Castle Wolfenstein, Jedi Academy.
2200|2350|9010|6020) ;;
*) return 0 ;;
esac
case " $* " in
*r_fullscreen*|*r_mode*|*r_customwidth*|*r_customheight*)
echo "bannerlator-proton: Quake-engine title; its launch options set the video mode, left as they are" >&2
return 0 ;;
esac
w=${BL_WIDTH:-1280}
h=${BL_HEIGHT:-720}
BL_EXTRA_ARGS=(+set r_fullscreen 0 +set r_mode -1 +set r_customwidth "$w" +set r_customheight "$h")
echo "bannerlator-proton: Quake-engine title: windowed at ${w}x${h}" >&2
}
"""
BL_DIRECTAUDIO_SETUP = """
bl_directaudio() {
base=$1
@@ -152,7 +177,8 @@ for name in %s; do
if [ -d "$depot/files/bin-arm64" ] && [ -x "$depot/proton" ]; then
bl_directaudio "$depot"
bl_game_cpus
exec ${BL_TASKSET:-} "$depot/proton" "$verb" "$@"
bl_idtech3 "$@"
exec ${BL_TASKSET:-} "$depot/proton" "$verb" "$@" "${BL_EXTRA_ARGS[@]}"
fi
done
echo "bannerlator-proton: no ARM64 Proton depot yet - the client downloads one as this tool's dependency; retry the launch once it has" >&2
@@ -162,7 +188,7 @@ exit 1
# and a % format would eat that into a single %, leaving a shell expansion that silently does
# something else.
LAUNCHER_SH = LAUNCHER_SH.replace(
"verb=$1; shift", "verb=$1; shift\n" + BL_DIRECTAUDIO_SETUP + BL_GAME_CPUS_SETUP, 1)
"verb=$1; shift", "verb=$1; shift\n" + BL_DIRECTAUDIO_SETUP + BL_GAME_CPUS_SETUP + BL_IDTECH3_SETUP, 1)
# A third-party Proton dropped into compatibilitytools.d - GE-Proton, proton-cachyos, anything of
# that shape - is adopted rather than used as shipped, for the same two reasons Valve's ARM64 depot
@@ -196,9 +222,10 @@ if [ -f "$fake" ]; then
fi
bl_directaudio "$here"
bl_game_cpus
bl_idtech3 "$@"
# Proton takes its base directory from dirname(sys.argv[0]) without resolving it, so it has to be
# started from its own tree.
exec ${BL_TASKSET:-} "$here/%s" "$verb" "$@"
exec ${BL_TASKSET:-} "$here/%s" "$verb" "$@" "${BL_EXTRA_ARGS[@]}"
"""
EXTRA_COMPAT_TOOL_VDF = """"compatibilitytools"
{
@@ -357,7 +384,7 @@ def adopt_extras(tools):
# ${major%%.*} of its own, and putting it through the format would eat both.
body = (EXTRA_WRAPPER_SH % entry_point).replace(
'here=$(dirname "$0")',
'here=$(dirname "$0")\n' + BL_DIRECTAUDIO_SETUP + BL_GAME_CPUS_SETUP, 1)
'here=$(dirname "$0")\n' + BL_DIRECTAUDIO_SETUP + BL_GAME_CPUS_SETUP + BL_IDTECH3_SETUP, 1)
with open(wrapper, "w", encoding="utf-8") as f:
f.write(body)
os.chmod(wrapper, 0o755)
+74
View File
@@ -0,0 +1,74 @@
#!/usr/bin/env python3
# axml_rename.py in.apk out.apk old=new ... : rewrite whole strings in AndroidManifest.xml's string
# pool (binary XML), leaving every other entry of the apk byte-identical. Unsigned output.
import struct, sys, zipfile
def dec_len8(b, o):
n = b[o]; o += 1
if n & 0x80: n = ((n & 0x7f) << 8) | b[o]; o += 1
return n, o
def dec_len16(b, o):
n = struct.unpack_from('<H', b, o)[0]; o += 2
if n & 0x8000: n = ((n & 0x7fff) << 16) | struct.unpack_from('<H', b, o)[0]; o += 2
return n, o
def enc_len8(n):
return bytes([n]) if n < 0x80 else bytes([0x80 | (n >> 8), n & 0xff])
def enc_len16(n):
return struct.pack('<H', n) if n < 0x8000 else struct.pack('<HH', 0x8000 | (n >> 16), n & 0xffff)
def rewrite(axml, repl):
ftype, fhs, fsize = struct.unpack_from('<HHI', axml, 0)
assert ftype == 0x0003, 'not binary XML'
sp = fhs
ctype, chs, csize, count, scount, flags, sstart, stystart = struct.unpack_from('<HHIIIIII', axml, sp)
assert ctype == 0x0001
utf8 = bool(flags & 0x100)
offs = struct.unpack_from('<%dI' % count, axml, sp + chs)
sty_offs = axml[sp + chs + 4 * count: sp + chs + 4 * count + 4 * scount]
base = sp + sstart
strings = []
for off in offs:
o = base + off
if utf8:
_, o = dec_len8(axml, o); n, o = dec_len8(axml, o)
strings.append(axml[o:o + n].decode('utf-8'))
else:
n, o = dec_len16(axml, o)
strings.append(axml[o:o + 2 * n].decode('utf-16-le'))
hits = 0
for i, s in enumerate(strings):
if s in repl: strings[i] = repl[s]; hits += 1
data = bytearray(); new_offs = []
for s in strings:
new_offs.append(len(data))
if utf8:
e = s.encode('utf-8'); data += enc_len8(len(s.encode('utf-16-le')) // 2) + enc_len8(len(e)) + e + b'\0'
else:
e = s.encode('utf-16-le'); data += enc_len16(len(e) // 2) + e + b'\0\0'
while len(data) % 4: data += b'\0'
styles = axml[sp + stystart: sp + csize] if scount else b''
new_sstart = chs + 4 * count + 4 * scount
new_stystart = new_sstart + len(data) if scount else 0
body = struct.pack('<%dI' % count, *new_offs) + sty_offs + bytes(data) + styles
new_csize = chs + len(body)
header = struct.pack('<HHIIIIII', ctype, chs, new_csize, count, scount, flags, new_sstart, new_stystart)
header += axml[sp + 28: sp + chs]
rest = axml[sp + csize:]
out = axml[:sp] + header + body + rest
out = out[:4] + struct.pack('<I', len(out)) + out[8:]
return out, hits
src, dst = sys.argv[1], sys.argv[2]
repl = dict(a.split('=', 1) for a in sys.argv[3:])
with zipfile.ZipFile(src) as zi, zipfile.ZipFile(dst, 'w') as zo:
for info in zi.infolist():
if info.filename.startswith('META-INF/') and info.filename.split('/')[-1].split('.')[-1] in ('SF', 'RSA', 'DSA', 'EC', 'MF'):
continue
b = zi.read(info)
if info.filename == 'AndroidManifest.xml':
b, hits = rewrite(b, repl)
print(f'manifest: {hits} strings replaced')
zo.writestr(info, b, compress_type=info.compress_type)
+56
View File
@@ -0,0 +1,56 @@
#!/usr/bin/env bash
set -euo pipefail
REPO=Droid-Deck/DroidDeck
ENVIRONMENTS=(signing-main release)
ALIAS=droiddeck
OUT=${DROIDDECK_KEY_DIR:-$HOME/droiddeck-release-key}
here=$(cd "$(dirname "$0")" && pwd)
repo=$(cd "$here/../.." && pwd)
die() { echo "error: $*" >&2; exit 1; }
command -v keytool >/dev/null || die "keytool not found (Termux: pkg install openjdk-17)"
command -v gh >/dev/null || die "gh not found"
gh auth status >/dev/null 2>&1 || die "gh is not logged in (gh auth login)"
[ -e "$OUT/droiddeck-release.p12" ] && die "a key already exists in $OUT - there must only ever be one"
case "$OUT" in "$repo"|"$repo"/*) die "the key must not be made inside the repo" ;; esac
for env in "${ENVIRONMENTS[@]}"; do
gh api "repos/$REPO/environments/$env" >/dev/null 2>&1 || die "GitHub environment '$env' does not exist yet"
done
umask 077
mkdir -p "$OUT"
KS="$OUT/droiddeck-release.p12"
KS_PW=$(head -c 60 /dev/urandom | base64 -w0 | tr -d '/+=')
KS_PW=${KS_PW:0:48}
[ ${#KS_PW} -ge 40 ] || die "could not make a password"
export KS_PW
printf '%s\n' "$KS_PW" > "$OUT/password.txt"
keytool -genkeypair -keystore "$KS" -storetype PKCS12 -alias "$ALIAS" \
-keyalg RSA -keysize 4096 -validity 10950 -dname "CN=DroidDeck, O=The412Banner" \
-storepass:env KS_PW -keypass:env KS_PW >/dev/null 2>&1 || die "keytool could not make the key"
listing=$(keytool -list -v -keystore "$KS" -storepass:env KS_PW -alias "$ALIAS")
SHA=$(awk '/SHA256:/ { print $2 }' <<<"$listing" | head -n1 | tr -d ':' | tr 'A-F' 'a-f')
[[ $SHA =~ ^[0-9a-f]{64}$ ]] || die "could not read the key's certificate digest"
for env in "${ENVIRONMENTS[@]}"; do
base64 -w0 < "$KS" | gh secret set RELEASE_KEYSTORE_B64 --env "$env" -R "$REPO" >/dev/null
printf '%s' "$KS_PW" | gh secret set RELEASE_STORE_PASSWORD --env "$env" -R "$REPO" >/dev/null
echo "secrets set for environment: $env"
done
unset KS_PW
printf '%s\n' "$SHA" > "$repo/keystore/release-signer.sha256"
cat <<EOF
DroidDeck's signing key is made and in GitHub.
Key: $KS
Password: $OUT/password.txt
Digest: $SHA (public - written to keystore/release-signer.sha256)
Now:
1. Back up the folder $OUT (both files) to at least two safe places.
2. Commit keystore/release-signer.sha256 (only that file).
EOF
+100
View File
@@ -0,0 +1,100 @@
#!/usr/bin/env bash
# sign-apk.sh <in.apk> <variant> <out.apk>
#
# Turns a CI build (signed with the public AOSP testkey) into one package of tools/release/variants.txt
# signed with DroidDeck's own key, carrying the hand-over from the testkey:
# - v1 + v2 are signed by the testkey, v3 by our key, with a signed lineage testkey -> our key;
# - an install from before (testkey) updates to it and keeps its data;
# - the testkey loses its rollback right, so an apk signed with the testkey alone - which anyone
# can make - is refused as an update to it (device-proven on the Pocket FIT, 2026-09-24);
# - --rotation-min-sdk-version 28: the lineage applies from Android 9, not only from 13.
#
# The key comes from the environment and is never written anywhere but the path given:
# RELEASE_KEYSTORE path to the PKCS12 keystore
# RELEASE_STORE_PASSWORD its password (the key has the same one: PKCS12)
# RELEASE_KEY_ALIAS the key's alias
# BUILD_TOOLS Android build-tools dir (zipalign, apksigner, aapt)
# RELEASE_SIGNER_SHA256 optional: the expected certificate digest, else keystore/release-signer.sha256
set -euo pipefail
die() { echo "::error::$*" >&2; exit 1; }
[ $# -eq 3 ] || die "usage: sign-apk.sh <in.apk> <variant> <out.apk>"
in=$1 variant=$2 out=$3
here=$(cd "$(dirname "$0")" && pwd)
repo=$(cd "$here/../.." && pwd)
pkg=$(awk -v v="$variant" '!/^#/ && $1 == v { print $2 }' "$here/variants.txt")
[ -n "$pkg" ] || die "no variant '$variant' in tools/release/variants.txt"
expected=${RELEASE_SIGNER_SHA256:-$(tr -d ' \n' < "$repo/keystore/release-signer.sha256" 2>/dev/null || true)}
[[ $expected =~ ^[0-9a-f]{64}$ ]] \
|| die "keystore/release-signer.sha256 is missing: make the key first (tools/release/make-release-key.sh)"
: "${RELEASE_KEYSTORE:?}" "${RELEASE_STORE_PASSWORD:?}" "${RELEASE_KEY_ALIAS:?}" "${BUILD_TOOLS:?}"
[ -s "$RELEASE_KEYSTORE" ] || die "the release keystore is empty - is the secret set for this environment?"
TESTKEY="$repo/keystore/testkey.p12"
TESTKEY_SHA256=a40da80a59d170caa950cf15c18c454d47a39b26989d8b640ecd745ba71bf5dc
export TESTKEY_PASSWORD=android
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
# The package name and everything the manifest derives from it; component class names stay.
P=com.droiddeck.launcher
if [ "$pkg" = "$P" ]; then
python3 "$here/axml_rename.py" "$in" "$work/renamed.apk"
else
python3 "$here/axml_rename.py" "$in" "$work/renamed.apk" \
"$P=$pkg" "$P.androidx-startup=$pkg.androidx-startup" "$P.logs=$pkg.logs" \
"$P.documents=$pkg.documents" "$P.home=$pkg.home" \
"$P.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION=$pkg.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION"
fi
got=$("$BUILD_TOOLS/aapt" dump badging "$work/renamed.apk" | sed -n "s/^package: name='\([^']*\)'.*/\1/p")
[ "$got" = "$pkg" ] || die "$variant: the apk says package '$got', not '$pkg'"
if [ "$pkg" != "$P" ]; then
# A provider left on the old authority would clash with the standard app installed beside it.
tree=$("$BUILD_TOOLS/aapt" dump xmltree "$work/renamed.apk" AndroidManifest.xml)
grep -E 'android:(authorities|taskAffinity)' <<<"$tree" | grep -F "\"$P." \
&& die "$variant: the manifest still names $P in an authority or task"
fi
"$BUILD_TOOLS/zipalign" -p -f 4 "$work/renamed.apk" "$work/aligned.apk"
# The lineage: the testkey vouches for our key. Made fresh each time (it is two certificates and
# a signature, nothing secret beyond the key that signs it).
"$BUILD_TOOLS/apksigner" rotate --out "$work/lineage" \
--old-signer --ks "$TESTKEY" --ks-type PKCS12 --ks-pass env:TESTKEY_PASSWORD \
--ks-key-alias testkey --key-pass env:TESTKEY_PASSWORD --set-rollback false \
--new-signer --ks "$RELEASE_KEYSTORE" --ks-type PKCS12 --ks-pass env:RELEASE_STORE_PASSWORD \
--ks-key-alias "$RELEASE_KEY_ALIAS" --key-pass env:RELEASE_STORE_PASSWORD
"$BUILD_TOOLS/apksigner" sign \
--ks "$TESTKEY" --ks-type PKCS12 --ks-pass env:TESTKEY_PASSWORD \
--ks-key-alias testkey --key-pass env:TESTKEY_PASSWORD \
--next-signer --ks "$RELEASE_KEYSTORE" --ks-type PKCS12 --ks-pass env:RELEASE_STORE_PASSWORD \
--ks-key-alias "$RELEASE_KEY_ALIAS" --key-pass env:RELEASE_STORE_PASSWORD \
--lineage "$work/lineage" --rotation-min-sdk-version 28 \
--v1-signing-enabled true --v2-signing-enabled true --v3-signing-enabled true --v4-signing-enabled false \
--out "$out" "$work/aligned.apk"
rm -f "$out.idsig"
# What the apk must carry, checked rather than assumed.
"$BUILD_TOOLS/apksigner" verify --min-sdk-version 26 "$out" >/dev/null || die "$variant: does not verify"
# (Listed first, then searched: grep -q stops reading early, which fails a pipe under pipefail.)
listing=$(unzip -l "$out")
grep -qE 'META-INF/.*\.(SF|RSA)$' <<<"$listing" || die "$variant: no JAR signature (old installers)"
# Android 9+ must see our key and only our key. apksigner labels signers "Signer #1", "Signer
# (minSdkVersion=28, ...)" or "V3.0 Signer:" depending on its version; the digests are what count.
certs=$("$BUILD_TOOLS/apksigner" verify --min-sdk-version 28 --print-certs "$out")
v3=$(sed -n 's/^.*Signer.* certificate SHA-256 digest: //p' <<<"$certs" | sort -u)
if [ "$v3" != "$expected" ]; then
echo "$certs" >&2
die "$variant: Android 9+ sees signer(s) [$(tr '\n' ' ' <<<"$v3")], not only DroidDeck's key $expected"
fi
lineage=$("$BUILD_TOOLS/apksigner" lineage --in "$out" --print-certs)
grep -q "Signer #1 in lineage certificate SHA-256 digest: $TESTKEY_SHA256" <<<"$lineage" \
|| die "$variant: the lineage does not start at the testkey - old installs could not update"
grep -q "Signer #2 in lineage certificate SHA-256 digest: $expected" <<<"$lineage" \
|| die "$variant: the lineage does not hand over to DroidDeck's key"
rollback=$(awk '/Signer #1 in lineage/{s=1} /Signer #2 in lineage/{s=0} s && /rollback capability/' <<<"$lineage")
grep -q false <<<"$rollback" \
|| die "$variant: the testkey kept its rollback right - a testkey-signed apk could replace this one"
echo "$variant ($pkg): signed, hand-over from the testkey, v3 signer $expected"
+9
View File
@@ -0,0 +1,9 @@
# The packages a signed build ships as. The same apk, with only the manifest's package name (and
# the names derived from it) changed: some phones give their performance modes only to apps with
# certain names. Each is its own app to Android - installs beside the others, with its own data.
#
# name package file suffix
standard com.droiddeck.launcher -
pubg com.tencent.ig -pubg
antutu com.antutu.benchmark.full -antutu
ludashi com.ludashi.benchmark -ludashi