SerializeXMLReader.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "SerializeXMLReader.h"
  10. #include <ISystem.h>
  11. #include <AzCore/Time/ITime.h>
  12. #define TAG_SCRIPT_VALUE "v"
  13. #define TAG_SCRIPT_TYPE "t"
  14. #define TAG_SCRIPT_NAME "n"
  15. //#define LOG_SERIALIZE_STACK(tag,szName) CryLogAlways( "<%s> %s/%s",tag,GetStackInfo().c_str(), szName);
  16. #define LOG_SERIALIZE_STACK(tag, szName)
  17. CSerializeXMLReaderImpl::CSerializeXMLReaderImpl(const XmlNodeRef& nodeRef)
  18. : m_nErrors(0)
  19. {
  20. assert(!!nodeRef);
  21. m_nodeStack.push_back(CParseState());
  22. m_nodeStack.back().Init(nodeRef);
  23. }
  24. bool CSerializeXMLReaderImpl::Value(const char* name, int8& value)
  25. {
  26. DefaultValue(value); // Set input value to default.
  27. if (m_nErrors)
  28. {
  29. return false;
  30. }
  31. int temp;
  32. bool bResult = Value(name, temp);
  33. if (temp < -128 || temp > 127)
  34. {
  35. CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "Attribute %s is out of range (%d)", name, temp);
  36. Failed();
  37. bResult = false;
  38. }
  39. else
  40. {
  41. value = static_cast<int8>(temp);
  42. }
  43. return bResult;
  44. }
  45. bool CSerializeXMLReaderImpl::Value(const char* name, AZStd::string& value)
  46. {
  47. DefaultValue(value); // Set input value to default.
  48. if (m_nErrors)
  49. {
  50. return false;
  51. }
  52. if (!CurNode()->haveAttr(name))
  53. {
  54. //CryWarning( VALIDATOR_MODULE_SYSTEM,VALIDATOR_WARNING,"No such attribute %s (invalid type?)", name);
  55. //Failed();
  56. return false;
  57. }
  58. else
  59. {
  60. value = CurNode()->getAttr(name);
  61. }
  62. return true;
  63. }
  64. bool CSerializeXMLReaderImpl::Value(const char* name, CTimeValue& value)
  65. {
  66. DefaultValue(value); // Set input value to default.
  67. if (m_nErrors)
  68. {
  69. return false;
  70. }
  71. XmlNodeRef nodeRef = CurNode();
  72. if (!nodeRef)
  73. {
  74. return false;
  75. }
  76. if (0 == strcmp("zero", nodeRef->getAttr(name)))
  77. {
  78. value = CTimeValue(0.0f);
  79. }
  80. else
  81. {
  82. const AZ::TimeMs elaspsedTimeMs = AZ::GetRealElapsedTimeMs();
  83. const double elaspedTimeSec = AZ::TimeMsToSecondsDouble(elaspsedTimeMs);
  84. const CTimeValue elaspedTime(elaspedTimeSec);
  85. float delta;
  86. if (!GetAttr(nodeRef, name, delta))
  87. {
  88. //CryWarning( VALIDATOR_MODULE_SYSTEM,VALIDATOR_WARNING,"Failed to read time value %s", name);
  89. //Failed();
  90. value = elaspedTime; // in case we don't find the node, it was assumed to be the default value (0.0)
  91. // 0.0 means current time, whereas "zero" really means CTimeValue(0.0), see above
  92. return false;
  93. }
  94. else
  95. {
  96. value = CTimeValue(elaspedTime + delta);
  97. }
  98. }
  99. return true;
  100. }
  101. void CSerializeXMLReaderImpl::BeginGroup(const char* szName)
  102. {
  103. if (m_nErrors)
  104. {
  105. //CryWarning( VALIDATOR_MODULE_SYSTEM,VALIDATOR_WARNING,"BeginGroup %s called on non-existant group", szName);
  106. m_nErrors++;
  107. }
  108. else if (XmlNodeRef node = NextOf(szName))
  109. {
  110. m_nodeStack.push_back(CParseState());
  111. m_nodeStack.back().Init(node);
  112. LOG_SERIALIZE_STACK("BeginGroup:ok", szName);
  113. }
  114. else
  115. {
  116. LOG_SERIALIZE_STACK("BeginGroup:fail", szName);
  117. CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING, "!BeginGroup( %s ) not found", szName);
  118. m_nErrors++;
  119. }
  120. }
  121. bool CSerializeXMLReaderImpl::BeginOptionalGroup(const char* szName, [[maybe_unused]] bool condition)
  122. {
  123. if (m_nErrors)
  124. {
  125. //CryWarning( VALIDATOR_MODULE_SYSTEM,VALIDATOR_WARNING,"BeginOptionalGroup %s called on non-existant group", szName);
  126. m_nErrors++;
  127. }
  128. else if (XmlNodeRef node = NextOf(szName))
  129. {
  130. m_nodeStack.push_back(CParseState());
  131. m_nodeStack.back().Init(node);
  132. LOG_SERIALIZE_STACK("BeginOptionalGroup:ok", szName);
  133. return true;
  134. }
  135. LOG_SERIALIZE_STACK("BeginOptionalGroup:fail", szName);
  136. return false;
  137. }
  138. void CSerializeXMLReaderImpl::EndGroup()
  139. {
  140. if (m_nErrors)
  141. {
  142. m_nErrors--;
  143. }
  144. else
  145. {
  146. LOG_SERIALIZE_STACK("EndGroup", "");
  147. m_nodeStack.pop_back();
  148. }
  149. assert(!m_nodeStack.empty());
  150. }