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

Need a little bit help (problem with disk drives)

Started by Marcono1234, 02 September 2014 - 06:44 PM
Marcono1234 #1
Posted 02 September 2014 - 08:44 PM
Sorry if this was already a topic or if my English is too bad :/

So I saw the password door tutorial on the homepage (http://computercraft.info/wiki/Making_a_Password_Protected_Door) and I thought maybe I could create some kind of interface, when you place a disk drive next to the computer and then add a disk with certain information which then gets detected and executed…
But somehow I don't understand, why always when there is either no disk driver attached/ no disk inside, I get the error message:
"disk:3: Expected string"


local changecode = "-- abcdef4"
local diskPresent = 1
local enabled = true -- Sets if computer is enabled
local side = "left" -- Side of door relative to computer
local password = "password1" -- Password to enter
local opentime = 5  -- Time (sec) how long door is open
function diskSideTest()
  for _, side in ipairs(rs.getSides()) do
    if disk.isPresent(side) and disk.getMountPath(side) == "disk" then
	  return side
    end
  end   
end
while enabled do
  if disk.isPresent(diskSideTest()) and diskPresent == 0 then
    print("DISK!")
    sleep(2)
    diskPresent = 1
    if disk.hasData(diskSideTest()) then
	  print("DATA!")
	  local h = fs.open("disk/startup","r")
	  local changeCodeDisk = h:readLine(1)
	  h:close()
	  if changeCodeDisk == changecode then
	    print ("Booting from Disk")
	    sleep(2)
	    shell.run("/disk/startup")
	  else
	    print ("Disk doesn't contain correct boot-key")
	  end
    end
  else
    print("What?")
    sleep(2)
    if not (disk.isPresent(diskSideTest())) and diskPresent == 1 then
	  diskPresent = 0
    end
  end
  
  term.clear()
  term.setCursorPos(1,1)
  print("Password: ")
  local input = read("*")
  if input == password then
    term.clear()
    term.setCursorPos(1,1)
    print("Password correct!")
    redstone.setOutput(side,true)
    sleep(opentime)
    redstone.setOutput(side,false)
  else
    print("Password incorrect!")
    sleep(2)
  end
end


And I have some questions, it would be very nice if you could help me, because I started programming with this one day ago ;)/>
  • Can you have multiple "threads", for example that when you stop typing for 15 seconds, that then the program restarts?
  • Is there a way to import/export programs to/from computercraft? Maybe also just documents created with notepad++ and are there also programs which can run/test them immediately, so without opening Minecraft first? (I saw, that there is somehow a way with "pastebin", but I don't understand how to use the command and I couldn't find a article in the wiki here about this)
  • *I saw that they are saved in the saves folder for each world, but can you also access them more easily?
I am using "Tekkit lite" for MC version 1.4.7 (via TechnicLauncher) and ComputerCraft is using CraftOS 1.5
natedogith1 #2
Posted 03 September 2014 - 02:02 AM
The reason you're getting that error is because diskSideTest returns null if it can't find anything, which disk.isPresent then tries to use and errors.

You can't have multiple threads running at once, but lua does have something called coroutines, which can be used to implement multi-tasking (as multishell and the parallel API do). Though for your example purpose, you'd be better off just starting a timer and checking to see if the event pulled was that timer.

You can "pastebin put <filename>" to upload a file to pastebin and "pastebin get <code> <filename>" to download a file from pastebin. Though if you're in a single-player world you can just modify the files in world-save/computer/computer-id and I'm not sure entirely what you mean by more easily? You could probably create a shortcut to the folder.
KingofGamesYami #3
Posted 03 September 2014 - 02:48 AM
Here's how the pastebin part works, I can explain that a little better.

Example url: http://pastebin.com/BSdryL92*
Example command: pastebin get BSdryL92 filename

*note: the code here does not work. It is from a different ask a pro topic.

If you want to understand the acutal code behind it, thats more complex.

local response = http.get( http://pastebin.com/raw.php?i=BSdryL92 ) --#get information
if response then --#make sure there was information at that url
  local file = fs.open( 'filename', 'w' ) --#open a file in write mode
  file.write( response.readAll() ) --#write the information we obtained
  file.close() --#close the file
  response.close() --#close the response... some say do, some say don't.  I personally choose to close it
else
  print( "no data received" )
end
Edited on 03 September 2014 - 12:49 AM
Marcono1234 #4
Posted 03 September 2014 - 01:04 PM
Thank you both of you :)/>

@natedogith1, do you mean nil, because I read that the null form java is nil in lua, is this correct? :)/>
theoriginalbit #5
Posted 03 September 2014 - 01:25 PM
yes. nil is the keyword used in Lua to represent what is commonly referred to in other languages as null.
Marcono1234 #6
Posted 03 September 2014 - 03:03 PM
Thank you :)/>
I have another question, I hope I can just post it here too, so if problems with the paths, because it seems like sometimes it need "&#46;&#46;/" and sometimes not and I get very confused
Edited on 03 September 2014 - 08:06 PM
Marcono1234 #7
Posted 27 September 2014 - 01:52 PM

if fs.exists("startup") then
term.clear()
term.setCursorPos(1,1)
print ("Startup already exists!")
print ("Do you want to overwrite or delete it?")
-- parallel.waitForAny(readInput("startupExists",""), readTime(), pressKey())
startupExists = read()

while not (startupExists == "overwrite" or startupExists == "delete" or startupExists == "exit") do
  term.clear()
  term.setCursorPos(1,1)
  print ("Startup already exists!")
  print ("Do you want to overwrite or delete  it?")
  print ("Possible options: overwrite delete exit")
  startupExists = read()
end
if startupExists == "overwrite" then
  fs.delete("startup")
  copyStartup()
  shell.run("startup")
end
if startupExists == "delete" then
  fs.delete("startup")
end
term.clear()
term.setCursorPos(1,1)
else
term.clear()
term.setCursorPos(1,1)
print("Do you want to install the startup?")
start = read()
  
while not (start == "false" or start == "true") do
  term.clear()
  term.setCursorPos(1,1)
  print("Do you want to install the startup?")
  print("Possible options: true false")
  start = read ()
end
if start == "true" then
  copyStartup()
  term.clear()
  term.setCursorPos(1,1)
  shell.run("startup")
else
  term.clear()
  term.setCursorPos(1,1)
end
end	
So basically the part with "overwrite" needs somehow shell.run("&#46;&#46;/startup"), but when I use this also for the install part, it is not working (this seems only to work with shell.run("startup"))
can you please help me :)/> ?

But what it also very strange, when I use the delete part, it deletes it, but when I then try the install part, it installs it but can't launch it, so it will instantaneously say that this already exists…
Edited on 27 September 2014 - 11:54 AM
Marcono1234 #8
Posted 01 October 2014 - 02:41 PM
Can someone please help me?
Bomb Bloke #9
Posted 01 October 2014 - 04:12 PM
So basically the part with "overwrite" needs somehow shell.run("&amp;#46;&amp;#46;/startup"), but when I use this also for the install part, it is not working (this seems only to work with shell.run("startup"))
can you please help me :)/> ?

shell.run("startup") should be what you want. Even if this script is running from a sub-directory, the function expects an absolute path.

What does it do instead of "working"? Are there any errors? If so, what do they say?

What's the code in "copyStartup()"?

But what it also very strange, when I use the delete part, it deletes it, but when I then try the install part, it installs it but can't launch it, so it will instantaneously say that this already exists…

That makes me think this script IS your startup script?
Lyqyd #10
Posted 01 October 2014 - 07:34 PM
Shell.run can accept absolute paths, but can also accept relative paths based on the shell's current working directory as well as checking through all of the paths listed in the shell's path variable.
Marcono1234 #11
Posted 02 October 2014 - 03:30 PM
Ok, but I also have fs.delete("startup") and it seems like this won't delete the startup file on the computer
or at least it has some problems with it, because the fs.exists("startup") doesn't find it after the "deleting", could that be because the program startup runs the program which tries to delete then the program startup?
Edited on 02 October 2014 - 01:32 PM
Lyqyd #12
Posted 02 October 2014 - 03:57 PM
If you delete a file, it won't exist, so of course fs.exists will return false.
Marcono1234 #13
Posted 11 October 2014 - 10:16 PM
If you delete a file, it won't exist, so of course fs.exists will return false.
Sorry described it maybe a little bad, actually what happens is, I read a string called startupExists then this code runs

if startupExists == "delete" then
  fs.delete("startup")
