Advertisement
podoko_Lua

xml_unpack [v1.0] + Explorateur Lua [v1.1]

Aug 28th, 2014
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.10 KB | None | 0 0
  1. --[[ xml_unpack [v1.0] + Explorateur Lua [v1.1]
  2.    
  3.     Ce module est une version de xml_unpack couplé au module Explorateur Lua permettant de connaitre toutes les informations sur une carte.
  4.    
  5.     Commandes :
  6.         !loadXml (permet de charger les informations concernant le code xml d'une carte, le tableau correspondant se trouve dans la variable xml_unpack.xml (à afficher grâce à l'Explorateur Lua)
  7.    
  8.    
  9.     N.B : ce module est inspiré d'une partie de l'excellent XML Editor : http://atelier801.com/topic?f=6&t=520844
  10.     N.B² : Il existe une version de ce module sans l'Explorateur Lua qui contient le strict nescessaire pour décompiler un code xml, simple à ajouter à un module plus grand : http://pastebin.com/rK0hCyik
  11.    
  12.  ]]
  13.  
  14.  
  15. --[[ Module exploLua (début)
  16.     Merci de ne rien modifier
  17. ]]
  18. local exploLua = {}
  19.  
  20. function exploLua.init()
  21.     exploLua.info = {}
  22.     for name in pairs(tfm.get.room.playerList) do exploLua.newP(name) end
  23. end
  24.  
  25. function exploLua.newP(name)
  26.     exploLua.info[name] = {}
  27.     exploLua.info[name].path = {"_G"}
  28.     ui.addTextArea(2112, "<a href='event:open'>open</a>", name, 5, 377, 50, 18, 0x27373f, 0x27373f, 1, true)
  29. end
  30.  
  31.  
  32. function exploLua.openWind(name)
  33.    
  34.     ui.addTextArea(2113, "<b><p align='right'><a href='event:'>X</a></p></b>", name, 150, 50, 500, 300, 0x27373f, 0x27373f, 1, true)    -- fond1
  35.     ui.addTextArea(2114, "", name, 150, 75, 500, 275, 0x324650, 0x27373f, 1, true)  -- fond2
  36.     ui.addTextArea(2115, "", name, 175, 85, 450, 20, 0x27373f, 0x27373f, 1, true)   -- chemin
  37.     ui.addTextArea(2116, "", name, 175, 120, 140, 200, 0x27373f, 0x27373f, 1, true) -- liste noms
  38.     ui.addTextArea(2117, "", name, 330, 120, 140, 200, 0x27373f, 0x27373f, 1, true) -- liste types
  39.     ui.addTextArea(2118, "", name, 485, 120, 140, 200, 0x27373f, 0x27373f, 1, true) -- liste valeurs
  40.    
  41.     eventTextAreaCallback(2115, name, #exploLua.info[name].path)
  42. end
  43.  
  44. function exploLua.closeWind(name)
  45.     ui.removeTextArea(2113, name); ui.removeTextArea(2114, name); ui.removeTextArea(2115, name); ui.removeTextArea(2116, name); ui.removeTextArea(2117, name); ui.removeTextArea(2118, name)
  46.     ui.addTextArea(2112, "<a href='event:open'>open</a>", name, 5, 377, 50, 20, 0x27373f, 0x27373f, 1, true)
  47. end
  48.  
  49. function exploLua.open(name)
  50.     local path = exploLua.info[name].path
  51.     local tab, path = exploLua.folder(table.copy(exploLua.info[name].path))
  52.     exploLua.info[name].path = path
  53.     local files = ""
  54.     local typ = ""
  55.     local values = ""
  56.     local way = ""
  57.    
  58.     for key, val in pairs(tab) do
  59.         files = files.."<a href='event:"..tostring(key).."'>"..tostring(key).."</a>\n"
  60.         values = values..tostring(val)..'\n'
  61.         typ = typ..type(val)..'\n'
  62.     end
  63.     for key, val in ipairs(path) do
  64.         way = way.."<a href='event:"..tostring(key).."'>"..val..".</a>"
  65.     end
  66.    
  67.     ui.updateTextArea(2115, way, name)
  68.     ui.updateTextArea(2116, files, name)
  69.     ui.updateTextArea(2117, typ, name)
  70.     ui.updateTextArea(2118, values, name)
  71. end
  72.  
  73.  
  74. function exploLua.folder(path, tab, way)
  75.    
  76.     tab = tab or _G
  77.     way = way or {"_G"}
  78.    
  79.     if path[2] then
  80.         if type(tab[path[2]]) == "table" then
  81.             tab = tab[path[2]]
  82.             table.insert(way, path[2])
  83.             table.remove(path, 2)
  84.             return exploLua.folder(path, tab, way)
  85.         else
  86.             local code = string.gsub (string.gsub (tostring(tab[path[2]]), '<', '&lt;'), '>', '&gt;')
  87.             local comp = 0
  88.             while comp < #code do
  89.                 print(code:sub(comp, comp+4500))
  90.                 comp = comp+4501
  91.             end
  92.            
  93.         end
  94.     end
  95.     return tab, way
  96. end
  97.  
  98. function table.copy(tab)
  99.     local res = {}
  100.     for key, val in pairs(tab) do res[key] = val end
  101.     return res
  102. end
  103.  
  104. --[[Module exploLua (fin)
  105.     Vous pouvez modifier les fonctions qui suivent
  106.     (attention à ne pas empècher les tests de la fonction eventTextAreaCallback)
  107. ]]
  108.  
  109.  
  110.  
  111. function eventTextAreaCallback(id, name, call)
  112.    
  113.     --
  114.  
  115. -- Module exploLua (début)
  116.     if id==2115 then
  117.         local i = #exploLua.info[name].path - tonumber(call)
  118.         for k=1,i do table.remove(exploLua.info[name].path) end
  119.         exploLua.open(name)
  120.     elseif id == 2116 then
  121.         table.insert(exploLua.info[name].path, tonumber(call) or call)
  122.         exploLua.open(name)
  123.     elseif id == 2112 then
  124.         ui.removeTextArea(2112, name)
  125.         exploLua.openWind(name)
  126.     elseif id == 2113 then exploLua.closeWind(name)
  127.     end
  128. -- Module exploLua (fin)
  129. end
  130. -- Ligne d'initialisation du module exploLua
  131. exploLua.init()
  132.  
  133.  
  134.  
  135.  
  136. -------------------------- Module xml_unpack
  137.  
  138.  
  139. xml_unpack = {}
  140.  
  141.  
  142. function xml_unpack.loadXml(code)
  143.     local xml = {}
  144.    
  145.     xml.xml = code
  146.     xml.xPropFull = xml.xml:match('<P .->')
  147.     xml.xProp = {}
  148.     for prop, val in xml.xPropFull:gmatch('(%S+)="(%S-)"') do xml.xProp[prop] = val or '' end
  149.    
  150.    
  151.     xml.xGroundFull = xml.xml:match('<S>.-</S>',#xml.xProp)
  152.     xml.xGround = { id={}, type={} }
  153.     if xml.xGroundFull then
  154.         local way, id = xml.xGround.id, 0
  155.         local wayB = xml.xGround.type
  156.         for str in xml.xGroundFull:gmatch('<S .-/>') do
  157.             way[id] = {xml=str}
  158.            
  159.             xml_unpack.unpackProp(way[id], str)
  160.            
  161.             local T = tonumber(way[id].T) or 0
  162.             if not wayB[T] then wayB[T] = {} end
  163.             wayB[T][id] = way[id]
  164.            
  165.             id = id+1
  166.         end
  167.     end
  168.    
  169.    
  170.     xml.xDecoFull = xml.xml:match('<D>.-</D>')
  171.     xml.xDeco = { id={}, T={}, F={}, DS={}, DC={}, P={}, type={} }
  172.     if xml.xDecoFull then
  173.         local way, id = xml.xDeco.id, 0
  174.         local wayB = xml.xDeco
  175.         for str in xml.xDecoFull:gmatch('<%a%a? .-/>') do
  176.             way[id] = {xml=str}
  177.            
  178.             xml_unpack.unpackProp(way[id], str)
  179.            
  180.             wayB[str:match('%a%a?')][id] = way[id]
  181.             id = id+1
  182.         end
  183.         local wayD = xml.xDeco.type
  184.         for id, prop in pairs(wayB.P) do
  185.             local T = tonumber(prop.T) or 0
  186.             if not wayD[T] then wayD[T] = {} end
  187.             wayD[T][id] = prop
  188.         end
  189.        
  190.     end
  191.    
  192.    
  193.     xml.xObjectFull = xml.xml:match('<O>.-</O>')
  194.     xml.xObject = { id={}, type={} }
  195.     if xml.xObjectFull then
  196.         local way, id = xml.xObject.id, 0
  197.         local wayB = xml.xObject.type
  198.         for str in xml.xObjectFull:gmatch('<O .-/>') do
  199.             way[id] = {xml=str}
  200.            
  201.             xml_unpack.unpackProp(way[id], str)
  202.            
  203.             local C = tonumber(way[id].C) or 0
  204.             if not wayB[C] then wayB[C] = {} end
  205.             wayB[C][id] = way[id]
  206.            
  207.             id = id+1
  208.         end
  209.     end
  210.    
  211.    
  212.     xml.xJointFull = xml.xml:match('<L>.-</L>')
  213.     xml.xJoint = { id={}, JD={}, JP={}, JR={}, JPL={} }
  214.     if xml.xJointFull then
  215.         local way, id = xml.xJoint.id, 0
  216.         local wayB = xml.xJoint
  217.         for str in xml.xJointFull:gmatch('<J.- />') do
  218.             way[id] = {xml=str}
  219.            
  220.             xml_unpack.unpackProp(way[id], str)
  221.             way[id].type = str:match('%a%a%a?')
  222.            
  223.             wayB[way[id].type][id] = way[id]
  224.             id = id+1
  225.         end
  226.     end
  227.     return xml
  228. end
  229.  
  230.  
  231. function xml_unpack.unpackProp(tab, str)
  232.     for prop, val in str:gmatch('([^%s"]+)="([^%s"]-)"') do
  233.         if val:find(',') then tab[prop]=string.split(val, ',') else tab[prop] = val or '' end
  234.     end
  235. end
  236.  
  237.  
  238.  
  239.  
  240.  
  241. function string.split(str, del)
  242.     local del=del or ','
  243.     local res={}
  244.     for s in str:gmatch('[^'..del..']+') do
  245.         table.insert(res, s)
  246.     end
  247.     return res
  248. end
  249.  
  250.  
  251.  
  252.  
  253. function eventChatCommand(name, command)
  254.     if command == "loadXml" then
  255.         if tfm.get.room.xmlMapInfo then
  256.             xml_unpack.xml = xml_unpack.loadXml(tfm.get.room.xmlMapInfo.xml)
  257.             print('xml chargé')
  258.         else
  259.             print('xml non chargé : pas de xml disponible.')
  260.         end
  261.     end
  262. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement