This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Transferring variables through scripts?
Started by Titantyler, 16 July 2012 - 05:49 AMPosted 16 July 2012 - 07:49 AM
Right now I am working on a disk drive that contains a lock script on it, and transfers it to another computer with an installer program. The installer program acts as a setup and it asks you questions like "What do you want your username to be" and "What do you want your password to be". I am stuck because I don't know how I can make it save what you put through one script to another. Any help would be appreciated. Thanks!
Posted 16 July 2012 - 08:37 AM
Well, first of all, you got to save the password protection to your startup file. you do something like:
Hope this helped.
password = "hurrdurr"
tProt = {
"some line",
"blah",
"blah",
"the program is here",
"if prot == "..password.." then",
"else",
"end"}
file = fs.open("startup","w") --Opens the file for writing.
file.write(tProt)
file.close()
I think it would work… not sure.Hope this helped.
Posted 17 July 2012 - 10:56 PM
Alright, I will try that. Thanks.
Posted 17 July 2012 - 11:49 PM
You should not save the password in the same file as your actual script. Have a seperate config file, where you put that kind of stuff into and have your script read that file on start.
Posted 13 August 2012 - 06:32 PM
I tried that, but I can't find any tutorials on how to properly use fs.open. Could I please have an example of how to use this?You should not save the password in the same file as your actual script. Have a seperate config file, where you put that kind of stuff into and have your script read that file on start.
Posted 13 August 2012 - 06:47 PM
local function saveState(saveTable)
local file = fs.open("status", "w")
if file then
for eNum, eInfo in ipairs(saveTable) do
file.writeLine(eInfo.type..","..eInfo.user..","..eInfo.pass)
end
file.close()
end
end
local function loadState()
local loadTable = {}
local file = fs.open("status", "r")
if file then
readLine = file.readLine()
while readLine do
local lineTable = {}
lineTable.type, lineTable.user, lineTable.pass = string.match(readLine, "(%w+),(%w+),(%w+)")
if lineTable.type then
table.insert(loadTable, lineTable)
end
readLine = file.readLine()
end
file.close()
end
return loadTable
Here is a save/load state function that I like to use. Basically, you add
local controlTable = {}
At the top of your code, and before declaring the new user/admin/whatever, use this:
local deviceTable = {type="NAME OF USER TYPE HERE"}
This is an example of declaring an admin:
while true do
local controlTable = {}
while true do
title()
local deviceTable = {type="Admin"}
while true do
print("Please enter Administrator name:")
deviceTable.name = read()
if deviceTable.name == "" then
print("Admin name cannot be blank")
sleep(1)
title()
else break
end
end
title()
while true do
while true do
print("Please enter Administrator password:")
tempadmin = read("*")
if tempadmin == "" then
print("Password cannot be blank")
sleep(1)
title()
else break
end
end
title()
while true do
print("Please re-enter password:")
tempadmin2 = read("*")
if tempadmin2 == "" then
print("Password cannot be blank")
sleep(1)
title()
else break
end
end
title()
if tempadmin ~= tempadmin2 then
print("Passwords do not match. Please try again.")
sleep(1)
title()
elseif tempadmin == tempadmin2 then
deviceTable.pass = tempadmin break
end
end
title()
print("Thank you. Admin profile created.")
sleep(1)
table.insert(controlTable,deviceTable)
saveState(controlTable)
tempadmin = nil
tempadmin2 = nil
break
end
end
end
The title() function actually is my way to redraw my title screen.edit: To load the script, simply call this:
print("Please enter username")
local input = read()
local controlTable = loadState()
if deviceTable.type == "Admin" and deviceTable.name == input then
term.clear() term.setCursorPos(1,1)
print("Please enter password")
local input = read("*")
local controlTable = loadState()
if deviceTable.type == "Admin" and deviceTable.pass == input then
--do stuff here
end
end
edit 2: Whoops, forgot to remane the variables in the example.
Edited on 13 August 2012 - 05:16 PM
Posted 13 August 2012 - 07:15 PM
why don't you just use textutils.serialize and deserialize for tabels ?Here is a save/load state function that I like to use. Basically, you addlocal function saveState(saveTable) local file = fs.open("status", "w") if file then for eNum, eInfo in ipairs(saveTable) do file.writeLine(eInfo.type..","..eInfo.user..","..eInfo.pass) end file.close() end end local function loadState() local loadTable = {} local file = fs.open("status", "r") if file then readLine = file.readLine() while readLine do local lineTable = {} lineTable.type, lineTable.user, lineTable.pass = string.match(readLine, "(%w+),(%w+),(%w+)") if lineTable.type then table.insert(loadTable, lineTable) end readLine = file.readLine() end file.close() end return loadTable
At the top of your code, and before declaring the new user/admin/whatever, use this:local controlTable = {}
This is an example of declaring an admin:local deviceTable = {type="NAME OF USER TYPE HERE"}
The title() function actually is my way to redraw my title screen.while true do local controlTable = {} while true do title() local deviceTable = {type="Admin"} while true do print("Please enter Administrator name:") deviceTable.color = read() if deviceTable.color == "" then print("Admin name cannot be blank") sleep(1) title() else break end end title() while true do while true do print("Please enter Administrator password:") tempadmin = read("*") if tempadmin == "" then print("Password cannot be blank") sleep(1) title() else break end end title() while true do print("Please re-enter password:") tempadmin2 = read("*") if tempadmin2 == "" then print("Password cannot be blank") sleep(1) title() else break end end title() if tempadmin ~= tempadmin2 then print("Passwords do not match. Please try again.") sleep(1) title() elseif tempadmin == tempadmin2 then deviceTable.status = tempadmin break end end title() print("Thank you. Admin profile created.") sleep(1) table.insert(controlTable,deviceTable) saveState(controlTable) tempadmin = nil tempadmin2 = nil break end end end
edit: To load the script, simply call this:print("Please enter username") local input = read() local controlTable = loadState() if deviceTable.type == "Admin" and deviceTable.name == input then term.clear() term.setCursorPos(1,1) print("Please enter password") local input = read("*") local controlTable = loadState() if deviceTable.type == "Admin" and deviceTable.pass == input then --do stuff here end end
Posted 13 August 2012 - 07:17 PM
Hrm….I guess that would work…LOL. :P/>/>
I guess I am just used to writing to a file now for many different tables. :D/>/>
I guess I am just used to writing to a file now for many different tables. :D/>/>
Posted 13 August 2012 - 07:28 PM
@craniumkid
It's even easier if you use the textutils API, it has a function serialize, which turns tables into strings and an unserialize function which turns strings into tables (if they are syntactically correct, of course).
@titantyler
You open files with "io.open (<file_name>, <mode>)", where <file_name> is the path to the file you want to open and <mode> is either "w" if you want to have write access to file or "r" if read access is sufficient for your application (if you just want to read something from a file, you really should not open it in write mode, so you don't accidentally mess something up).
As an example, I use the following API to load / write data from my programs to disk:
EDIT: Damn, too late.
It's even easier if you use the textutils API, it has a function serialize, which turns tables into strings and an unserialize function which turns strings into tables (if they are syntactically correct, of course).
@titantyler
You open files with "io.open (<file_name>, <mode>)", where <file_name> is the path to the file you want to open and <mode> is either "w" if you want to have write access to file or "r" if read access is sufficient for your application (if you just want to read something from a file, you really should not open it in write mode, so you don't accidentally mess something up).
As an example, I use the following API to load / write data from my programs to disk:
Spoiler
function configure (file, defaults)
-- convenience function since I have had that function in most of my programs and I am lazy
if not fs.exists (file) then
storage.dump (file, defaults)
end
local conf = storage.load (file)
if conf ~= -1 then
return conf
else
shell.exit (-1)
end
end
function load (file)
if fs.exists (file) then
local storage_file = io.open (file, 'r')
local storage_data = storage_file:read ('*a')
storage_data, _, _ = string.gsub (storage_data, 'n', ', ')
storage_file:close ()
return textutils.unserialize (storage_data)
else
return -1
end
end
function dump (file, data)
local _, _, path = string.find (file, "^(.*)/.*$")
if not fs.isDir (path) then
fs.makeDir (path)
end
local storage_file = io.open (file, 'w')
local data = string.gsub (textutils.serialize (data), ',', 'n')
storage_file:write (data)
storage_file:close ()
end
EDIT: Damn, too late.
Posted 13 August 2012 - 07:49 PM
ninja d 2 guys at the same time SUCCESS@craniumkid
It's even easier if you use the textutils API, it has a function serialize, which turns tables into strings and an unserialize function which turns strings into tables (if they are syntactically correct, of course).
@titantyler
You open files with "io.open (<file_name>, <mode>)", where <file_name> is the path to the file you want to open and <mode> is either "w" if you want to have write access to file or "r" if read access is sufficient for your application (if you just want to read something from a file, you really should not open it in write mode, so you don't accidentally mess something up).
As an example, I use the following API to load / write data from my programs to disk:Spoiler
function configure (file, defaults) -- convenience function since I have had that function in most of my programs and I am lazy if not fs.exists (file) then storage.dump (file, defaults) end local conf = storage.load (file) if conf ~= -1 then return conf else shell.exit (-1) end end function load (file) if fs.exists (file) then local storage_file = io.open (file, 'r') local storage_data = storage_file:read ('*a') storage_data, _, _ = string.gsub (storage_data, 'n', ', ') storage_file:close () return textutils.unserialize (storage_data) else return -1 end end function dump (file, data) local _, _, path = string.find (file, "^(.*)/.*$") if not fs.isDir (path) then fs.makeDir (path) end local storage_file = io.open (file, 'w') local data = string.gsub (textutils.serialize (data), ',', 'n') storage_file:write (data) storage_file:close () end
EDIT: Damn, too late.