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

Unexpected PC Shutdown

Started by hbomb79, 23 July 2014 - 04:57 AM
hbomb79 #1
Posted 23 July 2014 - 07:02 AM
Hello again, First off ill start with the fact that you guys on the forum have been absolutely amazing, all your help has gotten me to this point and i am very happy for it, but now, down to business.

I now have yet another very weird problem, My computer seems to close for no apparent reason at the same time every time! Although there is no error message i see a flicker of writing that has the word bios in it.. If thats any help, The point it crashes seems to be at the end when it runs function thank() and starts the Parallel wait event, the text is printed and the countdown doesnt do anything, it prints the first "Force Reboot In: 10" and then stops, 5 seconds later, computer turns off…
Any help?

Heres the FULL code:

-- This is the first time setup so the computer will
-- Download all the required scripts.
running = true
-- Checking System Support
if not http then
  error "You Must Enable HTTP In Computer-craft Configuration To Use The System!"
end
if pocket then
  error "Sorry, The Software Does Not Support Pocket Computer!"
end
if not term.isColor() then
  error "This Computer Does Not Appear To Be Advanced, If It Is Advanced, Then Please Report This"
end

function PrintCentered(text, y)
    term.setTextColor(1)
local w, h = term.getSize()
    x = math.ceil(math.ceil((w / 2) - (#text / 2)), 0)+1
    term.setCursorPos(x, y)
term.clearLine()
    write(text)
end
--[[
files = {
[1] = "https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/installer",
[2] = "https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/version",
[3] = "https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/checker"
}
fileNames = {
[1] = "installer",
[2] = "version",
[3] = "checker"
}
]]--
function initBackground(color) --Draw The Background In The Specified Color
  term.setBackgroundColor(color)
  term.clear()
end

function drawTitleBar()
  term.setBackgroundColor(128)
  term.setCursorPos(1,1)
  term.clearLine()
  term.setTextColor(colors.cyan)
  write "HbombOS Security Solutions"
  term.setCursorPos(1,2)
  term.clearLine()
  term.setTextColor(256)
  write "Installer"
  term.setTextColor(1)
end
initBackground(256)
drawTitleBar()
local function checkSite()
  local response = http.get("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/version")
  if response then
    return true
  else
    return false
  end
end
  
function open()
term.setBackgroundColor(256)
term.setTextColor(1)
  PrintCentered("Hello And Welcome To The", 4)
  PrintCentered("HbombOS Security Suite", 5)
  PrintCentered("Installer", 6)
  PrintCentered("Press Any Key To Begin", 18)
  os.pullEvent("key")
  PrintCentered("Setting Up Preferences", 18)
  sleep(0)
  PrintCentered("Reading GitHub Repository", 18)
  if not checkSite() then
    PrintCentered("The GitHub Version File Does Not", 18)
    PrintCentered("Appear To Be At The Destination URL", 19)
term.setTextColor(colors.red)
PrintCentered("The Installer Has Encountered An Issue!", 2)
return false
  end
  PrintCentered("GitHub Repository Located", 18)
  sleep(1)
  PrintCentered("GitHub Repository Connection Established", 18)
  sleep(1)
  PrintCentered("BEWARE All Files That Have The Same Path", 8)
  PrintCentered("Will Be Overwritten!", 9)
  PrintCentered("Press 'Y' To Continue, Else Cancel", 10)
  PrintCentered("Waiting For Prompt", 18)
  event, key = os.pullEvent("key")
    if key == keys.y then
   PrintCentered("Prompt Received, Continuing", 18)
   sleep(0)
   PrintCentered("Starting Download Sequence", 18)
   sleep(0.1)
   return true
else
   PrintCentered("Cancelling Download, Aborting Installer", 18)
   sleep(0)
   return false
end
end
function downloadFiles(getUrl, toPath)
term.setTextColor(1)
term.setBackgroundColor(256)
-- Download the files and scripts from github
  for i = 1, 3 do
    local response = http.get(getUrl)
if response then
   data = response.readAll()
	   if fs.exists(toPath) then
    fs.delete(toPath)
   -- PrintCentered ("Deleted Previous File At Location: "..toPath, 18)
  end
  if toPath then
    local file = io.open(toPath, "w")
    file:write(data)
    file:close()
   -- PrintCentered ("Data Updated To File: "..toPath.."!", 18)
    return true
  else
    error "We Believe The Path Specified Is Invalid, Report If Not Your Fault"
  end
else
   error ("The File Or Files At: "..getUrl.." Do Not Appear To Exist! Please Report This Issue On Forums Or Through GitHub")
end
  end
  error ("Failed To Download The File From URL: "..getUrl.." Please try again later, If this is the 2nd time you've seen this, then report it on the fourms")
end
function downloadQuery()
  term.setCursorPos(1,19)
  term.setTextColor(1)
  term.setBackgroundColor(256)
  PrintCentered("Attempting Download Of Files!", "5")
  for _, fileForDownload in ipairs(files) do
    sleep(0)
  for _, fileDownloadName in ipairs(fileNames) do
   downloadFiles(fileForDownload, fileDownloadName)
  end
  end
  return true
end
function wait()
  running = true
  running1 = false
  running2 = false
  running3 = false
  runningStart = true
  while runningStart == true do
    if running then
   if not haltDownload then
	    PrintCentered("Downloading", 19)
	 sleep(1)
	 running = false
	 running1 = true
   end
end
if running1 then
   if not haltDownload then
	    PrintCentered("Downloading.", 19)
	 sleep(1)
	 running1 = false
	 running2 = true
   end
end
if running2 then
   if not haltDownload then
	    PrintCentered("Downloading..", 19)
	 sleep(1)
	 running2 = false
	 running3 = true
   end
end
if running3 then
   if not haltDownload then
	    PrintCentered("Downloading...", 19)
	 sleep(1)
	 running = true
	 running1 = false
	 running2 = false
	 running3 = false
   end
end
  end
end
function stop()
haltDownload = true
end
function waitForKey(key)
  os.pullEvent("key")
end
function rebootCountdown()
  for x=10, 0, -1 do
    print("Force Reboot In: "..x)
    sleep(1)
  end
end
function thank()
  stop()
  local f = fs.open("version", "r")
  vCurrent = f.readLine()
  f.close()
  term.clear()
  drawTitleBar()
  term.setBackgroundColor(256)
  PrintCentered("Thank You For Updating The Latest Stable Build", 4)
  PrintCentered("Your Use Of This Program Is Much Appreciated", 5)
  PrintCentered("Report Any Bugs You Find", 7)
  PrintCentered("Avoid Posting If Already Reported", 8)
  PrintCentered("Thanks Again, Hope You Enjoy", 10)
  PrintCentered("Your Version: "..vCurrent.."", 12)
  PrintCentered("Please Press Any Key To Reboot This PC Or Wait", 18)
  parallel.waitForAny(waitForKey, rebootCountdown)
  PrintCentered("Rebooting..", 19)
  sleep(2)
  os.reboot()
end
function download()
term.setBackgroundColor(256)
PrintCentered ("Downloading Scripts... Please Wait", 18)
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/startup", "startup")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/version", "version")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Install/installer", "systemFiles/Install/installer")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Install/updater", "systemFiles/Install/updater")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/BootLogos/boot0.nfp", "systemFiles/BootLogos/boot0.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/BootLogos/boot1.nfp", "systemFiles/BootLogos/boot1.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/BootLogos/boot2.nfp", "systemFiles/BootLogos/boot2.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/BootLogos/boot3.nfp", "systemFiles/BootLogos/boot3.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/BootLogos/boot4.nfp", "systemFiles/BootLogos/boot4.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/BootLogos/boot5.nfp", "systemFiles/BootLogos/boot5.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/BootLogos/boot6.nfp", "systemFiles/BootLogos/boot6.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/BootLogos/boot7.nfp", "systemFiles/BootLogos/boot7.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/BootLogos/boot8.nfp", "systemFiles/BootLogos/boot8.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/bootFail.nfp", "systemFiles/bootFail.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/api/download", "/api/download")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/api/systemCheck", "/api/systemCheck")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/api/update", "/api/update")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/README", "/Documentation/README")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/LICENSE", "/Documentation/LISENCE")
PrintCentered("Complete", 19)
sleep(1)
term.clear()
stop()
thank()
end
if open() then
parallel.waitForAny(download, wait)
stop()
end

