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

Require 'label set' on first boot

Started by Andale, 25 March 2013 - 03:29 PM
Andale #1
Posted 25 March 2013 - 04:29 PM
I am certain I can probably figure out a program for it, but how can I make it so every new computer/turtle requires you to set a label on the first boot of it only?
My only idea is to modify the zip to have a default startup program which deletes itself after successfully setting a label.
theoriginalbit #2
Posted 25 March 2013 - 04:33 PM
You could do a label method (which I forgot to put in, oops, see Kingdaro's post). or you could do a file method like so

if not fs.exists('.firstRunRan') then
  -- create the label here
  local h = fs.open('.firstRunRan', 'r')
  h.close()
end
Makes it easy to do a 'reset' too, just delete the file and your program will think its first run.
Kingdaro #3
Posted 25 March 2013 - 04:40 PM
Why not just always ask for a label if there is no label, instead of deleting the program?


if not os.getComputerLabel() then
  write 'Set a label for this computer: '
  os.setComputerLabel(read())
end
Andale #4
Posted 26 March 2013 - 10:44 AM
Sorry, that self erasre idea was only if I had to make a default startup program. We reset automatically every 3 hours and most users have their programs saved as startup so they don't gotta touch them every time.
My question was maybe poorly stated. It is more like… Do I have to make a default 'startup' file? (which is why I would have it self erase, so people are not confused by it) Is there a method to make a first run only file other than a startup?
faubiguy #5
Posted 26 March 2013 - 12:34 PM
If you have access to the server files, you can modify the rom/shell program and add Kingdaro's code, so when the shell starts, it automatically asks for a label. (Anywhere before the main loop at the end would probably work, or before the part where it runs the startup file if you want to it to be done if a user has a startup script that doesn't exit).

You could also modify the part the checks for a startup file to run a second file that's automatically deleted like you said, but I think this makes the most sense for what you're trying to do.
Andale #6
Posted 27 March 2013 - 07:32 PM
Ok, so like this then?
I'm editing computercraft.zip\lua\rom\programs\shell.
If I save this shell. into the zip file in the mods folder it should beomce this on next server reload from what I'd guess. (I'm not our server owner, but I do a lot of post setup config these days so I'm double checking everything before doing anything.)
I tried to add a couple programs in the zip before, but I can't tell if I was the one who screwed up or my server owner just happened to wipe/replace it on the upgrade the next day.

