From d18264e7de1bb50affa6a91d426a602b8ff0f649 Mon Sep 17 00:00:00 2001 From: MistEO Date: Wed, 17 May 2023 23:50:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20adb=20=E6=9B=BF=E6=8D=A2=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E8=87=AA=E5=8A=A8=E7=94=A8=E6=9C=AC=E5=9C=B0=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MaaWpfGui/Res/Localizations/en-us.xaml | 2 + src/MaaWpfGui/Res/Localizations/zh-cn.xaml | 2 + src/MaaWpfGui/Res/Styles/ListBox.xaml | 5 +- .../ViewModels/UI/SettingsViewModel.cs | 94 +++++++++++++------ 4 files changed, 71 insertions(+), 32 deletions(-) diff --git a/src/MaaWpfGui/Res/Localizations/en-us.xaml b/src/MaaWpfGui/Res/Localizations/en-us.xaml index a470f7b644..95ca292f53 100644 --- a/src/MaaWpfGui/Res/Localizations/en-us.xaml +++ b/src/MaaWpfGui/Res/Localizations/en-us.xaml @@ -479,6 +479,8 @@ The video resolution is required to be 16:9, and there are no interference facto ADB Download Failed Please do it manually (rename adb.exe and replace the adb file in the emulator) Successfully Replaced ADB File + Falied to replace ADB file + Please use the administrator to open MAA, or restart the computer and try again.\nTemporarily connected using local ADB. Initialization error! Emulator resolution not supported, please set at least 720P with 16:9 ratio Fails to be obtain emulator resolution. It is recommended to restart the emulator and ADB, or restart the computer, or consider try other emulator diff --git a/src/MaaWpfGui/Res/Localizations/zh-cn.xaml b/src/MaaWpfGui/Res/Localizations/zh-cn.xaml index eab4817ca6..d18496e25f 100644 --- a/src/MaaWpfGui/Res/Localizations/zh-cn.xaml +++ b/src/MaaWpfGui/Res/Localizations/zh-cn.xaml @@ -500,6 +500,8 @@ ADB 下载失败 请手动进行操作(将 adb.exe 重命名后替换上方模拟器自带 adb 即可) 成功替换 ADB 文件 + 替换 ADB 失败 + 请使用管理员权限打开 MAA,或重启电脑后再试\n已临时使用本地 ADB 进行连接 初始化错误! 模拟器分辨率不支持,请设置为 720p 或更高,且为 16:9 比例 模拟器分辨率获取失败,建议重启模拟器与 ADB 或重启电脑,或更换模拟器后再次尝试 diff --git a/src/MaaWpfGui/Res/Styles/ListBox.xaml b/src/MaaWpfGui/Res/Styles/ListBox.xaml index eec0822757..c4962cfa59 100644 --- a/src/MaaWpfGui/Res/Styles/ListBox.xaml +++ b/src/MaaWpfGui/Res/Styles/ListBox.xaml @@ -1,6 +1,4 @@ - + - diff --git a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs index caedb657c2..ba180d1d70 100644 --- a/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/SettingsViewModel.cs @@ -2319,7 +2319,8 @@ namespace MaaWpfGui.ViewModels.UI } private static readonly string GoogleAdbDownloadUrl = "https://dl.google.com/android/repository/platform-tools-latest-windows.zip"; - private static readonly string GoogleAdbFilename = "adb.zip"; + private static readonly string AdbMaaMirrorDownloadUrl = "https://ota.maa.plus/MaaAssistantArknights/api/binaries/adb-windows.zip"; + private static readonly string GoogleAdbFilename = "adb-windows.zip"; public async void ReplaceADB() { @@ -2336,6 +2337,12 @@ namespace MaaWpfGui.ViewModels.UI if (!File.Exists(GoogleAdbFilename)) { var downloadResult = await Instances.HttpService.DownloadFileAsync(new Uri(GoogleAdbDownloadUrl), GoogleAdbFilename); + + if (!downloadResult) + { + downloadResult = await Instances.HttpService.DownloadFileAsync(new Uri(AdbMaaMirrorDownloadUrl), GoogleAdbFilename); + } + if (!downloadResult) { await Execute.OnUIThreadAsync(() => @@ -2347,35 +2354,66 @@ namespace MaaWpfGui.ViewModels.UI } } - var procTask = Task.Run(() => - { - // ErrorView.xaml.cs里有个报错的逻辑,这里如果改的话那边也要对应改一下 - foreach (var process in Process.GetProcessesByName(Path.GetFileName(AdbPath))) - { - process.Kill(); - } + const string UnzipDir = "adb"; + const string NewAdb = UnzipDir + "/platform-tools/adb.exe"; - File.Copy(AdbPath, AdbPath + ".bak", true); - - const string UnzipDir = "adb_unzip"; - if (Directory.Exists(UnzipDir)) - { - Directory.Delete(UnzipDir, true); - } - - ZipFile.ExtractToDirectory(GoogleAdbFilename, UnzipDir); - File.Copy(UnzipDir + "/platform-tools/adb.exe", AdbPath, true); - Directory.Delete(UnzipDir, true); - }); - await procTask; - - AdbReplaced = true; - ConfigurationHelper.SetValue(ConfigurationKeys.AdbReplaced, true.ToString()); - Execute.OnUIThread(() => + void unzip_adb() { - using var toast = new ToastNotification(LocalizationHelper.GetString("SuccessfullyReplacedADB")); - toast.Show(); - }); + if (Directory.Exists(UnzipDir)) + { + Directory.Delete(UnzipDir, true); + } + + ZipFile.ExtractToDirectory(GoogleAdbFilename, UnzipDir); + } + + unzip_adb(); + + bool replaced = false; + try + { + // ErrorView.xaml.cs里有个报错的逻辑,这里如果改的话那边也要对应改一下 + foreach (var process in Process.GetProcessesByName(Path.GetFileName(AdbPath))) + { + process.Kill(); + } + + File.Copy(AdbPath, AdbPath + ".bak", true); + File.Copy(NewAdb, AdbPath, true); + Directory.Delete(UnzipDir, true); + replaced = true; + } + catch (Exception ex) + { + _logger.Error(ex.ToString()); + replaced = false; + } + + if (replaced) + { + AdbReplaced = true; + ConfigurationHelper.SetValue(ConfigurationKeys.AdbReplaced, true.ToString()); + await Execute.OnUIThreadAsync(() => + { + using var toast = new ToastNotification(LocalizationHelper.GetString("SuccessfullyReplacedADB")); + toast.Show(); + }); + } + else + { + if (!File.Exists(NewAdb)) + { + unzip_adb(); + } + + AdbPath = UnzipDir + "/platform-tools/adb.exe"; + + await Execute.OnUIThreadAsync(() => + { + using var toast = new ToastNotification(LocalizationHelper.GetString("FailedToReplaceAdbAndUseLocal")); + toast.AppendContentText(LocalizationHelper.GetString("FailedToReplaceAdbAndUseLocalDesc")).Show(); + }); + } } public bool AdbReplaced { get; set; } = Convert.ToBoolean(ConfigurationHelper.GetValue(ConfigurationKeys.AdbReplaced, false.ToString()));