Example #1
0
        // Return attribute key and value by attribute index.
        public override bool getAttributeByIndex(int index, out string key, out string value)
        {
            key = ""; value = "";

            CryXMLNode pNode = _node( );

            if (index >= 0 && index < pNode.nAttributeCount)
            {
                CryXMLAttribute attr = m_pData.pAttributes[pNode.nFirstAttributeIndex + index];
                key   = _string(attr.nKeyStringOffset);
                value = _string(attr.nValueStringOffset);
                return(true);
            }
            return(false);
        }
Example #2
0
        /// <summary>
        /// Reads the binary buffer into the internal structures
        ///  Note: use GetErrorDescription() if the result is not Success
        /// </summary>
        /// <param name="fileContents">An filled byte buffer</param>
        /// <param name="result">Out: the result of the collection of file content</param>
        /// <returns>A valid memory context or null</returns>
        private CryXmlBinContext Create(byte[] fileContents, out EResult result)
        {
            result = EResult.Error;

            CryXmlBinContext pData = new CryXmlBinContext( );

            if (pData == null)
            {
                SetErrorDescription("Can't allocate memory for binary XML object.");
                return(null);
            }

            CryXMLHeader header = Conversions.ByteToType <CryXMLHeader>(fileContents); // reads the header, mapping from binary to struct

            if (!header.HasCorrectSignature)
            {
                SetErrorDescription("File is not a binary XML object (wrong header signature).");
                return(null);
            }

            // Create binary nodes to wrap the ones to read later
            pData.pBinaryNodes = new List <CryXmlBinNode>( ); // dynamic list used, not an array
            for (int i = 0; i < header.nNodeCount; i++)
            {
                pData.pBinaryNodes.Add(new CryXmlBinNode( ));
            }

            // map file content to binary.. here we really allocate arrays of elements and copy rather than the original which worked well with ptrs in c++

            try {
                pData.pAttributes = ( CryXMLAttribute[] )Array.CreateInstance(typeof(CryXMLAttribute), header.nAttributeCount); // alloc enough
                UInt32 incr = CryXMLAttribute.MySize( );                                                                        // size of one element to read
                for (UInt32 aIdx = 0; aIdx < header.nAttributeCount; aIdx++)
                {
                    pData.pAttributes[aIdx] = Conversions.ByteToType <CryXMLAttribute>(fileContents, header.nAttributeTablePosition + aIdx * incr);
                }
            } catch (Exception e) {
                SetErrorDescription(string.Format("EXC Attributes: {0}", e.Message));
                return(null);
            }

            try {
                pData.pChildIndices = ( CryXMLNodeIndex[] )Array.CreateInstance(typeof(CryXMLNodeIndex), header.nChildCount); // alloc enough
                UInt32 incr = CryXMLNodeIndex.MySize( );                                                                      // size of one element to read
                for (UInt32 aIdx = 0; aIdx < header.nChildCount; aIdx++)
                {
                    pData.pChildIndices[aIdx] = Conversions.ByteToType <CryXMLNodeIndex>(fileContents, header.nChildTablePosition + aIdx * incr);
                }
            } catch (Exception e) {
                SetErrorDescription(string.Format("EXC ChildIndices: {0}", e.Message));
                return(null);
            }

            try {
                pData.pNodes = ( CryXMLNode[] )Array.CreateInstance(typeof(CryXMLNode), header.nNodeCount); // alloc enough
                UInt32 incr = CryXMLNode.MySize( );                                                         // size of one element to read
                for (UInt32 aIdx = 0; aIdx < header.nNodeCount; aIdx++)
                {
                    pData.pNodes[aIdx] = Conversions.ByteToType <CryXMLNode>(fileContents, header.nNodeTablePosition + aIdx * incr);
                }
            } catch (Exception e) {
                SetErrorDescription(string.Format("EXC Nodes: {0}", e.Message));
                return(null);
            }

            try {
                pData.pStringDataLength = header.nStringDataSize;
                pData.pStringData       = fileContents.SliceL(header.nStringDataPosition, header.nStringDataSize);
            } catch (Exception e) {
                SetErrorDescription(string.Format("EXC StringData: {0}", e.Message));
                return(null);
            }

            // init binary nodes
            for (UInt32 nNode = 0; nNode < header.nNodeCount; ++nNode)
            {
                pData.pBinaryNodes[( int )nNode].m_pData      = pData; // add data space
                pData.pBinaryNodes[( int )nNode].m_pNodeIndex = nNode; // self ref..
            }

            // and back..
            result = EResult.Success;
            return(pData);
        }