This is where I put the code: (in spoiler cuz code usually won't color format on most sites)

Spoilerlocal parentShell = shell
local bExit = false
local sDir = (parentShell and parentShell.dir()) or ""
local sPath = (parentShell and parentShell.path()) or ".:/rom/programs"
local tAliases = (parentShell and parentShell.aliases()) or {}
local tProgramStack = {}
local shell = {}
local tEnv = {
["shell"] = shell,
}
– Colours
local promptColour, textColour, bgColour
if term.isColour() then
promptColour = colours.yellow
textColour = colours.white
bgColour = colours.black
else
promptColour = colours.white
textColour = colours.white
bgColour = colours.black
end

local function run( _sCommand, … )
local sPath = shell.resolveProgram( _sCommand )
if sPath ~= nil then
tProgramStack[#tProgramStack + 1] = sPath
local result = os.run( tEnv, sPath, … )
tProgramStack[#tProgramStack] = nil
return result
else
printError( "No such program" )
return false
end
end
local function runLine( _sLine )
local tWords = {}
for match in string.gmatch( _sLine, "[^ \t]+" ) do
table.insert( tWords, match )
end
local sCommand = tWords[1]
if sCommand then
return run( sCommand, unpack( tWords, 2 ) )
end
return false
end
– Install shell API
function shell.run( … )
return runLine( table.concat( { … }, " " ) )
end
function shell.exit()
bExit = true
end
function shell.dir()
return sDir
end
function shell.setDir( _sDir )
sDir = _sDir
end
function shell.path()
return sPath
end
function shell.setPath( _sPath )
sPath = _sPath
end
function shell.resolve( _sPath )
local sStartChar = string.sub( _sPath, 1, 1 )
if sStartChar == "/" or sStartChar == "\\" then
return fs.combine( "", _sPath )
else
return fs.combine( sDir, _sPath )
end
end
function shell.resolveProgram( _sCommand )
– Substitute aliases firsts
if tAliases[ _sCommand ] ~= nil then
_sCommand = tAliases[ _sCommand ]
end
– If the path is a global path, use it directly
local sStartChar = string.sub( _sCommand, 1, 1 )
if sStartChar == "/" or sStartChar == "\\" then
local sPath = fs.combine( "", _sCommand )
if fs.exists( sPath ) and not fs.isDir( sPath ) then
return sPath
end
return nil
end

– Otherwise, look on the path variable
for sPath in string.gmatch(sPath, "[^:]+") do
sPath = fs.combine( shell.resolve( sPath ), _sCommand )
if fs.exists( sPath ) and not fs.isDir( sPath ) then
return sPath
end
end

– Not found
return nil
end
function shell.programs( _bIncludeHidden )
local tItems = {}

– Add programs from the path
for sPath in string.gmatch(sPath, "[^:]+") do
sPath = shell.resolve( sPath )
if fs.isDir( sPath ) then
local tList = fs.list( sPath )
for n,sFile in pairs( tList ) do
if not fs.isDir( fs.combine( sPath, sFile ) ) and
(_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then
tItems[ sFile ] = true
end
end
end
end
– Sort and return
local tItemList = {}
for sItem, b in pairs( tItems ) do
table.insert( tItemList, sItem )
end
table.sort( tItemList )
return tItemList
end
function shell.getRunningProgram()
if #tProgramStack > 0 then
return tProgramStack[#tProgramStack]
end
return nil
end
function shell.setAlias( _sCommand, _sProgram )
tAliases[ _sCommand ] = _sProgram
end
function shell.clearAlias( _sCommand )
tAliases[ _sCommand ] = nil
end
function shell.aliases()
– Add aliases
local tCopy = {}
for sAlias, sCommand in pairs( tAliases ) do
tCopy[sAlias] = sCommand
end
return tCopy
end

term.setBackgroundColor( bgColour )
term.setTextColour( promptColour )
print( os.version() )
term.setTextColour( textColour )

– ask for a label on any computer without
if not os.getComputerLabel() then
write 'Set a label for this computer: '
os.setComputerLabel(read())
end


– If this is the toplevel shell, run the startup programs
if parentShell == nil then
– Run the startup from the ROM first
local sRomStartup = shell.resolveProgram( "/rom/startup" )
if sRomStartup then
shell.run( sRomStartup )
end

– Then run the user created startup, from the disks or the root
local sUserStartup = shell.resolveProgram( "/startup" )
for n,sSide in pairs( redstone.getSides() ) do
if disk.isPresent( sSide ) and disk.hasData( sSide ) then
local sDiskStartup = shell.resolveProgram( fs.combine(disk.getMountPath( sSide ), "startup") )
if sDiskStartup then
sUserStartup = sDiskStartup
break
end
end
end

if sUserStartup then
shell.run( sUserStartup )
end
end
– Run any programs passed in as arguments
local tArgs = { … }
if #tArgs > 0 then
shell.run( … )
end
– Read commands and execute them
local tCommandHistory = {}
while not bExit do
term.setBackgroundColor( bgColour )
term.setTextColour( promptColour )
write( shell.dir() .. "> " )
term.setTextColour( textColour )
local sLine = read( nil, tCommandHistory )
table.insert( tCommandHistory, sLine )
runLine( sLine )
end
– If this is the toplevel shell, run the shutdown program
if parentShell == nil then
if shell.resolveProgram( "shutdown" ) then
shell.run( "shutdown" )
end
os.shutdown() – just in case
end
faubiguy #7
Posted 27 March 2013 - 08:05 PM
Yes, that looks right. And I just thought of this, but I'd probably put
term.clear()
term.setCursorPos(1,1)
after the part where it sets the label, so that if the label gets asked for, the prompt and input don't stay on the screen. If you do that, it should go before the lines of code right before it (before the print(os.version()), so that won't get cleared as well.
Andale #8
Posted 28 March 2013 - 03:08 AM
ok, in the end i added faubi's suggestion AND I set it to reboot after just to get it to display the name which Iadded as an else to the if not os.getComputerLabel

Ok, I'm gonna save this to the zip and upload it back in to test

term.setBackgroundColor( bgColour )
term.setTextColour( promptColour )
print( os.version() )
term.setTextColour( textColour )


– ask for a label on any computer without
if not os.getComputerLabel() then
write 'Set a label for this computer: '
os.setComputerLabel(read())
os.reboot()
else
print(os.getComputerLabel())
end

term.clear()
term.setCursorPos(1,1)

– If this is the toplevel shell
theoriginalbit #9
Posted 28 March 2013 - 03:13 AM
use code tags man…

so why do you reboot the computer exactly? seems unnecessary.

also whats with the "– If this is the toplevel shell" comment?
faubiguy #10
Posted 28 March 2013 - 03:18 AM
What I meant was for the part
if not os.getComputerLabel() then
write 'Set a label for this computer: '
os.setComputerLabel(read())
reboot()
else
print(os.getComputerLabel())
end

term.clear()
term.setCursorPos(1,1)
to go before
term.setBackgroundColor( bgColour )
term.setTextColour( promptColour )
print( os.version() )
term.setTextColour( textColour )
, That way the terminal gets cleared before it prints CraftOS/TurtleOS, so that doesn't get cleared. Either way, the print(os.getComputerLabel()) wouldn't have an effect, because it gets cleared immediately after.
Dlcruz129 #11
Posted 28 March 2013 - 03:18 AM
also whats with the "– If this is the toplevel shell" comment?

That's already in the default shell.
Andale #12
Posted 28 March 2013 - 03:50 AM
so uploading it into the zip in the /mods/ folder doesn't do it
there's no 'computer' or 'lua' folder in either my base, mods, or worldname folders that contains the programs folder
there is only a folder named computer under worldname (worldname/computer) that contains the current computers and turtles by id number

did my server screw up somehow? where is it getting the default programs from?
Andale #13
Posted 28 March 2013 - 03:53 AM
What I meant was for the part
if not os.getComputerLabel() then
write 'Set a label for this computer: '
os.setComputerLabel(read())
reboot()
else
print(os.getComputerLabel())
end

term.clear()
term.setCursorPos(1,1)
to go before
term.setBackgroundColor( bgColour )
term.setTextColour( promptColour )
print( os.version() )
term.setTextColour( textColour )
, That way the terminal gets cleared before it prints CraftOS/TurtleOS, so that doesn't get cleared. Either way, the print(os.getComputerLabel()) wouldn't have an effect, because it gets cleared immediately after.


my fault, two different thought processes at same time, i'll rearrange the clear later

I left it after os version to show the computer is working, just stopped to do this

I guess if I have the reboot then I don't need the clear at all tho.


wait…. now that I think about it, i went in to test and it did show only the >_ terminal so it did work from the zip file. Ugh… it's too early for me. I'll do this later