BVB Source Codes

CRYENGINE Show XMLCPB_NodeLiveReader.cpp Source code

Return Download CRYENGINE: download XMLCPB_NodeLiveReader.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. /*************************************************************************
  4. *************************************************************************/
  5.  
  6. #include "StdAfx.h"
  7. #include "XMLCPB_NodeLiveReader.h"
  8. #include "XMLCPB_Reader.h"
  9.  
  10. using namespace XMLCPB;
  11.  
  12. //////////////////////////////////////////////////////////////////////////
  13.  
  14. void CNodeLiveReaderRef::FreeRef()
  15. {
  16.         if (m_nodeId != XMLCPB_INVALID_ID && m_nodeId != XMLCPB_ROOTNODE_ID)
  17.         {
  18.                 CNodeLiveReader* pNode = m_Reader.GetNodeLive(m_nodeId);
  19.  
  20.                 assert(pNode->IsValid());
  21.  
  22.                 if (pNode->IsValid())
  23.                 {
  24.                         assert(pNode->m_refCounter > 0);
  25.                         pNode->m_refCounter--;
  26.                         if (pNode->m_refCounter == 0)
  27.                                 m_Reader.FreeNodeLive(m_nodeId);
  28.                 }
  29.         }
  30.         m_nodeId = XMLCPB_INVALID_ID;
  31.         m_pNode_Debug = NULL;
  32. }
  33.  
  34. /////////////////
  35. void CNodeLiveReaderRef::CopyFrom(const CNodeLiveReaderRef& other)
  36. {
  37.         assert(m_nodeId == XMLCPB_INVALID_ID);
  38.         m_nodeId = other.m_nodeId;
  39.         m_pNode_Debug = NULL;
  40.         if (m_nodeId != XMLCPB_INVALID_ID)
  41.         {
  42.                 CNodeLiveReader* pNode = m_Reader.GetNodeLive(m_nodeId);
  43.                 m_pNode_Debug = pNode;
  44.                 assert(pNode->IsValid());
  45.                 pNode->m_refCounter++;
  46.         }
  47. }
  48.  
  49. ///////////////
  50. CNodeLiveReader* CNodeLiveReaderRef::GetNode() const
  51. {
  52.         if (m_nodeId == XMLCPB_INVALID_ID)
  53.                 return NULL;
  54.  
  55.         CNodeLiveReader* pNode = m_Reader.GetNodeLive(m_nodeId);
  56.  
  57.         assert(pNode->IsValid());
  58.  
  59.         if (!pNode->IsValid())
  60.                 return NULL;
  61.  
  62.         return pNode;
  63. }
  64.  
  65. /////////////////
  66. CNodeLiveReaderRef::CNodeLiveReaderRef(CReader& Reader, NodeLiveID nodeId)
  67.         : m_Reader(Reader)
  68.         , m_nodeId(nodeId)
  69. {
  70.         CNodeLiveReader* pNode = GetNode();
  71.         m_pNode_Debug = pNode;
  72.         if (pNode)
  73.                 pNode->m_refCounter++;
  74. }
  75.  
  76. //////////////////////////////////////////////////////////////////////////
  77.  
  78. CNodeLiveReader::~CNodeLiveReader()
  79. {
  80. }
  81.  
  82. //////////////////////////////////////////////////////////////////////////
  83.  
  84. CNodeLiveReaderRef CNodeLiveReader::GetChildNode(const char* pChildName)
  85. {
  86.         bool found = false;
  87.         NodeGlobalID childId = XMLCPB_INVALID_ID;
  88.  
  89.         if (m_numChildren > 0)
  90.         {
  91.                 // first try with the next child. if is not the one, then look from start
  92.                 childId = GetChildGlobalId(m_indexOfNextChild);
  93.                 if (m_indexOfNextChild < m_numChildren && strcmp(pChildName, GetTagNode(childId)) == 0)
  94.                 {
  95.                         found = true;
  96.                 }
  97.  
  98.                 // if not found, then just look on all from the start
  99.                 if (!found)
  100.                 {
  101.                         int childNumber = 0;
  102.  
  103.                         uint32 childrenLeftToCheck = m_numChildren;
  104.                         uint32 numChildBlocks = m_childBlocks.size();
  105.                         for (uint block = 0; block < numChildBlocks && !found; ++block)
  106.                         {
  107.                                 uint childrenInBlock = min(uint32(CHILDREN_PER_BLOCK), childrenLeftToCheck);
  108.                                 childrenLeftToCheck -= childrenInBlock;
  109.                                 childId = m_childBlocks[block];
  110.  
  111.                                 for (int i = 0; i < childrenInBlock; ++i)
  112.                                 {
  113.                                         if (strcmp(pChildName, GetTagNode(childId)) == 0)
  114.                                         {
  115.                                                 found = true;
  116.                                                 m_indexOfNextChild = childNumber;
  117.                                                 break;
  118.                                         }
  119.                                         ++childNumber;
  120.                                         ++childId;
  121.                                 }
  122.                         }
  123.                 }
  124.         }
  125.  
  126.         CNodeLiveReaderRef nodeRef(m_Reader);
  127.  
  128.         if (found)
  129.         {
  130.                 const CNodeLiveReader& node = m_Reader.ActivateLiveNodeFromCompact(childId);
  131.                 nodeRef = CNodeLiveReaderRef(m_Reader, node.GetLiveId());
  132.                 m_indexOfNextChild = (m_indexOfNextChild + 1) % m_numChildren;
  133.         }
  134.  
  135.         return nodeRef;
  136. }
  137.  
  138. //////////////////////////////////////////////////////////////////////////
  139.  
  140. CNodeLiveReaderRef CNodeLiveReader::GetChildNode(uint32 index)
  141. {
  142.         assert(index < m_numChildren);
  143.  
  144.         // create the reference object
  145.         const CNodeLiveReader& child = m_Reader.ActivateLiveNodeFromCompact(GetChildGlobalId(index));
  146.         CNodeLiveReaderRef childRef(m_Reader, child.GetLiveId());
  147.  
  148.         // now prepares to point to the next child
  149.         m_indexOfNextChild = (index + 1) % m_numChildren;
  150.  
  151.         return childRef;
  152. }
  153.  
  154. //////////////////////////////////////////////////////////////////////////
  155.  
  156. NodeGlobalID CNodeLiveReader::GetChildGlobalId(uint32 indChild) const
  157. {
  158.         uint32 block = indChild / CHILDREN_PER_BLOCK;
  159.         uint32 indInBlock = indChild % CHILDREN_PER_BLOCK;
  160.         NodeGlobalID childId = m_childBlocks[block] + indInBlock;
  161.         return childId;
  162. }
  163.  
  164. //////////////////////////////////////////////////////////////////////////
  165.  
  166. uint32 CNodeLiveReader::GetSizeWithoutChilds() const
  167. {
  168.         FlatAddr addr0 = m_Reader.GetAddrNode(m_globalId);
  169.         FlatAddr addr1 = m_globalId + 1 == m_Reader.GetNumNodes() ? m_Reader.GetNodesDataSize() : m_Reader.GetAddrNode(m_globalId + 1);
  170.  
  171.         uint32 size = addr1 - addr0;
  172.  
  173.         if (addr1 == XMLCPB_INVALID_FLATADDR)
  174.                 size = m_Reader.GetNodesDataSize() - addr0;
  175.  
  176.         return size;
  177. }
  178.  
  179. //////////////////////////////////////////////////////////////////////////
  180. // constructs the NodeLive from the compacted (raw) binary data
  181. // CNodeLive::Compact() has the format description for the data
  182.  
  183. void CNodeLiveReader::ActivateFromCompact(NodeLiveID liveId, NodeGlobalID globalId)
  184. {
  185.         assert(!IsValid());
  186.  
  187.         Reset();
  188.  
  189.         m_liveId = liveId;
  190.         m_globalId = globalId;
  191.         FlatAddr nextAddr = m_Reader.GetAddrNode(m_globalId);
  192.         m_valid = true;
  193.  
  194.         // --- read header----
  195.         uint16 header;
  196.         nextAddr = m_Reader.ReadFromBuffer(nextAddr, header);
  197.  
  198.         StringID stringId = header & MASK_TAGID;
  199.         m_pTag = m_Reader.GetTagsTable().GetString(stringId);
  200.  
  201.         bool childrenAreRightBefore = IsFlagActive(header, FLN_CHILDREN_ARE_RIGHT_BEFORE);
  202.         bool hasAttrs = IsFlagActive(header, FLN_HASATTRS);
  203.  
  204.         // --------- children ------------
  205.         m_numChildren = (header & CHILDREN_HEADER_MASK) >> CHILDREN_HEADER_BIT_POSITION;
  206.         bool childrenAndDistStoredInFirstByte = false;
  207.         if (m_numChildren == CHILDREN_HEADER_CANTFIT) // if cant fit in the header, it meants that there are additional byte(s) to store the number
  208.         {
  209.                 uint8 val;
  210.                 nextAddr = m_Reader.ReadFromBuffer(nextAddr, val);
  211.                 if (IsFlagActive(val, FL_CHILDREN_NUMBER_IS_16BITS))
  212.                 {
  213.                         uint8 val2;
  214.                         nextAddr = m_Reader.ReadFromBuffer(nextAddr, val2);
  215.                         m_numChildren = (val2 | (val << 8)) & MAX_NUMBER_OF_CHILDREN_IN_16BITS; // build the number of children
  216.                 }
  217.                 else
  218.                 {
  219.                         if (IsFlagActive(val, FL_CHILDREN_NUMBER_AND_BLOCKDIST_IN_ONE_BYTE)) // number of children and dist to them is stored in the single byte
  220.                         {
  221.                                 m_numChildren = (val & CHILDANDDISTINONEBYTE_MAX_AMOUNT_CHILDREN) + CHILDREN_HEADER_MAXNUM;          // +CHILDREN_HEADER_MAXNUM because that is subtracted when stored
  222.                                 uint32 distChild = ((val >> CHILDANDDISTINONEBYTE_CHILD_BITS) & CHILDANDDISTINONEBYTE_MAX_DIST) + 1; // +1 because is subtracted when stored
  223.                                 m_childBlocks.resize(1);
  224.                                 m_childBlocks[0] = m_globalId - (distChild + m_numChildren);
  225.                                 childrenAndDistStoredInFirstByte = true;
  226.                         }
  227.                         else
  228.                         {
  229.                                 m_numChildren = val;
  230.                         }
  231.                 }
  232.         }
  233.         uint32 numChildBlocks = m_numChildren / CHILDREN_PER_BLOCK + ((m_numChildren % CHILDREN_PER_BLOCK) > 0 ? 1 : 0);
  234.         m_childBlocks.resize(numChildBlocks);
  235.  
  236.         // ---------- attrSetId ------------------
  237.         if (hasAttrs)
  238.         {
  239.                 uint8 val;
  240.                 nextAddr = m_Reader.ReadFromBuffer(nextAddr, val);
  241.                 uint32 highBits = (header & HIGHPART_ATTRID_MASK) >> (HIGHPART_ATTRID_BIT_POSITION - 8);
  242.                 m_attrsSetId = highBits | val;
  243.  
  244.                 if (m_attrsSetId == ATRID_USING_16_BITS)
  245.                 {
  246.                         uint16 val16;
  247.                         nextAddr = m_Reader.ReadFromBuffer(nextAddr, val16);
  248.                         m_attrsSetId = val16;
  249.                 }
  250.  
  251.                 m_numAttrs = m_Reader.GetDataTypeSetsTable().GetNumAttrs(m_attrsSetId);
  252.         }
  253.  
  254.         // --------- children blocks ----------
  255.         if (!childrenAndDistStoredInFirstByte && !childrenAreRightBefore && numChildBlocks > 0)
  256.         {
  257.                 if (numChildBlocks == 1)
  258.                 {
  259.                         uint8 val;
  260.                         nextAddr = m_Reader.ReadFromBuffer(nextAddr, val);
  261.                         if (!IsFlagActive(val, CHILDBLOCKS_USING_MORE_THAN_8BITS))
  262.                         {
  263.                                 m_childBlocks[0] = m_globalId - (val + m_numChildren);
  264.                         }
  265.                         else if (!IsFlagActive(val, CHILDBLOCKS_USING_24BITS))
  266.                         {
  267.                                 uint8 val2;
  268.                                 nextAddr = m_Reader.ReadFromBuffer(nextAddr, val2);
  269.                                 uint32 dist = ((val & CHILDBLOCKS_MASK_TO_REMOVE_FLAGS) << 8) | val2;
  270.                                 m_childBlocks[0] = m_globalId - (dist + m_numChildren);
  271.                         }
  272.                         else // as 24 bits
  273.                         {
  274.                                 val = val & CHILDBLOCKS_MASK_TO_REMOVE_FLAGS;
  275.                                 uint16 low;
  276.                                 nextAddr = m_Reader.ReadFromBuffer(nextAddr, low);
  277.                                 uint32 dist = low | (val << 16);
  278.                                 m_childBlocks[0] = m_globalId - (dist + m_numChildren);
  279.                         }
  280.                 }
  281.                 else // when there are more than 1 block, they are always stored as 24 bits
  282.                 {
  283.                         for (uint32 i = 0; i < numChildBlocks; ++i)
  284.                         {
  285.                                 uint8 high;
  286.                                 nextAddr = m_Reader.ReadFromBuffer(nextAddr, high);
  287.                                 high &= CHILDBLOCKS_MASK_TO_REMOVE_FLAGS;
  288.                                 uint16 low;
  289.                                 nextAddr = m_Reader.ReadFromBuffer(nextAddr, low);
  290.                                 uint32 dist = low | (high << 16);
  291.                                 uint32 numChildsInBlock = min(uint32(CHILDREN_PER_BLOCK), m_numChildren - (i * CHILDREN_PER_BLOCK));
  292.                                 m_childBlocks[i] = m_globalId - (dist + numChildsInBlock);
  293.                         }
  294.                 }
  295.         }
  296.  
  297.         if (childrenAreRightBefore)
  298.         {
  299.                 m_childBlocks[0] = m_globalId - m_numChildren;
  300.         }
  301.  
  302.         m_addrFirstAttr = nextAddr;
  303. }
  304.  
  305. //////////////////////////////////////////////////////////////////////////
  306.  
  307. const char* CNodeLiveReader::GetTagNode(NodeGlobalID nodeId) const
  308. {
  309.         FlatAddr addr = m_Reader.GetAddrNode(nodeId);
  310.  
  311.         uint16 header;
  312.         m_Reader.ReadFromBuffer(addr, header);
  313.  
  314.         StringID stringId = header & MASK_TAGID;
  315.         const char* pTag = m_Reader.GetTagsTable().GetString(stringId);
  316.         return pTag;
  317. }
  318.  
  319. //////////////////////////////////////////////////////////////////////////
  320.  
  321. void CNodeLiveReader::Reset()
  322. {
  323.         m_liveId = XMLCPB_INVALID_ID;
  324.         m_globalId = XMLCPB_INVALID_ID;
  325.         m_refCounter = 0;
  326.         m_valid = false;
  327.         m_pTag = NULL;
  328.         m_numChildren = 0;
  329.         m_numAttrs = 0;
  330.         m_addrFirstAttr = XMLCPB_INVALID_FLATADDR;
  331.         m_indexOfNextChild = 0;
  332.         m_childBlocks.clear();
  333.         m_attrsSetId = XMLCPB_INVALID_ID;
  334. }
  335.  
  336. //////////////////////////////////////////////////////////////////////////
  337.  
  338. FlatAddr CNodeLiveReader::GetAddrNextNode() const
  339. {
  340.         FlatAddr nextAddr = m_addrFirstAttr;
  341.         for (int a = 0; a < m_numAttrs; a++)
  342.         {
  343.                 CAttrReader attr(m_Reader);
  344.                 attr.InitFromCompact(nextAddr, m_Reader.GetDataTypeSetsTable().GetHeaderAttr(m_attrsSetId, a));
  345.                 nextAddr = attr.GetAddrNextAttr();
  346.         }
  347.  
  348.         return nextAddr;
  349. }
  350.  
  351. //////////////////////////////////////////////////////////////////////////
  352.  
  353. bool CNodeLiveReader::ReadAttr(const char* pAttrName, uint8*& rdata, uint32& outSize) const
  354. {
  355.         CAttrReader attr(m_Reader);
  356.         bool found = FindAttr(pAttrName, attr);
  357.  
  358.         if (found)
  359.                 attr.Get(rdata, outSize);
  360.  
  361.         return found;
  362. }
  363.  
  364. //////////////////////////////////////////////////////////////////////////
  365.  
  366. void CNodeLiveReader::ReadAttr(uint32 index, uint8*& rdata, uint32& outSize) const
  367. {
  368.         CAttrReader attr(m_Reader);
  369.         GetAttr(index, attr);
  370.         attr.Get(rdata, outSize);
  371. }
  372.  
  373. //////////////////////////////////////////////////////////////////////////
  374.  
  375. bool CNodeLiveReader::HaveAttr(const char* pAttrName) const
  376. {
  377.         CAttrReader attr(m_Reader);
  378.         bool have = FindAttr(pAttrName, attr);
  379.         return have;
  380. }
  381.  
  382. //////////////////////////////////////////////////////////////////////////
  383.  
  384. bool CNodeLiveReader::FindAttr(const char* pAttrName, CAttrReader& attr) const
  385. {
  386.         FlatAddr nextAddr = m_addrFirstAttr;
  387.  
  388.         bool found = false;
  389.         for (int a = 0; a < m_numAttrs; a++)
  390.         {
  391.                 attr.InitFromCompact(nextAddr, m_Reader.GetDataTypeSetsTable().GetHeaderAttr(m_attrsSetId, a));
  392.                 if (strcmp(pAttrName, attr.GetName()) == 0)
  393.                 {
  394.                         found = true;
  395.                         break;
  396.                 }
  397.                 nextAddr = attr.GetAddrNextAttr();
  398.         }
  399.  
  400.         return found;
  401. }
  402.  
  403. //////////////////////////////////////////////////////////////////////////
  404.  
  405. void CNodeLiveReader::GetAttr(uint index, CAttrReader& attr) const
  406. {
  407.         assert(index < m_numAttrs);
  408.  
  409.         FlatAddr nextAddr = m_addrFirstAttr;
  410.  
  411.         for (int a = 0; a <= index; a++)
  412.         {
  413.                 attr.InitFromCompact(nextAddr, m_Reader.GetDataTypeSetsTable().GetHeaderAttr(m_attrsSetId, a));
  414.                 nextAddr = attr.GetAddrNextAttr();
  415.         }
  416. }
  417.  
  418. //////////////////////////////////////////////////////////////////////////
  419.  
  420. CAttrReader CNodeLiveReader::ObtainAttr(const char* pAttrName) const
  421. {
  422.         CAttrReader attr(m_Reader);
  423.  
  424.         bool found = FindAttr(pAttrName, attr);
  425.         assert(found);
  426.  
  427.         return attr;
  428. }
  429.  
  430. //////////////////////////////////////////////////////////////////////////
  431.  
  432. CAttrReader CNodeLiveReader::ObtainAttr(uint index) const
  433. {
  434.         CAttrReader attr(m_Reader);
  435.         GetAttr(index, attr);
  436.  
  437.         return attr;
  438. }
  439.  
  440. //////////////////////////////////////////////////////////////////////////
  441.  
  442. uint16 CNodeLiveReader::GetHeaderAttr(uint32 attrInd) const
  443. {
  444.         return m_Reader.GetDataTypeSetsTable().GetHeaderAttr(m_attrsSetId, attrInd);
  445. }
  446.  
  447. //////////////////////////////////////////////////////////////////////////
  448. // reads any attr value, converts to string and store it into an string
  449. // is separated from the normal ReadAttr functions in purpose: This function should be used only for error handling or other special situations
  450.  
  451. bool CNodeLiveReader::ReadAttrAsString(const char* pAttrName, string& str) const
  452. {
  453.         CAttrReader attr(m_Reader);
  454.  
  455.         bool found = FindAttr(pAttrName, attr);
  456.  
  457.         if (found)
  458.                 attr.GetValueAsString(str);
  459.  
  460.         return found;
  461. }
  462.  
  463. //////////////////////////////////////////////////////////////////////////
  464. // for debugging use only
  465. #ifndef _RELEASE
  466. void CNodeLiveReader::Log() const
  467. {
  468.         // read tag id again, is not stored
  469.         FlatAddr myAddr = m_Reader.GetAddrNode(m_globalId);
  470.         uint16 header;
  471.         m_Reader.ReadFromBuffer(myAddr, header);
  472.         StringID tagId = header & MASK_TAGID;
  473.  
  474.         string str;
  475.         str.Format("--- globalId: %u   tag: (%u) %s   size: %u  children: %u  numAttrs: %u ", m_globalId, tagId, m_pTag, GetAddrNextNode() - myAddr, m_numChildren, m_numAttrs);
  476.  
  477.         if (m_numAttrs > 0)
  478.         {
  479.                 str += "ATTRS: ";
  480.  
  481.                 CAttrReader attr(m_Reader);
  482.                 FlatAddr nextAddr = m_addrFirstAttr;
  483.  
  484.                 for (uint32 a = 0; a < m_numAttrs; a++)
  485.                 {
  486.                         attr.InitFromCompact(nextAddr, GetHeaderAttr(a));
  487.                         nextAddr = attr.GetAddrNextAttr();
  488.                         string strAttr;
  489.                         string strAttrVal;
  490.                         attr.GetValueAsString(strAttrVal);
  491.                         strAttr.Format("<(%u) '%s'='%s'> ", attr.GetNameId(), attr.GetName(), strAttrVal.c_str());
  492.                         str += strAttr;
  493.                 }
  494.         }
  495.         CryLog("%s", str.c_str());
  496. }
  497. #endif
  498.  
downloadXMLCPB_NodeLiveReader.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top