Skip to content

Commit

Permalink
No hard-code kernel version
Browse files Browse the repository at this point in the history
  • Loading branch information
Howard20181 committed Feb 8, 2024
1 parent ea61cd4 commit bd7dba0
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 96 deletions.
94 changes: 56 additions & 38 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -348,36 +348,71 @@ update_gapps_file_name() {
GAPPS_PATH=$DOWNLOAD_DIR/$GAPPS_FILE_NAME
}
WSA_MAJOR_VER=0
getKernelVersion() {
local bintype kernel_string kernel_version
bintype="$(file -b "$1")"
if [[ $bintype == *"version"* ]]; then
readarray -td '' kernel_string < <(awk '{ gsub(/, /,"\0"); print; }' <<<"$bintype, ")
unset 'kernel_string[-1]'
for i in "${kernel_string[@]}"; do
if [[ $i == *"version"* ]]; then
IFS=" " read -r -a kernel_string <<<"$i"
kernel_version="${kernel_string[1]}"
fi
done
else
IFS=" " read -r -a kernel_string <<<"$(strings "$1" | grep 'Linux version')"
kernel_version="${kernel_string[2]}"
fi
IFS=" " read -r -a arr <<<"${kernel_version//-/ }"
printf '%s' "${arr[0]}"
}
update_ksu_zip_name() {
KERNEL_VER=""
case "$WSA_MAJOR_VER" in
"2305") KERNEL_VER="5.15.94.2" ;;
"2306") KERNEL_VER="5.15.104.1" ;;
"2307") KERNEL_VER="5.15.104.2" ;;
"2308") KERNEL_VER="5.15.104.3" ;;
"2309") KERNEL_VER="5.15.104.4" ;;
*) abort "KernelSU is not supported in this WSA version: $WSA_MAJOR_VER" ;;
esac
if [ -f "$WORK_DIR/wsa/$ARCH/Tools/kernel" ]; then
KERNEL_VER=$(getKernelVersion "$WORK_DIR/wsa/$ARCH/Tools/kernel")
fi
KERNELSU_ZIP_NAME=kernelsu-$ARCH-$KERNEL_VER.zip
KERNELSU_PATH=$DOWNLOAD_DIR/$KERNELSU_ZIP_NAME
KERNELSU_INFO="$KERNELSU_PATH.info"
}

if [ -z ${OFFLINE+x} ]; then
echo "Generating WSA Download Links"
require_su
if [ "$DOWN_WSA" != "no" ]; then
echo "Generate Download Links"
python3 generateWSALinks.py "$ARCH" "$RELEASE_TYPE" "$DOWNLOAD_DIR" "$DOWNLOAD_CONF_NAME" || abort
# shellcheck disable=SC1090
source "$WSA_WORK_ENV" || abort
echo "Downloading WSA"
else
echo "Generate Download Links"
python3 generateWSALinks.py "$ARCH" "$RELEASE_TYPE" "$DOWNLOAD_DIR" "$DOWNLOAD_CONF_NAME" "$DOWN_WSA" || abort
WSA_MAJOR_VER=$(python3 getWSAMajorVersion.py "$ARCH" "$WSA_ZIP_PATH")
echo "Skip download WSA, downloading WSA depends"
fi
if [[ "$WSA_MAJOR_VER" -lt 2211 ]]; then
ANDROID_API=32
if ! aria2c --no-conf --log-level=info --log="$DOWNLOAD_DIR/aria2_download.log" -x16 -s16 -j5 -c -R -m0 --async-dns=false --check-integrity=true --continue=true --allow-overwrite=true --conditional-get=true -d"$DOWNLOAD_DIR" -i"$DOWNLOAD_DIR/$DOWNLOAD_CONF_NAME"; then
echo "We have encountered an error while downloading files."
exit 1
fi
if [ "$ROOT_SOL" = "magisk" ]; then
rm -f "${DOWNLOAD_DIR:?}/$DOWNLOAD_CONF_NAME"
fi

