Advertisement
podoko_Lua

xml_unpack [v1.0]

Aug 28th, 2014
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.31 KB | None | 0 0
  1. --[[ xml_unpack [v1.0]
  2.    
  3.     Ce module contient une fonction permettant de décompiler le code xml d'une carte puis de mettre toutes les informations le concernant dans une table, il peut être très facilement ajouté à un script plus important puisqu'il n'utilise pas de fonction de l'API lua de transformice.
  4.     La fonction xml_unpack.loadXml reçoit le code xml de la carte à traiter et renvoie la table contenant les informations sur le code.
  5.    
  6.     Variables globales utilisées :
  7.         xml_unpack
  8.         string.split
  9.    
  10.     N.B : ce module est inspiré d'une partie de l'excellent XML Editor : http://atelier801.com/topic?f=6&t=520844
  11.     N.B² : Il existe une version de ce module couplé à l'Explorateur Lua permet de savoir à quoi ressemble le tableau renvoyé une fois le code xml traité : http://pastebin.com/muWAf1Dh
  12. ]]
  13.  
  14.  
  15. xml_unpack = {}
  16.  
  17. function xml_unpack.loadXml(code)
  18.     local xml = {}
  19.    
  20.     xml.xml = code
  21.     xml.xPropFull = xml.xml:match('<P .->')
  22.     xml.xProp = {}
  23.     for prop, val in xml.xPropFull:gmatch('(%S+)="(%S-)"') do xml.xProp[prop] = val or '' end
  24.    
  25.    
  26.     xml.xGroundFull = xml.xml:match('<S>.-</S>',#xml.xProp)
  27.     xml.xGround = { id={}, type={} }
  28.     if xml.xGroundFull then
  29.         local way, id = xml.xGround.id, 0
  30.         local wayB = xml.xGround.type
  31.         for str in xml.xGroundFull:gmatch('<S .-/>') do
  32.             way[id] = {xml=str}
  33.            
  34.             xml_unpack.unpackProp(way[id], str)
  35.            
  36.             local T = tonumber(way[id].T) or 0
  37.             if not wayB[T] then wayB[T] = {} end
  38.             wayB[T][id] = way[id]
  39.            
  40.             id = id+1
  41.         end
  42.     end
  43.    
  44.    
  45.     xml.xDecoFull = xml.xml:match('<D>.-</D>')
  46.     xml.xDeco = { id={}, T={}, F={}, DS={}, DC={}, P={}, type={} }
  47.     if xml.xDecoFull then
  48.         local way, id = xml.xDeco.id, 0
  49.         local wayB = xml.xDeco
  50.         for str in xml.xDecoFull:gmatch('<%a%a? .-/>') do
  51.             way[id] = {xml=str}
  52.            
  53.             xml_unpack.unpackProp(way[id], str)
  54.            
  55.             wayB[str:match('%a%a?')][id] = way[id]
  56.             id = id+1
  57.         end
  58.         local wayD = xml.xDeco.type
  59.         for id, prop in pairs(wayB.P) do
  60.             local T = tonumber(prop.T) or 0
  61.             if not wayD[T] then wayD[T] = {} end
  62.             wayD[T][id] = prop
  63.         end
  64.        
  65.     end
  66.    
  67.    
  68.     xml.xObjectFull = xml.xml:match('<O>.-</O>')
  69.     xml.xObject = { id={}, type={} }
  70.     if xml.xObjectFull then
  71.         local way, id = xml.xObject.id, 0
  72.         local wayB = xml.xObject.type
  73.         for str in xml.xObjectFull:gmatch('<O .-/>') do
  74.             way[id] = {xml=str}
  75.            
  76.             xml_unpack.unpackProp(way[id], str)
  77.            
  78.             local C = tonumber(way[id].C) or 0
  79.             if not wayB[C] then wayB[C] = {} end
  80.             wayB[C][id] = way[id]
  81.            
  82.             id = id+1
  83.         end
  84.     end
  85.    
  86.    
  87.     xml.xJointFull = xml.xml:match('<L>.-</L>')
  88.     xml.xJoint = { id={}, JD={}, JP={}, JR={}, JPL={} }
  89.     if xml.xJointFull then
  90.         local way, id = xml.xJoint.id, 0
  91.         local wayB = xml.xJoint
  92.         for str in xml.xJointFull:gmatch('<J.- />') do
  93.             way[id] = {xml=str}
  94.            
  95.             xml_unpack.unpackProp(way[id], str)
  96.             way[id].type = str:match('%a%a%a?')
  97.            
  98.             wayB[way[id].type][id] = way[id]
  99.             id = id+1
  100.         end
  101.     end
  102.     return xml
  103. end
  104.  
  105.  
  106. function xml_unpack.unpackProp(tab, str)
  107.     for prop, val in str:gmatch('([^%s"]+)="([^%s"]-)"') do
  108.         if val:find(',') then tab[prop]=string.split(val, ',') else tab[prop] = val or '' end
  109.     end
  110. end
  111.  
  112. function string.split(str, del)
  113.     local del=del or ','
  114.     local res={}
  115.     for s in str:gmatch('[^'..del..']+') do
  116.         table.insert(res, s)
  117.     end
  118.     return res
  119. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement