Skip to content

Commit

Permalink
fix: 修复连接失败后启动模拟器发生错误的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
SweetSmellFox committed Jan 25, 2025
1 parent 3159ac8 commit c8a5321
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 31 deletions.
11 changes: 1 addition & 10 deletions Custom/CustomPasswordBox.cs → Controls/CustomPasswordBox.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
using HandyControl.Interactivity;
using MFAWPF.Utils;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using PasswordBox = HandyControl.Controls.PasswordBox;

namespace MFAWPF.Custom;
namespace MFAWPF.Controls;

[TemplatePart(Name = ElementPasswordBox, Type = typeof(System.Windows.Controls.PasswordBox))]
[TemplatePart(Name = ElementTextBox, Type = typeof(TextBox))]
Expand Down
2 changes: 2 additions & 0 deletions MFAWPF.sln.DotSettings.user
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
<s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=Miscellaneous_0020Files_002FDyroseb/@EntryIndexedValue">False</s:Boolean>
<s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=Miscellaneous_0020Files_002FDyroseb/@EntryIndexRemoved">True</s:Boolean>
<s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=Miscellaneous_0020Files_002FFeruber/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=Miscellaneous_0020Files_002FFykyvoc/@EntryIndexedValue">False</s:Boolean>




Expand Down
3 changes: 3 additions & 0 deletions Res/Localization/Strings.en-us.resx
Original file line number Diff line number Diff line change
Expand Up @@ -764,4 +764,7 @@
<data name="MinimizeToTray" xml:space="preserve">
<value>Hide to tray when minimized</value>
</data>
<data name="HardRestartAdb" xml:space="preserve">
<value>Trying to kill and restart the ADB process</value>
</data>
</root>
3 changes: 3 additions & 0 deletions Res/Localization/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -771,4 +771,7 @@
<data name="MinimizeToTray" xml:space="preserve">
<value>最小化时隐藏至托盘</value>
</data>
<data name="HardRestartAdb" xml:space="preserve">
<value>正在尝试关闭并重启 ADB 进程</value>
</data>
</root>
3 changes: 3 additions & 0 deletions Res/Localization/Strings.zh-hant.resx
Original file line number Diff line number Diff line change
Expand Up @@ -764,4 +764,7 @@
<data name="MinimizeToTray" xml:space="preserve">
<value>最小化時隱藏至工作列</value>
</data>
<data name="HardRestartAdb" xml:space="preserve">
<value>正在嘗試重開 ADB 程式</value>
</data>
</root>
37 changes: 25 additions & 12 deletions Utils/MaaProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,14 @@ public void Start(List<DragItemViewModel>? tasks, bool onlyStart = false)
Stop();
return;
}
MainWindow.Instance.AutoDetectDevice();

instance = Task.Run(GetCurrentTasker, token);
instance.Wait(token);
connected = instance.Result is { Initialized: true };
}


if (!connected && MainWindow.Data.IsAdb)
{
MainWindow.AddLog("ConnectFailed".GetLocalizationString() + "\n" + "TryToReconnectByAdb".GetLocalizationString());
Expand Down Expand Up @@ -187,16 +191,17 @@ public void Start(List<DragItemViewModel>? tasks, bool onlyStart = false)
instance.Wait(token);
connected = instance.Result is { Initialized: true };
}

if (!connected)
{
LoggerService.LogWarning("ConnectFailed".GetLocalizationString());
MainWindow.AddLogByKey("ConnectFailed");
MainWindow.Instance.SetConnected(false);
MainWindow.Instance.deviceComboBox.ItemsSource = new List<string>();
Stop();
throw new Exception();
}

if (connected) MainWindow.Instance.SetConnected(true);

if (!MainWindow.Instance.IsConnected())
{
Growls.Warning("Warning_CannotConnect".GetLocalizationString()
Expand Down Expand Up @@ -234,7 +239,7 @@ public void Start(List<DragItemViewModel>? tasks, bool onlyStart = false)
{
Name = "结束",
Type = MFATask.MFATaskType.MFA,
Action = () => { MainWindow.Instance?.RunScript("Post-script"); }
Action = () => { MainWindow.Instance.RunScript("Post-script"); }
});

TaskManager.RunTaskAsync(async () =>
Expand Down Expand Up @@ -330,7 +335,7 @@ async private static Task<bool> DingTalkMessageAsync(string accessToken, string
}


public async static Task ExternalNotificationAsync()
public async static Task ExternalNotificationAsync()
{
var enabledProviders = MainWindow.Data.EnabledExternalNotificationProviderList;

Expand Down Expand Up @@ -389,20 +394,32 @@ private void StartRunnableFile(string exePath, double waitTimeInSeconds, Cancell
if (string.IsNullOrWhiteSpace(exePath) || !File.Exists(exePath))
return;
var processName = Path.GetFileNameWithoutExtension(exePath);
var startInfo = new ProcessStartInfo
{
FileName = exePath,
UseShellExecute = true,
CreateNoWindow = false
};
if (Process.GetProcessesByName(processName).Length == 0)
{
if (!string.IsNullOrWhiteSpace(DataSet.GetData("EmulatorConfig", string.Empty)))
{
startInfo.Arguments = DataSet.GetData("EmulatorConfig", string.Empty);
_softwareProcess =
Process.Start(exePath, DataSet.GetData("EmulatorConfig", string.Empty) ?? string.Empty);
Process.Start(startInfo);
}
else
_softwareProcess = Process.Start(exePath);
_softwareProcess = Process.Start(startInfo);
}
else
{
if (!string.IsNullOrWhiteSpace(DataSet.GetData("EmulatorConfig", string.Empty)))
Process.Start(exePath, DataSet.GetData("EmulatorConfig", string.Empty) ?? string.Empty);
{
startInfo.Arguments = DataSet.GetData("EmulatorConfig", string.Empty);
_softwareProcess = Process.Start(startInfo);
}
else
Process.Start(exePath);
_softwareProcess = Process.Start(startInfo);
}

for (double remainingTime = waitTimeInSeconds + 1; remainingTime > 0; remainingTime -= 1)
Expand Down Expand Up @@ -1214,10 +1231,6 @@ private void HandleInitializationError(Exception e,
string waringMessage = "")
{
Console.WriteLine(e);
TaskQueue.Clear();
OnTaskQueueChanged();
if (MainWindow.Data != null)
MainWindow.Data.Idle = true;
Growls.Error(message);
if (hasWarning)
LoggerService.LogWarning(waringMessage);
Expand Down
14 changes: 7 additions & 7 deletions Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ public static int ExtractNumberFromEmulatorConfig(string emulatorConfig)
return 0;
}

public async void AutoDetectDevice()
public void AutoDetectDevice()
{
try
{
Expand All @@ -543,8 +543,8 @@ public async void AutoDetectDevice()
SetConnected(false);
if ((Data?.IsAdb).IsTrue())
{
var devices = await _maaToolkit.AdbDevice.FindAsync();
deviceComboBox.ItemsSource = devices;
var devices = _maaToolkit.AdbDevice.Find();
Growls.Process(() => deviceComboBox.ItemsSource = devices);
SetConnected(devices.Count > 0);
var emulatorConfig = DataSet.GetData("EmulatorConfig", string.Empty);
var resultIndex = 0;
Expand All @@ -567,7 +567,7 @@ public async void AutoDetectDevice()
}
else
resultIndex = 0;
deviceComboBox.SelectedIndex = resultIndex;
Growls.Process(() => deviceComboBox.SelectedIndex = resultIndex);
if (MaaInterface.Instance?.Controller != null)
{
if (MaaInterface.Instance.Controller.Any(controller => controller.Type != null && controller.Type.ToLower().Equals("adb")))
Expand Down Expand Up @@ -628,7 +628,7 @@ public async void AutoDetectDevice()
else
{
var windows = _maaToolkit.Desktop.Window.Find().ToList();
deviceComboBox.ItemsSource = windows;
Growls.Process(() =>deviceComboBox.ItemsSource = windows);
SetConnected(windows.Count > 0);
var resultIndex = windows.Count > 0
? windows.ToList().FindIndex(win => !string.IsNullOrWhiteSpace(win.Name))
Expand Down Expand Up @@ -694,7 +694,7 @@ public async void AutoDetectDevice()
}
}
}
deviceComboBox.SelectedIndex = resultIndex;
Growls.Process(() => deviceComboBox.SelectedIndex = resultIndex);
}

if (!IsConnected())
Expand Down Expand Up @@ -1989,7 +1989,7 @@ private static bool ExecuteScript(string? scriptPath)

private void InitializationSettings()
{
settingsView.MinimizeToTrayCheckBox.IsChecked = DataSet.GetData("ShouldMinimizeToTray", true);
settingsView.MinimizeToTrayCheckBox.IsChecked = DataSet.GetData("ShouldMinimizeToTray", true);
settingsView.MinimizeToTrayCheckBox.Checked += (_, _) => { DataSet.SetData("ShouldMinimizeToTray", true); };
settingsView.MinimizeToTrayCheckBox.Unchecked += (_, _) => { DataSet.SetData("ShouldMinimizeToTray", false); };

Expand Down
4 changes: 2 additions & 2 deletions Views/SettingsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,12 @@
</hc:ComboBox>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="33 2 5 5"
Visibility="{calcBinding:Binding 'DownloadSourceIndex==1'}">
<custom:CustomPasswordBox Margin="20 2 5 10" x:Name="CdkPassword"
<controls:CustomPasswordBox Margin="20 2 5 10" x:Name="CdkPassword"
IsEnabled="{calcBinding:Binding 'Idle'}" ShowEyeButton="True"
hc:TitleElement.HorizontalAlignment="Left"
hc:TitleElement.Title="CDK"
Width="200">
</custom:CustomPasswordBox>
</controls:CustomPasswordBox>
<TextBlock Margin="2 12 5 -2" VerticalAlignment="Center">
<Hyperlink NavigateUri="{Binding Source={x:Static utils:MFAUrls.PurchaseLink}}"
RequestNavigate="Hyperlink_OnClick">
Expand Down

0 comments on commit c8a5321

Please sign in to comment.