echo "Extract WSA"
if [ -f "$WSA_ZIP_PATH" ]; then
if ! python3 extractWSA.py "$ARCH" "$WSA_ZIP_PATH" "$WORK_DIR" "$WSA_WORK_ENV"; then
CLEAN_DOWNLOAD_WSA=1
abort "Unzip WSA failed, is the download incomplete?"
fi
echo -e "Extract done\n"
# shellcheck disable=SC1090
source "$WSA_WORK_ENV" || abort
else
echo "The WSA zip package does not exist, is the download incomplete?"
exit 1
fi
if [[ "$WSA_MAJOR_VER" -lt 2211 ]]; then
ANDROID_API=32
fi
if [ -z ${OFFLINE+x} ]; then
echo "Generating Download Links"
if [ "$HAS_GAPPS" ] || [ "$ROOT_SOL" = "magisk" ]; then
if [ -z ${CUSTOM_MAGISK+x} ]; then
python3 generateMagiskLink.py "$MAGISK_VER" "$DOWNLOAD_DIR" "$DOWNLOAD_CONF_NAME" || abort
fi
Expand All @@ -395,18 +430,14 @@ if [ -z ${OFFLINE+x} ]; then
python3 generateGappsLink.py "$ARCH" "$DOWNLOAD_DIR" "$DOWNLOAD_CONF_NAME" "$ANDROID_API" "$GAPPS_FILE_NAME" || abort
fi

echo "Download Artifacts"
echo "Downloading Artifacts"
if ! aria2c --no-conf --log-level=info --log="$DOWNLOAD_DIR/aria2_download.log" -x16 -s16 -j5 -c -R -m0 --async-dns=false --check-integrity=true --continue=true --allow-overwrite=true --conditional-get=true -d"$DOWNLOAD_DIR" -i"$DOWNLOAD_DIR/$DOWNLOAD_CONF_NAME"; then
echo "We have encountered an error while downloading files."
exit 1
fi
else # Offline mode
WSA_MAJOR_VER=$(python3 getWSAMajorVersion.py "$ARCH" "$WSA_ZIP_PATH")
if [[ "$WSA_MAJOR_VER" -lt 2211 ]]; then
ANDROID_API=32
fi
declare -A FILES_CHECK_LIST=([WSA_ZIP_PATH]="$WSA_ZIP_PATH" [xaml_PATH]="$xaml_PATH" [vclibs_PATH]="$vclibs_PATH" [UWPVCLibs_PATH]="$UWPVCLibs_PATH")
if [ "$ROOT_SOL" = "magisk" ]; then
declare -A FILES_CHECK_LIST=([xaml_PATH]="$xaml_PATH" [vclibs_PATH]="$vclibs_PATH" [UWPVCLibs_PATH]="$UWPVCLibs_PATH")
if [ "$HAS_GAPPS" ] || [ "$ROOT_SOL" = "magisk" ]; then
FILES_CHECK_LIST+=(["MAGISK_PATH"]="$MAGISK_PATH")
fi
if [ "$ROOT_SOL" = "kernelsu" ]; then
Expand All @@ -430,20 +461,6 @@ else # Offline mode
require_su
fi

echo "Extract WSA"
if [ -f "$WSA_ZIP_PATH" ]; then
if ! python3 extractWSA.py "$ARCH" "$WSA_ZIP_PATH" "$WORK_DIR" "$WSA_WORK_ENV"; then
CLEAN_DOWNLOAD_WSA=1
abort "Unzip WSA failed, is the download incomplete?"
fi
echo -e "Extract done\n"
# shellcheck disable=SC1090
source "$WSA_WORK_ENV" || abort
else
echo "The WSA zip package does not exist, is the download incomplete?"
exit 1
fi

