Advertisement
ROFLCopter64bit

RAMDisk

Jun 22nd, 2014
922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.33 KB | None | 0 0
  1. local pasteCode = "ZT1euz3x"
  2.  
  3. --if vfs then os.unloadAPI('vfs') end
  4. if (not vfs) and (os.loadAPI("vfs") == false) then
  5.     if http then
  6.         get = http.get("http://pastebin.com/raw.php?i=" .. pasteCode)
  7.         if get then
  8.             text = get.readAll()
  9.             f = fs.open("vfs", "w")
  10.             f.write(text)
  11.             f.close()
  12.             get.close()
  13.             if os.loadAPI("vfs") == false then
  14.                 printError( "RAMDisk could not load VFS API" )
  15.                 return
  16.             end
  17.         else
  18.             printError( "RAMDisk could not load VFS API" )
  19.             return
  20.         end
  21.     else
  22.         printError( "RAMDisk could not load VFS API" )
  23.         return
  24.     end
  25. end
  26.  
  27. local function printUsage()
  28.     if term.isColour() then
  29.         term.setTextColor( colors.yellow )
  30.        
  31.         print( "Loaded Images:" )
  32.         tList1 = vfs.listMountedImages()
  33.         --print(textutils.serialize(tList1))
  34.         tList2 = {}
  35.         for i = 1, #tList1 do
  36.             table.insert(tList2, "//" .. tList1[i])
  37.         end
  38.         textutils.pagedTabulate( colors.green, tList2 , colors.yellow )
  39.        
  40.         print( "Usages:" )
  41.        
  42.         term.setTextColor( colors.white )
  43.         write( "ramdisk" )
  44.         term.setTextColor( colors.blue )
  45.         write( " create " )
  46.         term.setTextColor( colors.red )
  47.         print( "<drive name>")
  48.        
  49.         term.setTextColor( colors.white )
  50.         write( "ramdisk" )
  51.         term.setTextColor( colors.blue )
  52.         write( " save " )
  53.         term.setTextColor( colors.red )
  54.         print( "<drive name> <filename>")
  55.        
  56.         term.setTextColor( colors.white )
  57.         write( "ramdisk" )
  58.         term.setTextColor( colors.blue )
  59.         write( " load " )
  60.         term.setTextColor( colors.red )
  61.         print( "<filename>")
  62.        
  63.         term.setTextColor( colors.white )
  64.         write( "ramdisk" )
  65.         term.setTextColor( colors.blue )
  66.         write( " unload " )
  67.         term.setTextColor( colors.red )
  68.         print( "<drive name>" )
  69.        
  70.         term.setTextColor( colors.white )
  71.     else
  72.        
  73.         print( "Loaded Images:" )
  74.         tList1 = vfs.listMountedImages()
  75.         tList2 = {}
  76.         for i = 1, #tList1 do
  77.             table.insert(tList2, "//" .. tList1[i])
  78.         end
  79.         textutils.pagedTabulate( tList2 )
  80.        
  81.         print( "Usages:" )
  82.         print( "ramdisk create <drive name>" )
  83.         print( "ramdisk save <drive name> <filename>" )
  84.         print( "ramdisk load <filename>" )
  85.         print( "ramdisk unload <drive name>" )
  86.     end
  87. end
  88.  
  89. local tArgs = { ... }
  90. if #tArgs < 2 then
  91.     printUsage()
  92.     return
  93. end
  94.  
  95. local count = 0
  96. local function serializeImpl( t, tTracking, sIndent )
  97.     count = count + 1
  98.     if count % 499 == 0 then sleep(0) end
  99.     local sType = type(t)
  100.     if sType == "table" then
  101.         if tTracking[t] ~= nil then
  102.             error( "Cannot serialize table with recursive entries", 0 )
  103.         end
  104.         tTracking[t] = true
  105.  
  106.         if next(t) == nil then
  107.             -- Empty tables are simple
  108.             return "{}"
  109.         else
  110.             -- Other tables take more work
  111.             local sResult = "{"--\n"
  112.             local sSubIndent = "" -- sIndent .. "  "
  113.             local tSeen = {}
  114.             for k,v in ipairs(t) do
  115.                 print('k: ' .. k .. ';v: ' .. v)
  116.                 tSeen[k] = true
  117.                 sResult = sResult .. sSubIndent .. serializeImpl( v, tTracking, sSubIndent ) .. ";" --\n"
  118.             end
  119.             for k,v in pairs(t) do
  120.                 if not tSeen[k] then
  121.                     local sEntry
  122.                     if type(k) == "string" and string.match( k, "^[%a_][%a%d_]*$" ) then
  123.                         sEntry = k .. "=" .. serializeImpl( v, tTracking, sSubIndent ) .. ";" --\n"
  124.                     else
  125.                         sEntry = "[" .. serializeImpl( k, tTracking, sSubIndent ) .. "]=" .. serializeImpl( v, tTracking, sSubIndent ) .. ";" --\n"
  126.                     end
  127.                     sResult = sResult .. sSubIndent .. sEntry
  128.                 end
  129.             end
  130.             sResult = sResult .. sIndent .. "}"
  131.             return sResult
  132.         end
  133.        
  134.     elseif sType == "string" then
  135.         return string.format( "%q", t )
  136.    
  137.     --[[elseif sType == "number" or sType == "boolean" or sType == "nil" then
  138.         return tostring(t)]]
  139.        
  140.     elseif sType == "boolean" or sType == "nil" then
  141.         return tostring(t)
  142.        
  143.     elseif sType == "number" then
  144.         return string.format('%02x', t)
  145.        
  146.     else
  147.         error( "Cannot serialize type "..sType, 0 )
  148.        
  149.     end
  150. end
  151.  
  152. function bigSerialize( t ) --patched so it doesn't hang when encoding big tables, and in this case, drives
  153.     local tTracking = {}
  154.     return serializeImpl( t, tTracking, "" )
  155. end
  156.  
  157. function getSize(path)
  158.     if fs.isDir(path) then
  159.         list = fs.list(path)
  160.         size = 0
  161.         for i = 1, #list do
  162.             size = size + getSize(fs.combine(path, list[i]))
  163.         end
  164.         return size
  165.     else
  166.         return fs.getSize(path)
  167.     end
  168. end
  169.  
  170. function ssize(bytes)
  171.     if bytes > (2 ^ 10) - 1 then
  172.         if bytes > (2 ^ 20) - 1 then
  173.             return math.floor(bytes / (2 ^ 20)) .. '.' .. string.sub(bytes % (2 ^ 20), 1, 2) .. 'MB'
  174.         else
  175.             return math.floor(bytes / (2 ^ 10)) .. '.' .. string.sub(bytes % (2 ^ 10), 1, 2) .. 'KB'
  176.         end
  177.     else
  178.         return bytes .. 'B'
  179.     end
  180. end
  181.  
  182. local func = tArgs[1]
  183. if func == "create" then
  184.     vfs.mount(vfs.createImage(tArgs[2]))
  185. elseif func == "save" then
  186.     if #tArgs < 3 then
  187.         printUsage()
  188.         return
  189.     end
  190.     if vfs.getMountedImage(tArgs[2]) == nil then
  191.         error(tArgs[2] .. " is not a mounted image")
  192.     end
  193.    
  194.     path = shell.resolve(tArgs[3])
  195.     f = fs.open(path, "w")
  196.     f.write('return ' .. bigSerialize(vfs.getMountedImage(tArgs[2])))
  197.     f.close()
  198. elseif func == "load" then
  199.     local path = shell.resolve(tArgs[2])
  200.     local func
  201.     if fs.exists(path) == false then
  202.         error(path .. " does not exist")
  203.     elseif fs.isDir(path) == true then
  204.         local currentSize = 0
  205.         local totalSize = getSize(path)
  206.         local x, y = term.getCursorPos()
  207.         local w, h = term.getSize()
  208.         local seconds = 0
  209.         func = function()
  210.             local function copyProgress(fromPath, toPath)
  211.                 if fs.isDir(fromPath) then
  212.                     if fs.exists(toPath) == false then fs.createDir(toPath) end
  213.                     local tList = fs.list(fromPath)
  214.                     for i = 1, #tList do
  215.                         copyProgress(fromPath .. '/' .. fs.getName(tList[i]), toPath .. '/' .. fs.getName(tList[i]))
  216.                     end
  217.                     return
  218.                 end
  219.                 local f = fs.open(fromPath, "rb")              
  220.                 local wr = fs.open(toPath, "wb")
  221.                 local concat = ''
  222.                 for b in f.read do
  223.                     currentSize = currentSize + 1
  224.                     term.setCursorPos(x, y)
  225.                     local str = math.floor(100 * currentSize / totalSize) .. '% (' .. fromPath .. ' > ' .. toPath .. ') ' .. ssize(currentSize) .. '/' .. ssize(totalSize) .. ' ' .. seconds .. 's'
  226.                    
  227.                     write(str .. string.rep(' ', w - #str))
  228.                     --wr.write(b)
  229.                     concat = concat .. string.format('%02x', b)
  230.                     if currentSize % 2111 == 0 then
  231.                         sleep(0)
  232.                     end
  233.                 end
  234.                 wr.writeRaw(concat)
  235.                 wr.close()
  236.                 f.close()
  237.             end
  238.            
  239.             local function t()
  240.                 while true do
  241.                     sleep(1)
  242.                     seconds = seconds + 1
  243.                 end
  244.             end
  245.                
  246.             vfs.mount(vfs.createImage(fs.getName(path)))
  247.            
  248.             parallel.waitForAny(function() copyProgress(path, "//" .. fs.getName(path)) end, t)
  249.         end
  250.     else
  251.         func = function()
  252.             local f = fs.open(path, "r")
  253.             --vfs.mount(textutils.unserialize(f.readAll()))
  254.             local x = loadfile(path)
  255.             vfs.mount(x())
  256.             f.close()
  257.         end
  258.     end
  259.    
  260.     --[[local function wait()
  261.         while true do
  262.             sleep(999999)
  263.         end
  264.     end
  265.    
  266.     parallel.waitForAny(func, wait)]]
  267.     func()
  268. elseif func == "unload" then
  269.     if vfs.getMountedImage(tArgs[2]) == nil then
  270.         error(tArgs[2] .. " is not a mounted image")
  271.     end
  272.     vfs.unmount(tArgs[2])
  273. else
  274.     printUsage()
  275.     return
  276. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement