Skip to content

Commit

Permalink
feat: 整体接口搭建
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Dec 5, 2024
1 parent e8b94a3 commit a08a0ea
Show file tree
Hide file tree
Showing 33 changed files with 369 additions and 43 deletions.
3 changes: 3 additions & 0 deletions source/MaaAdbControlUnit/Base/UnitBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,16 @@ class InputBase
, public ControlUnitSink
{
public:
using SwipeParam = ControlUnitAPI::SwipeParam;

virtual ~InputBase() override = default;

virtual bool init() = 0;

public:
virtual bool click(int x, int y) = 0;
virtual bool swipe(int x1, int y1, int x2, int y2, int duration) = 0;
virtual bool multi_swipe(const std::vector<SwipeParam>& swipes) = 0;

virtual bool touch_down(int contact, int x, int y, int pressure) = 0;
virtual bool touch_move(int contact, int x, int y, int pressure) = 0;
Expand Down
9 changes: 9 additions & 0 deletions source/MaaAdbControlUnit/EmulatorExtras/MuMuPlayerExtras.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ bool MuMuPlayerExtras::swipe(int x1, int y1, int x2, int y2, int duration)
return true;
}

bool MuMuPlayerExtras::multi_swipe(const std::vector<SwipeParam>& swipes)
{
LogInfo << VAR(swipes.size());

// TODO

return false;
}

