Skip to content

Commit

Permalink
refactor: use factory function instead of class
Browse files Browse the repository at this point in the history
  • Loading branch information
horror-proton committed Jun 19, 2024
1 parent ab484ff commit fa51662
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 63 deletions.
47 changes: 41 additions & 6 deletions src/MaaCore/Controller/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#endif

#include "AdbController.h"
#include "ControllerAPI.h"
#include "MaatouchController.h"
#include "MinitouchController.h"
#include "PlayToolsController.h"

#include "Common/AsstTypes.h"
#include "Utils/Logger.hpp"
Expand All @@ -30,15 +34,49 @@ asst::Controller::Controller(const AsstCallback& callback, Assistant* inst)
, m_rand_engine(std::random_device {}())
{
LogTraceFunction;

m_controller_factory = std::make_unique<ControllerFactory>(callback, inst);
}

asst::Controller::~Controller()
{
LogTraceFunction;
}

std::shared_ptr<asst::ControllerAPI> asst::Controller::create_controller(
ControllerType type,
const std::string& adb_path,
const std::string& address,
const std::string& config,
PlatformType platform_type) const
{
std::shared_ptr<ControllerAPI> controller;
try {
switch (type) {
case ControllerType::Adb:
controller = std::make_shared<AdbController>(m_callback, m_inst, platform_type);
break;
case ControllerType::Minitouch:
controller = std::make_shared<MinitouchController>(m_callback, m_inst, platform_type);
break;
case ControllerType::Maatouch:
controller = std::make_shared<MaatouchController>(m_callback, m_inst, platform_type);
break;
case ControllerType::MacPlayTools:
controller = std::make_shared<PlayToolsController>(m_callback, m_inst, platform_type);
break;
default:
return nullptr;
}
}
catch (const std::exception& e) {
Log.error("Unable to create controller: {}", e.what());
return nullptr;
}
if (controller->connect(adb_path, address, config)) {
return controller;
}
return nullptr;
}

size_t asst::Controller::get_pipe_data_size() const noexcept
{
return m_controller->get_pipe_data_size();
Expand Down Expand Up @@ -186,10 +224,7 @@ bool asst::Controller::connect(

clear_info();

m_controller =
m_controller_factory
->create_controller(m_controller_type, adb_path, address, config, m_platform_type);

m_controller = create_controller(m_controller_type, adb_path, address, config, m_platform_type);
if (!m_controller) {
Log.error("connect failed");
return false;
Expand Down
10 changes: 7 additions & 3 deletions src/MaaCore/Controller/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "Platform/AdbLiteIO.h"

#include "ControllerAPI.h"
#include "ControllerFactory.h"

#include "ControlScaleProxy.h"

Expand All @@ -38,6 +37,13 @@ class Controller : private InstHelper
Controller(Controller&&) = delete;
~Controller();

std::shared_ptr<ControllerAPI> create_controller(
ControllerType type,
const std::string& adb_path,
const std::string& address,
const std::string& config,
PlatformType platform_type) const;

bool
connect(const std::string& adb_path, const std::string& address, const std::string& config);
bool inited() noexcept;
Expand Down Expand Up @@ -110,8 +116,6 @@ class Controller : private InstHelper

std::shared_ptr<ControllerAPI> m_controller = nullptr;

std::unique_ptr<ControllerFactory> m_controller_factory = nullptr;

std::shared_ptr<ControlScaleProxy> m_scale_proxy = nullptr;

std::string m_uuid;
Expand Down
54 changes: 0 additions & 54 deletions src/MaaCore/Controller/ControllerFactory.h

This file was deleted.

0 comments on commit fa51662

Please sign in to comment.