1212#include " ControllerDescriptor.h"
1313#include " NullController.h"
1414
15- // #include <iostream>
16- // using std::cout;
17- // using std::endl;
15+ // REMOVE
16+ #include < iostream>
17+ using std::cout;
18+ using std::endl;
1819
1920namespace PokemonAutomation {
2021
@@ -80,12 +81,28 @@ get_compatible_descriptors(const ControllerRequirements& requirements){
8081
8182
8283ControllerOption::ControllerOption ()
83- : m_descriptor( new NullControllerDescriptor() )
84- , m_controller_type(ControllerType::None )
84+ : m_controller_type(ControllerType::None )
85+ , m_descriptor( new NullControllerDescriptor() )
8586{}
8687
88+
89+ void ControllerOption::set_descriptor (std::shared_ptr<const ControllerDescriptor> descriptor){
90+ m_descriptor_cache[descriptor->interface_type ] = descriptor;
91+ m_descriptor = std::move (descriptor);
92+ }
93+
94+ std::shared_ptr<const ControllerDescriptor> ControllerOption::get_descriptor_from_cache (ControllerInterface interface_type) const {
95+ auto iter = m_descriptor_cache.find (interface_type);
96+ if (iter == m_descriptor_cache.end ()){
97+ return nullptr ;
98+ }
99+ return iter->second ;
100+ }
101+
102+
103+
87104void ControllerOption::load_json (const JsonValue& json){
88- std::unique_ptr <const ControllerDescriptor> descriptor;
105+ std::shared_ptr <const ControllerDescriptor> descriptor;
89106 ControllerType controller_type = ControllerType::None;
90107 do {
91108 if (json.is_null ()){
@@ -104,17 +121,26 @@ void ControllerOption::load_json(const JsonValue& json){
104121 if (controller != nullptr ){
105122 controller_type = CONTROLLER_TYPE_STRINGS.get_enum (*controller, ControllerType::None);
106123 }
107- const JsonValue* params = obj->get_value (" Parameters" );
108- if (params == nullptr ){
109- break ;
124+
125+ for (const auto & item : ALL_CONTROLLER_INTERFACES){
126+ const JsonValue* params = obj->get_value (CONTROLLER_INTERFACE_STRINGS.get_string (item.first ));
127+ if (params == nullptr ){
128+ continue ;
129+ }
130+ m_descriptor_cache[item.first ] = item.second ->make (*params);
110131 }
111132
112- auto iter = ALL_CONTROLLER_INTERFACES.find (CONTROLLER_INTERFACE_STRINGS.get_enum (*type, ControllerInterface::None));
113- if (iter == ALL_CONTROLLER_INTERFACES.end ()){
133+ // const JsonValue* params = obj->get_value("Parameters");
134+ // if (params == nullptr){
135+ // break;
136+ // }
137+
138+ auto iter = m_descriptor_cache.find (CONTROLLER_INTERFACE_STRINGS.get_enum (*type, ControllerInterface::None));
139+ if (iter == m_descriptor_cache.end ()){
114140 break ;
115141 }
116142
117- descriptor = iter->second -> make (*params) ;
143+ descriptor = iter->second ;
118144
119145 }while (false );
120146
@@ -129,11 +155,16 @@ JsonValue ControllerOption::to_json() const{
129155 if (!m_descriptor){
130156 return JsonValue ();
131157 }
132-
133158 JsonObject obj;
134159 obj[" Interface" ] = CONTROLLER_INTERFACE_STRINGS.get_string (m_descriptor->interface_type );
135160 obj[" Controller" ] = CONTROLLER_TYPE_STRINGS.get_string (m_controller_type);
136- obj[" Parameters" ] = m_descriptor->to_json ();
161+ // obj["Parameters"] = m_descriptor->to_json();
162+
163+ for (const auto & item : m_descriptor_cache){
164+ obj[CONTROLLER_INTERFACE_STRINGS.get_string (item.first )] = item.second ->to_json ();
165+ }
166+
167+
137168 return obj;
138169}
139170
0 commit comments