From aa1207e005a36cea20598c4a6457a4f369a7c63f Mon Sep 17 00:00:00 2001 From: starik222 Date: Mon, 5 Aug 2024 04:47:30 +0900 Subject: [PATCH] - Run the program with the -f option to specify a fixed version - Change apk download to xapk download --- BAdownload/APK.cs | 26 +++++++++++++++++++--- BAdownload/APKzip.cs | 48 ++++++++++++++++++++++++++++++++++------ BAdownload/GlobalData.cs | 15 +++++++++++++ BAdownload/ver.cs | 3 ++- README.md | 2 ++ python/download_apk.py | 46 +++++++++++++++++++++----------------- 6 files changed, 109 insertions(+), 31 deletions(-) create mode 100644 BAdownload/GlobalData.cs diff --git a/BAdownload/APK.cs b/BAdownload/APK.cs index e5c1231..47dd7f2 100644 --- a/BAdownload/APK.cs +++ b/BAdownload/APK.cs @@ -1,14 +1,26 @@ +using BAdownload; using System; using System.Diagnostics; using System.IO; +using System.Text.RegularExpressions; class APK { public static void Main(string[] args) { - - string pythonInterpreterRelativePath = @"python\python.exe"; - string pythonScriptRelativePath = @"python\download_apk.py"; + Console.WriteLine("To specify a specific version of the game, use the \"-f\" argument."); + Console.WriteLine("For example:"); + Console.WriteLine("BAdownload.exe -f 1.987654321"); + if (args.Length >= 2) + { + if (args[0] == "-f") + { + GlobalData.IsForcedVersion = true; + GlobalData.ForcedVersion = args[1]; + } + } + string pythonInterpreterRelativePath = @"python\python.exe"; + string pythonScriptRelativePath = @"python\download_apk.py"; string currentDirectory = Environment.CurrentDirectory; string fullPathToPythonInterpreter = Path.GetFullPath(Path.Combine(currentDirectory, pythonInterpreterRelativePath)); string fullPathToPythonScript = Path.GetFullPath(Path.Combine(currentDirectory, pythonScriptRelativePath)); @@ -29,6 +41,8 @@ public static void Main(string[] args) ProcessStartInfo start = new ProcessStartInfo(); start.FileName = fullPathToPythonInterpreter; start.Arguments = fullPathToPythonScript; + if (GlobalData.IsForcedVersion) + start.Arguments += $" -f {GlobalData.ForcedVersion}"; start.WorkingDirectory = Path.GetDirectoryName(fullPathToPythonInterpreter); start.UseShellExecute = false; start.RedirectStandardOutput = true; @@ -40,6 +54,12 @@ public static void Main(string[] args) using (StreamReader reader = process.StandardOutput) { string result = reader.ReadToEnd(); + Regex r = new Regex("APK file: (.*).+?\r?$", RegexOptions.Compiled); + Match match = r.Match(result); + if (match.Success) + { + GlobalData.XapkFile = Path.Combine(currentDirectory, "python", match.Groups[1].Value); + } Console.WriteLine(result); } } diff --git a/BAdownload/APKzip.cs b/BAdownload/APKzip.cs index a747599..428c447 100644 --- a/BAdownload/APKzip.cs +++ b/BAdownload/APKzip.cs @@ -1,3 +1,4 @@ +using BAdownload; using System; using System.IO; using System.IO.Compression; @@ -6,24 +7,58 @@ class APKzip { public static async void zipMain(string[] args) { - string downloadedApkRelativePath = @"python\APK\com.YostarJP.BlueArchive.apk"; + if (string.IsNullOrEmpty(GlobalData.XapkFile)) + { + Console.WriteLine($"Error: APK file not found, the program will be closed"); + } + //string downloadedApkRelativePath = @"python\APK\com.YostarJP.BlueArchive.apk"; string extractionRelativePath = @"python\APK\unzip"; try { string currentDirectory = Environment.CurrentDirectory; - string downloadedApkFilePath = Path.Combine(currentDirectory, downloadedApkRelativePath); string extractionPath = Path.Combine(currentDirectory, extractionRelativePath); + if (await UnpackZip(GlobalData.XapkFile, extractionPath)) + { + foreach (var apkFile in Directory.GetFiles(extractionPath, "*.apk", SearchOption.TopDirectoryOnly)) + { + if (!await UnpackZip(apkFile, extractionPath)) + { + Console.WriteLine($"Error unpacking apk file: {apkFile}"); + return; + } + } + } + else + { + Console.WriteLine("Error unpacking xapk file"); + return; + } + + + + Console.WriteLine("APK extracted successfully!"); + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + await APKver.verMain(args); + } + private static async Task UnpackZip(string zipFile, string extractionPath) + { + try + { Directory.CreateDirectory(extractionPath); - using (ZipArchive archive = ZipFile.OpenRead(downloadedApkFilePath)) + using (ZipArchive archive = ZipFile.OpenRead(zipFile)) { foreach (ZipArchiveEntry entry in archive.Entries) { string entryFullName = Path.Combine(extractionPath, entry.FullName); string entryDirectory = Path.GetDirectoryName(entryFullName); - + if (string.IsNullOrEmpty(entryDirectory)) continue; // Skip if the entry is for directory @@ -39,14 +74,13 @@ public static async void zipMain(string[] args) entry.ExtractToFile(entryFullName); } } - - Console.WriteLine("APK extracted successfully!"); + return true; } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); + return false; } - await APKver.verMain(args); } } diff --git a/BAdownload/GlobalData.cs b/BAdownload/GlobalData.cs new file mode 100644 index 0000000..98815a7 --- /dev/null +++ b/BAdownload/GlobalData.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BAdownload +{ + public static class GlobalData + { + public static bool IsForcedVersion = false; + public static string ForcedVersion = ""; + public static string XapkFile = ""; + } +} diff --git a/BAdownload/ver.cs b/BAdownload/ver.cs index f21fbe4..1bbbce7 100644 --- a/BAdownload/ver.cs +++ b/BAdownload/ver.cs @@ -1,5 +1,6 @@ using System.Net.Http; using System.Threading.Tasks; +using BAdownload; using Newtonsoft.Json.Linq; class ver @@ -37,7 +38,7 @@ public static async Task verMain(string[] args) if (File.Exists(apkVersionPath)) { string apkVersion = File.ReadAllText(apkVersionPath); - if (apkVersion == latestClientVersion) + if (GlobalData.IsForcedVersion || apkVersion == latestClientVersion) { Console.WriteLine("APK version matches the online version. Starting download..."); Program.ProgremMain(args); diff --git a/README.md b/README.md index a5a656f..f1f7c74 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ A small project that downloads all assets of the Japan-Server version of Blue Archive . The script updates the assets and even its own parameters on its own, so all you have to do is execute the BAdownloader.exe after every update to get the latest files. +To specify a specific version of the apk file, run the program with the -f parameter, for example: `BAdownload.exe -f 1.456789` + [Releases]( "Title") diff --git a/python/download_apk.py b/python/download_apk.py index 8e9b83e..d08b551 100644 --- a/python/download_apk.py +++ b/python/download_apk.py @@ -7,12 +7,13 @@ from url import get_jp_version from lib.env import ENV from lib.filepath import FP_JP_APK +import argparse def download_jp_apk_with_ver(version: str): package = FP_JP_APK.stem code = version.split(".")[-1] q = {"versionCode": code, "nc": "arm64-v8a", "sv": 24} - url = f"https://d.apkpure.com/b/APK/{package}?{urlencode(q)}" + url = f"https://d.apkpure.com/b/XAPK/{package}?{urlencode(q)}" print(f"Constructed URL: {url}") # 调试日志 @@ -26,29 +27,34 @@ def download_jp_apk_with_ver(version: str): apk_folder = Path("APK") apk_folder.mkdir(exist_ok=True) # 创建APK文件夹,如果不存在 - apk_path = apk_folder / FP_JP_APK.name # 将FP_JP_APK文件名附加到APK文件夹路径 - - if ENV.DISABLE_TQDM: - with open(apk_path, "wb") as f: - for chunk in response.iter_content(chunk_size=None): - if chunk: - f.write(chunk) - else: - with tqdm.wrapattr( # type: ignore - response.raw, - "read", - desc=f"Download {package} v{version}", - total=int(response.headers.get("content-length", 0)), - ) as r_raw: + apk_path = apk_folder / (FP_JP_APK.name + "." + version + ".xapk") # 将FP_JP_APK文件名附加到APK文件夹路径 + if not apk_path.exists(): + if ENV.DISABLE_TQDM: with open(apk_path, "wb") as f: - shutil.copyfileobj(r_raw, f) - + for chunk in response.iter_content(chunk_size=None): + if chunk: + f.write(chunk) + else: + with tqdm.wrapattr( # type: ignore + response.raw, + "read", + desc=f"Download {package} v{version}", + total=int(response.headers.get("content-length", 0)), + ) as r_raw: + with open(apk_path, "wb") as f: + shutil.copyfileobj(r_raw, f) + print(f"APK file: {apk_path}") return apk_path # 返回下载的apk路径 if __name__ == "__main__": - # 获取新版本号 - new_ver = get_jp_version() - + parser = argparse.ArgumentParser() + parser.add_argument("-f", dest="forcedVer", required=False) + args = parser.parse_args() + if args.forcedVer is None: + # 获取新版本号 + new_ver = get_jp_version() + else: + new_ver = args.forcedVer # 调试日志,显示获取的新版本号 print(f"New version: {new_ver}")