Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
TheReduxPL

Prevent script termination

Question

Hello!

Thanks to the guys from IRC I managed to make a script which asks for password. If the password is right, it sends a redstone signal for 3 seconds. If it's wrong, it asks again. Now I want the script to be protected against using Ctrl+C to terminate it. This is what we managed to do:

<old, broken code was here>

After trying to implement the "anti terminating thingy", this script is not working as fully expected.

1. When I enter a wrong password, it says "Invalid!" and terminates itself. I would like it to ask again instead.

2. When I enter a correct password, it says "Valid!", sends a redstone signal for 3 seconds, then asks for a password again. Exactly what I would like it to be. Except the script terminates itself when the entered password is wrong, same as with first point.

3. Script still can be terminated.

 

I just started messing with LUA. Thanks for any advance!

 

EDIT: After messing with the script for another few hours. I made it to work! It's my first script that actually works.

local component = require("component")
local computer = require("computer")
local filesystem = require("filesystem")
local event = require("event")
local gpu = component.gpu
local io = require("io")
local os = require("os")
local rs = component.redstone
local sides = require("sides")
local term = require("term")

term.clear()
gpu.setResolution(37, 5) -- makes the resolution smaller, can be safely removed
rs.setOutput(sides.right, 0) -- makes sure the redstone signal is off; change "right" to the direction you want to emit redstone signal from
print("A room full of diamonds - access restricted") -- replace the text in quotas with anything you want
term.write("Password: ")
password = io.read()
if password == "POTATOE" then -- replace POTATOE with your password
	print("ACCESS GRANTED!")
	rs.setOutput(sides.right, 5) -- change "right" to the direction you want to emit redstone signal from
	gpu.setResolution(50, 16) -- bring the resolution back to normal; if you use a bigger screen, you might want to change it
	os.sleep(1)
	term.clear()
	os.exit()
else
	print("ACCESS DENIED!")
	computer.shutdown(true)
end

To make it safe from terminating, it needs to be saved as "autorun.lua" on any drive. Each time someone tries to terminate it, computer restarts and the scripts is being launched again.

When someone enters a wrong password, computer restarts and the script is being launched again.

When someone enters a right password, the redstone signal is being emited (for a door for example), then the script shuts down . It can be easily launched without digging through directories just by rebooting a computer. Redstone signal is turned off after relaunching the script.

Anyway, thanks for guys from IRC for help!

Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0

you should not do this though io.read (), build the string yourself through computer.pullSignal, that way you cant bypass by pressing ctrl-alt-c either.

How do I use this? I replaced io.read() to computer.pullSignal() and now my computer got in a reboot loop >.<

Link to post
Share on other sites
  • 0

computer.pullSignal works like event.pull does...

 

And you should most certainly read up on these functions before just using them.

 

if you do computer.pullSignal, it'll block until a signal is recieved, this could be component_added, component_removed, key_down or key_up alot of information is being sent ...

So you take down each keypress, and build it into a string

 

A very very crude example would be

local str = ''
local c = true
while c == true do
 local e,_, uni, code = computer.pullSignal ()
 if e == 'key_down' then -- So a key was pressed, yay, this is what we want
  if uni == 8 then uni = 0 end -- So some characters got both a unicode code, and a 'regular' code, example being backspace ... lets ensure we handle it through its code, again, crrruuuuude
  if uni == 13 then uni = 0 end -- enter, its \r character, not a \n
 
  if uni ~= 0 then
   str = str .. string.char(uni)
  elseif code == 14 then -- Backspace
   str = str:sub(1, #str - 1 )
  elseif code == 28 then -- Enter
   c = false
  end
 end
end
So you listen to each key_down, you build the string, one character at the time...

Do note above code have not been tested, its just approx

Edited by JoshTheEnder
Made code look nicer :)
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.