home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 10933 / XML_binary_reader_2.7z / XML_binary_reader_2.ms
Encoding:
Text File  |  2016-05-12  |  10.8 KB  |  331 lines

  1. --joqqyhez@gmail.com
  2.  
  3. --/*
  4. --////// FileDialog //////
  5. fname = getOpenFileName \ 
  6. caption:"Mtl File" \
  7. types:"CryTek mtl File(*.mtl)|*.mtl" \
  8. historyCategory:"CryTek mtl Presets"
  9. f = fopen fname "rb"
  10. p = getFilenamePath fname -- return the path "c:\\test\\"
  11. CryXmlB_path = fname -- return the file "test"
  12. --*/
  13.  
  14.  
  15. xmlB_arr = #( \
  16.                     #(),     --Binary XML header        [1]
  17.                     #(),     --node structs            [2]
  18.                     #(),     --child index                [3]
  19.                     #()    --attrib_child_indices    [4]
  20.                 )
  21.  
  22. --XMLBinaryHeaders.h---------------------
  23. struct _Node
  24. (
  25.     nTagStringOffset             = 0,      --uint32 --offset in CBinaryXmlData::pStringData
  26.         nTagString                    = "",
  27.     nContentStringOffset         = 0,       --uint32 --offset in CBinaryXmlData::pStringData
  28.     nAttributeCount                 = 0,        --uint16
  29.     nChildCount                     = 0,        --uint16
  30.     nParentIndex                     = 0,         --uint32(p?) -1 prob means root node    --NodeIndex          typedef uint32 NodeIndex;  // note: only uint32 or uint16 are supported
  31.     nFirstAttributeIndex             = 0,      --uint32(p?)                --NodeIndex         typedef uint32 NodeIndex;  // note: only uint32 or uint16 are supported
  32.     nFirstChildIndex                = 0,        --uint32(p?)                --NodeIndex         typedef uint32 NodeIndex;  // note: only uint32 or uint16 are supported
  33.     
  34.     Pad                                 = 1         --int32(p?) (1 readlong)    --Pad<sizeof(uint32) - sizeof(NodeIndex)> reserved_for_alignment; 
  35. )
  36.  
  37. struct Attribute
  38. (
  39.     nKeyStringOffset = 0,                --uint32 --offset in CBinaryXmlData::pStringData
  40.     nValueStringOffset = 0              --uint32 --offset in CBinaryXmlData::pStringData
  41. )
  42.  
  43. struct BinaryFileHeader
  44. (
  45.     szSignature                 = "",         --char [8]
  46.     nXMLSize                     = 0,         --uint32
  47.     nNodeTablePosition         = 0,         --uint32
  48.     nNodeCount                 = 0,        --uint32
  49.     nAttributeTablePosition = 0,         --uint32
  50.     nAttributeCount             = 0,         --uint32
  51.     nChildTablePosition         = 0,         --uint32
  52.     nChildCount                 = 0,         --uint32
  53.     nStringDataPosition         = 0,        --uint32
  54.     nStringDataSize            = 0         --uint32
  55. )
  56. ----------------------------------------------
  57.  
  58. szSignature = "CryXmlB"
  59.  
  60. fn ReadFixedString \
  61.                             bstream \
  62.                             fixedLen =
  63. (
  64.     local str = ""
  65.     for i = 1 to fixedLen do
  66.     (
  67.         str += bit.intAsChar (ReadByte bstream #unsigned)
  68.     )
  69.     return str
  70. )
  71.  
  72. fn read_BinaryFileHeader  \
  73.                                     bstream \
  74.                                     struct2fill \
  75.                                     signature2check =
  76. (
  77.     --if the signature is "CryXmlB", then we can proceed
  78.     if struct2fill.szSignature == signature2check then
  79.     (
  80.         format "Correct signature, we continue parsing...\n"    
  81.         
  82.         struct2fill.nXMLSize                         = readlong bstream #unsigned            
  83.         struct2fill.nNodeTablePosition         = readlong bstream #unsigned        
  84.         struct2fill.nNodeCount                     = readlong bstream #unsigned        
  85.         struct2fill.nAttributeTablePosition     = readlong bstream #unsigned
  86.         struct2fill.nAttributeCount             = readlong bstream #unsigned
  87.         struct2fill.nChildTablePosition            = readlong bstream #unsigned
  88.         struct2fill.nChildCount                     = readlong bstream #unsigned
  89.         struct2fill.nStringDataPosition         = readlong bstream #unsigned
  90.         struct2fill.nStringDataSize                 = readlong bstream #unsigned            
  91.         
  92.         --Append header to array
  93.         Append xmlB_arr[1] struct2fill
  94.     )
  95.     else
  96.     (
  97.         format "Wrong signature. Signature must be %. We cannot continue - aborting!" signature2check
  98.     )
  99. )
  100.  
  101. fn read_Node \
  102.                     bstream \                    
  103.                     struct2read =
  104. (
  105.     for i=1 to struct2read.nNodeCount do
  106.     (
  107.         --create struct instance
  108.         struct2fill = _Node()
  109.         
  110.         struct2fill.nTagStringOffset = readlong bstream #unsigned        
  111.         struct2fill.nContentStringOffset = readlong bstream #unsigned        
  112.         struct2fill.nAttributeCount = readshort bstream #unsigned        
  113.         struct2fill.nChildCount = readshort bstream #unsigned
  114.         
  115.         ---------------------------------- Nodeindices ----------------------------------
  116.         struct2fill.nParentIndex = readlong bstream #unsigned    
  117.         struct2fill.nFirstAttributeIndex = readlong bstream #unsigned
  118.         struct2fill.nFirstChildIndex = readlong bstream #unsigned            
  119.         
  120.         --pad
  121.         for i=1 to struct2fill.Pad do
  122.         (
  123.             readlong bstream
  124.         )
  125.         
  126.         --Append all the node structs to the array
  127.         Append xmlB_arr[2] struct2fill
  128.     )
  129. )
  130.  
  131. fn read_nChildTable \
  132.                             bstream \
  133.                             struct2read =
  134. (
  135.     fseek bstream struct2read.nChildTablePosition #seek_set
  136.     
  137.     for i=1 to struct2read.nChildCount do
  138.     (
  139.         childIndex = readlong bstream #unsigned
  140.         
  141.         Append xmlB_arr[3] childIndex
  142.     )    
  143. )
  144.  
  145. struct attrib_child_indices
  146. (
  147.     nFirstAttributeIndex         = 0,
  148.     nFirstChildIndex             = 0,
  149.     
  150.     nFirstAttribute_val         = "",
  151.     nFirstChild_val             = ""
  152. )
  153.  
  154. fn read_nTagString \
  155.                             bstream \
  156.                             struct2read =
  157. (    
  158.     for i=1 to xmlB_arr[2].count do
  159.     (
  160.         fseek bstream (struct2read.nStringDataPosition + xmlB_arr[2][i].nTagStringOffset)  #seek_set        
  161.     
  162.         xmlB_arr[2][i].nTagString = ReadString bstream
  163.     )
  164. )
  165.  
  166. fn read_nAttributeTable \
  167.                                     bstream \
  168.                                     struct2read =
  169. (
  170.     fseek bstream struct2read.nAttributeTablePosition #seek_set
  171.     
  172.     for i=1 to struct2read.nAttributeCount do
  173.     (
  174.         struct2fill = attrib_child_indices()
  175.         
  176.         --These are the offsets from where we read the strings, starting from the nStringDataPosition offset (last chunk of the file)
  177.         struct2fill.nFirstAttributeIndex     =         (readlong bstream #unsigned)
  178.         struct2fill.nFirstChildIndex             =         (readlong bstream #unsigned)    
  179.         
  180.         Append xmlB_arr[4] struct2fill
  181.     )
  182.     
  183.     for i=1 to struct2read.nAttributeCount do
  184.     (
  185.         fseek bstream struct2read.nStringDataPosition #seek_set        
  186.             fseek bstream xmlB_arr[4][i].nFirstAttributeIndex #seek_cur
  187.                 xmlB_arr[4][i].nFirstAttribute_val = ReadString bstream
  188.         
  189.         fseek bstream struct2read.nStringDataPosition #seek_set
  190.             fseek bstream xmlB_arr[4][i].nFirstChildIndex #seek_cur
  191.                 xmlB_arr[4][i].nFirstChild_val = ReadString bstream
  192.     )
  193. )
  194.  
  195. --////////////// Open binary stream //////////////
  196. xbs = fopen CryXmlB_path "rb"
  197. -------------------------------------------
  198.  
  199. inst_BinaryFileHeader = BinaryFileHeader()
  200. inst_BinaryFileHeader.szSignature = ReadFixedString xbs 8
  201.  
  202.  
  203. clearlistener()
  204. ------        ------        ------        ------        ------        ------        ------        ------        ------        ------        ------        ------        ------
  205. format "inst_BinaryFileHeader.szSignature: %\n" inst_BinaryFileHeader.szSignature
  206. ------        ------        ------        ------        ------        ------        ------        ------        ------        ------        ------        ------        ------
  207.  
  208.  
  209. read_BinaryFileHeader \
  210.                                 xbs \
  211.                                 inst_BinaryFileHeader \
  212.                                 szSignature
  213.  
  214. read_Node \
  215.                 xbs \
  216.                 inst_BinaryFileHeader
  217.  
  218. read_nChildTable \
  219.                         xbs \
  220.                         inst_BinaryFileHeader
  221.  
  222. read_nAttributeTable \
  223.                                 xbs \
  224.                                 inst_BinaryFileHeader
  225.  
  226. read_nTagString \
  227.                         xbs \
  228.                         inst_BinaryFileHeader
  229.  
  230.  
  231. ftell xbs
  232. --////////////// Close binarystream //////////////
  233. fclose xbs
  234.  
  235.  
  236. fn debug_xmlB_arr \
  237.                         xmlArr =
  238. (    
  239.     ---------    ---------    ---------    ---------    ---------    ---------    ---------    ---------    ---------    ---------    ---------
  240.     --     xmlB_arr = #( \
  241.     --                     #(),     --Binary XML header        [1]
  242.     --                     #(),     --node structs            [2]
  243.     --                     #(),     --child index                [3]
  244.     --                     #()    --attrib_child_indices    [4]
  245.     --                 )
  246.  
  247.     (
  248.         format "\n\n\n REPORT - remember all indices are zero based (must be converted to 1 based in max\n\n"
  249.         format "---------------------------This is the header table info-------------------------------\n\n"
  250.         for i=1 to xmlArr[1].count do
  251.         (
  252.             format "struct2fill.nXMLSize: % \n"                                                                                         xmlArr[1][i].nXMLSize
  253.             format "struct2fill.nNodeTablePosition: % \n"                                                                            xmlArr[1][i].nNodeTablePosition
  254.             format "struct2fill.nNodeCount: % \n"                                                                                     xmlArr[1][i].nNodeCount
  255.             format "struct2fill.nAttributeTablePosition: % \n"                                                                    xmlArr[1][i].nAttributeTablePosition
  256.             format "struct2fill.nAttributeCount: % \n"                                                                             xmlArr[1][i].nAttributeCount
  257.             format "struct2fill.nChildTablePosition: % \n"                                                                         xmlArr[1][i].nChildTablePosition
  258.             format "struct2fill.nChildCount: % (Root NOT included-so these are the children for the root)\n"     xmlArr[1][i].nChildCount
  259.             format "struct2fill.nStringDataPosition: % \n"                                                                            xmlArr[1][i].nStringDataPosition
  260.             format "struct2fill.nStringDataSize: % \n"                                                                             xmlArr[1][i].nStringDataSize
  261.         )
  262.         format "-------------------------------------------------------------------------------------\n\n\n"
  263.  
  264.         format "-----------These are the children (the node with nParentIndex: -1 is the root node)-----------\n\n"
  265.         for i=1 to xmlArr[2].count do
  266.         (
  267.             tab = "\t" --for children
  268.             
  269.             if xmlArr[2][i].nFirstChildIndex == 0 AND xmlArr[2][i].nParentIndex == -1 then --Root
  270.             (
  271.                 tab = ""
  272.             )
  273.             -------------------------
  274.             format "%\t struct2fill.nTagStringOffset: %\n"             tab xmlArr[2][i].nTagStringOffset
  275.                 format "%\t struct2fill.nTagStringOffset: <%>\n"     tab xmlArr[2][i].nTagString
  276.             format "%\t struct2fill.nContentStringOffset: %\n"     tab xmlArr[2][i].nContentStringOffset
  277.             format "%\t struct2fill.nAttributeCount: %\n"             tab xmlArr[2][i].nAttributeCount
  278.             format "%\t struct2fill.nChildCount: %   (this is the count from xmlArr[3][i] <---this val counts these nodes from the starting node (see last for each of these nodes) \n"                     tab xmlArr[2][i].nChildCount
  279.             format "%\t struct2fill.nParentIndex: %\n"                 tab xmlArr[2][i].nParentIndex
  280.             format "%\t struct2fill.nFirstAttributeIndex: %\n"         tab xmlArr[2][i].nFirstAttributeIndex
  281.             format "%\t struct2fill.nFirstChildIndex: %   (xmlArr[3][i] <---this val, points to the starting node in these nodes (children to the root) here) \n"             tab xmlArr[2][i].nFirstChildIndex
  282.             -------------------------
  283.             format "\n"
  284.         )    
  285.         format "------------------------------------------------------------------------------------------\n\n\n"
  286.  
  287.  
  288.         format "-----------These are the childrens to the root (i.e. the root is not included here!)-----------\n\n"
  289.         for i=1 to xmlArr[3].count do
  290.         (
  291.             format "nFirstChildIndex %: \n" xmlArr[3][i]
  292.         )
  293.         format "------------------------------------------------------------------------------------------\n\n\n"
  294.  
  295.  
  296.  
  297.  
  298.         format "-------------These are the attributes (the root will reference these as well)-------------\n\n"
  299.         for i=1 to xmlArr[4].count do
  300.         (
  301.             format     "nFirstAttributeIndex %: \n"     xmlArr[4][i].nFirstAttributeIndex
  302.             format     "nFirstChildIndex %: \n"         xmlArr[4][i].nFirstChildIndex
  303.             format "\n"
  304.         )
  305.         format "--------------------------------------------------------------------------------------\n"
  306.         
  307.         for i=1 to xmlArr[4].count do
  308.         (
  309.             format     "nFirstAttributeIndex %: \n"     xmlArr[4][i].nFirstAttributeIndex
  310.                 format     "\t nFirstAttribute_val %: \n"     xmlArr[4][i].nFirstAttribute_val
  311.             format "\n"
  312.             
  313.             format     "nFirstChildIndex %: \n"         xmlArr[4][i].nFirstChildIndex    
  314.                 format     "\t nFirstChild_val %: \n"     xmlArr[4][i].nFirstChild_val
  315.             format "\n"
  316.             
  317.         )
  318.         
  319.         format "END OF REPORT\n\n\n"
  320.     )
  321.     ---------    ---------    ---------    ---------    ---------    ---------    ---------    ---------    ---------    ---------    ---------
  322. )
  323.  
  324. debug_xmlB_arr \
  325.                     xmlB_arr
  326.  
  327.  
  328.  
  329.  
  330.  
  331.