Advertisement
guitarplayer616

parse

Aug 20th, 2018
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.10 KB | None | 0 0
  1. local filename = ...
  2. ConsoleClass = {
  3.    
  4.     filename = "",
  5.     mt = {},
  6.  
  7.     New = function(filename)
  8.         local instance = {};
  9.         setmetatable(instance,ConsoleClass.mt);
  10.         instance.filename = filename;
  11.         if fs.exists(filename) then
  12.             fs.delete(filename);
  13.         end
  14.         return instance;
  15.     end,
  16.  
  17.     Color = function(txtcol,backcol)
  18.         txtcol = txtcol or colors.white
  19.         backcol = backcol or colors.black
  20.         term.setTextColor(txtcol)
  21.         term.setBackgroundColor(backcol)
  22.     end,    
  23.  
  24.     Write = function(self,value,txtcol,backcol)
  25.         self.Color(txtcol,backcol);
  26.         local h = fs.open(self.filename,"a");
  27.         value = value or "nil";
  28.         h.write(tostring(value));
  29.         h.close();
  30.     end,
  31.    
  32.     Write2 = function(self,name,value,txtcol,backcol)
  33.         self.Color(txtcol,backcol);
  34.         local h = fs.open(self.filename,"a");
  35.         value = value or "nil";
  36.         h.write(tostring(name));
  37.         if value then
  38.             h.write(", ");
  39.             h.write(tostring(value));
  40.         end
  41.         h.close();
  42.     end,
  43. }
  44.  
  45. ConsoleClass.mt.__index = ConsoleClass;
  46. ConsoleClass.mt.__tostring = function(self)
  47.     local h = fs.open(self.filename,"r");
  48.     local str = h.readAll();
  49.     h.close();
  50.     return str;
  51. end
  52.  
  53. function readname(h)  
  54.   --write("readname(")
  55.  
  56.   n1 = h.read()
  57.   n2 = h.read()
  58.  
  59.   if(n1 == nil or n2 == nil) then
  60.     return ""
  61.   end
  62.  
  63.   --log1:Write2("  n1",n1,colors.green);
  64.   --log1:Write2("\n  n2",n2,colors.green);
  65.  
  66.   n = n1*256 + n2
  67.   --log1:Write("\n  n=n1*256+n2",n,colors.green);
  68.  
  69.   str = ""
  70.   for i=1,n do
  71.     c = h.read()
  72.     if c == nil then
  73.       return
  74.     end  
  75.     str = str .. string.char(c)
  76.   end
  77.   return str
  78. end
  79.  
  80. function readbytes(h, n)
  81.   --print("readbytes")
  82.   for i=1,n do
  83.     h.read()
  84.   end
  85. end
  86.  
  87. function readString(h, n)
  88.     local str = ""
  89.     for i=1,n do
  90.         local c = h.read()
  91.         if not c then
  92.             return
  93.         end
  94.         str = str..string.char(c)
  95.     end
  96.     return str
  97. end
  98.  
  99. function parse(a, h, containsName)
  100.   containsName = containsName or true
  101.   log1:Write("parse(",colors.red)
  102.   log1:Write(a,colors.red)
  103.   log1:Write(")\n",colors.red)
  104.   if a==0 then
  105.     return
  106.   end
  107.   if containsName then
  108.     name = readname(h)
  109.     log1:Write("  ")
  110.     log1:Write(name,colors.yellow)
  111.     log1:Write("\n")
  112.   end
  113.    
  114.   if a==1 then
  115.     readbytes(h,1)
  116.     log1:Write("  Skipping 1 byte\n")  
  117.   elseif a==2 then
  118.     i1 = h.read()
  119.     i2 = h.read()
  120.     i = i1*256 + i2
  121.     log1:Write("  ")
  122.     log1:Write(i)
  123.     log1:Write("\n")
  124.     if(name=="Height") then
  125.       height = i
  126.     elseif (name=="Length") then
  127.       length = i
  128.     elseif (name=="Width") then
  129.       width = i
  130.     end
  131.   elseif a==3 then
  132.     readbytes(h,4)
  133.     log1:Write("  Skipping 4 bytes\n")
  134.   elseif a==4 then
  135.     readbytes(h,8)
  136.     log1:Write("  Skipping 8 bytes\n")
  137.   elseif a==5 then
  138.     readbytes(h,4)
  139.     log1:Write("  Skipping 4 bytes\n")
  140.   elseif a==6 then
  141.     readbytes(h,8)
  142.     log1:Write("  Skipping 8 bytes\n")
  143.   elseif a==7 then
  144.     i1 = h.read()
  145.     i2 = h.read()
  146.     i3 = h.read()
  147.     i4 = h.read()
  148.     i = i1*256*256*256 + i2*256*256 + i3*256 + i4
  149.     if name == "Blocks" then
  150.       log1:Write("  Recording ")
  151.       for i=1,i do
  152.         table.insert(blocks, h.read())
  153.       end
  154.     elseif name == "Data" then
  155.       log1:Write("  Recording ")
  156.       for i=1,i do
  157.         table.insert(data, h.read())
  158.       end
  159.     else
  160.       readbytes(h,i)
  161.       log1:Write("  Skipping ")
  162.     end
  163.     log1:Write(i)
  164.     log1:Write(" bytes\n")
  165.   elseif a==8 then
  166.     i1 = h.read()
  167.     i2 = h.read()
  168.     i = i1*256 + i2
  169.     local str = readString(h,i)
  170.    
  171.     log1:Write("  ")
  172.     log1:Write(str)
  173.     log1:Write(" \n")
  174.   elseif a==9 then
  175.         --readbytes(h,5)
  176.         type = h.read()
  177.         i1 = h.read()
  178.     i2 = h.read()
  179.     i3 = h.read()
  180.     i4 = h.read()
  181.     i = i1*256*256*256 + i2*256*256 + i3*256 + i4
  182.     for j=1,i do
  183.       parse(h.read(), h, false)
  184.     end
  185.     log1:Write("  End of List\n")
  186.   end
  187. end
  188.  
  189. length = 0
  190. height = 0
  191. width = 0
  192. blocks = {}
  193. data = {}
  194.  
  195. h = fs.open(filename, "rb")
  196. log1 = ConsoleClass.New("log1")
  197.  
  198. a = 0
  199. while (a ~= nil) do
  200.   a = h.read()
  201.   parse(a, h)
  202. end
  203.  
  204. textutils.pagedPrint(log1);
  205. write("length: " .. length)
  206. write("   width: " .. width)
  207. write("   height: " .. height .. "\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement