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

[LUA][Question] How to make Floppy startup check hdd startup

Started by 1337patchy, 02 July 2012 - 08:29 PM
1337patchy #1
Posted 02 July 2012 - 10:29 PM
Hello guys, i have followed the wiki tutorial on the password door, and i have the local password variable set with the password i want, everything is great, it works if i boot the computer.
But i want to use a floppy as a key, so, this is the code i have on the floppy's startup
if password == "my password" then
rs.SetOutput("back", true)
sleep(3)
rs.SetOutput("back", false)
else
end

How do i, however, make it so that the floppy's code (one above), can check the hdd's startup code
and get the value of its password variable, and if it matches with the one in the floppy,
the door opens. So i can unlock either with floppy, or without it, using password.
I know i need something above if password == "my password" then, so that it knows where local password is defined..but i dont know how to make it so that it checks the startup on the hdd
I would appreciate help on the subject
thank you in advance :P/>/>
OmegaVest #2
Posted 03 July 2012 - 03:35 PM
Hmm. Interesting question. Well, I would first make line 1 in the terminal's program something like

-- [Password]  To comment it out so the computer won't try to look at it.  '

or

password1 = "[Password]"  -- So you have the variable in the program already. And you need the 1 if you want to be able to grab it from the floppy.



For the first one, use this:


file = fs.open(startup, r)
passLine = file.readLine()
passLine = string.sub(passLine, 5, #passLine)  --Change the 5 to 3 if you omit spaces from around your comments. I usually put one on either end so I know it when I'm skimming.

Then just check if passLine is equal to the password, and you're golden. For the second one, though, it's a little more complex, but you don't need to omit anything.



file = fs.open(startup, r)
passLine = file.readLine()
loadstring(passLine)

Then check whether password1 is equal to password. But, I must warn here, because I've seen about two dozen attempts at loadstring that fail horribly. So, until I can check whether this will work or not, I would suggest not using the second one. Unless you would like to debug it yourself.
MysticT #3
Posted 03 July 2012 - 03:47 PM
I think it would be easier to have a file in the disk that contains the password, and then the startup program waits for a disk to be inserted and checks if the file is there and it contains the correct password.
The startup program would be like this:

local Password = "Your Password Here"

local function clear()
  term.clear()
  term.setCursorPos(1, 1)
end

local function open()
  -- code to open the door
end

local function pass()
  while true do
	clear()
	local pass = read("*")
	if pass == Password then
	  print("Accepted. Opening door.")
	  open()
	else
	  print("Wrong password. Try again.")
	end
  end
end

local function checkDisk()
  while true do
	local evt, side = os.pullEvent("disk")
	local sPath = fs.combine(disk.getMountPath(side), "passcode")
	if fs.exist(sPath) then
	  local file = fs.open(sPath)
	  if file then
		local pass = file.readAll()
		if pass == Password then
		  open()
		end
		file.close()
	  end
	end
  end
end

parallel.waitForAny(pass, checkDisk)
So you can write the password in the console to open the door, and use a disk also.
It checks for a file named "passcode" in the disk and if it's there, it reads it's contents and compares it to the password. (It's not tested, so there might be some errors).
tfoote #4
Posted 03 July 2012 - 04:06 PM
You could have the computer/floppy check a rednet server… it sends the name code and the server sends back a password.
1337patchy #5
Posted 03 July 2012 - 07:54 PM
Thank you everyone for the help, appreciate it much.
I ended up finding a way to do it myself, I made the floppy check os.computerID()
as in
local a = os.computerID()
if a == 5 then
(code to open door)
else
(code to say invalid blabla)
end

:P/>/> thats how i did it hehehe
thanks for everyone's help though!