end
But when this ends, the program startup is still open (maybe because the program startup ran this program (disk/startup), but now it gets strange, it seems like the startup (on the computer) is still there, but the fs.exists doesn't find it

I hope you understood what I meant :)/>
Lyqyd #14
Posted 11 October 2014 - 10:39 PM
What makes it seem like the /startup file is still there if fs.exists doesn't find it?
Bomb Bloke #15
Posted 12 October 2014 - 01:41 AM
If this program is disk/startup, then this is the program that'll run when the computer boots - not the startup script on the computer's internal drive.
Marcono1234 #16
Posted 12 October 2014 - 05:28 PM
What makes it seem like the /startup file is still there if fs.exists doesn't find it?
Because the program (startup on computer) which ran the disk/startup (which then "deleted" startup on the computer), continues its work after it was deleted!?
Lyqyd #17
Posted 12 October 2014 - 07:44 PM
So, you're saying that it starts executing the /startup program. Then at some point while executing that, the /startup program becomes deleted. Code from the /startup program continues to execute at this point, which leads you to believe that the file still exists, is that right?

If that's what you're thinking, then the thing is, it is actually deleted. When you first start running a program, the whole thing is loaded up as a Lua function, which then gets run. The file is only necessary when the program is first loaded to be run, it doesn't read each line from the file individually as it goes along. If you want the /startup file to stop executing itself after the actual file is deleted, you'd have to put in fs.exists("/startup") checks in and end the program if they return false. I'm not sure why you'd need that to be the case, though.

In any event, once you've deleted the file, it no longer exists. fs.exists won't lie to you. It's just that programs that were already running when they were deleted won't be affected.
Marcono1234 #18
Posted 12 October 2014 - 09:16 PM
It's just that programs that were already running when they were deleted won't be affected.
So does this mean, running programs can't get deleted!? But this is exactly what I am talking about…

Well maybe I will add then the complete code:
This is the code for the startup on the computer:

local bootKey = "-- abcdef4"
local diskPresent = 1
local driveSide
local enabled = true -- Sets if computer is enabled
local side = "left" -- Side of door relative to computer
local password = "Minecraft" -- Password to enter
local opentime = 5  -- Time (sec) how long door is open
-- os.pullEvent = os.pullEventRaw
-- Enable terminate: os.pullEvent = os.pullEvent
function bootFromDisk()
for _, side in ipairs(rs.getSides()) do
  if side == nil then
   driveSide = side
  end
  if side ~= nil and peripheral.isPresent(side) and peripheral.getType(side) == "drive" and disk.isPresent(side) and disk.hasData(side) then
   driveSide = side
  end
end
if driveSide ~= nil and disk.isPresent(driveSide) and diskPresent == 0 then
  diskPresent = 1
  if disk.hasData(driveSide) and fs.exists("disk/startup") then
   local h = fs.open("disk/startup","r")
   local diskBootKey = h:readLine(3)
   h:close()
   if diskBootKey == bootKey then
    print ("Booting from Disk")
    sleep(2)
    shell.run("disk/startup")
   else
    print ("Disk doesn't contain correct boot-key")
    sleep(2)
   end
  end
else
  if diskPresent == 1 then
   diskPresent = 0
  end
end 
end
while enabled do
bootFromDisk()
term.clear()
term.setCursorPos(1,1)
print("Password: ")
local input = read("*")
if input == password then
  term.clear()
  term.setCursorPos(1,1)
  print("Password correct!")
  redstone.setOutput(side,true)
  sleep(opentime)
  redstone.setOutput(side,false)
else
  print("Password incorrect!")
  sleep(2)
end
end
while not enabled do
bootFromDisk()
term.clear()
term.setCursorPos(1,1)
print("Computer disabled!")
sleep(10)
end

And this is the code for the startup on the disk:

-- abcdef4
-- ENABLE TERMINATE!!!
local start
local startupExists
local readTimeOut = 7 -- time in seconds until read() stops
local readTimeOutActive
function copyStartup()
fs.copy("disk/startup.file","startup")
end
function readInput(inputVariable, readType)
inputVariable = read(readType)
return
end
function readTimer()
readTimeOutActive = true
while readTimeOutActive do
  readTimeOutActive = false
  sleep(readTimeOut)
