Advertisement
jesusthekiller

uOS Startup

Jun 16th, 2013
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.10 KB | None | 0 0
  1. -- UOS STARTUP IS _startup
  2.  
  3. --[[
  4.     Author of original code: NeverCast
  5. ]]--
  6.  
  7. --[[
  8.     Project info:
  9.    
  10.     Name: uOS
  11.     Creator: Jesusthekiller
  12.     Language: Lua (CC)
  13.     Website: None
  14.     License: GNU GPL
  15.         License file can be fount at www.jesusthekiller.com/license-gpl.html
  16.  
  17.     Version: 1.0
  18. ]]--
  19.  
  20. --[[
  21.     Changelog:
  22.       1.0:
  23.         Initial Release
  24. ]]--
  25.  
  26. --[[
  27.     LICENSE:
  28.    
  29.     uOS
  30.     Copyright (c) 2013 Jesusthekiller
  31.  
  32.     This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  33.  
  34.     This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  35.  
  36.     See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
  37. ]]--
  38.  
  39. if not fs.exists("uos") then
  40.     shell.run("pastebin", "get", "7jGYum4B", "uos")
  41. end
  42.  
  43. if os.myEnv then
  44.     return
  45. end
  46.  
  47. local prevState = {}
  48.  
  49. table.size = function(tbl)
  50.     local count = 0
  51.     for _,__ in pairs(tbl) do
  52.         count = count + 1
  53.     end
  54.     return count
  55. end
  56.  
  57. local function getParentShell()
  58.   local at=0
  59.   while true do
  60.     at=at+1
  61.     local ok,env = pcall(function() return getfenv(at) end)
  62.     if not ok then break end
  63.     if table.size(env) == 1 then
  64.           local i,v = next(env) -- Grab first
  65.           if i == "shell" then
  66.             return v
  67.           end
  68.     end  
  69.   end
  70.   return nil
  71. end
  72.  
  73. local function recover()
  74.     -- Set flag
  75.     os.myEnv = true
  76.     -- Put back the trampled environment
  77.     os.startTimer = prevState.startTimer
  78.     os.shutdown = prevState.shutdown
  79.     os.pullEvent = prevState.pullEvent
  80.     --
  81.    
  82.     prevState = nil
  83.     -- start rednet
  84.     os.unloadAPI("rednet")
  85.     os.loadAPI("rednet")
  86.    
  87.     -- start uos shell
  88.     os.run({}, "uos")
  89. end
  90.  
  91. os.stage = {}
  92.  
  93. local function setStage(stage)
  94.     os.stage.currentStage = stage
  95. end
  96.  
  97. local function getStage()
  98.     return os.stage.currentStage
  99. end
  100.  
  101. local function _os_pullEvent(_filter)
  102.     _filter = _filter or ""
  103.     if _filter == "key" then
  104.         setStage("bioswait")
  105.         return "key", 0
  106.     elseif _filter == "timer" then
  107.         setStage("shutdown")
  108.         return "timer", 0
  109.     end
  110. end
  111.  
  112. local function _replSleep(dur)
  113.     local timer = prevState.startTimer( dur )
  114.     repeat
  115.         local sEvent, param = prevState.pullEvent( "timer" )
  116.     until param == timer
  117. end
  118.  
  119. local function _os_shutdown()
  120.     if getStage() == "shutdown" then
  121.         setStage("jstincse")
  122.     elseif getStage() == "bioswait" then
  123.         setStage("biosexit")
  124.     end
  125.     if getStage() == "biosexit" then
  126.         recover()
  127.     end
  128. end
  129.  
  130. local function _os_startTimer(seconds)
  131.     return 0
  132. end
  133.  
  134. local function inject()
  135.     prevState.startTimer = os.startTimer
  136.     prevState.shutdown = os.shutdown
  137.     prevState.pullEvent = os.pullEvent
  138.     os.shutdown = _os_shutdown
  139.     os.pullEvent = _os_pullEvent
  140.     os.startTimer = _os_startTimer
  141.     setStage("injected")
  142.     local shellImpl = getParentShell()
  143.     shellImpl.exit()
  144. end
  145.  
  146. inject()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement