SerializeXMLWriter.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #ifndef CRYINCLUDE_CRYSYSTEM_XML_SERIALIZEXMLWRITER_H
  9. #define CRYINCLUDE_CRYSYSTEM_XML_SERIALIZEXMLWRITER_H
  10. #pragma once
  11. #include <ISystem.h>
  12. #include <IXml.h>
  13. #include "SimpleSerialize.h"
  14. class CSerializeXMLWriterImpl
  15. : public CSimpleSerializeImpl<false, ESerializationTarget::eST_SaveGame>
  16. {
  17. public:
  18. CSerializeXMLWriterImpl(const XmlNodeRef& nodeRef);
  19. ~CSerializeXMLWriterImpl();
  20. template <class T_Value>
  21. bool Value(const char* name, T_Value& value)
  22. {
  23. AddValue(name, value);
  24. return true;
  25. }
  26. bool Value(const char* name, CTimeValue value);
  27. void BeginGroup(const char* szName);
  28. bool BeginOptionalGroup(const char* szName, bool condition);
  29. void EndGroup();
  30. private:
  31. //////////////////////////////////////////////////////////////////////////
  32. // Vars.
  33. //////////////////////////////////////////////////////////////////////////
  34. CTimeValue m_curTime;
  35. std::vector<XmlNodeRef> m_nodeStack;
  36. std::vector<const char*> m_luaSaveStack;
  37. //////////////////////////////////////////////////////////////////////////
  38. ILINE const XmlNodeRef& CurNode()
  39. {
  40. assert(!m_nodeStack.empty());
  41. if (m_nodeStack.empty())
  42. {
  43. static XmlNodeRef temp = GetISystem()->CreateXmlNode("Error");
  44. return temp;
  45. }
  46. return m_nodeStack.back();
  47. }
  48. XmlNodeRef CreateNodeNamed(const char* name);
  49. template <class T>
  50. void AddValue(const char* name, const T& value)
  51. {
  52. if (strchr(name, ' ') != 0)
  53. {
  54. assert(0 && "Spaces in Value name not supported");
  55. CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "!Spaces in Value name not supported: %s in Group %s", name, GetStackInfo().c_str());
  56. return;
  57. }
  58. if (GetISystem()->IsDevMode() && CurNode())
  59. {
  60. // Check if this attribute already added.
  61. if (CurNode()->haveAttr(name))
  62. {
  63. assert(0);
  64. CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "!Duplicate tag Value( \"%s\" ) in Group %s", name, GetStackInfo().c_str());
  65. }
  66. }
  67. if (!IsDefaultValue(value))
  68. {
  69. CurNode()->setAttr(name, value);
  70. }
  71. }
  72. void AddValue(const char* name, const SSerializeString& value)
  73. {
  74. AddValue(name, value.c_str());
  75. }
  76. template <class T>
  77. void AddTypedValue(const char* name, const T& value, const char* type)
  78. {
  79. if (!IsDefaultValue(value))
  80. {
  81. XmlNodeRef newNode = CreateNodeNamed(name);
  82. newNode->setAttr("v", value);
  83. newNode->setAttr("t", type);
  84. }
  85. }
  86. // Used for printing currebnt stack info for warnings.
  87. AZStd::string GetStackInfo() const;
  88. AZStd::string GetLuaStackInfo() const;
  89. //////////////////////////////////////////////////////////////////////////
  90. // Check For Defaults.
  91. //////////////////////////////////////////////////////////////////////////
  92. bool IsDefaultValue(bool v) const { return v == false; };
  93. bool IsDefaultValue(float v) const { return v == 0; };
  94. bool IsDefaultValue(double v) const { return v == 0; };
  95. bool IsDefaultValue(int8 v) const { return v == 0; };
  96. bool IsDefaultValue(uint8 v) const { return v == 0; };
  97. bool IsDefaultValue(int16 v) const { return v == 0; };
  98. bool IsDefaultValue(uint16 v) const { return v == 0; };
  99. bool IsDefaultValue(int32 v) const { return v == 0; };
  100. bool IsDefaultValue(uint32 v) const { return v == 0; };
  101. bool IsDefaultValue(int64 v) const { return v == 0; };
  102. bool IsDefaultValue(uint64 v) const { return v == 0; };
  103. bool IsDefaultValue(const Vec2& v) const { return v.x == 0 && v.y == 0; };
  104. bool IsDefaultValue(const Vec3& v) const { return v.x == 0 && v.y == 0 && v.z == 0; };
  105. bool IsDefaultValue(const Ang3& v) const { return v.x == 0 && v.y == 0 && v.z == 0; };
  106. bool IsDefaultValue(const Quat& v) const { return v.w == 1.0f && v.v.x == 0 && v.v.y == 0 && v.v.z == 0; };
  107. bool IsDefaultValue(const CTimeValue& v) const { return v.GetValue() == 0; };
  108. bool IsDefaultValue(const char* str) const { return !str || !*str; };
  109. bool IsDefaultValue(const AZStd::string& str) const { return str.empty(); };
  110. bool IsDefaultValue(const SSerializeString& str) const { return str.empty(); };
  111. //////////////////////////////////////////////////////////////////////////
  112. /*
  113. template <class T>
  114. bool IsDefaultValue( const T& v ) const { return false; };
  115. */
  116. };
  117. #endif // CRYINCLUDE_CRYSYSTEM_XML_SERIALIZEXMLWRITER_H