if [ "$HAS_GAPPS" ] || [ "$ROOT_SOL" = "magisk" ]; then
echo "Extract Magisk"
if [ -f "$MAGISK_PATH" ]; then
Expand Down Expand Up @@ -479,10 +496,11 @@ if [ "$ROOT_SOL" = "magisk" ]; then
"$WORK_DIR/magisk/magiskboot" cpio "$WORK_DIR/wsa/$ARCH/Tools/initrd.img" "mv /init /wsainit" "add 0750 /lspinit ../bin/$ARCH/lspinit" "ln /lspinit /init" "add 0750 /magiskinit $WORK_DIR/magisk/magiskinit" "mkdir 0750 overlay.d" "mkdir 0750 overlay.d/sbin" "add 0644 overlay.d/sbin/magisk64.xz $WORK_DIR/magisk/magisk64.xz" "add 0644 overlay.d/sbin/magisk32.xz $WORK_DIR/magisk/magisk32.xz" "add 0644 overlay.d/sbin/stub.xz $WORK_DIR/magisk/stub.xz" "mkdir 000 .backup" "add 000 .backup/.magisk $WORK_DIR/magisk/config" || abort "Unable to patch initrd"
echo -e "Integrate Magisk done\n"
elif [ "$ROOT_SOL" = "kernelsu" ]; then
update_ksu_zip_name
echo "Extract KernelSU"
# shellcheck disable=SC1090
source "${KERNELSU_INFO:?}" || abort
echo "WSA Kernel Version: $KERNEL_VER"
echo "KernelSU Version: $KERNELSU_VER"
if ! unzip "$KERNELSU_PATH" -d "$WORK_DIR/kernelsu"; then
CLEAN_DOWNLOAD_KERNELSU=1
abort "Unzip KernelSU failed, package is corrupted?"
Expand Down
26 changes: 21 additions & 5 deletions scripts/generateKernelSULink.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import json
import re
from pathlib import Path
from packaging import version


class Prop(OrderedDict):
Expand All @@ -53,20 +54,34 @@ def __repr__(self):
kernelVersion = sys.argv[4]
file_name = sys.argv[5]
abi_map = {"x64": "x86_64", "arm64": "arm64"}
print(f"Generating KernelSU download link: arch={abi_map[arch]}, kernel version={kernelVersion}", flush=True)
res = requests.get(f"https://api.github.com/repos/tiann/KernelSU/releases/latest")
print(
f"Generating KernelSU download link: arch={abi_map[arch]}, kernel version={kernelVersion}", flush=True)
res = requests.get(
f"https://api.github.com/repos/tiann/KernelSU/releases/latest")
json_data = json.loads(res.content)
headers = res.headers
x_ratelimit_remaining = headers["x-ratelimit-remaining"]
kernel_ver = 0
if res.status_code == 200:
link = ""
assets = json_data["assets"]
for asset in assets:
if re.match(f'kernel-WSA-{abi_map[arch]}-{kernelVersion}.*\.zip$', asset["name"]) and asset["content_type"] == "application/zip":
asset_name = asset["name"]
if re.match(f'kernel-WSA-{abi_map[arch]}-{kernelVersion}.*\.zip$', asset_name) and asset["content_type"] == "application/zip":
tmp_kernel_ver = re.search(
u'\d{1}.\d{1,}.\d{1,}.\d{1,}', asset_name.split("-")[3]).group()
if (kernel_ver == 0):
kernel_ver = tmp_kernel_ver
elif version.parse(kernel_ver) < version.parse(tmp_kernel_ver):
kernel_ver = tmp_kernel_ver
print(f"Kernel version: {kernel_ver}", flush=True)
for asset in assets:
if re.match(f'kernel-WSA-{abi_map[arch]}-{kernel_ver}.*\.zip$', asset["name"]) and asset["content_type"] == "application/zip":
link = asset["browser_download_url"]
break
if link == "":
print(f"Error: No KernelSU release found for arch={abi_map[arch]}, kernel version={kernelVersion}", flush=True)
print(
f"Error: No KernelSU release found for arch={abi_map[arch]}, kernel version={kernelVersion}", flush=True)
exit(1)
release_name = json_data["name"]
with open(os.environ['WSA_WORK_ENV'], 'r') as environ_file:
Expand All @@ -79,7 +94,8 @@ def __repr__(self):
print(f"Github API Error: {message}", flush=True)
ratelimit_reset = headers["x-ratelimit-reset"]
ratelimit_reset = datetime.fromtimestamp(int(ratelimit_reset))
print(f"The current rate limit window resets in {ratelimit_reset}", flush=True)
print(
f"The current rate limit window resets in {ratelimit_reset}", flush=True)
exit(1)