Thanks again everyone!

Also after removing the

parallel.waitForAny(waitForKey, rebootCountdown)
It says rebooting and then hangs, and shutdown?!?!?!

And sometimes it crashes at the start! When its supposed to say downloading it crashes… Maybe its the wait function, has anyone got a better way of doing it?? I need it to stop as soon as i change the variable tho otherwise oon the next screen it will still say, Downloading!
hbomb79 #2
Posted 23 July 2014 - 07:14 AM
The system works as it should if the Wait function is removed, i would really like something like that implemented though, Anyone got any ideas why its causing trouble?
Bomb Bloke #3
Posted 23 July 2014 - 09:51 AM
When a computer/turtle starts running code, ComputerCraft starts a ten second timer. If that code doesn't yield before that timer ends then ComputerCraft crashes that computer. After each yield, processing continues with a new time limit.

The reason why is that running your code chews up valuable server processing power, and so it shouldn't be able to monopolise it. In fact, only ONE CC device can run code at a time: While one is doing something, none of the others can do anything until it yields.

Whether or not it takes more then ten seconds for your code to execute has a lot to do with the power of the Minecraft server it's running on, and how often you allow your code to yield. Pulling events (eg getting typed characters) triggers a yield, as does sleeping (which pulls a timer event) and many commands (eg turtle movements or rednet.receive()) also pull events to work.

In the case of your "wait" function, you only allow it to yield (via sleep()) if at least one of your pairs of conditions are true. If none are, then it'll loop flat out until ComputerCraft kills it.
hbomb79 #4
Posted 23 July 2014 - 10:24 AM
So all I have to do is add a sleep at the very beginning to ensure there is a yield

Like This:


function wait()
  running = true
  running1 = false
  running2 = false
  running3 = false
  runningStart = true
  while runningStart == true do
   sleep(0)
    if running then
   if not haltDownload then
		    PrintCentered("Downloading", 19)
		 sleep(1)
		 running = false
		 running1 = true
   end
end
if running1 then
   if not haltDownload then
		    PrintCentered("Downloading.", 19)
		 sleep(1)
		 running1 = false
		 running2 = true
   end
end
if running2 then
   if not haltDownload then
		    PrintCentered("Downloading..", 19)
		 sleep(1)
		 running2 = false
		 running3 = true
   end
end
if running3 then
   if not haltDownload then
		    PrintCentered("Downloading...", 19)
		 sleep(1)
		 running = true
		 running1 = false
		 running2 = false
		 running3 = false
   end
end
  end
end
Edited on 23 July 2014 - 08:24 AM
Bomb Bloke #5
Posted 23 July 2014 - 10:52 AM
Sounds like that should work.

But really that's a long-winded way of doing it. You could shrink it down loads:

local function wait()
	local counter = 0
	
	while true do
		PrintCentered("Downloading"..string.rep(".",counter), 19)
		
		counter = counter + 1
		if counter > 3 then counter = 0 end
			
		if haltDownload then break else sleep(1) end
	end
end