Advertisement
CloneTrooper1019

ConvertVMF

Aug 3rd, 2014
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.53 KB | None | 0 0
  1. -- This converts a Source Engine .VMF Hammer File into a readable table in Lua.
  2.  
  3. -- Example VMF File String.
  4. -- http://pastebin.com/ib6RtFDk
  5. -- This function just parses the file (as a string) into a table.
  6. -- If there's a better version for this stuff online, please for the love of god let me know lol.
  7.  
  8. function ConvertVMF(file)
  9.     local fileConv = {}
  10.     local current = 0
  11.     local stack = 0
  12.     local stackRefs = {[0] = fileConv}
  13.     local lines = {}
  14.     for line in string.gmatch(file,"[^\r\n]+") do
  15.         table.insert(lines,line)
  16.     end
  17.     while current <= #lines do
  18.         current = current + 1
  19.         local line = lines[current]
  20.         if line then
  21.             local tabs = 0
  22.             for i in string.gmatch(line,"\t") do
  23.                 tabs = tabs + 1
  24.             end
  25.             local k,v = string.match(line,'%"(.-)%" %"(.-)%"')
  26.             if k and v then
  27.                 stackRefs[stack][k] = tonumber(v) or v
  28.             elseif string.find(line,"{") then
  29.                 local previousLine = lines[current-1]
  30.                 local nextLine = lines[current+1]
  31.                 if previousLine and nextLine then
  32.                     local f,l = string.find(previousLine,string.rep("\t",tabs))
  33.                     if f and l then
  34.                         local id = string.match(nextLine,'"id" %"(.-)%"')
  35.                         local key = string.sub(previousLine,l+1)
  36.                         if id then
  37.                             current = current + 1
  38.                             key = key..id
  39.                         end
  40.                         local loc = stackRefs[stack]
  41.                         local self = {}
  42.                         stack = stack + 1
  43.                         stackRefs[stack] = self
  44.                         loc[key] = self
  45.                     end
  46.                 end
  47.             elseif string.find(line,"}") then
  48.                 stackRefs[stack] = nil
  49.                 stack = stack - 1
  50.             end
  51.         end
  52.     end
  53.     return fileConv
  54. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement