From 6893e94e71e7dbdb669d647baffbbd5271f4e5b4 Mon Sep 17 00:00:00 2001 From: hiker Date: Fri, 24 Oct 2014 21:47:50 +1100 Subject: [PATCH] Replaced irrXML usage with out XMLNode. --- src/config/device_config.cpp | 87 +++++++++-------- src/config/device_config.hpp | 4 +- src/input/binding.cpp | 48 ++++------ src/input/binding.hpp | 13 ++- src/input/device_manager.cpp | 177 ++++++++++++++--------------------- src/input/device_manager.hpp | 2 +- 6 files changed, 149 insertions(+), 182 deletions(-) diff --git a/src/config/device_config.cpp b/src/config/device_config.cpp index 96a671899..ca76f298e 100644 --- a/src/config/device_config.cpp +++ b/src/config/device_config.cpp @@ -16,11 +16,16 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -#include #include "config/device_config.hpp" + +#include "io/xml_node.hpp" #include "utils/log.hpp" + #include + +#include + using namespace irr; //==== D E V I C E C O N F I G ================================================= @@ -223,39 +228,51 @@ void DeviceConfig::serialize (std::ofstream& stream) } // serialize //------------------------------------------------------------------------------ - -bool DeviceConfig::deserializeAction(irr::io::IrrXMLReader* xml) +/** Reads a device configuration from input.xml. + * \param config The XML Node of the configuration. + * \return False if an error occurred. + */ +bool DeviceConfig::load(const XMLNode *config) { - int binding_id = -1; - - // Never hurts to check ;) - if (xml == NULL) + bool error = false; + for(unsigned int i=0; igetNumNodes(); i++) { - Log::error("DeviceConfig", "Null pointer (DeviceConfig::deserializeAction)"); - return false; - } - - // Read tags from XML - const char *name_string = xml->getAttributeValue("name"); - // Try to determine action # for verbose action name - for (int i = 0; i < PA_COUNT; i++) - { - if (strcmp(name_string, KartActionStrings[i].c_str()) == 0) + const XMLNode *action = config->getNode(i); + if(action->getName()!="action") { - binding_id = i; - break; + Log::warn("DeviceConfig", "Invalid configuration '%s' - ignored."); + continue; + } + std::string name; + action->get("name", &name); + int binding_id = -1; + for (int i = 0; i < PA_COUNT; i++) + { + if (name==KartActionStrings[i]) + { + binding_id = i; + break; + } + } + if (binding_id == -1) + { + Log::warn("DeviceConfig", + "DeviceConfig::deserializeAction : action '%s' is unknown.", + name.c_str()); + error=true; } - } - if(binding_id==-1) - { - Log::warn("DeviceConfig", "DeviceConfig::deserializeAction : action '%s' is unknown.", - name_string); - return false; - } - return m_bindings[binding_id].deserialize(xml); -} // deserializeAction + if(!m_bindings[binding_id].load(action)) + { + Log::error("Device manager", + "Ignoring an ill-formed keyboard action in input config."); + error=true; + } + } // for i in nodes + return !error; +} // load +// --------------------------------------------------------------------------- // KeyboardConfig & GamepadConfig classes really should be in a separate cpp // file but they are so small that we'll just leave them here for now. @@ -350,19 +367,13 @@ GamepadConfig::GamepadConfig ( const std::string &name, //------------------------------------------------------------------------------ -GamepadConfig::GamepadConfig(irr::io::IrrXMLReader* xml) : DeviceConfig( DEVICE_CONFIG_TYPE_GAMEPAD ) +GamepadConfig::GamepadConfig(const XMLNode *config) + : DeviceConfig( DEVICE_CONFIG_TYPE_GAMEPAD ) { - const char* name_string = xml->getAttributeValue("name"); - if(name_string == NULL) + if(!config->get("name", &m_name)) Log::error("DeviceConfig", "Unnamed joystick in config file."); - else - m_name = name_string; - const char* enabled_string = xml->getAttributeValue("enabled"); - if (enabled_string != NULL) - m_enabled = (strcmp(enabled_string, "true") == 0); - else - m_enabled = true; + config->get("enabled", &m_enabled); m_plugged = 0; setDefaultBinds(); diff --git a/src/config/device_config.hpp b/src/config/device_config.hpp index 775d8506f..2728bed53 100644 --- a/src/config/device_config.hpp +++ b/src/config/device_config.hpp @@ -86,7 +86,7 @@ public: irr::core::stringw getMappingIdString (const PlayerAction action) const; void serialize (std::ofstream& stream); - bool deserializeAction (irr::io::IrrXMLReader* xml); + bool load(const XMLNode *config); void setBinding (const PlayerAction action, const Input::InputType type, @@ -172,7 +172,7 @@ public: void serialize (std::ofstream& stream); void setDefaultBinds (); - GamepadConfig (irr::io::IrrXMLReader* xml); + GamepadConfig (const XMLNode *config); GamepadConfig (const std::string &name, const int axis_count=0, const int button_ount=0); diff --git a/src/input/binding.cpp b/src/input/binding.cpp index 062f8172c..176ed7fda 100644 --- a/src/input/binding.cpp +++ b/src/input/binding.cpp @@ -16,12 +16,14 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -#include #include "input/binding.hpp" +#include "io/xml_node.hpp" #include "utils/string_utils.hpp" #include "utils/translation.hpp" #include "utils/log.hpp" +#include + /** Convert thjis binding to XML attributes. The full XML node is actually * written by device_config, so we only have to add the attributes here. */ @@ -40,51 +42,37 @@ void Binding::serialize(std::ofstream& stream) const } // serialize // ---------------------------------------------------------------------------- -bool Binding::deserialize(irr::io::IrrXMLReader* xml) +bool Binding::load(const XMLNode *action) { - const char *id_string = xml->getAttributeValue("id"); - const char *event_string = xml->getAttributeValue("event"); - const char *dir_string = xml->getAttributeValue("direction"); - const char *range_string = xml->getAttributeValue("range"); - const char *character = xml->getAttributeValue("character"); - - // Proceed only if neccesary tags were found - if ((id_string == NULL) || (event_string == NULL)) + int n; + if(!action->get("id", &m_id) || !action->get("event", &n) ) { Log::warn("Binding", "No id-string or event-string given - ignored."); return false; } - - // Convert strings to string tags to integer types - m_type = (Input::InputType)atoi(event_string); - m_id = atoi(id_string); - m_character = character ? atoi(character) : 0; + m_type = (Input::InputType)n; + core::stringw s; + m_character = 0; + action->get("character", &s); + if(s.size()>0) + m_character = s[0]; // If the action is not a stick motion (button or key) if (m_type == Input::IT_STICKMOTION) { - // If the action is a stick motion & a direction is defined - if (dir_string == NULL) + if(!action->get("direction", &n)) { Log::warn("Binding", "IT_STICKMOTION without direction, ignoring."); return false; } - - // If the action is a stick motion & a range is defined - if (range_string == NULL) - { - m_range = Input::AR_HALF; - } - else - { - m_range = (Input::AxisRange)atoi(range_string); - } - - m_dir = (Input::AxisDirection)atoi(dir_string); + m_dir = (Input::AxisDirection)n; + + if(!action->get("range", &n)) m_range = Input::AR_HALF; + else m_range = (Input::AxisRange)n; } // if m_type!=stickmotion return true; -} // deserialize +} // load // ---------------------------------------------------------------------------- /** Returns a string representing this binding, which can be displayed on the diff --git a/src/input/binding.hpp b/src/input/binding.hpp index 0362f2d05..3be2b4366 100644 --- a/src/input/binding.hpp +++ b/src/input/binding.hpp @@ -20,17 +20,20 @@ #ifndef BINDING_HPP #define BINDING_HPP -#include -#include #include "input/input.hpp" #include "utils/no_copy.hpp" -#include + +#include "irrString.h" + +#include + +class XMLNode; /** * \ingroup config */ -class Binding +class Binding : public NoCopy { private: Input::InputType m_type; @@ -62,7 +65,7 @@ public: // ------------------------------------------------------------------------ void serialize (std::ofstream& stream) const; - bool deserialize(irr::io::IrrXMLReader* xml); + bool load(const XMLNode *action); irr::core::stringw getAsString() const; }; #endif diff --git a/src/input/device_manager.cpp b/src/input/device_manager.cpp index 65a85a563..a62bb29b6 100644 --- a/src/input/device_manager.cpp +++ b/src/input/device_manager.cpp @@ -61,7 +61,7 @@ bool DeviceManager::initialize() Log::info("-","---------------------------"); } - deserialize(); + load(); // Assign a configuration to the keyboard, or create one if we haven't yet if(UserConfigParams::logMisc()) Log::info("Device manager","Initializing keyboard support."); @@ -437,123 +437,88 @@ void DeviceManager::clearLatestUsedDevice() } // clearLatestUsedDevice // ----------------------------------------------------------------------------- -bool DeviceManager::deserialize() +/** Loads the configuration from the user's input.xml file. + */ +bool DeviceManager::load() { - static std::string filepath = file_manager->getUserConfigFile(INPUT_FILE_NAME); + std::string filepath = file_manager->getUserConfigFile(INPUT_FILE_NAME); if(UserConfigParams::logMisc()) - Log::info("Device manager","Deserializing input.xml..."); + Log::info("Device manager","Loading input.xml..."); - if(!file_manager->fileExists(filepath)) + const XMLNode *input = file_manager->createXMLTree(filepath); + + if(!input) { if(UserConfigParams::logMisc()) Log::warn("Device manager","No configuration file exists."); + return false; } - else + + if(input->getName()!="input") { - irr::io::IrrXMLReader* xml = irr::io::createIrrXMLReader( filepath.c_str() ); - - const int GAMEPAD = 1; - const int KEYBOARD = 2; - const int NOTHING = 3; - - int reading_now = NOTHING; - - KeyboardConfig* keyboard_config = NULL; - GamepadConfig* gamepad_config = NULL; - - - // parse XML file - while(xml && xml->read()) - { - switch(xml->getNodeType()) - { - case irr::io::EXN_TEXT: - break; - - case irr::io::EXN_ELEMENT: - { - if (strcmp("input", xml->getNodeName()) == 0) - { - const char *version_string = xml->getAttributeValue("version"); - if (version_string == NULL || atoi(version_string) != INPUT_FILE_VERSION) - { - //I18N: shown when config file is too old - GUIEngine::showMessage( _("Please re-configure your key bindings.") ); - - GUIEngine::showMessage( _("Your input config file is not compatible with this version of STK.") ); - return false; - } - } - if (strcmp("keyboard", xml->getNodeName()) == 0) - { - keyboard_config = new KeyboardConfig(); - reading_now = KEYBOARD; - } - else if (strcmp("gamepad", xml->getNodeName()) == 0) - { - gamepad_config = new GamepadConfig(xml); - reading_now = GAMEPAD; - } - else if (strcmp("action", xml->getNodeName()) == 0) - { - if(reading_now == KEYBOARD) - { - if(keyboard_config != NULL) - if(!keyboard_config->deserializeAction(xml)) - Log::error("Device manager","Ignoring an ill-formed keyboard action in input config."); - } - else if(reading_now == GAMEPAD) - { - if(gamepad_config != NULL) - if(!gamepad_config->deserializeAction(xml)) - Log::error("Device manager","Ignoring an ill-formed gamepad action in input config."); - } - else Log::warn("Device manager","An action is placed in an unexpected area in the input config file."); - } - } - break; - // ---- section ending - case irr::io::EXN_ELEMENT_END: - { - if (strcmp("keyboard", xml->getNodeName()) == 0) - { - m_keyboard_configs.push_back(keyboard_config); - reading_now = NOTHING; - } - else if (strcmp("gamepad", xml->getNodeName()) == 0) - { - m_gamepad_configs.push_back(gamepad_config); - reading_now = NOTHING; - } - } - break; - - default: break; - - } // end switch - } // end while - - if(UserConfigParams::logMisc()) - { - Log::info("Device manager","Found %d keyboard and %d gamepad configurations.", - m_keyboard_configs.size(), m_gamepad_configs.size()); - } - - // For Debugging.... - /* - for (int n = 0; n < m_keyboard_configs.size(); n++) - printf("Config #%d\n%s", n + 1, m_keyboard_configs[n].toString().c_str()); - - for (int n = 0; n < m_gamepad_configs.size(); n++) - printf("%s", m_gamepad_configs[n].toString().c_str()); - */ - - delete xml; + Log::warn("DeviceManager", "Invalid input.xml file - no input node."); + return false; } + int version=0; + if(!input->get("version", &version) || version != INPUT_FILE_VERSION ) + { + //I18N: shown when config file is too old + GUIEngine::showMessage(_("Please re-configure your key bindings.")); + GUIEngine::showMessage(_("Your input config file is not compatible " + "with this version of STK.")); + return false; + } + + for(unsigned int i=0; igetNumNodes(); i++) + { + const XMLNode *config = input->getNode(i); + if(config->getName()=="keyboard") + { + KeyboardConfig* keyboard_config = new KeyboardConfig(); + if(!keyboard_config->load(config)) + { + Log::error("Device manager", + "Ignoring an ill-formed keyboard action in input config."); + } + m_keyboard_configs.push_back(keyboard_config); + } + else if (config->getName()=="gamepad") + { + GamepadConfig* gamepad_config = new GamepadConfig(config); + gamepad_config->load(config); + m_gamepad_configs.push_back(gamepad_config); + } + else + { + Log::warn("DeviceManager", + "Invalid node '%s' in input.xml - ignored.", + config->getName().c_str()); + continue; + } + } // for i < getNumNodes + + if (UserConfigParams::logMisc()) + { + Log::info("Device manager", + "Found %d keyboard and %d gamepad configurations.", + m_keyboard_configs.size(), m_gamepad_configs.size()); + } + + // For Debugging.... + /* + for (int n = 0; n < m_keyboard_configs.size(); n++) + printf("Config #%d\n%s", n + 1, m_keyboard_configs[n].toString().c_str()); + + for (int n = 0; n < m_gamepad_configs.size(); n++) + printf("%s", m_gamepad_configs[n].toString().c_str()); + */ + + delete input; + return true; -} // deserialize +} // load // ----------------------------------------------------------------------------- void DeviceManager::serialize() diff --git a/src/input/device_manager.hpp b/src/input/device_manager.hpp index c18a5b43f..637e4d2ce 100644 --- a/src/input/device_manager.hpp +++ b/src/input/device_manager.hpp @@ -99,7 +99,7 @@ private: * pressing fire. */ bool m_map_fire_to_select; - bool deserialize(); + bool load(); void shutdown(); public: