Advertisement
programcreator

Executable

Oct 8th, 2016
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.28 KB | None | 0 0
  1. --[[
  2.     Builder
  3.     by Creator
  4. ]]--
  5.  
  6. local ignore = {}
  7. local function parse(data)
  8.     for token in string.gmatch(data,"[^\n]+") do
  9.         ignore[#ignore+1] = token
  10.     end
  11. end
  12.  
  13. local args = {...}
  14. if args[3] and args[3]:sub(1,1) == "$" then
  15.     parse(args[3]:sub(2,-1))
  16. elseif args[3] then
  17.     local file = fs.open(args[3],"r")
  18.     local data = file.readAll()
  19.     file.close()
  20.     parse(data)
  21. end
  22.  
  23. local filesystem = {}
  24.  
  25. local function readFile(path)
  26.     local file = fs.open(path,"r")
  27.     local variable = file.readAll()
  28.     file.close()
  29.     return variable
  30. end
  31.  
  32. local function isNotBanned(path)
  33.     for i,v in pairs(ignore) do
  34.         if v == path then
  35.             return false
  36.         end
  37.     end
  38.     return true
  39. end
  40.  
  41. local function explore(dir)
  42.     local buffer = {}
  43.     local sBuffer = fs.list(dir)
  44.     for i,v in pairs(sBuffer) do
  45.         sleep(0.05)
  46.         if isNotBanned(dir.."/"..v) then
  47.             if fs.isDir(dir.."/"..v) then
  48.                 if v ~= ".git" then
  49.                     print("Compressing directory: "..dir.."/"..v)
  50.                     buffer[v] = explore(dir.."/"..v)
  51.                 end
  52.             else
  53.                 print("Compressing file: "..dir.."/"..v)
  54.                 buffer[v] = readFile(dir.."/"..v)
  55.             end
  56.         else
  57.             print(dir.."/"..v)
  58.         end
  59.     end
  60.     return buffer
  61. end
  62.  
  63. append = [[
  64.  
  65. local archpath = ...
  66. local args = {unpack({...}, 2)}
  67.  
  68. if not type(archpath) == "string" then
  69.     error("Please provide a path.")
  70. end
  71.  
  72. archpath = fs.combine("/", archpath)
  73.  
  74. local len = #archpath
  75.  
  76. local nfs = {}
  77.  
  78. local oldfs = {}
  79.  
  80. for i,v in pairs(_G.fs) do
  81.     oldfs[i] = v
  82. end
  83.  
  84. local function getFirstElement(path)
  85.     if not type(path) == "string" then error("This is not a string: "..tostring(path),2) end
  86.     return string.sub(path,1,string.find(path,"/") and string.find(path,"/")-1 or -1)
  87. end
  88.  
  89. local function getRest(path)
  90.     if not type(path) == "string" then error("This is not a string: "..tostring(path),2) end
  91.     return string.sub(path, string.find(path, "/") and string.find(path, "/") +1 or 1, -1)
  92. end
  93.  
  94. local function getElement(path, tbl)
  95.     if path == "/" or path == "" then
  96.         return tbl
  97.     end
  98.     if not tbl then
  99.         print(tbl)
  100.         error("Error, bruh: "..path, 2)
  101.     end
  102.     local first = getFirstElement(path)
  103.     if tbl[first] then
  104.         if first == path then
  105.             return tbl[first]
  106.         else
  107.             return getElement(getRest(path), tbl[first])
  108.         end
  109.     else
  110.         return false
  111.     end
  112. end
  113.  
  114. local function isInPackage(path)
  115.     local first = path:sub(1, len)
  116.     return first == archpath
  117. end
  118.  
  119. local function removePackagePath(path)
  120.     return path:sub(len+2,-1)
  121. end
  122.  
  123. local function writeFile(path, content)
  124.     local file = fs.open(path,"w")
  125.     file.write(content)
  126.     file.close()
  127. end
  128.  
  129. function writeDown(input, dir)
  130.     for i,v in pairs(input) do
  131.         if type(v) == "table" then
  132.             writeDown(v, dir.."/"..i)
  133.         elseif type(v) == "string" then
  134.             writeFile(dir.."/"..i, v)
  135.         end
  136.     end
  137. end
  138.  
  139. local function generateFileHandle(data)
  140.     local data = data
  141.     local self = {}
  142.     function self.readAll()
  143.         return data
  144.     end
  145.     local lineNum = 1
  146.     local lines = {}
  147.     for token in data:gmatch("[^\n]+") do
  148.         lines[lineNum] = token
  149.         lineNum = lineNum + 1
  150.     end
  151.     lineNum = 0
  152.     function self.readLine()
  153.         lineNum = lineNum + 1
  154.         return lines[lineNum]
  155.     end
  156.  
  157.     function self.close()
  158.         self.readLine = nil
  159.         self.readAll = nil
  160.         self["close"] = nil
  161.         self = nil
  162.     end
  163.  
  164.     return self
  165. end
  166.  
  167. nfs.open = function(path, mode)
  168.     path = fs.combine("/",path)
  169.     if isInPackage(path) then
  170.         local file = getElement(removePackagePath(path), inputTable)
  171.         if type(file) == "string" then
  172.             if mode == "r" then
  173.                 return generateFileHandle(file)
  174.             else
  175.                 return false, "Cannot write to executable."
  176.             end
  177.         else
  178.             return false, "Cannot open folder."
  179.         end
  180.     else
  181.         return oldfs.open(path, mode)
  182.     end
  183. end
  184.  
  185. nfs.getName = oldfs.getName
  186.  
  187. nfs.combine = oldfs.combine
  188.  
  189. nfs.isReadOnly = oldfs.isReadOnly
  190.  
  191. nfs.move = function(path, dest)
  192.     if isInPackage(dest) or isInPackage(path) then
  193.         return false
  194.     end
  195.     return oldfs.move(path, dest)
  196. end
  197.  
  198. nfs.copy = function(path, dest)
  199.     path = fs.combine("/",path)
  200.     dest = fs.combine("/",dest)
  201.  
  202.     if isInPackage(dest) then
  203.         return false
  204.     end
  205.     if isInPackage(path) then
  206.         local element = getElement(removePackagePath(path), inputTable)
  207.         if type(element) == "table" then
  208.             writeDown(element, dest)
  209.             return true
  210.         elseif type(element) == "string" then
  211.             writeFile(dest, element)
  212.             return true
  213.         end
  214.         return false, "Element is not directory nor file."
  215.     else
  216.         return oldfs.copy(path, dest)
  217.     end
  218. end
  219.  
  220. nfs.delete = function(path)
  221.     path = fs.combine("/",path)
  222.     if isInPackage(path) then
  223.         return false, "Cannot delete files in executable."
  224.     else
  225.         return oldfs.delete(path)
  226.     end
  227. end
  228.  
  229. nfs.isDir = function(path)
  230.     path = fs.combine("/",path)
  231.     if isInPackage(path) then
  232.         if path == archpath then
  233.             return true
  234.         end
  235.         return type(getElement(removePackagePath(path), inputTable)) == "table" and true or false
  236.     else
  237.         return oldfs.isDir(path)
  238.     end
  239. end
  240.  
  241. nfs.getFreeSpace = oldfs.getFreeSpace
  242.  
  243. nfs.complete = oldfs.complete
  244.  
  245. nfs.getDrive = oldfs.getDrive
  246.  
  247. nfs.getSize = function(path)
  248.     path = fs.combine("/",path)
  249.     if isInPackage(path) then
  250.         local element = getElement(removePackagePath(path), inputTable)
  251.         return type(element) == "string" and #element or 0
  252.     else
  253.         return oldfs.getSize(path)
  254.     end
  255. end
  256.  
  257. nfs.list = function(path)
  258.     path = fs.combine("/",path)
  259.     if not nfs.isDir(path) then
  260.         return false, "Can't list file."
  261.     end
  262.     if isInPackage(path) then
  263.         local element = getElement(removePackagePath(path), inputTable)
  264.         if type(element) == "table" then
  265.             local ret = {}
  266.             for i,v in pairs(element) do
  267.                 ret[#ret+1] = i
  268.             end
  269.             return ret
  270.         else
  271.             return false, "Cannot list anything else than directories."
  272.         end
  273.     else
  274.         return oldfs.list(path)
  275.     end
  276. end
  277.  
  278. nfs.exists = function(path)
  279.     path = fs.combine("/",path)
  280.     if isInPackage(path) then
  281.         return getElement(removePackagePath(path), inputTable) and true or false
  282.     else
  283.         return oldfs.exists(path)
  284.     end
  285. end
  286.  
  287. nfs.makeDir = function(path)
  288.     path = fs.combine("/",path)
  289.     if isInPackage(path) then
  290.         return false, "Cannot make directory in executable."
  291.     else
  292.         return oldfs.makeDir(path)
  293.     end
  294. end
  295.  
  296. nfs.find = function(path)
  297.     path = fs.combine("/",path)
  298.     if isInPackage(path) then
  299.         return false, "Cannot search executable."
  300.     else
  301.         return oldfs.find(path)
  302.     end
  303. end
  304.  
  305. nfs.getDir = oldfs.getDir
  306.  
  307. local env = {}
  308.  
  309. --_G.fs = nfs
  310.  
  311. for i,v in pairs(_G) do
  312.     env[i] = v
  313. end
  314.  
  315. for i,v in pairs(nfs) do
  316.     _G.fs[i] = v
  317. end
  318.  
  319. local function readFile(path)
  320.     local file = fs.open(path,"r")
  321.     local variable = file.readAll()
  322.     file.close()
  323.     return variable
  324. end
  325.  
  326. local dofile = function(path,...)
  327.     local data = readFile(path)
  328.     local f, err = loadstring(data)
  329.     if not f then
  330.         print(err)
  331.     end
  332.     setfenv(f, env)
  333.     return f(...)
  334. end
  335.  
  336. function env.getfenv(arg)
  337.     local e = getfenv(type(arg) == "number" and arg + 1 or arg)
  338.     if e == _G then
  339.         return env
  340.     else
  341.         return e
  342.     end
  343. end
  344.  
  345. local init = getElement("init.lua", inputTable)
  346.  
  347. if init then
  348.     if type(init) == "string" then
  349.         dofile(archpath.."/init.lua", unpack(args))
  350.     else
  351.         error("init.lua is a directory.")
  352.     end
  353. else
  354.     error("No init file.")
  355. end
  356.  
  357.  
  358.  
  359. ]]
  360.  
  361. local filesystem = explore(args[1])
  362. local file = fs.open(args[2],"w")
  363. file.write("local inputTable = "..textutils.serialize(filesystem).."\n\n\n\n\n\n\n\n\n"..append)
  364. file.close()
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371. --[[Code]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement