Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 改用 MuMuManager 来获取 mumu 模拟器的地址 #462

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions source/MaaToolkit/AdbDevice/AdbDeviceFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,7 @@ std::vector<AdbDevice> AdbDeviceFinder::find() const
auto all_emulators = find_emulators();
for (const Emulator& e : all_emulators) {
std::filesystem::path adb_path = get_adb_path(e.const_data, e.process.pid);

std::vector<std::string> serials = e.const_data.adb_common_serials;

auto requested = request_adb_serials(adb_path);

serials.insert(serials.end(), std::make_move_iterator(requested.begin()), std::make_move_iterator(requested.end()));

// Deduplication
std::sort(serials.begin(), serials.end());
serials.erase(std::unique(serials.begin(), serials.end()), serials.end());

serials = check_available_adb_serials(adb_path, serials);
std::vector<std::string> serials = find_adb_serials(adb_path, e);

for (const std::string& ser : serials) {
AdbDevice device;
Expand Down Expand Up @@ -68,7 +57,7 @@ std::vector<AdbDevice> AdbDeviceFinder::find_specified(const std::filesystem::pa

std::vector<AdbDevice> result;

auto serials = request_adb_serials(adb_path);
auto serials = find_serials_by_adb_command(adb_path);

for (const std::string& ser : serials) {
AdbDevice device;
Expand All @@ -86,6 +75,23 @@ std::vector<AdbDevice> AdbDeviceFinder::find_specified(const std::filesystem::pa
return result;
}

std::vector<std::string> AdbDeviceFinder::find_adb_serials(const std::filesystem::path& adb_path, const Emulator& emulator) const
{
auto serials = emulator.const_data.adb_common_serials;

auto requested = find_serials_by_adb_command(adb_path);

serials.insert(serials.end(), std::make_move_iterator(requested.begin()), std::make_move_iterator(requested.end()));

// Deduplication
std::sort(serials.begin(), serials.end());
serials.erase(std::unique(serials.begin(), serials.end()), serials.end());

serials = check_available_adb_serials(adb_path, serials);

return serials;
}

json::object AdbDeviceFinder::get_adb_config(const Emulator& emulator, const std::string& adb_serial) const
{
std::ignore = emulator;
Expand Down Expand Up @@ -143,7 +149,7 @@ std::filesystem::path AdbDeviceFinder::get_adb_path(const EmulatorConstantData&
return {};
}

std::vector<std::string> AdbDeviceFinder::request_adb_serials(const std::filesystem::path& adb_path) const
std::vector<std::string> AdbDeviceFinder::find_serials_by_adb_command(const std::filesystem::path& adb_path) const
{
LogFunc << VAR(adb_path);

Expand Down Expand Up @@ -202,6 +208,8 @@ bool AdbDeviceFinder::request_adb_connect(const std::filesystem::path& adb_path,
std::vector<std::string>
AdbDeviceFinder::check_available_adb_serials(const std::filesystem::path& adb_path, const std::vector<std::string>& serials) const
{
LogFunc;

std::vector<std::string> available;
for (const std::string& ser : serials) {
if (!request_adb_connect(adb_path, ser)) {
Expand Down
3 changes: 2 additions & 1 deletion source/MaaToolkit/AdbDevice/AdbDeviceFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class AdbDeviceFinder
std::vector<AdbDevice> find_specified(const std::filesystem::path& adb_path) const;

protected:
virtual std::vector<std::string> find_adb_serials(const std::filesystem::path& adb_path, const Emulator& emulator) const;
virtual json::object get_adb_config(const Emulator& emulator, const std::string& adb_serial) const;

protected:
Expand All @@ -47,7 +48,7 @@ class AdbDeviceFinder
std::vector<Emulator> find_emulators() const;
std::filesystem::path get_adb_path(const EmulatorConstantData& emulator, os_pid pid) const;

std::vector<std::string> request_adb_serials(const std::filesystem::path& adb_path) const;
std::vector<std::string> find_serials_by_adb_command(const std::filesystem::path& adb_path) const;
bool request_adb_connect(const std::filesystem::path& adb_path, const std::string& serial) const;
std::vector<std::string>
check_available_adb_serials(const std::filesystem::path& adb_path, const std::vector<std::string>& serials) const;
Expand Down
65 changes: 57 additions & 8 deletions source/MaaToolkit/AdbDevice/AdbDeviceWin32Finder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <unordered_map>

#include "Utils/IOStream/ChildPipeIOStream.h"
#include "Utils/Logger.h"
#include "Utils/StringMisc.hpp"

Expand All @@ -18,9 +19,10 @@ AdbDeviceWin32Finder::AdbDeviceWin32Finder()
.adb_candidate_paths = { "HD-Adb.exe"_path, "Engine\\ProgramFiles\\HD-Adb.exe"_path },
.adb_common_serials = { "127.0.0.1:5555", "127.0.0.1:5556", "127.0.0.1:5565", "127.0.0.1:5575" } } },
{ "LDPlayer",
{ .keyword = "dnplayer",
.adb_candidate_paths = { "adb.exe"_path },
.adb_common_serials = { /*"emulator-5554", "emulator-5556", "127.0.0.1:5555", "127.0.0.1:5556"*/ } } },
{
.keyword = "dnplayer",
.adb_candidate_paths = { "adb.exe"_path },
} },
{ "Nox",
{ .keyword = "Nox",
.adb_candidate_paths = { "nox_adb.exe"_path },
Expand All @@ -34,11 +36,12 @@ AdbDeviceWin32Finder::AdbDeviceWin32Finder()
.adb_common_serials = { "127.0.0.1:7555" } } },

{ "MuMuPlayer12",
{ .keyword = "MuMuPlayer",
.adb_candidate_paths = { "vmonitor\\bin\\adb_server.exe"_path,
"MuMu\\emulator\\nemu\\vmonitor\\bin\\adb_server.exe"_path,
"adb.exe"_path },
.adb_common_serials = { "127.0.0.1:16384", "127.0.0.1:16416", "127.0.0.1:16448" } } },
{
.keyword = "MuMuPlayer.exe",
.adb_candidate_paths = { "vmonitor\\bin\\adb_server.exe"_path,
"MuMu\\emulator\\nemu\\vmonitor\\bin\\adb_server.exe"_path,
"adb.exe"_path },
} },

{ "MEmuPlayer", { .keyword = "MEmu", .adb_candidate_paths = { "adb.exe"_path }, .adb_common_serials = { "127.0.0.1:21503" } } },

Expand All @@ -51,6 +54,19 @@ AdbDeviceWin32Finder::AdbDeviceWin32Finder()
set_emulator_const_data(std::move(emulators));
}

std::vector<std::string> AdbDeviceWin32Finder::find_adb_serials(const std::filesystem::path& adb_path, const Emulator& emulator) const
{
if (emulator.name == "LDPlayer") {
return find_serials_by_adb_command(adb_path);
}
else if (emulator.name == "MuMuPlayer12") {
return find_mumu_serials(adb_path, emulator);
}
else {
return AdbDeviceFinder::find_adb_serials(adb_path, emulator);
}
}

json::object AdbDeviceWin32Finder::get_adb_config(const Emulator& emulator, const std::string& adb_serial) const
{
if (emulator.name == "MuMuPlayer12") {
Expand Down Expand Up @@ -94,6 +110,39 @@ json::object AdbDeviceWin32Finder::get_adb_config(const Emulator& emulator, cons
return {};
}

std::vector<std::string> AdbDeviceWin32Finder::find_mumu_serials(const std::filesystem::path& adb_path, const Emulator& emulator) const
{
std::ignore = emulator;

std::filesystem::path mumu_mgr_path = adb_path.parent_path() / "MuMuManager.exe";
if (!std::filesystem::exists(mumu_mgr_path)) {
LogWarn << "MuMuManager not found" << VAR(mumu_mgr_path);
return find_serials_by_adb_command(adb_path);
}

static const std::vector<std::string> mumu_mgr_args = { "info", "--vmindex", "all" };
ChildPipeIOStream ios(mumu_mgr_path, mumu_mgr_args);
std::string output = ios.read();
LogDebug << VAR(mumu_mgr_path) << VAR(mumu_mgr_args) << VAR(output);

auto jopt = json::parse(output);
if (!jopt) {
LogError << "Parse MuMuManager info failed" << VAR(output);
return {};
}

std::vector<std::string> serials;
for (const auto& [key, obj] : jopt->as_object()) {
auto ip_opt = obj.find<std::string>("adb_host_ip");
auto port_opt = obj.find<int>("adb_port");
if (!ip_opt || !port_opt) {
continue;
}
serials.emplace_back(std::format("{}:{}", *ip_opt, *port_opt));
}
return serials;
}

int AdbDeviceWin32Finder::get_mumu_index(const std::string& adb_serial)
{
auto sp = string_split(adb_serial, ':');
Expand Down
4 changes: 3 additions & 1 deletion source/MaaToolkit/AdbDevice/AdbDeviceWin32Finder.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ class AdbDeviceWin32Finder
virtual ~AdbDeviceWin32Finder() override = default;

protected:
virtual json::object get_adb_config(const Emulator& emulator, const std::string& adb_serial) const;
virtual std::vector<std::string> find_adb_serials(const std::filesystem::path& adb_path, const Emulator& emulator) const override;
virtual json::object get_adb_config(const Emulator& emulator, const std::string& adb_serial) const override;

private:
AdbDeviceWin32Finder();

std::vector<std::string> find_mumu_serials(const std::filesystem::path& adb_path, const Emulator& emulator) const;
static int get_mumu_index(const std::string& adb_serial);
static int get_ld_index(const std::string& adb_serial);
};
Expand Down
Loading