Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local filename = ...
- ConsoleClass = {
- filename = "",
- mt = {},
- New = function(filename)
- local instance = {};
- setmetatable(instance,ConsoleClass.mt);
- instance.filename = filename;
- if fs.exists(filename) then
- fs.delete(filename);
- end
- return instance;
- end,
- Color = function(txtcol,backcol)
- txtcol = txtcol or colors.white
- backcol = backcol or colors.black
- term.setTextColor(txtcol)
- term.setBackgroundColor(backcol)
- end,
- Write = function(self,value,txtcol,backcol)
- self.Color(txtcol,backcol);
- local h = fs.open(self.filename,"a");
- value = value or "nil";
- h.write(tostring(value));
- h.close();
- end,
- Write2 = function(self,name,value,txtcol,backcol)
- self.Color(txtcol,backcol);
- local h = fs.open(self.filename,"a");
- value = value or "nil";
- h.write(tostring(name));
- if value then
- h.write(", ");
- h.write(tostring(value));
- end
- h.close();
- end,
- }
- ConsoleClass.mt.__index = ConsoleClass;
- ConsoleClass.mt.__tostring = function(self)
- local h = fs.open(self.filename,"r");
- local str = h.readAll();
- h.close();
- return str;
- end
- function readname(h)
- --write("readname(")
- n1 = h.read()
- n2 = h.read()
- if(n1 == nil or n2 == nil) then
- return ""
- end
- --log1:Write2(" n1",n1,colors.green);
- --log1:Write2("\n n2",n2,colors.green);
- n = n1*256 + n2
- --log1:Write("\n n=n1*256+n2",n,colors.green);
- str = ""
- for i=1,n do
- c = h.read()
- if c == nil then
- return
- end
- str = str .. string.char(c)
- end
- return str
- end
- function readbytes(h, n)
- --print("readbytes")
- for i=1,n do
- h.read()
- end
- end
- function readString(h, n)
- local str = ""
- for i=1,n do
- local c = h.read()
- if not c then
- return
- end
- str = str..string.char(c)
- end
- return str
- end
- function parse(a, h, containsName)
- containsName = containsName or true
- log1:Write("parse(",colors.red)
- log1:Write(a,colors.red)
- log1:Write(")\n",colors.red)
- if a==0 then
- return
- end
- if containsName then
- name = readname(h)
- log1:Write(" ")
- log1:Write(name,colors.yellow)
- log1:Write("\n")
- end
- if a==1 then
- readbytes(h,1)
- log1:Write(" Skipping 1 byte\n")
- elseif a==2 then
- i1 = h.read()
- i2 = h.read()
- i = i1*256 + i2
- log1:Write(" ")
- log1:Write(i)
- log1:Write("\n")
- if(name=="Height") then
- height = i
- elseif (name=="Length") then
- length = i
- elseif (name=="Width") then
- width = i
- end
- elseif a==3 then
- readbytes(h,4)
- log1:Write(" Skipping 4 bytes\n")
- elseif a==4 then
- readbytes(h,8)
- log1:Write(" Skipping 8 bytes\n")
- elseif a==5 then
- readbytes(h,4)
- log1:Write(" Skipping 4 bytes\n")
- elseif a==6 then
- readbytes(h,8)
- log1:Write(" Skipping 8 bytes\n")
- elseif a==7 then
- i1 = h.read()
- i2 = h.read()
- i3 = h.read()
- i4 = h.read()
- i = i1*256*256*256 + i2*256*256 + i3*256 + i4
- if name == "Blocks" then
- log1:Write(" Recording ")
- for i=1,i do
- table.insert(blocks, h.read())
- end
- elseif name == "Data" then
- log1:Write(" Recording ")
- for i=1,i do
- table.insert(data, h.read())
- end
- else
- readbytes(h,i)
- log1:Write(" Skipping ")
- end
- log1:Write(i)
- log1:Write(" bytes\n")
- elseif a==8 then
- i1 = h.read()
- i2 = h.read()
- i = i1*256 + i2
- local str = readString(h,i)
- log1:Write(" ")
- log1:Write(str)
- log1:Write(" \n")
- elseif a==9 then
- --readbytes(h,5)
- type = h.read()
- i1 = h.read()
- i2 = h.read()
- i3 = h.read()
- i4 = h.read()
- i = i1*256*256*256 + i2*256*256 + i3*256 + i4
- for j=1,i do
- parse(h.read(), h, false)
- end
- log1:Write(" End of List\n")
- end
- end
- length = 0
- height = 0
- width = 0
- blocks = {}
- data = {}
- h = fs.open(filename, "rb")
- log1 = ConsoleClass.New("log1")
- a = 0
- while (a ~= nil) do
- a = h.read()
- parse(a, h)
- end
- textutils.pagedPrint(log1);
- write("length: " .. length)
- write(" width: " .. width)
- write(" height: " .. height .. "\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement