Advertisement
9551

old NIMG image exporter

Oct 26th, 2021 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.27 KB | None | 0 0
  1. local arg = ...
  2. local self = shell.getRunningProgram()
  3. if not arg or arg == "" then error("use: "..self.." <filename>",0) end
  4. if not fs.exists(arg..".nimg") then error("file doesnt exist. try using filename wihnout .nimg",0) end
  5. local chars = "0123456789abcdef"
  6. local saveCols = {}
  7. for i = 0, 15 do
  8.   saveCols[2^i] = chars:sub(i + 1, i + 1)
  9. end
  10. local encode = function(tbl)
  11.   local output = setmetatable({},{
  12.       __index=function(t,k)
  13.       local new = {}
  14.       t[k]=new
  15.       return new
  16.     end
  17.   })
  18.   output["offset"] = tbl["offset"]
  19.   for k,v in pairs(tbl) do
  20.     for ko,vo in pairs(v) do
  21.         if type(vo) == "table" then
  22.             output[k][ko] = {}
  23.             if vo then
  24.                 output[k][ko].t = saveCols[vo.t]
  25.                 output[k][ko].b = saveCols[vo.b]
  26.                 output[k][ko].s = vo.s
  27.             end
  28.         end
  29.      end
  30.   end
  31.   return setmetatable(output,getmetatable(tbl))
  32. end
  33. ----------------------------
  34. --https://github.com/cc-tweaked/CC-Tweaked/blob/544bcaa599b296aaf9affe55c68ee1810c6a38c6/src/main/resources/data/computercraft/lua/rom/apis/textutils.lua#L710
  35. --serialising function
  36. local e={["and"]=true,["break"]=true,["do"]=true,["else"]=true,["elseif"]=true,["end"]=true,["false"]=true,["for"]=true,["function"]=true,["if"]=true,["in"]=true,["local"]=true,["nil"]=true,["not"]=true,["or"]=true,["repeat"]=true,["return"]=true,["then"]=true,["true"]=true,["until"]=true,["while"]=true,}local
  37. t=math.huge local a=dofile("rom/modules/main/cc/expect.lua")local
  38. o,i=a.expect,a.field local function a(n,s,h,r)local d=type(n)if d=="table"then
  39. if s[n]~=nil then if s[n]==false then
  40. error("Cannot serialize table with repeated entries",0)else
  41. error("Cannot serialize table with recursive entries",0)end end s[n]=true local
  42. l if next(n)==nil then l="{}"else local
  43. u,c,m,f,w,y="{\n",h.."  ","[ "," ] = "," = ",",\n"if r.compact then
  44. u,c,m,f,w,y="{","","[","]=","=",","end l=u local p={}for v,b in ipairs(n)do
  45. p[v]=true l=l..c..a(b,s,c,r)..y end for g,k in pairs(n)do if not p[g]then local
  46. q if type(g)=="string"and not e[g]and string.match(g,"^[%a_][%a%d_]*$")then
  47. q=g..w..a(k,s,c,r)..y else q=m..a(g,s,c,r)..f..a(k,s,c,r)..y end l=l..c..q end
  48. end l=l..h.."}"end if r.allow_repetitions then s[n]=nil else s[n]=false end
  49. return l elseif d=="string"then return string.format("%q",n)elseif
  50. d=="number"then if n~=n then return"0/0"elseif n==t then return"1/0"elseif
  51. n==-t then return"-1/0"else return tostring(n)end elseif d=="boolean"or
  52. d=="nil"then return tostring(n)else error("Cannot serialize type "..d,0)end end
  53. function serialize(j,x)local z={}o(2,x,"table","nil")if x then
  54. i(x,"compact","boolean","nil")i(x,"allow_repetitions","boolean","nil")else
  55. x={}end return
  56. a(j,z,"",x)end
  57. textutils.serialise = serialize
  58. textutils.serialize = serialize
  59. ------------------------
  60. local remover = function(c)
  61.   return c:gsub(" ","")
  62. end
  63. local file = fs.open(arg..".nimg","r")
  64. local data = file.readAll()
  65. file.close()
  66. local noTcol = data:gsub("tcol","t")
  67. local noBcol = noTcol:gsub("bcol","b")
  68. local noSym = noBcol:gsub("sym","s")
  69. local toEncode = textutils.unserialise(noSym)
  70. local encoded = encode(toEncode)
  71. local writeFile = fs.open(arg..".nimg","w")
  72. local ser = textutils.serialize(encoded,{compact = true})
  73. writeFile.write(ser)
  74. writeFile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement