This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
cyanisaac's profile picture

This script is not functioning.

Started by cyanisaac, 14 August 2015 - 10:52 PM
cyanisaac #1
Posted 15 August 2015 - 12:52 AM
The following code is not working:

Bootloader Script

local function customParse(parseString, parseCharacter)
local parseResults = {}
local stringCycle = ""
for i = 1, #parseString do
	 local c = parseString:sub(i,i)
	 if c == parseCharacter then
	  if stringCycle == "" then
	  else
	   table.insert(parseResults, stringCycle)
	   stringCycle = ""
	  end
	 else
	  stringCycle = stringCycle .. c
	 end
	 if i == #parseString and c ~= parseCharacter then
	  table.insert(parseResults, stringCycle)
	  stringCycle = ""
	 end
    end
    return parseResults
end
print("Unlocker Bootloader v0")
print("Created by cyanisaac")
-- fs.normalize function created by Lignum.
fs.normalise=function(a)if a==nil then error("path can't be nil",2)end;local b=a;if b~=nil then local c=true;if b==""then b="/"c=false end;if b=="/"or b=="\\"then b="/"c=false end;if c then b=b:gsub("\\","/")if b:sub(1,1)~="/"then b="/"..b end;if b:sub(#b,#B)/>=="/"then b=b:sub(1,#b-1)end end end;return b end;fs.normalize=fs.normalise
local safeNormalize = fs.normalize
fs.normalize = nil
fs.normalise = nil
local oldFs = fs
local vFs = oldFs -- This is changed for compatibility reasons, to keep the unlocker from being affected as a file.
local vDirectory = "/.vhdd"
-- Functions for virtual filesystem
local function resolvePath(path)
local path = safeNormalize(path)
local customParsedTable = customParse(path, "/")
if customParsedTable[1] == "rom" then
  return path
else
  return vDirectory .. path
end
end
function vFs.list(path)
return oldFs.list(resolvePath(path))
end
function vFs.exists(path)
return oldFs.exists(resolvePath(path))
end
function vFs.isDir(path)
return oldFs.isDir(resolvePath(path))
end
function vFs.isReadOnly(path)
return oldFs.isReadOnly(resolvePath(path))
end
function vFs.getName(path)
return oldFs.getName(resolvePath(path))
end
function vFs.getDrive(path)
return oldFs.getDrive(resolvePath(path))
end
function vFs.getSize(path)
return oldFs.getSize(resolvePath(path))
end
function vFs.getFreeSpace(path)
return oldFs.getFreeSpace(resolvePath(path))
end
function vFs.makeDir(path)
return oldFs.makeDir(resolvePath(path))
end
function vFs.move(toPath, fromPath)
return oldFs.move(resolvePath(fromPath), resolvePath(toPath))
end
function vFs.copy(toPath, fromPath)
return oldFs.copy(resolvePath(fromPath), resolvePath(toPath))
end
function vFs.delete(path)
return oldFs.delete(resolvePath(path))
end
vFs.combine = fs.combine
function vFs.open(path, mode)
return oldFs.open(resolvePath(path), mode)
end
function vFs.find(wildcard)
return oldFs.find(resolvePath(wildcard))
end
vFs.getDir = fs.getDir
function vFs.complete()
return "" --temporary until I can figure out how to get this to work well
end

-- Functions to unlock filesystem
local function mainProcess()
if oldFs.exists(vDirectory .. "/startup") then
  shell.run("/startup")
end
shell.run("/rom/programs/shell")
end
local function restoreFs()
while true do
  if fs ~= vFs then
   fs = vFs
  end
  coroutine.yield()
end
end
parallel.waitForAny(restoreFs, mainProcess)

Expected Result: It should make the fs api be the vFs api, and backup the old fs api into oldFs. From there it creates a sandboxed environment. Then, it should make sure that the FS api is constantly the same as the vFs api to prevent modification (thus creating a permanently unlocked filesystem).

Actual Result: On ComputerCraft it crashes the computer after lagging for some time, in CCemuredux it crashes the java vm and gives me an out of bounds error and crashes all computers in that session.

Does anyone know what is causing this, and if so, how does one fix this?
HPWebcamAble #2
Posted 15 August 2015 - 01:06 AM
Well, for one thing, you can't compare tables with the == operator.

You have to check that manually, using a for loop, that each entry is identical.
Bomb Bloke #3
Posted 15 August 2015 - 01:08 AM
Well, you can, if you specifically want to check that they're the same table. But yes, checking two different tables to see if they have the same contents is a different process.

local oldFs = fs
local vFs = oldFs -- This is changed for compatibility reasons, to keep the unlocker from being affected as a file.

So fs == oldFs == vFs?

In that case, the function pointers in vFs are identical to those in oldFs (be; eg, when vFs.open tries to call oldFs.open, it ends up calling itself; the new function instance calls itself again, and so on until the function stack overflows (leading to your "out of bounds" error).

If you want to copy the contents of a table into a different table, use a "pairs" loop.
cyanisaac #4
Posted 15 August 2015 - 02:55 AM
Well, you can, if you specifically want to check that they're the same table. But yes, checking two different tables to see if they have the same contents is a different process.

local oldFs = fs
local vFs = oldFs -- This is changed for compatibility reasons, to keep the unlocker from being affected as a file.

So fs == oldFs == vFs?

In that case, the function pointers in vFs are identical to those in oldFs (be; eg, when vFs.open tries to call oldFs.open, it ends up calling itself; the new function instance calls itself again, and so on until the function stack overflows (leading to your "out of bounds" error).

If you want to copy the contents of a table into a different table, use a "pairs" loop.

How would one do this? (sorry, I am still noobish at lua lol)
KingofGamesYami #5
Posted 15 August 2015 - 03:14 AM

local oldFs = {}
for k,v in pairs( fs ) do
  oldFs[ k ] = v
end