Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ xml_unpack [v1.0]
- 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.
- La fonction xml_unpack.loadXml reçoit le code xml de la carte à traiter et renvoie la table contenant les informations sur le code.
- Variables globales utilisées :
- xml_unpack
- string.split
- N.B : ce module est inspiré d'une partie de l'excellent XML Editor : http://atelier801.com/topic?f=6&t=520844
- 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
- ]]
- xml_unpack = {}
- function xml_unpack.loadXml(code)
- local xml = {}
- xml.xml = code
- xml.xPropFull = xml.xml:match('<P .->')
- xml.xProp = {}
- for prop, val in xml.xPropFull:gmatch('(%S+)="(%S-)"') do xml.xProp[prop] = val or '' end
- xml.xGroundFull = xml.xml:match('<S>.-</S>',#xml.xProp)
- xml.xGround = { id={}, type={} }
- if xml.xGroundFull then
- local way, id = xml.xGround.id, 0
- local wayB = xml.xGround.type
- for str in xml.xGroundFull:gmatch('<S .-/>') do
- way[id] = {xml=str}
- xml_unpack.unpackProp(way[id], str)
- local T = tonumber(way[id].T) or 0
- if not wayB[T] then wayB[T] = {} end
- wayB[T][id] = way[id]
- id = id+1
- end
- end
- xml.xDecoFull = xml.xml:match('<D>.-</D>')
- xml.xDeco = { id={}, T={}, F={}, DS={}, DC={}, P={}, type={} }
- if xml.xDecoFull then
- local way, id = xml.xDeco.id, 0
- local wayB = xml.xDeco
- for str in xml.xDecoFull:gmatch('<%a%a? .-/>') do
- way[id] = {xml=str}
- xml_unpack.unpackProp(way[id], str)
- wayB[str:match('%a%a?')][id] = way[id]
- id = id+1
- end
- local wayD = xml.xDeco.type
- for id, prop in pairs(wayB.P) do
- local T = tonumber(prop.T) or 0
- if not wayD[T] then wayD[T] = {} end
- wayD[T][id] = prop
- end
- end
- xml.xObjectFull = xml.xml:match('<O>.-</O>')
- xml.xObject = { id={}, type={} }
- if xml.xObjectFull then
- local way, id = xml.xObject.id, 0
- local wayB = xml.xObject.type
- for str in xml.xObjectFull:gmatch('<O .-/>') do
- way[id] = {xml=str}
- xml_unpack.unpackProp(way[id], str)
- local C = tonumber(way[id].C) or 0
- if not wayB[C] then wayB[C] = {} end
- wayB[C][id] = way[id]
- id = id+1
- end
- end
- xml.xJointFull = xml.xml:match('<L>.-</L>')
- xml.xJoint = { id={}, JD={}, JP={}, JR={}, JPL={} }
- if xml.xJointFull then
- local way, id = xml.xJoint.id, 0
- local wayB = xml.xJoint
- for str in xml.xJointFull:gmatch('<J.- />') do
- way[id] = {xml=str}
- xml_unpack.unpackProp(way[id], str)
- way[id].type = str:match('%a%a%a?')
- wayB[way[id].type][id] = way[id]
- id = id+1
- end
- end
- return xml
- end
- function xml_unpack.unpackProp(tab, str)
- for prop, val in str:gmatch('([^%s"]+)="([^%s"]-)"') do
- if val:find(',') then tab[prop]=string.split(val, ',') else tab[prop] = val or '' end
- end
- end
- function string.split(str, del)
- local del=del or ','
- local res={}
- for s in str:gmatch('[^'..del..']+') do
- table.insert(res, s)
- end
- return res
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement