Advertisement
Revector

dc_revector17

Dec 19th, 2019 (edited)
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.09 KB | None | 0 0
  1. local component = require("component");
  2. local fs = require("filesystem");
  3. local serialization = require("serialization");
  4. local term = require("term");
  5. local internet = nil;
  6.  
  7. --[[
  8. -- pastebin get 0MbYVXpc dc_revector17.lua
  9. -- /usr/bin/dc_gui.lua
  10. --]]
  11. local packages = {"oop", "libGUI", "draconic_control17", "dc_gui17"};
  12. local repositoryURL = "http://xypm.tenyx.de/";
  13.  
  14. local gpu = component.gpu;
  15.  
  16. function fetchPackage(pack)
  17.     local url = repositoryURL .. pack .. ".xypm";
  18.    
  19.     local request = internet.request(url);
  20.     if request == nil then
  21.         return nil;
  22.     end
  23.    
  24.     local data = "";
  25.    
  26.     for chunk in request do
  27.         data = data .. chunk;
  28.     end
  29.    
  30.     return serialization.unserialize(data);
  31. end
  32.  
  33. local function verifyPackage(package)
  34.     if not component.isAvailable("data") then
  35.         -- If we don't have a data card we can't verify anything.
  36.         -- However in order to make data cards optional we just return true
  37.         return true;
  38.     end
  39.    
  40.     for fileName, fileChecksum in pairs(package.checksums or {}) do
  41.         if package.files[fileName] and component.data.sha256(package.files[fileName]) ~= fileChecksum then
  42.             return false;
  43.         end
  44.     end
  45.  
  46.     return true;
  47. end
  48.  
  49. function installPackage(package_data)
  50.     for fileName, fileContents in pairs(package_data.files) do
  51.         local filePath = fs.path(fileName);
  52.         if fs.exists(fileName) then
  53.             fs.remove(fileName);
  54.         end
  55.        
  56.         if filePath ~= nil then
  57.             local pathSegments = fs.segments(filePath);
  58.             local pathChecked = "/";
  59.             for _, segment in pairs(pathSegments) do
  60.                 pathChecked = fs.concat(pathChecked, segment);
  61.                 if not fs.exists(pathChecked) then
  62.                     fs.makeDirectory(pathChecked);
  63.                 end
  64.             end
  65.         end
  66.        
  67.         local file = io.open(fileName, "wb");
  68.         if file == nil then
  69.             return false;
  70.         end
  71.         file:write(fileContents);
  72.         file:close();
  73.     end
  74.    
  75.     for linkName, linkTarget in pairs(package_data.links) do
  76.         local filePath = fs.path(fileName);
  77.         if fs.exists(fileName) then
  78.             fs.remove(fileName);
  79.         end
  80.        
  81.         if filePath ~= nil then
  82.             local pathSegments = fs.segments(filePath);
  83.             local pathChecked = "/";
  84.             for _, segment in pairs(pathSegments) do
  85.                 pathChecked = fs.concat(pathChecked, segment);
  86.                 if not fs.exists(pathChecked) then
  87.                     fs.makeDirectory(pathChecked);
  88.                 end
  89.             end
  90.         end
  91.        
  92.         if not fs.link(linkTarget, linkname) then
  93.             return false;
  94.         end
  95.     end
  96.    
  97.     return true;
  98. end
  99.  
  100. local packageNameMaxLen = 0;
  101.  
  102. if component.isAvailable("internet") then
  103.     internet = component.internet;
  104. else
  105.     print("You need an Internet Card for this to work.");
  106.     return 1;
  107. end
  108.  
  109. internet = require("internet");
  110.  
  111. for _, p in pairs(packages) do
  112.     print(" - " .. p);
  113.     packageNameMaxLen = math.max(packageNameMaxLen, string.len(p));
  114. end
  115. print("");
  116.  
  117. local termWidth = gpu.getResolution();
  118. local _, termY = term.getCursor();
  119. local barWidthMax = termWidth - packageNameMaxLen - 11;
  120.  
  121. for i, p in pairs(packages) do
  122.     local percent = (2 * (i-1) / #packages / 2);
  123.     local barRep = math.floor(barWidthMax * percent + 0.5);
  124.     term.setCursor(1, termY);
  125.     local pName = string.sub(p .. string.rep(" ", packageNameMaxLen), 1, packageNameMaxLen);
  126.    
  127.     term.write(pName .. " |" .. string.rep("=", barRep) .. ">" .. string.rep(" ", barWidthMax - barRep) .. "|" .. string.format("%6.2f%%", percent * 100), false); 
  128.     local packageData = fetchPackage(p);
  129.     if packageData == nil then
  130.         print("");
  131.         print("Failed to download " .. p);
  132.         return;
  133.     end
  134.    
  135.     term.setCursor(1, termY);
  136.     percent = ((2 * (i-1) + 1) / #packages / 2);
  137.     barRep = math.floor(barWidthMax * percent + 0.5);
  138.     term.write(pName .. " |" .. string.rep("=", barRep) .. ">" .. string.rep(" ", barWidthMax - barRep) .. "|" .. string.format("%6.2f%%", percent * 100), false);
  139.     if not verifyPackage(packageData) then
  140.         print("");
  141.         print("Integrity check for " .. p .. " failed.");
  142.     end
  143.    
  144.     if not installPackage(packageData) then
  145.         print("");
  146.         print("Failed to install " .. p);
  147.     end
  148. end
  149. term.setCursor(1, termY);
  150. local pName = string.sub("Done" .. string.rep(" ", packageNameMaxLen), 1, packageNameMaxLen);
  151. term.write(pName .. " |" .. string.rep("=", barWidthMax) .. ">" .. "|" .. "100.00%", false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement