Files

115 lines
5.2 KiB
Python

"""Fetch only compiler/desktop-runtime members from the official AIR SDK ZIP.
Opt-in dependency acquisition, never imported by the offline adapter. Everything
is scoped to ignored local_state; the SDK is not installed or placed on PATH.
"""
from __future__ import annotations
import argparse
import hashlib
import http.cookiejar
import io
import json
from pathlib import Path
import urllib.request
import zipfile
import zlib
ROOT = Path(__file__).resolve().parents[2]
BASE = "https://airsdk.wxlift.com"
SDK_VERSION = "51.5.1.1" # Must match the reviewed executable hashes in trusted.py.
class RemoteZip(io.RawIOBase):
def __init__(self, opener, url):
self.opener, self.url, self.position = opener, url, 0
self.cached_start, self.cached_data = 0, b""
with self.fetch(0, 1) as response:
self.size = int(response.headers["Content-Range"].split("/")[-1])
def fetch(self, start, size):
return self.opener.open(urllib.request.Request(self.url, headers={
"Range": f"bytes={start}-{start + size - 1}",
"Referer": BASE + "/download"}), timeout=45)
def seekable(self):
return True
def seek(self, offset, whence=0):
self.position = offset + (0 if whence == 0 else self.position if whence == 1 else self.size)
return self.position
def tell(self):
return self.position
def read(self, size=-1):
size = min(self.size - self.position, size if size >= 0 else self.size)
if size <= 0:
return b""
if self.cached_start <= self.position and self.position + size <= self.cached_start + len(self.cached_data):
offset = self.position - self.cached_start
self.position += size
return self.cached_data[offset:offset + size]
requested = min(self.size - self.position, max(size, 128 * 1024))
with self.fetch(self.position, requested) as response:
if response.status != 206:
raise ValueError("Official SDK server did not honor byte range")
fetched = response.read()
if len(fetched) != requested:
raise ValueError("Truncated SDK byte range")
self.cached_start, self.cached_data = self.position, fetched
data = fetched[:size]
self.position += size
return data
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--list", action="store_true")
parser.add_argument("--runtime-only", action="store_true")
args = parser.parse_args()
root = ROOT / "local_state/battle_runtime"
acquisition = root / "acquisition"
acquisition.mkdir(parents=True, exist_ok=True)
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()))
with opener.open(BASE + "/api/config-settings/download", timeout=30) as response:
settings = json.load(response)
link = f"/api/versions/{SDK_VERSION}/sdks/AIRSDK_Windows.zip"
url = BASE + link + "?id=" + str(settings["id"])
remote = RemoteZip(opener, url)
with zipfile.ZipFile(remote) as archive:
inventory = [{"name": item.filename, "bytes": item.file_size,
"compressed": item.compress_size, "crc32": item.CRC}
for item in archive.infolist()]
(acquisition / "sdk_inventory.json").write_text(json.dumps(inventory, indent=2), encoding="utf-8")
selected = [item for item in archive.infolist() if not item.is_dir() and (
item.filename.startswith("runtimes/air/win64/")
or (item.filename.startswith("lib/") and item.filename.count("/") == 1)
or (item.filename.startswith("frameworks/") and not item.filename.startswith(("frameworks/projects/", "frameworks/locale/")))
or item.filename in ("bin/adl.exe", "bin/adl64.exe", "air-sdk-description.xml"))]
if args.runtime_only:
selected = [item for item in selected if item.filename.startswith("runtimes/") or item.filename == "bin/adl64.exe"]
print(json.dumps({"version": SDK_VERSION, "archive_bytes": remote.size,
"selected_files": len(selected), "selected_compressed_bytes": sum(i.compress_size for i in selected)}), flush=True)
if args.list:
for item in selected:
print(item.filename, item.file_size)
return
destination = root / "air_sdk"
identity = {"version": SDK_VERSION, "source": BASE + link, "files": {}}
for item in selected:
path = (destination / item.filename).resolve()
if not path.is_relative_to(destination.resolve()):
raise ValueError("SDK path escapes local destination")
path.parent.mkdir(parents=True, exist_ok=True)
existing = path.read_bytes() if path.is_file() else b""
data = existing if len(existing) == item.file_size and zlib.crc32(existing) == item.CRC else archive.read(item)
path.write_bytes(data)
identity["files"][item.filename] = hashlib.sha256(data).hexdigest()
print(item.filename, len(data), flush=True)
(acquisition / "sdk_identity.json").write_text(json.dumps(identity, indent=2), encoding="utf-8")
if __name__ == "__main__":
main()