VGearsXMLSerializer.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <OgreException.h>
  16. #include <OgreLogManager.h>
  17. #include <OgreStringConverter.h>
  18. #include "data/VGearsXMLSerializer.h"
  19. namespace VGears{
  20. XMLSerializer::XMLSerializer(){}
  21. XMLSerializer::~XMLSerializer(){}
  22. void XMLSerializer::Parse(Ogre::DataStreamPtr &stream, TiXmlDocument &dest){
  23. dest.Parse(stream->getAsString().c_str());
  24. }
  25. const String* XMLSerializer::ReadAttribute(
  26. TiXmlNode &node, const String &attribute
  27. ){
  28. assertElement(node);
  29. return node.ToElement()->Attribute(attribute);
  30. }
  31. bool XMLSerializer::ReadAttribute(
  32. TiXmlNode &node, const String &attribute, bool &dest, const bool &def
  33. ){
  34. const String *value(ReadAttribute(node, attribute));
  35. if (value == nullptr){
  36. dest = def;
  37. return false;
  38. }
  39. dest = Ogre::StringConverter::parseBool(*value);
  40. return true;
  41. }
  42. bool XMLSerializer::ReadAttribute(
  43. TiXmlNode &node, const String &attribute, int &dest, const int &def
  44. ){
  45. const String *value(ReadAttribute(node, attribute));
  46. if (value == nullptr){
  47. dest = def;
  48. return false;
  49. }
  50. dest = Ogre::StringConverter::parseInt(*value);
  51. return true;
  52. }
  53. bool XMLSerializer::ReadAttribute(TiXmlNode &node, const String &attribute
  54. , String &dest, const String &def){
  55. const String *value(ReadAttribute(node, attribute));
  56. if (value == nullptr){
  57. dest = def;
  58. return false;
  59. }
  60. dest = *value;
  61. return true;
  62. }
  63. bool XMLSerializer::ReadAttribute(
  64. TiXmlNode &node, const String &attribute, Ogre::Real &dest,
  65. const Ogre::Real &def
  66. ){
  67. const String *value(ReadAttribute(node, attribute));
  68. if (value == nullptr){
  69. dest = def;
  70. return false;
  71. }
  72. dest = Ogre::StringConverter::parseReal(*value);
  73. return true;
  74. }
  75. bool XMLSerializer::ReadAttribute(
  76. TiXmlNode &node, const String &attribute, Ogre::Vector2 &dest,
  77. const Ogre::Vector2 &def
  78. ){
  79. const String *value(ReadAttribute(node, attribute));
  80. if (value == nullptr){
  81. dest = def;
  82. return false;
  83. }
  84. dest = Ogre::StringConverter::parseVector2(*value);
  85. return true;
  86. }
  87. bool XMLSerializer::ReadAttribute(
  88. TiXmlNode &node, const String &attribute, Ogre::Vector3 &dest,
  89. const Ogre::Vector3 &def
  90. ){
  91. const String *value(ReadAttribute(node, attribute));
  92. if (value == nullptr){
  93. dest = def;
  94. return false;
  95. }
  96. dest = Ogre::StringConverter::parseVector3(*value);
  97. return true;
  98. }
  99. bool XMLSerializer::ReadAttribute(
  100. TiXmlNode &node, const String &attribute, Ogre::Vector4 &dest,
  101. const Ogre::Vector4 &def
  102. ){
  103. const String *value(ReadAttribute(node, attribute));
  104. if (value == nullptr){
  105. dest = def;
  106. return false;
  107. }
  108. dest = Ogre::StringConverter::parseVector4(*value);
  109. return true;
  110. }
  111. bool XMLSerializer::ReadAttribute(
  112. TiXmlNode &node, const String &attribute, Ogre::Quaternion &dest,
  113. const Ogre::Quaternion &def
  114. ){
  115. const String *value(ReadAttribute(node, attribute));
  116. if (value == nullptr){
  117. dest = def;
  118. return false;
  119. }
  120. dest = Ogre::StringConverter::parseQuaternion(*value);
  121. return true;
  122. }
  123. TiXmlNode* XMLSerializer::FindChildNode(TiXmlNode &node, const String &tag){
  124. TiXmlNode* child(node.FirstChild());
  125. while (child != nullptr && child->ValueStr() != tag)
  126. child = child->NextSibling();
  127. return child;
  128. }
  129. }