SerializeXMLReader.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include "SimpleSerialize.h"
  10. #include <stack>
  11. #include <IXml.h>
  12. #include "xml.h"
  13. class CSerializeXMLReaderImpl
  14. : public CSimpleSerializeImpl<true, ESerializationTarget::eST_SaveGame>
  15. {
  16. public:
  17. CSerializeXMLReaderImpl(const XmlNodeRef& nodeRef);
  18. template <class T_Value>
  19. ILINE bool GetAttr(const XmlNodeRef& node, const char* name, T_Value& value)
  20. {
  21. XmlStrCmpFunc pPrevCmpFunc = g_pXmlStrCmp;
  22. g_pXmlStrCmp = &strcmp; // Do case-sensitive compare
  23. bool bReturn = node->getAttr(name, value);
  24. g_pXmlStrCmp = pPrevCmpFunc;
  25. return bReturn;
  26. }
  27. ILINE bool GetAttr(const XmlNodeRef& node, const char* name, SSerializeString& value)
  28. {
  29. XmlStrCmpFunc pPrevCmpFunc = g_pXmlStrCmp;
  30. g_pXmlStrCmp = &strcmp; // Do case-sensitive compare
  31. bool bReturn = node->haveAttr(name);
  32. if (bReturn)
  33. {
  34. value = node->getAttr(name);
  35. }
  36. g_pXmlStrCmp = pPrevCmpFunc;
  37. return bReturn;
  38. }
  39. ILINE bool GetAttr([[maybe_unused]] XmlNodeRef& node, [[maybe_unused]] const char* name, [[maybe_unused]] const AZStd::string& value)
  40. {
  41. return false;
  42. }
  43. template <class T_Value>
  44. bool Value(const char* name, T_Value& value)
  45. {
  46. DefaultValue(value); // Set input value to default.
  47. if (m_nErrors)
  48. {
  49. return false;
  50. }
  51. if (!GetAttr(CurNode(), name, value))
  52. {
  53. //CryWarning( VALIDATOR_MODULE_SYSTEM,VALIDATOR_WARNING,"Unable to read attribute %s (invalid type?)", name);
  54. //Failed();
  55. return false;
  56. }
  57. return true;
  58. }
  59. bool Value(const char* name, int8& value);
  60. bool Value(const char* name, AZStd::string& value);
  61. bool Value(const char* name, CTimeValue& value);
  62. void BeginGroup(const char* szName);
  63. bool BeginOptionalGroup(const char* szName, bool condition);
  64. void EndGroup();
  65. private:
  66. XmlNodeRef CurNode() { return m_nodeStack.back().m_node; }
  67. XmlNodeRef NextOf(const char* name)
  68. {
  69. XmlStrCmpFunc pPrevCmpFunc = g_pXmlStrCmp;
  70. g_pXmlStrCmp = &strcmp; // Do case-sensitive compare
  71. assert(!m_nodeStack.empty());
  72. CParseState& ps = m_nodeStack.back();
  73. XmlNodeRef node = ps.GetNext(name);
  74. g_pXmlStrCmp = pPrevCmpFunc;
  75. return node;
  76. }
  77. class CParseState
  78. {
  79. public:
  80. CParseState() {}
  81. void Init(const XmlNodeRef& node)
  82. {
  83. m_node = node;
  84. m_nCurrent = 0;
  85. }
  86. XmlNodeRef GetNext(const char* name)
  87. {
  88. int i;
  89. int num = m_node->getChildCount();
  90. for (i = m_nCurrent; i < num; i++)
  91. {
  92. XmlNodeRef child = m_node->getChild(i);
  93. if (strcmp(child->getTag(), name) == 0)
  94. {
  95. m_nCurrent = i + 1;
  96. return child;
  97. }
  98. }
  99. int ncount = min(m_nCurrent, num);
  100. // Try searching from begining.
  101. for (i = 0; i < ncount; i++)
  102. {
  103. XmlNodeRef child = m_node->getChild(i);
  104. if (strcmp(child->getTag(), name) == 0)
  105. {
  106. m_nCurrent = i + 1;
  107. return child;
  108. }
  109. }
  110. return XmlNodeRef();
  111. }
  112. public:
  113. // TODO: make this much more efficient
  114. int m_nCurrent;
  115. XmlNodeRef m_node;
  116. };
  117. int m_nErrors;
  118. std::vector<CParseState> m_nodeStack;
  119. //////////////////////////////////////////////////////////////////////////
  120. // Set Defaults.
  121. //////////////////////////////////////////////////////////////////////////
  122. void DefaultValue(bool& v) const { v = false; }
  123. void DefaultValue(float& v) const { v = 0; }
  124. void DefaultValue(double& v) const { v = 0; }
  125. void DefaultValue(int8& v) const { v = 0; }
  126. void DefaultValue(uint8& v) const { v = 0; }
  127. void DefaultValue(int16& v) const { v = 0; }
  128. void DefaultValue(uint16& v) const { v = 0; }
  129. void DefaultValue(int32& v) const { v = 0; }
  130. void DefaultValue(uint32& v) const { v = 0; }
  131. void DefaultValue(int64& v) const { v = 0; }
  132. void DefaultValue(uint64& v) const { v = 0; }
  133. void DefaultValue(Vec2& v) const { v.x = 0; v.y = 0; }
  134. void DefaultValue(Vec3& v) const { v.x = 0; v.y = 0; v.z = 0; }
  135. void DefaultValue(Ang3& v) const { v.x = 0; v.y = 0; v.z = 0; }
  136. void DefaultValue(Quat& v) const { v.w = 1.0f; v.v.x = 0; v.v.y = 0; v.v.z = 0; }
  137. void DefaultValue(CTimeValue& v) const { v.SetValue(0); }
  138. void DefaultValue(AZStd::string& str) const { str = ""; }
  139. void DefaultValue([[maybe_unused]] const AZStd::string& str) const {}
  140. void DefaultValue([[maybe_unused]] SSerializeString& str) const {}
  141. void DefaultValue(XmlNodeRef& ref) const { ref = NULL; }
  142. //////////////////////////////////////////////////////////////////////////
  143. };