Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local program, args = (select(1, ...)), table.pack(select(2, ...))
- if select('#', ...) < 1 then program = 'shell' end
- if shell then
- local resolved = shell.resolveProgram(program)
- if not resolved then
- printError('Couldn\'t find program: ' .. program)
- return 1
- end
- program = resolved
- end
- -- else hope the user knows what they are doing
- -- it probably won't even work unless some parent env has shell
- local function pullEventAbsolute(filter)
- local data
- while true do
- data = table.pack(coroutine.yield(filter))
- if data[1] == filter then
- return table.unpack(data, 1, data.n)
- end
- end
- end
- local originalQueueEvent = os.queueEvent
- local function onError(err)
- printError(err)
- print('Press any key to continue')
- originalQueueEvent('fast-forward')
- pullEventAbsolute('fast-forward')
- pullEventAbsolute('key')
- os.shutdown()
- os.shutdown()
- end
- local eventName = 'tlco_' .. tostring(math.random(-20000, 20000))
- local originalPrint = print
- local originalShutdown = os.shutdown
- local originalPullEvent = os.pullEvent
- local function restore()
- _G.print = originalPrint
- os.shutdown = originalShutdown
- os.pullEvent = originalPullEvent
- end
- function _G.print() end -- prevent error text, and "press any key to continue"
- local methodUsed = 'Unknown'
- function os.shutdown() -- called at end of bios
- pullEventAbsolute(eventName)
- restore()
- -- We have now achieved top-level coroutine override
- term.setTextColor(colors.cyan)
- print('TLCO Activated, ' .. methodUsed .. ' method')
- term.setTextColor(colors.white)
- local preShutdown = os.shutdown
- -- run directly in _G
- local func, err = loadfile(program, nil, _G)
- if not func then return onError(err) end
- local ok, err = pcall(func, table.unpack(args, 1, args.n))
- if not ok then return onError(err) end
- -- Only run os.shutdown if it has changed, for nested TLCOs
- if os.shutdown ~= preShutdown then
- -- Run os.shutdown twice. Why? Because regular bios does it. Sane TLCOs hook the second shutdown after shell exit.
- -- There is no rednet to crash, so TLCO will try to exit the shell. Give them something to hook if they so please.
- -- I don't know why people would nest TLCOs, but maybe they do
- os.shutdown()
- os.shutdown()
- end
- end
- function os.pullEvent(filter) -- called in middle of bios
- if filter == 'key' then
- -- haha lol
- else
- return originalPullEvent(filter)
- end
- end
- methodUsed = 'Rednet'
- os.queueEvent('modem_message', 0) -- crash rednet
- os.queueEvent(eventName)
- pullEventAbsolute(eventName) -- wait for the crash
- -- If we are still running, try a different method
- methodUsed = 'Shell'
- do -- Expect two shutdowns
- local hookedShutdown = os.shutdown
- function os.shutdown()
- os.shutdown = hookedShutdown
- end
- end
- do -- Prevent one second delay caused by rom/programs/shutdown.lua
- local originalSleep = _G.sleep
- function _G.sleep() end
- -- Restore os.sleep once TLCO executes
- local originalRestore = restore
- restore = function()
- originalRestore()
- _G.sleep = originalSleep
- end
- end
- do -- Exit all shells
- local exited = false
- for i = 1, 100 do
- local ok, fenv = pcall(getfenv, i)
- if ok and fenv.shell then
- fenv.shell.exit()
- exited = true
- end
- end
- if not exited then
- restore()
- printError('TLCO failed: not running under CraftOS')
- return 1
- end
- end
- os.queueEvent(eventName)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement