Advertisement
_pacificador

PACIFICADOR's item_proto converter - source

Jul 7th, 2014
2,470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.42 KB | None | 0 0
  1. -- Item proto converter made by PACIFICADOR.
  2. -- It converts client's item_proto (*.xml) to server's item_proto (item_proto and item_names (*.txt) or item_proto.sql)
  3.  
  4. -- To prevent copyright's change
  5. local by,ver = "PACIFICADOR","1.5"
  6.  
  7. function _error(t)
  8.     if t ~= nil then
  9.         print("\nAn error occurred.\nHint: "..t)
  10.     end
  11.     print("\nPress Enter to exit...")
  12.     io.stdin:read('*l')
  13. end
  14.  
  15. dat = io.open("ItemData.lua", "r")
  16. if not dat then
  17.     _error("Could not find ItemData.lua")
  18.     return
  19. end
  20. dat:close()
  21.  
  22. require "itemdata"  -- item's lib
  23. require "os"
  24.  
  25. print("\nItem proto converter made by "..by..". Version "..ver.."\nContact the creator of this software if you have any issue.\n")
  26. io.write("Do you want to convert it to *.txt or *.sql?\n")
  27. io.flush()
  28. answ = io.read()
  29. toTxt, toSQL = answ == "txt", answ == "sql"
  30. if not toTxt and not toSQL then
  31.     _error("You can only choose between 'txt' or 'sql'.")
  32.     return
  33. end
  34.  
  35. output = toTxt and io.open("output/item_names.txt","w") or io.open("output/item_proto.sql","w")
  36. linetab, TimStr, isTimArchiver, LocalizedName, name = {}, {"Tim N","\t<ItemDef%s"}, false, "gb2312Name", "name"
  37.  
  38. if not output then
  39.     os.execute("mkdir output")
  40.     output = toSQL and "item_proto.sql" or "item_names.txt"
  41.     io.open("output/"..output, "w")
  42. end
  43.  
  44. dmp = io.open("item_proto_dump.xml")
  45.  
  46. if not dmp then
  47.     _error("Could not find item_proto_dump.xml.")
  48.     return
  49. end
  50.  
  51. print("\nReading item_proto_dump.xml...")
  52. for line in dmp:lines() do
  53.  
  54.     if not string.find(line, "</") then
  55.         name = string.find(line, "hashName") and "hashName" or name
  56.         LocalizedName = (string.find(line, "LocalizedName") and "LocalizedName") or (string.find(line, "gb2312name") and "gb2312Name") or "name"
  57.  
  58.         -- Tim's Archiver
  59.         for i = 1, 2 do
  60.             if string.find(line, TimStr[i]) then
  61.                 line = string.gsub(line, TimStr[i], "")
  62.                 isTimArchiver = true
  63.                 break
  64.             end
  65.         end
  66.  
  67.     -- Nexus Extractor (Mr. Lucifer)
  68.     elseif string.find(line, '<!--AddonType="0"-->') or string.find(line, '<!--AddonType="99"-->') then
  69.         line = string.gsub(string.gsub(line, '\t<!--AddonType="99"-->', ''), '\t<!--AddonType="0"-->', '')
  70.     end
  71.  
  72.     line = string.gsub(string.gsub(line, string.find(line, "<Item%s") and "\t<Item%s" or "\t<ItemDef%s", ""), '"%s', '"ENTER')
  73.  
  74.     if isTimArchiver then
  75.         TimArchiverTables = {"Name","Type","SubType","Weight","Size","AntiFlags","Flags","WearFlags","ImmuneFlags","Gold","ShopBuyPrice","LimitType0","LimitValue0","LimitType1","LimitValue1","ApplyType0","ApplyValue0","ApplyType1","ApplyValue1","ApplyType2","ApplyValue2","Value0","Value1","Value2","Value3","Value4","Value5","Socket0","Socket1","Socket2","RefinedVnum","RefineSet","AlterToMagicItemPercent","Specular","GainSocketPercent"}
  76.         for i = 1, #TimArchiverTables do
  77.             if string.find(line, '"ENTER'..TimArchiverTables[i]) then
  78.                 local main = {["AntiFlags"] = "antiflag", ["Flags"] = "Flag", ["WearFlags"] = "wearflag", ["ImmuneFlags"] = "immuneflag", ["AlterToMagicItemPercent"] = "magic_pct",["GainSocketPercent"] = "socket_pct",["RefinedVnum"] = "refine_vnum",["RefineSet"] = "refine_set", ["ShopBuyPrice"] = "buy_price"}
  79.                 line = string.gsub(line, '"ENTER'..TimArchiverTables[i], '"ENTER'..(main[TimArchiverTables[i]] == nil and string.lower(TimArchiverTables[i]) or main[TimArchiverTables[i]]))
  80.             end
  81.         end
  82.     else
  83.         if string.find(line, "vnum", 1, 5) then
  84.             line = string.gsub(string.gsub(line, "vnum", "Vnum", 1, 5), '"ENTERflag', '"ENTERFlag')
  85.         end
  86.     end
  87.     line = string.gsub(line, "subtype", "subType")
  88.     if string.find(line, string.lower(LocalizedName)) then
  89.         line = string.gsub(line, string.lower(LocalizedName), LocalizedName)
  90.     end
  91.     table.insert(linetab, line)
  92. end
  93. dmp:close()
  94.  
  95. -- Separates a string in X parts (returns a table)
  96. -- Taken from Lua.org
  97. function string:split(inSplitPattern, outResults)
  98.    if not outResults then
  99.       outResults = {}
  100.    end
  101.    local theStart = 1
  102.    local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
  103.    while theSplitStart do
  104.       table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
  105.       theStart = theSplitEnd + 1
  106.       theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
  107.    end
  108.    table.insert( outResults, string.sub( self, theStart ) )
  109.    return outResults
  110. end
  111.  
  112. -- Used to get the value or string of each item_proto table
  113. function get(line, w)
  114.     local a, res = string.split(line, '"ENTER'),0
  115.     for i = 1, #a do
  116.         if string.find(a[i], w.."=") then
  117.             res = string.gsub(string.split(a[i], w.."=")[2], '"', '')
  118.         end
  119.     end
  120.     return res
  121. end
  122.  
  123. -- Used to get the value of each item_proto flag table (except immuneflag)
  124. function getf(value, w)
  125.     tmp = io.open("output/temp.txt", "w")
  126.     for i = 22, 0, -1 do
  127.         pot = 2^i
  128.         toWrite = {["antiflag"] = data.antiflag[pot],["flag"] = data.flag[pot],["wearflag"] = data.wearflag[pot]}
  129.         if pot == value then
  130.             tmp:write(toWrite[w])
  131.             break
  132.         elseif pot < value then
  133.             tmp:write(toWrite[w].." | ")
  134.             value = value - pot
  135.         end
  136.     end
  137.     tmp:close()
  138.     tmp = io.open("output/temp.txt", "r")
  139.     out = tmp:read("*l")
  140.     tmp:close()
  141.     return out
  142. end
  143.  
  144. -- Not really necessary, used for short if-statements
  145. function isnil(k) return k == nil end
  146.  
  147. -- Used to get if a weapon has SD and MD
  148. function get_addon_type(vnum)
  149.     local ret = 0
  150.     if #data.addon_type_items == 0 then return 0 end
  151.     for i = 1, #data.addon_type_items,2 do
  152.         if tonumber(vnum) >= data.addon_type_items[i] and tonumber(vnum) <= data.addon_type_items[i+1] then
  153.             ret = -1
  154.             break
  155.         end
  156.     end
  157.     return ret
  158. end
  159.  
  160. print("\nAttempt to convert "..#linetab.." lines. This may take a few seconds.\nPlease wait...\n")
  161. conv,skp = 0, 0 -- Used to count the succesfuly/ignored converted lines
  162.  
  163. -- Creates item_names.txt archive and writes it's content
  164. if toTxt then
  165.     for _,v in pairs(linetab) do
  166.         if get(v,"Vnum") ~= 0 and get(v, LocalizedName) ~= 0 then
  167.             output:write(get(v, "Vnum").."\t"..get(v, LocalizedName).."\n")
  168.         end
  169.     end
  170.     output:close()
  171.     output = io.open("output/item_proto.txt", "w")
  172. end
  173.  
  174. -- Creates item_proto.txt/.sql archive and writes it's content
  175. for _,v in pairs(linetab) do
  176.     if get(v,"Vnum") ~= 0 and get(v, name) ~= 0 then
  177.         for i = 1, 10 do
  178.             local str = tostring(4294967280+i)
  179.             if string.find(v, str) then
  180.                 v = string.gsub(v, str, tostring(-4294967296+4294967280+i))
  181.             end
  182.         end
  183.         if toSQL then
  184.             local function hexstr(str) return (string.gsub(str, "(.)", function(s) return string.format("%02X%s", string.byte(s), "") end)) end
  185.             -- (vnum,name,locale_name,type,subtype,weight,size,antiflag,flag,wearflag,immuneflag,gold,shop_buy_price,refined_vnum,refine_set,magic_pct,limittype0,limitvalue0,limittype1,limitvalue1,applytype0,applyvalue0,applytype1,applyvalue1,applytype2,applyvalue2,value0,value1,value2,value3,value4,value5,socket0,socket1,socket2,socket3,socket4,socket5,specular,socket_pct,addon_type)
  186.             output:write("INSERT INTO `item_proto` VALUES('"..get(v, "Vnum").."',0x"..hexstr(get(v, name))..",0x"..hexstr(get(v,LocalizedName))..",'"..get(v, "type").."','"..get(v, "subType").."','"..get(v,"weight").."','"..get(v, "size").."','"..get(v, "antiflag").."','"..get(v, "Flag").."','"..get(v, "wearflag").."','0','"..get(v, "gold").."','"..get(v, "buy_price").."','"..get(v, "refine_vnum").."','"..get(v, "refine_set").."','"..get(v, "magic_pct").."','"..get(v,"limittype0").."','"..get(v,"limitvalue0").."','"..get(v,"limittype1").."','"..get(v,"limitvalue1").."','"..get(v,"applytype0").."','"..get(v,"applyvalue0").."','"..get(v,"applytype1").."','"..get(v,"applyvalue1").."','"..get(v,"applytype2").."','"..get(v,"applyvalue2").."','"..get(v,"value0").."','"..get(v,"value1").."','"..get(v,"value2").."','"..get(v,"value3").."','"..get(v,"value4").."','"..get(v,"value5").."','-1','-1','-1','-1','-1','-1','"..get(v,"specular").."','"..get(v,"socket_pct").."','"..get_addon_type(get(v,"Vnum")).."');\n")
  187.         elseif toTxt then
  188.             output:write(get(v, "Vnum").."\t"..get(v, name).."\t"..data.type[tonumber(get(v, "type"))+1].."\t"..(isnil(data.subtype[tonumber(get(v, "type"))]) and 0 or data.subtype[tonumber(get(v, "type"))][tonumber(get(v, "subType"))+1]).."\t"..tonumber(get(v, "size")).."\t"..(isnil(getf(tonumber(get(v, "antiflag")), "antiflag")) and "NONE" or getf(tonumber(get(v, "antiflag")), "antiflag")).."\t"..(isnil(getf(tonumber(get(v, "Flag")), "flag")) and "NONE" or getf(tonumber(get(v, "Flag")), "flag")).."\t"..(isnil(getf(tonumber(get(v, "wearflag")), "wearflag")) and "NONE" or getf(tonumber(get(v, "wearflag")), "wearflag")).."\tNONE\t"..get(v, "gold").."\t"..get(v, "buy_price").."\t"..get(v, "refine_vnum").."\t"..get(v, "refine_set").."\t"..get(v, "magic_pct").."\t"..data.limittype[tonumber(get(v,"limittype0"))+1].."\t"..get(v,"limitvalue0").."\t"..data.limittype[tonumber(get(v,"limittype1"))+1].."\t"..get(v,"limitvalue1").."\t"..data.applytype[tonumber(get(v,"applytype0"))+1].."\t"..get(v,"applyvalue0").."\t"..data.applytype[tonumber(get(v,"applytype1"))+1].."\t"..get(v,"applyvalue1").."\t"..data.applytype[tonumber(get(v,"applytype2"))+1].."\t"..get(v,"applyvalue2").."\t"..get(v,"value0").."\t"..get(v,"value1").."\t"..get(v,"value2").."\t"..get(v,"value3").."\t"..get(v,"value4").."\t"..get(v,"value5").."\t"..get(v,"specular").."\t"..get(v,"socket_pct").."\t"..get_addon_type(get(v,"Vnum")).."\n")
  189.         end
  190.         conv = conv+1
  191.     else skp = skp+1 end
  192. end
  193.  
  194. os.remove("output/temp.txt")
  195. print("\n"..conv.."/"..#linetab.." lines have been converted succesfuly.")
  196. print(skp.."/"..#linetab.." lines have been ignored.")
  197. output:close()
  198. _error()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement