diff --git a/.gitignore b/.gitignore index 18d4b6bb07..bf142b26b3 100644 --- a/.gitignore +++ b/.gitignore @@ -440,3 +440,4 @@ resource/infrast # Nuke .nuke/temp/* +/tools/ItemMappingCvt/Arknights-Bot-Resource diff --git a/MeoAssistantArknights.sln b/MeoAssistantArknights.sln index abf67a215b..96fc04f7fc 100644 --- a/MeoAssistantArknights.sln +++ b/MeoAssistantArknights.sln @@ -70,7 +70,6 @@ Global {C9EA2837-0A4B-488F-A289-643B9D0BFCEB}.Release|x86.ActiveCfg = Release|Win32 {C9EA2837-0A4B-488F-A289-643B9D0BFCEB}.Release|x86.Build.0 = Release|Win32 {C9EA2837-0A4B-488F-A289-643B9D0BFCEB}.RelWithDebInfo|x64.ActiveCfg = Release|x64 - {C9EA2837-0A4B-488F-A289-643B9D0BFCEB}.RelWithDebInfo|x64.Build.0 = Release|x64 {C9EA2837-0A4B-488F-A289-643B9D0BFCEB}.RelWithDebInfo|x86.ActiveCfg = Release|Win32 {C9EA2837-0A4B-488F-A289-643B9D0BFCEB}.RelWithDebInfo|x86.Build.0 = Release|Win32 EndGlobalSection diff --git a/src/MeoAsstGui/Helper/AsstProxy.cs b/src/MeoAsstGui/Helper/AsstProxy.cs index 411d22b535..9bf23303a8 100644 --- a/src/MeoAsstGui/Helper/AsstProxy.cs +++ b/src/MeoAsstGui/Helper/AsstProxy.cs @@ -137,7 +137,8 @@ namespace MeoAsstGui Environment.Exit(0); break; - case AsstMsg.ConnectionError: + case AsstMsg.ConnectionInfo: + procConnectInfo(details); break; case AsstMsg.AllTasksCompleted: @@ -157,6 +158,15 @@ namespace MeoAsstGui } } + private void procConnectInfo(JObject details) + { + if (details["what"].ToString() == "Connected") + { + var svm = _container.Get(); + svm.ConnectAddress = details["details"]["address"].ToString(); + } + } + private void procTaskChainMsg(AsstMsg msg, JObject details) { string taskChain = details["taskchain"].ToString(); @@ -587,18 +597,23 @@ namespace MeoAsstGui } } - public bool AsstConnect() + public bool AsstConnect(ref string error) { var settings = _container.Get(); if (settings.AdbPath == String.Empty || settings.ConnectAddress == String.Empty) { - return false; + if (!settings.RefreshAdbConfig(ref error)) + { + return false; + } } - else + bool ret = AsstConnect(_handle, settings.AdbPath, settings.ConnectAddress, settings.ConnectConfig); + if (!ret) { - return AsstConnect(_handle, settings.AdbPath, settings.ConnectAddress, settings.ConnectConfig); + error = "连接失败\n请检查连接设置"; } + return ret; } private bool AsstAppendTaskWithEncoding(string type, JObject task_params = null) @@ -720,7 +735,7 @@ namespace MeoAsstGui /* Global Info */ InternalError = 0, // 内部错误 InitFailed, // 初始化失败 - ConnectionError, // 连接相关错误 + ConnectionInfo, // 连接相关错误 AllTasksCompleted, // 全部任务完成 /* TaskChain Info */ TaskChainError = 10000, // 任务链执行/识别错误 diff --git a/src/MeoAsstGui/Helper/WinAdapter.cs b/src/MeoAsstGui/Helper/WinAdapter.cs new file mode 100644 index 0000000000..efcd3172dd --- /dev/null +++ b/src/MeoAsstGui/Helper/WinAdapter.cs @@ -0,0 +1,81 @@ +// MeoAsstGui - A part of the MeoAssistantArknights project +// Copyright (C) 2021 MistEO and Contributors +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Diagnostics; +using System.IO; + +namespace MeoAsstGui +{ + public class WinAdapter + { + private static readonly Dictionary emulatorIdDict = new Dictionary { + { "HD-Player", "BlueStacks"}, + { "LdVBoxHeadless", "LDPlayer"}, + { "NoxVMHandle", "Nox"}, + { "NemuHeadless", "MuMuEmulator"}, + { "MEmuHeadless", "XYAZ"} + }; + + private static readonly Dictionary> adbRelativePathDict = new Dictionary> { + { "BlueStacks", new List { + ".\\HD-Adb.exe", + ".\\Engine\\ProgramFiles\\HD-Adb.exe" + } }, + { "LDPlayer", new List { ".\\adb.exe" } }, + { "Nox", new List { ".\\nox_adb.exe" } }, + { "MuMuEmulator", new List { + "..\\vmonitor\\bin\\adb_server.exe", + "..\\..\\MuMu\\emulator\\nemu\\vmonitor\\bin\\adb_server.exe" + } }, + { "XYAZ", new List { ".\\adb.exe"} } + }; + + private Dictionary adbAbsoultePathDict = new Dictionary(); + + public List RefreshEmulatorsInfo() + { + var allProcess = Process.GetProcesses(); + var emulators = new List(); + foreach (var process in allProcess) + { + if (emulatorIdDict.Keys.Contains(process.ProcessName)) + { + var emulatorId = emulatorIdDict[process.ProcessName]; + emulators.Add(emulatorId); + var processPath = process.MainModule.FileName; + foreach (var path in adbRelativePathDict[emulatorId]) + { + var adbPath = Path.GetDirectoryName(processPath) + "\\" + path; + if (File.Exists(adbPath)) + { + adbAbsoultePathDict.Add(emulatorId, adbPath); + } + } + } + } + return emulators; + } + + public string GetAdbPathByEmulatorName(string emulatorName) + { + if (adbAbsoultePathDict.Keys.Contains(emulatorName)) + { + return adbAbsoultePathDict[emulatorName]; + } + return null; + } + } +} diff --git a/src/MeoAsstGui/MeoAsstGui.csproj b/src/MeoAsstGui/MeoAsstGui.csproj index 66fe563800..4a912c1437 100644 --- a/src/MeoAsstGui/MeoAsstGui.csproj +++ b/src/MeoAsstGui/MeoAsstGui.csproj @@ -100,6 +100,7 @@ + AutoRecruitSettingsUserControl.xaml diff --git a/src/MeoAsstGui/ViewModels/CopilotViewModel.cs b/src/MeoAsstGui/ViewModels/CopilotViewModel.cs index 784380d636..df0159ddf9 100644 --- a/src/MeoAsstGui/ViewModels/CopilotViewModel.cs +++ b/src/MeoAsstGui/ViewModels/CopilotViewModel.cs @@ -98,17 +98,18 @@ namespace MeoAsstGui AddLog("正在连接模拟器……"); var asstProxy = _container.Get(); + string errMsg = ""; if (!_catched) { var task = Task.Run(() => { - return asstProxy.AsstConnect(); + return asstProxy.AsstConnect(ref errMsg); }); _catched = await task; } if (!_catched) { - AddLog("连接模拟器失败\n请检查连接设置", "darkred"); + AddLog(errMsg, "darkred"); return; } if (Filename.Length == 0 || !File.Exists(Filename)) diff --git a/src/MeoAsstGui/ViewModels/RecruitViewModel.cs b/src/MeoAsstGui/ViewModels/RecruitViewModel.cs index 84c07dd419..97afee9210 100644 --- a/src/MeoAsstGui/ViewModels/RecruitViewModel.cs +++ b/src/MeoAsstGui/ViewModels/RecruitViewModel.cs @@ -115,18 +115,19 @@ namespace MeoAsstGui public async void StartCalc() { var asstProxy = _container.Get(); + string errMsg = ""; if (!_catched) { RecruitInfo = "正在连接模拟器……"; var task = Task.Run(() => { - return asstProxy.AsstConnect(); + return asstProxy.AsstConnect(ref errMsg); }); _catched = await task; } if (!_catched) { - RecruitInfo = "连接模拟器失败,请检查连接设置"; + RecruitInfo = errMsg; return; } RecruitInfo = "正在识别……"; diff --git a/src/MeoAsstGui/ViewModels/SettingsViewModel.cs b/src/MeoAsstGui/ViewModels/SettingsViewModel.cs index 8b00892244..a700397f28 100644 --- a/src/MeoAsstGui/ViewModels/SettingsViewModel.cs +++ b/src/MeoAsstGui/ViewModels/SettingsViewModel.cs @@ -131,11 +131,6 @@ namespace MeoAsstGui }; ConnectAddressList = new ObservableCollection(); - - if (ConnectAddress.Length == 0) - { - UpdateAddressByConfig(); - } } private bool _idle = true; @@ -891,7 +886,7 @@ namespace MeoAsstGui { "WSA", new List { "127.0.0.1:58526" } }, }; - public void UpdateAddressByConfig() + private void UpdateAddressByConfig() { var addresses = ConfigAddressesMapping[ConnectConfig]; ConnectAddress = addresses.FirstOrDefault(); @@ -902,6 +897,26 @@ namespace MeoAsstGui //} } + public bool RefreshAdbConfig(ref string error) + { + var adapter = new WinAdapter(); + var emulators = adapter.RefreshEmulatorsInfo(); + if (emulators.Count == 0) + { + error = "未检测到任何模拟器\n请尝试使用管理员权限打开本软件\n或手动设置连接"; + return false; + } + else if (emulators.Count > 1) + { + error = "检测到多个模拟器\n请关闭不需要的模拟器\n或手动设置连接"; + return false; + } + ConnectConfig = emulators.First(); + AdbPath = adapter.GetAdbPathByEmulatorName(ConnectConfig) ?? AdbPath; + UpdateAddressByConfig(); + return true; + } + public void SelectFile() { var dialog = new Microsoft.Win32.OpenFileDialog(); diff --git a/src/MeoAsstGui/ViewModels/TaskQueueViewModel.cs b/src/MeoAsstGui/ViewModels/TaskQueueViewModel.cs index a3aebc6681..03f76696d0 100644 --- a/src/MeoAsstGui/ViewModels/TaskQueueViewModel.cs +++ b/src/MeoAsstGui/ViewModels/TaskQueueViewModel.cs @@ -80,7 +80,7 @@ namespace MeoAsstGui //new CombData { Display = "SN-8", Value = "SN-8" }, //new CombData { Display = "SN-9", Value = "SN-9" }, //new CombData { Display = "SN-10", Value = "SN-10" }, - + //// “风雪过境” 活动关卡 //new CombData { Display = "BI-7", Value = "BI-7" }, //new CombData { Display = "BI-8", Value = "BI-8" } @@ -139,14 +139,15 @@ namespace MeoAsstGui AddLog("正在连接模拟器……"); var asstProxy = _container.Get(); + string errMsg = ""; var task = Task.Run(() => { - return asstProxy.AsstConnect(); + return asstProxy.AsstConnect(ref errMsg); }); bool catchd = await task; if (!catchd) { - AddLog("连接模拟器失败\n请检查连接设置", "darkred"); + AddLog(errMsg, "darkred"); Idle = true; return; }