bool MuMuPlayerExtras::touch_down(int contact, int x, int y, int pressure)
{
if (!input_event_touch_down_func_) {
Expand Down
1 change: 1 addition & 0 deletions source/MaaAdbControlUnit/EmulatorExtras/MuMuPlayerExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class MuMuPlayerExtras
public: // from InputBase
virtual bool click(int x, int y) override;
virtual bool swipe(int x1, int y1, int x2, int y2, int duration) override;
virtual bool multi_swipe(const std::vector<SwipeParam>& swipes) override;

virtual bool touch_down(int contact, int x, int y, int pressure) override;
virtual bool touch_move(int contact, int x, int y, int pressure) override;
Expand Down
6 changes: 6 additions & 0 deletions source/MaaAdbControlUnit/Input/AdbShellInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ bool AdbShellInput::swipe(int x1, int y1, int x2, int y2, int duration)
return output_opt && output_opt->empty();
}

bool AdbShellInput::multi_swipe(const std::vector<SwipeParam>& swipes)
{
LogError << "AdbShellInput not supports" << VAR(swipes.size());
return false;
}

bool AdbShellInput::touch_down(int contact, int x, int y, int pressure)
{
LogError << "AdbShellInput not supports" << VAR(contact) << VAR(x) << VAR(y) << VAR(pressure);
Expand Down
1 change: 1 addition & 0 deletions source/MaaAdbControlUnit/Input/AdbShellInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class AdbShellInput : public InputBase

virtual bool click(int x, int y) override;
virtual bool swipe(int x1, int y1, int x2, int y2, int duration) override;
virtual bool multi_swipe(const std::vector<SwipeParam>& swipes) override;

virtual bool touch_down(int contact, int x, int y, int pressure) override;
virtual bool touch_move(int contact, int x, int y, int pressure) override;
Expand Down
9 changes: 9 additions & 0 deletions source/MaaAdbControlUnit/Input/MtouchHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ bool MtouchHelper::swipe(int x1, int y1, int x2, int y2, int duration)
return ret;
}

bool MtouchHelper::multi_swipe(const std::vector<SwipeParam>& swipes)
{
LogInfo << VAR(swipes.size());

// TODO

return false;
}

bool MtouchHelper::touch_down(int contact, int x, int y, int pressure)
{
if (!pipe_ios_) {
Expand Down
1 change: 1 addition & 0 deletions source/MaaAdbControlUnit/Input/MtouchHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class MtouchHelper : public InputBase
public: // from InputBase
virtual bool click(int x, int y) override;
virtual bool swipe(int x1, int y1, int x2, int y2, int duration) override;
virtual bool multi_swipe(const std::vector<SwipeParam>& swipes) override;

virtual bool touch_down(int contact, int x, int y, int pressure) override;
virtual bool touch_move(int contact, int x, int y, int pressure) override;
Expand Down
10 changes: 10 additions & 0 deletions source/MaaAdbControlUnit/Manager/ControlUnitMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ bool ControlUnitMgr::swipe(int x1, int y1, int x2, int y2, int duration)
return input_->swipe(x1, y1, x2, y2, duration);
}

bool ControlUnitMgr::multi_swipe(const std::vector<SwipeParam>& swipes)
{
if (!input_) {
LogError << "input_ is null";
return false;
}

return input_->multi_swipe(swipes);
}

bool ControlUnitMgr::touch_down(int contact, int x, int y, int pressure)
{
if (!input_) {
Expand Down
1 change: 1 addition & 0 deletions source/MaaAdbControlUnit/Manager/ControlUnitMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ControlUnitMgr

virtual bool click(int x, int y) override;
virtual bool swipe(int x1, int y1, int x2, int y2, int duration) override;
virtual bool multi_swipe(const std::vector<SwipeParam>& swipes) override;

virtual bool touch_down(int contact, int x, int y, int pressure) override;
virtual bool touch_move(int contact, int x, int y, int pressure) override;
Expand Down
10 changes: 10 additions & 0 deletions source/MaaAdbControlUnit/Manager/InputAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ bool InputAgent::swipe(int x1, int y1, int x2, int y2, int duration)
return active_unit_->swipe(x1, y1, x2, y2, duration);
}

bool InputAgent::multi_swipe(const std::vector<SwipeParam>& swipes)
{
if (!active_unit_) {
LogError << "No available input method" << VAR(active_unit_);
return false;
}

return active_unit_->multi_swipe(swipes);
}

bool InputAgent::touch_down(int contact, int x, int y, int pressure)
{
if (!active_unit_) {
Expand Down
1 change: 1 addition & 0 deletions source/MaaAdbControlUnit/Manager/InputAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class InputAgent : public InputBase

virtual bool click(int x, int y) override;
virtual bool swipe(int x1, int y1, int x2, int y2, int duration) override;
virtual bool multi_swipe(const std::vector<SwipeParam>& swipes) override;

virtual bool touch_down(int contact, int x, int y, int pressure) override;
virtual bool touch_move(int contact, int x, int y, int pressure) override;
Expand Down
7 changes: 7 additions & 0 deletions source/MaaDbgControlUnit/CarouselImage/CarouselImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ bool CarouselImage::swipe(int x1, int y1, int x2, int y2, int duration)
return true;
}

bool CarouselImage::multi_swipe(const std::vector<SwipeParam>& swipes)
{
std::ignore = swipes;

return true;
}

bool CarouselImage::touch_down(int contact, int x, int y, int pressure)
{
std::ignore = contact;
Expand Down
1 change: 1 addition & 0 deletions source/MaaDbgControlUnit/CarouselImage/CarouselImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class CarouselImage : public ControlUnitAPI

virtual bool click(int x, int y) override;
virtual bool swipe(int x1, int y1, int x2, int y2, int duration) override;
virtual bool multi_swipe(const std::vector<SwipeParam>& swipes) override;

virtual bool touch_down(int contact, int x, int y, int pressure) override;
virtual bool touch_move(int contact, int x, int y, int pressure) override;
Expand Down
7 changes: 6 additions & 1 deletion source/MaaDbgControlUnit/ReplayRecording/Record.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct Record
int x2 = 0;
int y2 = 0;
int duration = 0;
int starting = 0;
};

struct TouchParam
Expand Down Expand Up @@ -72,7 +73,7 @@ struct Record
};

using Param = std::
variant<std::monostate, ConnectParam, ClickParam, SwipeParam, TouchParam, PressKeyParam, InputTextParam, AppParam, ScreencapParam>;
variant<std::monostate, ConnectParam, ClickParam, SwipeParam, std::vector<SwipeParam>, TouchParam, PressKeyParam, InputTextParam, AppParam, ScreencapParam>;

struct Action
{
Expand All @@ -82,6 +83,7 @@ struct Record
connect,
click,
swipe,
multi_swipe,
touch_down,
touch_move,
touch_up,
Expand Down Expand Up @@ -124,6 +126,9 @@ inline std::ostream& operator<<(std::ostream& os, Record::Action::Type type)
case Record::Action::Type::swipe:
os << "swipe";
break;
case Record::Action::Type::multi_swipe:
os << "multi_swipe";
break;
case Record::Action::Type::touch_down:
os << "touch_down";
break;
Expand Down
33 changes: 28 additions & 5 deletions source/MaaDbgControlUnit/ReplayRecording/RecordParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ std::optional<Record> RecordParser::parse_record(const json::value& record_json,
std::string type_str = record_json.get("type", std::string());
static const std::unordered_map<std::string, Record::Action::Type> kTypeMap = {
{ "connect", Record::Action::Type::connect }, { "click", Record::Action::Type::click },
{ "swipe", Record::Action::Type::swipe }, { "touch_down", Record::Action::Type::touch_down },
{ "touch_move", Record::Action::Type::touch_move }, { "touch_up", Record::Action::Type::touch_up },
{ "press_key", Record::Action::Type::press_key }, { "input_text", Record::Action::Type::input_text },
{ "screencap", Record::Action::Type::screencap }, { "start_app", Record::Action::Type::start_app },
{ "stop_app", Record::Action::Type::stop_app },
{ "swipe", Record::Action::Type::swipe }, { "multi_swipe", Record::Action::Type::multi_swipe },
{ "touch_down", Record::Action::Type::touch_down }, { "touch_move", Record::Action::Type::touch_move },
{ "touch_up", Record::Action::Type::touch_up }, { "press_key", Record::Action::Type::press_key },
{ "input_text", Record::Action::Type::input_text }, { "screencap", Record::Action::Type::screencap },
{ "start_app", Record::Action::Type::start_app }, { "stop_app", Record::Action::Type::stop_app },
};

auto it = kTypeMap.find(type_str);
Expand All @@ -99,6 +99,9 @@ std::optional<Record> RecordParser::parse_record(const json::value& record_json,
case Record::Action::Type::swipe:
action_opt = parse_swipe(record_json);
break;
case Record::Action::Type::multi_swipe:
action_opt = parse_multi_swipe(record_json);
break;
case Record::Action::Type::touch_down:
case Record::Action::Type::touch_move:
case Record::Action::Type::touch_up:
Expand Down Expand Up @@ -224,6 +227,26 @@ std::optional<Record::Param> RecordParser::parse_swipe(const json::value& record
return result;
}

std::optional<Record::Param> RecordParser::parse_multi_swipe(const json::value& record_json)
{
auto swipes_opt = record_json.find<json::array>("swipes");
if (!swipes_opt) {
LogError << "Failed to find swipes:" << VAR(record_json);
return std::nullopt;
}

std::vector<Record::SwipeParam> result;
for (const json::value& swipe : *swipes_opt) {
auto s_opt = parse_swipe(swipe);
if (!s_opt) {
return std::nullopt;
}
auto s = std::get<Record::SwipeParam>(*std::move(s_opt));
result.emplace_back(std::move(s));
}
return result;
}

std::optional<Record::Param> RecordParser::parse_touch(const json::value& record_json)
{
Record::TouchParam result;
Expand Down
1 change: 1 addition & 0 deletions source/MaaDbgControlUnit/ReplayRecording/RecordParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class RecordParser
static std::optional<Record::Param> parse_connect(const json::value& record_json);
static std::optional<Record::Param> parse_click(const json::value& record_json);
static std::optional<Record::Param> parse_swipe(const json::value& record_json);
static std::optional<Record::Param> parse_multi_swipe(const json::value& record_json);
static std::optional<Record::Param> parse_touch(const json::value& record_json);
static std::optional<Record::Param> parse_press_key(const json::value& record_json);
static std::optional<Record::Param> parse_input_text(const json::value& record_json);
Expand Down
22 changes: 22 additions & 0 deletions source/MaaDbgControlUnit/ReplayRecording/ReplayRecording.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,28 @@ bool ReplayRecording::swipe(int x1, int y1, int x2, int y2, int duration)
return record.success;
}

bool ReplayRecording::multi_swipe(const std::vector<SwipeParam>& swipes)
{
LogInfo << VAR(swipes.size());

if (record_index_ >= recording_.records.size()) {
LogError << "record index out of range" << VAR(record_index_) << VAR(recording_.records.size());
return false;
}

const Record& record = recording_.records.at(record_index_);
if (record.action.type != Record::Action::Type::multi_swipe) {
LogError << "record type is not swipe" << VAR(record.action.type) << VAR(record.raw_data);
return false;
}

// TODO: 现在点击的点是随机区域,没法直接检查

sleep(record.cost);
++record_index_;
return record.success;
}

bool ReplayRecording::touch_down(int contact, int x, int y, int pressure)
{
LogInfo << VAR(contact) << VAR(x) << VAR(y) << VAR(pressure);
Expand Down
1 change: 1 addition & 0 deletions source/MaaDbgControlUnit/ReplayRecording/ReplayRecording.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ReplayRecording : public ControlUnitAPI

virtual bool click(int x, int y) override;
virtual bool swipe(int x1, int y1, int x2, int y2, int duration) override;
virtual bool multi_swipe(const std::vector<SwipeParam>& swipes) override;

virtual bool touch_down(int contact, int x, int y, int pressure) override;
virtual bool touch_move(int contact, int x, int y, int pressure) override;
Expand Down
Loading

0 comments on commit a08a0ea

Please sign in to comment.