JacobHodgkins420

CFWInstaller

Apr 20th, 2024 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.40 KB | Gaming | 0 0
  1. local function downloadBios()
  2.     local url = "https://raw.githubusercontent.com/cc-tweaked/CC-Tweaked/mc-1.20.x/projects/core/src/main/resources/data/computercraft/lua/bios.lua"
  3.     local response = http.get(url)
  4.     if response then
  5.         local biosContent = response.readAll()
  6.         response.close()
  7.         local file = fs.open("bios.lua", "w")
  8.         if file then
  9.             file.write(biosContent)
  10.             file.close()
  11.         end
  12.     end
  13. end
  14.  
  15. local function VERSION()
  16.     local version_ID = os.version()
  17.     return version_ID
  18. end
  19.  
  20. function PatchBiosFile(VER)
  21.     local version = VER
  22.     local versionstring = 'return "' .. version ..' CFW                     Computer ID:" .. os.computerID()'
  23.      local biosPath = "bios.lua"
  24.      local biosFile = fs.open(biosPath, "r")
  25.      if not biosFile then
  26.          return
  27.      end
  28.  
  29.      local content = biosFile.readAll()
  30.      biosFile.close()
  31.      content = content:gsub("rom/", "crom/")
  32.      content = content:gsub('return "CraftOS 1.9"', versionstring)
  33.  
  34.      local editedBiosFile = fs.open(biosPath, "w")
  35.      if not editedBiosFile then
  36.          return
  37.      end
  38.  
  39.     editedBiosFile.write(content)
  40.     editedBiosFile.close()
  41. end
  42.  
  43. function CondenseBios()
  44.     local biosPath = "/bios.lua"
  45.     local biosFile = fs.open(biosPath, "r")
  46.     if not biosFile then
  47.         return
  48.     end
  49.  
  50.     local content = biosFile.readAll()
  51.     biosFile.close()
  52.  
  53.     content = content:gsub("%-%-[^\n]*", "")
  54.     content = content:gsub("[\r\n]+", "\n")
  55.     content = content:gsub("^\n", "")
  56.  
  57.     local editedBiosFile = fs.open(biosPath, "w")
  58.     if not editedBiosFile then
  59.         return
  60.     end
  61.  
  62.     editedBiosFile.write(content)
  63.     editedBiosFile.close()
  64. end
  65.  
  66. function PatchCustomRom()
  67.     local directory = "crom/"
  68.  
  69.     local function replaceInString(content)
  70.         return content:gsub("([^%w_])rom/", "%1crom/"):gsub("^rom/", "crom/")
  71.                :gsub("([^%w_])/rom", "%1/crom"):gsub("^/rom", "/crom")
  72.     end
  73.  
  74.     local function replaceInFile(path)
  75.         local file = fs.open(path, "r")
  76.         if file then
  77.             local content = file.readAll()
  78.             file.close()
  79.  
  80.             local newContent = replaceInString(content)
  81.             if newContent ~= content then
  82.                 file = fs.open(path, "w")
  83.                 if file then
  84.                     file.write(newContent)
  85.                     file.close()
  86.                 end
  87.             end
  88.         end
  89.     end
  90.  
  91.     local function processDirectory(dir)
  92.         local items = fs.list(dir)
  93.         for _, item in ipairs(items) do
  94.             local path = fs.combine(dir, item)
  95.             if fs.isDir(path) then
  96.                 processDirectory(path)
  97.             else
  98.                 replaceInFile(path)
  99.             end
  100.         end
  101.     end
  102.  
  103.     if not fs.exists(directory) or not fs.isDir(directory) then
  104.         return
  105.     end
  106.  
  107.     processDirectory(directory)
  108. end
  109.  
  110. local function downloadUnBios()
  111.     local url = "https://gist.githubusercontent.com/MCJack123/42bc69d3757226c966da752df80437dc/raw/887547d8283fbfe6693c6586566c16b79d47a6ae/unbios.lua"
  112.     local response = http.get(url)
  113.     if response then
  114.         local unbiosContent = response.readAll()
  115.         response.close()
  116.         local file = fs.open("unbios.lua", "w")
  117.         if file then
  118.             file.write(unbiosContent)
  119.             file.close()
  120.         end
  121.     end
  122. end
  123.  
  124. function CFW_OnBoot()
  125.     local startupPath = "startup.lua"
  126.     local file = fs.open(startupPath, "w")
  127.     if file then
  128.         file.writeLine('shell.run("unbios.lua")')
  129.         file.close()
  130.     end
  131. end
  132.  
  133. -- Function to download a file from a URL and save it locally
  134. local function downloadFile(url, filename)
  135.     -- Ensure that HTTP API is enabled
  136.     if not http then
  137.         print("HTTP API is not enabled")
  138.         return
  139.     end
  140.  
  141.     -- Attempt to download the file
  142.     local response = http.get(url)
  143.     if response then
  144.         -- Read the contents of the file
  145.         local content = response.readAll()
  146.         response.close()
  147.  
  148.         -- Save the contents to a file
  149.         local file = fs.open(filename, "w")
  150.         if file then
  151.             file.write(content)
  152.             file.close()
  153.             print("File downloaded successfully as " .. filename)
  154.         else
  155.             print("Failed to save file")
  156.         end
  157.     else
  158.         print("Failed to download file")
  159.     end
  160. end
  161.  
  162.  
  163. function CondenseUnBios()
  164.     local biosPath = "unbios.lua"
  165.     local biosFile = fs.open(biosPath, "r")
  166.     if not biosFile then
  167.         return
  168.     end
  169.  
  170.     local content = biosFile.readAll()
  171.     biosFile.close()
  172.  
  173.     content = content:gsub("%-%-[^\n]*", "")
  174.     content = content:gsub("\n[\n%s]*\n", "\n")
  175.  
  176.     if content:sub(1, 1) == "\n" then
  177.         content = content:sub(2)
  178.     end
  179.  
  180.     local editedBiosFile = fs.open(biosPath, "w")
  181.     if not editedBiosFile then
  182.         return
  183.     end
  184.  
  185.     editedBiosFile.write(content)
  186.     editedBiosFile.close()
  187. end
  188.  
  189. function AddCC_File_System_Access_Control()
  190.     local biosPath = "bios.lua"
  191.     local biosFile = fs.open(biosPath, "r")
  192.     if not biosFile then
  193.         return
  194.     end
  195.  
  196.     local content = biosFile.readAll()
  197.     biosFile.close()
  198.  
  199.     local startIdx, endIdx = content:find('load_apis%("crom/apis"%s*%)')
  200.     if not startIdx then
  201.         return
  202.     end
  203.  
  204.     local codeAbove = content:sub(1, startIdx - 1)
  205.     local codeBelow = content:sub(endIdx + 1)
  206.  
  207.     local newContent = codeAbove .. [[
  208. local readOnlyErrorColor = 0x4000 -- Red color code
  209.  
  210. local colors = {
  211.     white = 0x1,
  212.     orange = 0x2,
  213.     magenta = 0x4,
  214.     lightBlue = 0x8,
  215.     yellow = 0x10,
  216.     lime = 0x20,
  217.     pink = 0x40,
  218.     gray = 0x80,
  219.     lightGray = 0x100,
  220.     cyan = 0x200,
  221.     purple = 0x400,
  222.     blue = 0x800,
  223.     brown = 0x1000,
  224.     green = 0x2000,
  225.     red = 0x4000,
  226.     black = 0x8000
  227. }
  228.  
  229. local readOnlyPaths = {"startup.lua", "unbios.lua", "bios.lua"}
  230. local hiddenPaths = {
  231.     ["rom"] = true,
  232.     ["startup.lua"] = true,
  233.     ["bios.lua"] = true,
  234.     ["unbios.lua"] = true
  235. }
  236.  
  237. local function normalizePath(path)
  238.     path = path:gsub("%.%.", "")
  239.     path = path:gsub("[\\/]([\\/]+)", "/")
  240.     path = path:gsub("[\\/]$", "")
  241.     return path
  242. end
  243.  
  244. local function hidePath(path)
  245.     local normalizedPath = fs.combine(path, "")
  246.     hiddenPaths[normalizedPath] = true
  247. end
  248.  
  249. local function unhidePath(path)
  250.     local normalizedPath = fs.combine(path, "")
  251.     hiddenPaths[normalizedPath] = nil
  252. end
  253.  
  254. local old_fsList = fs.list
  255. fs.list = function(path)
  256.     local files = old_fsList(path)
  257.     local filteredFiles = {}
  258.     for _, file in ipairs(files) do
  259.         local fullPath = fs.combine(path, file)
  260.         local normalizedPath = fs.combine(path, "")
  261.         if not hiddenPaths[fullPath] and not hiddenPaths[normalizedPath .. "." .. file] then
  262.             table.insert(filteredFiles, file)
  263.         end
  264.     end
  265.     return filteredFiles
  266. end
  267.  
  268. local old_fsOpen = _G["fs"]["open"]
  269. local old_fsIsReadOnly = _G["fs"]["isReadOnly"]
  270. local old_fsDelete = _G["fs"]["delete"]
  271. local old_fsMove = _G["fs"]["move"]
  272. local old_fsCopy = _G["fs"]["copy"]
  273. local old_fsWrite = _G["fs"]["write"]
  274. local old_fsAppend = _G["fs"]["append"]
  275. local old_fsMakeDir = _G["fs"]["makeDir"]
  276. local old_fsDeleteDir = _G["fs"]["deleteDir"]
  277.  
  278. _G["fs"]["open"] = function(path, mode)
  279.     local isReadOnlyPath = false
  280.     path = normalizePath(path)
  281.     for _, readOnlyPath in ipairs(readOnlyPaths) do
  282.         if path:sub(1, #normalizePath(readOnlyPath)) == normalizePath(readOnlyPath) then
  283.             isReadOnlyPath = true
  284.             break
  285.         end
  286.     end
  287.     if not isReadOnlyPath then
  288.         return old_fsOpen(path, mode)
  289.     elseif mode == 'w' or mode == 'd' then
  290.         return nil
  291.     else
  292.         return old_fsOpen(path, 'r')
  293.     end
  294. end
  295.  
  296. function fs.setReadOnlyPath(path, readOnly)
  297.     if type(path) == "string" and type(readOnly) == "boolean" then
  298.         if readOnly then
  299.             table.insert(readOnlyPaths, path)
  300.         else
  301.             for i, readOnlyPath in ipairs(readOnlyPaths) do
  302.                 if readOnlyPath == path then
  303.                     table.remove(readOnlyPaths, i)
  304.                     break
  305.                 end
  306.             end
  307.         end
  308.     end
  309. end
  310.  
  311. fs.hide = function(path)
  312.     hidePath(path)
  313. end
  314.  
  315. fs.unhide = function(path)
  316.     unhidePath(path)
  317. end
  318.  
  319. _G["fs"]["delete"] = function(path)
  320.     path = normalizePath(path)
  321.     for _, readOnlyPath in ipairs(readOnlyPaths) do
  322.         if path:sub(1, #normalizePath(readOnlyPath)) == normalizePath(readOnlyPath) then
  323.             term.setTextColor(readOnlyErrorColor)
  324.             print("Cannot delete read-only file: " .. path)
  325.             term.setTextColor(colors.white) -- Reset text color to white
  326.             return false -- Prevent deletion of readonly paths
  327.         end
  328.     end
  329.     return old_fsDelete(path)
  330. end
  331. load_apis("crom/apis")
  332. ]] .. codeBelow
  333.  
  334.     local biosFile = fs.open(biosPath, "w")
  335.     if not biosFile then
  336.         return
  337.     end
  338.     biosFile.write(newContent)
  339.     biosFile.close()
  340. end
  341.  
  342. local function removeComments(luaString)
  343.     local cleanedString = ""
  344.     local inBlockComment = false
  345.     local blockCommentStart = "--["
  346.     local blockCommentEnd = "]]"
  347.     local blockCommentPattern = "%-%-%[(=*)%["
  348.     for line in luaString:gmatch("[^\r\n]+") do
  349.         if inBlockComment then
  350.             local endBlockComment = line:find(blockCommentEnd)
  351.             if endBlockComment then
  352.                 line = line:sub(endBlockComment + 2)
  353.                 inBlockComment = false
  354.             else
  355.                 line = ""
  356.             end
  357.         else
  358.             local startBlockCommentIndex = line:find(blockCommentPattern)
  359.             if startBlockCommentIndex then
  360.                 local equalSigns = line:sub(startBlockCommentIndex + 3)
  361.                 local endBlockCommentPattern = "%]" .. equalSigns .. "%]"
  362.                 local endBlockCommentIndex = line:find(endBlockCommentPattern)
  363.                 if endBlockCommentIndex then
  364.                     line = line:sub(1, startBlockCommentIndex - 1) .. line:sub(endBlockCommentIndex + 2)
  365.                 else
  366.                     inBlockComment = true
  367.                     line = line:sub(1, startBlockCommentIndex - 1)
  368.                 end
  369.             end
  370.             line = line:gsub("%-%-.*", "")
  371.         end
  372.         if line:gsub("%s", "") ~= "" then
  373.             cleanedString = cleanedString .. line .. "\n"
  374.         end
  375.     end
  376.     return cleanedString
  377. end
  378.  
  379. function CondenseCustomRom()
  380.     local cromDir = "crom/"
  381.     local function processFile(path)
  382.         local isDirectory = fs.isDir(path)
  383.         if isDirectory then
  384.             for _, file in ipairs(fs.list(path)) do
  385.                 local subPath = fs.combine(path, file)
  386.                 processFile(subPath)
  387.             end
  388.         else
  389.             if fs.exists(path) and not fs.isDir(path) then
  390.                 local file = fs.open(path, "r")
  391.                 local content = file.readAll()
  392.                 file.close()
  393.                 content = removeComments(content)
  394.                 local lines = {}
  395.                 for line in content:gmatch("[^\r\n]+") do
  396.                     table.insert(lines, line)
  397.                 end
  398.                 local firstLineIndex = 1
  399.                 if #lines > 0 and lines[1]:gsub("%s", "") == "" then
  400.                     firstLineIndex = 2
  401.                 end
  402.                 content = table.concat(lines, "\n", firstLineIndex)
  403.                 file = fs.open(path, "w")
  404.                 file.write(content)
  405.                 file.close()
  406.             end
  407.         end
  408.     end
  409.     processFile(cromDir)
  410. end
  411.  
  412. -- Function to replace a line in a file
  413. local function replaceLine(filePath, lineNumber, newText)
  414.     -- Check if the file exists
  415.     if not fs.exists(filePath) then
  416.         print("File not found")
  417.         return
  418.     end
  419.  
  420.     -- Read the existing content of the file
  421.     local file = fs.open(filePath, "r")
  422.     local lines = {}
  423.     local lineCounter = 1
  424.     local currentLine = file.readLine()
  425.     while currentLine do
  426.         -- Store each line of the file in a table
  427.         if lineNumber == lineCounter then
  428.             lines[lineCounter] = newText
  429.         else
  430.             lines[lineCounter] = currentLine
  431.         end
  432.         lineCounter = lineCounter + 1
  433.         currentLine = file.readLine()
  434.     end
  435.     file.close()
  436.  
  437.     -- Rewrite the file with the modified lines
  438.     file = fs.open(filePath, "w")
  439.     for i, line in ipairs(lines) do
  440.         file.writeLine(line)
  441.     end
  442.     file.close()
  443. end
  444.  
  445. function Unrestrict()
  446.     replaceLine("./bios.lua",486,"load_apis(\"crom/apis/http\")")
  447.     replaceLine("./bios.lua",487,"")
  448.     replaceLine("./bios.lua",488,"load_apis(\"crom/apis/pocket\")")
  449.     replaceLine("./bios.lua",637,"          sShell = \"crom/programs/advanced/multishell.lua\"")
  450.     replaceLine("./crom/startup.lua",3,"sPath = sPath .. \":/crom/programs/advanced\"")
  451.     replaceLine("./crom/startup.lua",4,"")
  452.     replaceLine("./crom/startup.lua",5,"")
  453.     replaceLine("./crom/startup.lua",6,"")
  454.     replaceLine("./crom/startup.lua",7,"sPath = sPath .. \":/crom/programs/turtle\"")
  455.     replaceLine("./crom/startup.lua",8,"")
  456.     replaceLine("./crom/startup.lua",9,"sPath = sPath .. \":/crom/programs/rednet:/crom/programs/fun\"")
  457.     replaceLine("./crom/startup.lua",10,"")
  458.     replaceLine("./crom/startup.lua",11,"sPath = sPath .. \":/crom/programs/fun/advanced\"")
  459.     replaceLine("./crom/startup.lua",12,"")
  460.     replaceLine("./crom/startup.lua",13,"")
  461.     replaceLine("./crom/startup.lua",14,"")
  462.     replaceLine("./crom/startup.lua",15,"sPath = sPath .. \":/crom/programs/pocket\"")
  463.     replaceLine("./crom/startup.lua",16,"")
  464.     replaceLine("./crom/startup.lua",17,"")
  465.     replaceLine("./crom/startup.lua",18,"sPath = sPath .. \":/crom/programs/command\"")
  466.     replaceLine("./crom/startup.lua",19,"")
  467.     replaceLine("./crom/startup.lua",30,"")
  468.     replaceLine("./crom/startup.lua",31,"shell.setAlias(\"background\", \"bg\")")
  469.     replaceLine("./crom/startup.lua",32,"shell.setAlias(\"foreground\", \"fg\")")
  470.     replaceLine("./crom/startup.lua",33,"")
  471. end
  472.  
  473. local function main()
  474.     local Get_Version = VERSION()
  475.     shell.run("clear")
  476.     print("CFWInstaller 0.1")
  477.     print("Created by Jacob Hodgkins")
  478.     print("")
  479.     print("Copying entire rom/ to crom/")
  480.     print("")
  481.     shell.run("copy rom/ crom/")
  482.     print("Downloading BIOS from offical CC:Tweaked github")
  483.     print("")
  484.     downloadBios()
  485.     PatchBiosFile(Get_Version)
  486.     print("Patching BIOS to load crom/ instead of rom/")
  487.     PatchCustomRom()
  488.     print("")
  489.     print("Downloading Unbios from offical github")
  490.     downloadUnBios()
  491.     print("")
  492.     print("Making Unbios run at boot to unload BIOS/rom and load patched BIOS/crom instead")
  493.     CFW_OnBoot()
  494.     print("")
  495.     print("Downloading PKG Manager")
  496.     local url = "https://raw.githubusercontent.com/HodgkinsDev/PKGManager/main/pkg.lua"
  497.     local filename = "pkg.lua"
  498.     downloadFile(url, filename)
  499.     print("")
  500.     print("Installing PKG Manager")
  501.     local CMD = "mv pkg.lua /crom/programs/"
  502.     shell.run(CMD)
  503.     print("")
  504.     print("Condensing BIOS file")
  505.     --AddCC_File_System_Access_Control()
  506.     CondenseBios()
  507.     print("")
  508.     print("Condensing all files in crom/")
  509.     CondenseCustomRom()
  510.     print("")
  511.     print("Condensing Unbios file")
  512.     CondenseUnBios()
  513.     print("")
  514.     print("Unrestricting Programs/APIS")
  515.     Unrestrict()
  516.     print("")
  517.     print("Recondensing Rom/Bios")
  518.     CondenseCustomRom()
  519.     CondenseBios()
  520.     print("")
  521.     print("CFW install complete")
  522.     sleep(3)
  523.     shell.run("reboot")
  524. end
  525.  
  526. main()
Advertisement
Add Comment
Please, Sign In to add comment