feat: adb 替换失败自动用本地的

This commit is contained in:
MistEO
2023-05-17 23:50:13 +08:00
parent fc75ec535f
commit d18264e7de
4 changed files with 71 additions and 32 deletions

View File

@@ -479,6 +479,8 @@ The video resolution is required to be 16:9, and there are no interference facto
<system:String x:Key="AdbDownloadFailedTitle">ADB Download Failed</system:String>
<system:String x:Key="AdbDownloadFailedDesc">Please do it manually (rename adb.exe and replace the adb file in the emulator)</system:String>
<system:String x:Key="SuccessfullyReplacedADB">Successfully Replaced ADB File</system:String>
<system:String x:Key="FailedToReplaceAdbAndUseLocal">Falied to replace ADB file</system:String>
<system:String x:Key="FailedToReplaceAdbAndUseLocalDesc">Please use the administrator to open MAA, or restart the computer and try again.\nTemporarily connected using local ADB.</system:String>
<system:String x:Key="InitializationError">Initialization error!</system:String>
<system:String x:Key="ResolutionNotSupported">Emulator resolution not supported, please set at least 720P with 16:9 ratio</system:String>
<system:String x:Key="ResolutionAcquisitionFailure">Fails to be obtain emulator resolution. It is recommended to restart the emulator and ADB, or restart the computer, or consider try other emulator</system:String>

View File

@@ -500,6 +500,8 @@
<system:String x:Key="AdbDownloadFailedTitle">ADB 下载失败</system:String>
<system:String x:Key="AdbDownloadFailedDesc">请手动进行操作(将 adb.exe 重命名后替换上方模拟器自带 adb 即可)</system:String>
<system:String x:Key="SuccessfullyReplacedADB">成功替换 ADB 文件</system:String>
<system:String x:Key="FailedToReplaceAdbAndUseLocal">替换 ADB 失败</system:String>
<system:String x:Key="FailedToReplaceAdbAndUseLocalDesc">请使用管理员权限打开 MAA或重启电脑后再试\n已临时使用本地 ADB 进行连接</system:String>
<system:String x:Key="InitializationError">初始化错误!</system:String>
<system:String x:Key="ResolutionNotSupported">模拟器分辨率不支持,请设置为 720p 或更高,且为 16:9 比例</system:String>
<system:String x:Key="ResolutionAcquisitionFailure">模拟器分辨率获取失败,建议重启模拟器与 ADB 或重启电脑,或更换模拟器后再次尝试</system:String>

View File

@@ -1,6 +1,4 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style
x:Key="CustomListBoxItemStyle"
@@ -13,5 +11,4 @@
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>

View File

@@ -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()));