print(f"download link: {link}", flush=True)
Expand Down
23 changes: 10 additions & 13 deletions scripts/generateWSALinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import html
import logging
import os
import re
import sys

Expand Down Expand Up @@ -70,7 +69,8 @@ def __repr__(self):
with open(ms_account_conf, "r") as f:
conf = Prop(f.read())
user = conf.get('user_code')
print(f"Generating WSA download link: arch={arch} release_type={release_name}\n", flush=True)
print(
f"Generating WSA download link: arch={arch} release_type={release_name}\n", flush=True)
with open(Path.cwd().parent / ("xml/GetCookie.xml"), "r") as f:
cookie_content = f.read().format(user)

Expand Down Expand Up @@ -146,8 +146,9 @@ def send_req(i, v, out_file_name):
wsa_build_ver = 0
for filename, values in identities.items():
if re.match(f"MicrosoftCorporationII\.WindowsSubsystemForAndroid_.*\.msixbundle", filename):
tmp_wsa_build_ver = re.search(u'\d{4}.\d{5}.\d{1,}.\d{1,}', filename).group()
if(wsa_build_ver == 0):
tmp_wsa_build_ver = re.search(
u'\d{4}.\d{5}.\d{1,}.\d{1,}', filename).group()
if (wsa_build_ver == 0):
wsa_build_ver = tmp_wsa_build_ver
elif version.parse(wsa_build_ver) < version.parse(tmp_wsa_build_ver):
wsa_build_ver = tmp_wsa_build_ver
Expand All @@ -162,25 +163,21 @@ def send_req(i, v, out_file_name):
out_file_name = f"{values[1]}_{arch}.appx"
out_file = download_dir / out_file_name
elif not skip_wsa_download and re.match(f"MicrosoftCorporationII\.WindowsSubsystemForAndroid_.*\.msixbundle", filename):
tmp_wsa_build_ver = re.search(u'\d{4}.\d{5}.\d{1,}.\d{1,}', filename).group()
if(wsa_build_ver != tmp_wsa_build_ver):
tmp_wsa_build_ver = re.search(
u'\d{4}.\d{5}.\d{1,}.\d{1,}', filename).group()
if (wsa_build_ver != tmp_wsa_build_ver):
continue
version_splitted = wsa_build_ver.split(".")
major_ver = version_splitted[0]
minor_ver = version_splitted[1]
build_ver = version_splitted[2]
revision_ver = version_splitted[3]
with open(os.environ['WSA_WORK_ENV'], 'r') as environ_file:
env = Prop(environ_file.read())
env.WSA_VER = wsa_build_ver
env.WSA_MAJOR_VER = major_ver
with open(os.environ['WSA_WORK_ENV'], 'w') as environ_file:
environ_file.write(str(env))
out_file_name = f"wsa-{release_type}.zip"
out_file = download_dir / out_file_name
else:
continue
th = Thread(target=send_req, args=(values[0][0], values[0][1], out_file_name))
th = Thread(target=send_req, args=(
values[0][0], values[0][1], out_file_name))
threads.append(th)
th.daemon = True
th.start()
Expand Down
40 changes: 0 additions & 40 deletions scripts/getWSAMajorVersion.py

This file was deleted.

0 comments on commit bd7dba0

Please sign in to comment.