SerializeXMLWriter.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #include "CrySystem_precompiled.h"
  9. #include "SerializeXMLWriter.h"
  10. #include <AzCore/Time/ITime.h>
  11. static const size_t MAX_NODE_STACK_DEPTH = 40;
  12. #define TAG_SCRIPT_VALUE "v"
  13. #define TAG_SCRIPT_TYPE "t"
  14. #define TAG_SCRIPT_NAME "n"
  15. CSerializeXMLWriterImpl::CSerializeXMLWriterImpl(const XmlNodeRef& nodeRef)
  16. {
  17. const AZ::TimeMs elaspsedTimeMs = AZ::GetRealElapsedTimeMs();
  18. const double elaspedTimeSec = AZ::TimeMsToSecondsDouble(elaspsedTimeMs);
  19. m_curTime = CTimeValue(elaspedTimeSec);
  20. assert(!!nodeRef);
  21. m_nodeStack.push_back(nodeRef);
  22. m_luaSaveStack.reserve(10);
  23. }
  24. //////////////////////////////////////////////////////////////////////////
  25. CSerializeXMLWriterImpl::~CSerializeXMLWriterImpl()
  26. {
  27. if (m_nodeStack.size() != 1)
  28. {
  29. // Node stack is incorrect.
  30. CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "!BeginGroup/EndGroup mismatch in SaveGame");
  31. }
  32. }
  33. //////////////////////////////////////////////////////////////////////////
  34. bool CSerializeXMLWriterImpl::Value(const char* name, CTimeValue value)
  35. {
  36. if (value == CTimeValue(0.0f))
  37. {
  38. AddValue(name, "zero");
  39. }
  40. else
  41. {
  42. AddValue(name, (value - m_curTime).GetSeconds());
  43. }
  44. return true;
  45. }
  46. void CSerializeXMLWriterImpl::BeginGroup(const char* szName)
  47. {
  48. if (strchr(szName, ' ') != 0)
  49. {
  50. assert(0 && "Spaces in group name not supported");
  51. CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "!Spaces in group name not supported: %s/%s", GetStackInfo().c_str(), szName);
  52. }
  53. XmlNodeRef node = CreateNodeNamed(szName);
  54. CurNode()->addChild(node);
  55. m_nodeStack.push_back(node);
  56. if (m_nodeStack.size() > MAX_NODE_STACK_DEPTH)
  57. {
  58. CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "!Too Deep Node Stack:\r\n%s", GetStackInfo().c_str());
  59. }
  60. }
  61. bool CSerializeXMLWriterImpl::BeginOptionalGroup(const char* szName, bool condition)
  62. {
  63. if (condition)
  64. {
  65. BeginGroup(szName);
  66. return true;
  67. }
  68. return condition;
  69. }
  70. XmlNodeRef CSerializeXMLWriterImpl::CreateNodeNamed(const char* name)
  71. {
  72. XmlNodeRef newNode = CurNode()->createNode(name);
  73. return newNode;
  74. }
  75. void CSerializeXMLWriterImpl::EndGroup()
  76. {
  77. if (m_nodeStack.size() == 1)
  78. {
  79. //
  80. CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "!Misplaced EndGroup() for BeginGroup(%s)", CurNode()->getTag());
  81. }
  82. assert(!m_nodeStack.empty());
  83. m_nodeStack.pop_back();
  84. assert(!m_nodeStack.empty());
  85. }
  86. //////////////////////////////////////////////////////////////////////////
  87. AZStd::string CSerializeXMLWriterImpl::GetStackInfo() const
  88. {
  89. AZStd::string str;
  90. for (int i = 0; i < (int)m_nodeStack.size(); i++)
  91. {
  92. const char* name = m_nodeStack[i]->getAttr(TAG_SCRIPT_NAME);
  93. if (name && name[0])
  94. {
  95. str += name;
  96. }
  97. else
  98. {
  99. str += m_nodeStack[i]->getTag();
  100. }
  101. if (i != m_nodeStack.size() - 1)
  102. {
  103. str += "/";
  104. }
  105. }
  106. return str;
  107. }
  108. //////////////////////////////////////////////////////////////////////////
  109. AZStd::string CSerializeXMLWriterImpl::GetLuaStackInfo() const
  110. {
  111. AZStd::string str;
  112. for (int i = 0; i < (int)m_luaSaveStack.size(); i++)
  113. {
  114. const char* name = m_luaSaveStack[i];
  115. str += name;
  116. if (i != m_luaSaveStack.size() - 1)
  117. {
  118. str += ".";
  119. }
  120. }
  121. return str;
  122. }