@@ -22,16 +22,19 @@ namespace PokemonAutomation{
2222//
2323// Here we store a map of all controller types in the program.
2424//
25- std::map<std::string , std::unique_ptr<AbstractControllerType >> CONTROLLER_TYPES ;
25+ std::map<ControllerInterface , std::unique_ptr<InterfaceType >> ALL_CONTROLLER_INTERFACES ;
2626
2727
28- void AbstractControllerType ::register_factory (
29- const std::string& name ,
30- std::unique_ptr<AbstractControllerType > factory
28+ void InterfaceType ::register_factory (
29+ ControllerInterface controller_interface ,
30+ std::unique_ptr<InterfaceType > factory
3131){
32- auto ret = CONTROLLER_TYPES .emplace (name , std::move (factory));
32+ auto ret = ALL_CONTROLLER_INTERFACES .emplace (controller_interface , std::move (factory));
3333 if (!ret.second ){
34- throw InternalProgramError (nullptr , PA_CURRENT_FUNCTION, " Duplicate Factory Name: " + name);
34+ throw InternalProgramError (
35+ nullptr , PA_CURRENT_FUNCTION,
36+ " Duplicate Factory Name: " + CONTROLLER_INTERFACE_STRINGS.get_string (controller_interface)
37+ );
3538 }
3639}
3740
@@ -45,8 +48,8 @@ get_compatible_descriptors(const ControllerRequirements& requirements){
4548 ret.emplace_back (new NullControllerDescriptor ());
4649
4750 // Add all other controllers. We don't filter them at this time.
48- for (const auto & controller_interface : CONTROLLER_TYPES ){
49- if (controller_interface.first == NullControllerDescriptor::TYPENAME ){
51+ for (const auto & controller_interface : ALL_CONTROLLER_INTERFACES ){
52+ if (controller_interface.first == ControllerInterface::None ){
5053 continue ;
5154 }
5255 std::vector<std::shared_ptr<const ControllerDescriptor>> list = controller_interface.second ->list ();
@@ -58,8 +61,8 @@ get_compatible_descriptors(const ControllerRequirements& requirements){
5861 // required list. For each of those, enumerate all the descriptors and
5962 // combine them into a single list.
6063 for (const auto& device : requirements.map()){
61- auto iter = CONTROLLER_TYPES .find(device.first);
62- if (iter != CONTROLLER_TYPES .end()){
64+ auto iter = ALL_CONTROLLER_INTERFACES .find(device.first);
65+ if (iter != ALL_CONTROLLER_INTERFACES .end()){
6366 std::vector<std::shared_ptr<const ControllerDescriptor>> list = iter->second->list();
6467 std::move(list.begin(), list.end(), std::back_inserter(ret));
6568 }
@@ -76,32 +79,60 @@ get_compatible_descriptors(const ControllerRequirements& requirements){
7679
7780
7881ControllerOption::ControllerOption ()
79- : m_current(new NullControllerDescriptor())
82+ : m_descriptor(new NullControllerDescriptor())
83+ , m_controller_type(ControllerType::None)
8084{}
8185
8286void ControllerOption::load_json (const JsonValue& json){
83- if (json.is_null ()){
84- m_current.reset (new NullControllerDescriptor ());
85- }
86- const JsonObject& obj = json.to_object_throw ();
87- const std::string& type = obj.get_string_throw (" DeviceType" );
88- const JsonValue& params = obj.get_value_throw (" Parameters" );
89-
90- auto iter = CONTROLLER_TYPES.find (type);
91- if (iter == CONTROLLER_TYPES.end ()){
92- m_current.reset (new NullControllerDescriptor ());
93- return ;
87+ std::unique_ptr<const ControllerDescriptor> descriptor;
88+ ControllerType controller_type = ControllerType::None;
89+ do {
90+ if (json.is_null ()){
91+ break ;
92+ }
93+
94+ const JsonObject* obj = json.to_object ();
95+ if (obj == nullptr ){
96+ break ;
97+ }
98+ const std::string* type = obj->get_string (" Interface" );
99+ if (type == nullptr ){
100+ break ;
101+ }
102+ const std::string* controller = obj->get_string (" Controller" );
103+ if (controller != nullptr ){
104+ controller_type = CONTROLLER_TYPE_STRINGS.get_enum (*controller);
105+ }
106+ const JsonValue* params = obj->get_value (" Parameters" );
107+ if (params == nullptr ){
108+ break ;
109+ }
110+
111+ auto iter = ALL_CONTROLLER_INTERFACES.find (CONTROLLER_INTERFACE_STRINGS.get_enum (*type, ControllerInterface::None));
112+ if (iter == ALL_CONTROLLER_INTERFACES.end ()){
113+ break ;
114+ }
115+
116+ descriptor = iter->second ->make (*params);
117+
118+ }while (false );
119+
120+ if (descriptor == nullptr ){
121+ descriptor.reset (new NullControllerDescriptor ());
94122 }
95123
96- m_current = iter->second ->make (params);
124+ m_descriptor = std::move (descriptor);
125+ m_controller_type = controller_type;
97126}
98127JsonValue ControllerOption::to_json () const {
99- if (!m_current ){
128+ if (!m_descriptor ){
100129 return JsonValue ();
101130 }
131+
102132 JsonObject obj;
103- obj[" DeviceType" ] = m_current->type_name ();
104- obj[" Parameters" ] = m_current->to_json ();
133+ obj[" Interface" ] = CONTROLLER_INTERFACE_STRINGS.get_string (m_descriptor->interface_type );
134+ obj[" Controller" ] = CONTROLLER_TYPE_STRINGS.get_string (m_controller_type);
135+ obj[" Parameters" ] = m_descriptor->to_json ();
105136 return obj;
106137}
107138
0 commit comments