Skip to content

Commit

Permalink
- Run the program with the -f option to specify a fixed version
Browse files Browse the repository at this point in the history
- Change apk download to xapk download
  • Loading branch information
starik222 committed Aug 4, 2024
1 parent bf7a53f commit aa1207e
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 31 deletions.
26 changes: 23 additions & 3 deletions BAdownload/APK.cs
Original file line number Diff line number Diff line change
@@ -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));
Expand All @@ -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;
Expand All @@ -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);
}
}
Expand Down
48 changes: 41 additions & 7 deletions BAdownload/APKzip.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using BAdownload;
using System;
using System.IO;
using System.IO.Compression;
Expand All @@ -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<bool> 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

Expand All @@ -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);
}
}

15 changes: 15 additions & 0 deletions BAdownload/GlobalData.cs
Original file line number Diff line number Diff line change
@@ -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 = "";
}
}
3 changes: 2 additions & 1 deletion BAdownload/ver.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net.Http;
using System.Threading.Tasks;
using BAdownload;
using Newtonsoft.Json.Linq;

class ver
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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](<https://github.com/fiseleo/BlueArchiveDownloaderJP/tree/main> "Title")

Expand Down
46 changes: 26 additions & 20 deletions python/download_apk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}") # 调试日志

Expand All @@ -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}")

Expand Down

0 comments on commit aa1207e

Please sign in to comment.