end
return
end
--[[
function pressKey()
local sEvent, param = os.pullEvent("key")
if(sEvent == "key") then
  readTimeOutActive = true
end
end
]]
if fs.exists("startup") then
term.clear()
term.setCursorPos(1,1)
print ("Startup already exists!")
print ("Do you want to overwrite or delete it?")
-- parallel.waitForAny(readInput("startupExists",""), readTime(), pressKey())
startupExists = read()
 
while not (startupExists == "overwrite" or startupExists == "delete" or startupExists == "exit") do
  term.clear()
  term.setCursorPos(1,1)
  print ("Startup already exists!")
  print ("Do you want to overwrite or delete  it?")
  print ("Possible options: overwrite delete exit")
  startupExists = read()
end
if startupExists == "overwrite" then
  fs.delete("startup")
  copyStartup()
  shell.run("startup")
end
if startupExists == "delete" then
  print("DELETING")
  sleep(4)
  fs.delete("startup")
end
term.clear()
term.setCursorPos(1,1)
else
term.clear()
term.setCursorPos(1,1)
print("Do you want to install the startup?")
start = read()
  
while not (start == "false" or start == "true") do
  term.clear()
  term.setCursorPos(1,1)
  print("Do you want to install the startup?")
  print("Possible options: true false")
  start = read () 
end
if start == "true" then
  copyStartup()
  term.clear()
  term.setCursorPos(1,1)
  shell.run("startup")
else
  term.clear()
  term.setCursorPos(1,1)
end
end

So what basically happens is, I enter the correct password twice while a disk drive with a disk containing this startup is connected, the startup on the computer detects that and runs this startup on the disk.
Now I enter "delete" when it asks me what I want to do. It deletes it, but somehow returns to the startup on the computer (this should be deleted now!), which runs the startup on the disk.
When I now enter the wrong password again twice, it asks me if I want to install the startup, because it can't find it on the computer anymore.

Also here two other questions:
  • I have the option "exit", which should lead to the normal interface without having any program open, is there a way to achieve this (maybe this could also fix the other problem above then…)
  • Where can I report bugs with this forum here?
Bomb Bloke #19
Posted 13 October 2014 - 12:51 AM
So does this mean, running programs can't get deleted!? But this is exactly what I am talking about…

When a script starts, you can delete the file they were loaded from, but doing so won't delete the copy which is actively running from RAM.

Thus, if "startup" calls "disk\startup", when "disk\startup" completes "startup" will continue from the point where it left off - regardless as to whether the file it was originally loaded from still exists.

If you want it to exit to the shell prompt at that point, have "startup" call something like error() after calling shell.run(). It'll still stay loaded while "disk\startup" is doing its thing, but it'll close as soon as that finishes.
Marcono1234 #20
Posted 13 October 2014 - 09:10 AM
So does this mean, running programs can't get deleted!? But this is exactly what I am talking about…

When a script starts, you can delete the file they were loaded from, but doing so won't delete the copy which is actively running from RAM.

Thus, if "startup" calls "disk\startup", when "disk\startup" completes "startup" will continue from the point where it left off - regardless as to whether the file it was originally loaded from still exists.

If you want it to exit to the shell prompt at that point, have "startup" call something like error() after calling shell.run(). It'll still stay loaded while "disk\startup" is doing its thing, but it'll close as soon as that finishes.
Thank you :)/>
And where can I post bugs with this forum here, do you know that?
Bomb Bloke #21
Posted 13 October 2014 - 09:27 AM
That'd probably be the section labeled Forum Discussion. Though I suspect that if you'd asked your question here already you'd have your answer by now.

If you're wondering about the indentation thing, turn off the rich text editor using the little light switch up the top left of the posting box. Heck, even if your problem isn't that, there are good odds that'll be your fix anyway.
Marcono1234 #22
Posted 13 October 2014 - 08:57 PM
That'd probably be the section labeled Forum Discussion. Though I suspect that if you'd asked your question here already you'd have your answer by now.

If you're wondering about the indentation thing, turn off the rich text editor using the little light switch up the top left of the posting box. Heck, even if your problem isn't that, there are good odds that'll be your fix anyway.
Oh yes, that is the problem, alright thank you :)/>
Marcono1234 #23
Posted 14 October 2014 - 06:35 PM
Or can you use the shell.exit() also to exit other running programs?