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

computer shuts down on os.pullEventRaw() override

Started by cmdpwnd, 31 December 2015 - 07:49 PM
cmdpwnd #1
Posted 31 December 2015 - 08:49 PM
I'm trying to make a processManager of sorts and when I override os.pullEventRaw() the computer just shuts off immediately which gives me no time to see of any kind of error etc and then when i right click on the computer again it just boots back into craftOS. Any help appreciated.


--processManager.lua

pullEventRaw_Backup = os.pullEventRaw

local function _init_()
	if not _PM_RUN_STATUS then
		print('loading api')
		_PM_RUN_STATUS=1
		--define coroutines
		co_func = coroutine.create(func)
		return
	end
end

local function func()
	print('func is running')
	local received = {}
	for i=1,#interface.tInterfaceWrap do
		local event = {os.pullEventRaw()}
		if event and event[2] == "modem_message" and event[6] then
			table.insert(received,event[6])
		end
	end
	return received
end

function _G.os.pullEventRaw(sFilter)
	print('os.pullEventRaw is called')
  while true do
	local event = {pullEventRaw_Backup()}
	print('event pulled')
	if coroutine.status(co_func) == "suspended" then
		print('resuming coroutine')
	  coroutine.resume(co_func, table.unpack(event))
	end
	if sFilter == event[1] or not sFilter then
	  return table.unpack(event)
	end
  end
end

_init_()

CORRECT ANSWER HERE

--processManager.lua

function _init_()
	if not _PM_RUN_STATUS then
		_PM_RUN_STATUS=1
		return
	end
end

function func()
	while true do
		local received
		for i=1,#interface.tInterfaceWrap do
			local event = {os.pullEventRaw()}
			if event and event[2] == "modem_message" and event[4] == interface.channel and interface.tInterfaceState[event[3]] == 'up' and event[6] then
				table.insert(received,event[6])
			end
		end
		coroutine.yield(received)
	end
end

local pullEventRaw_Backup = os.pullEventRaw
local co_func = coroutine.create(func)

function os.pullEventRaw(sFilter)
  while true do
	local event = {pullEventRaw_Backup()}
	if coroutine.status(co_func) == "suspended" then
	  coroutine.resume(co_func, table.unpack(event))
	end
	--Return any events
	if sFilter == event[1] or not sFilter then
	  return table.unpack(event)
	end
  end
end

_init_()
Edited on 07 March 2016 - 01:28 AM
LBPHacker #2
Posted 31 December 2015 - 09:09 PM
I'm assuming that you know how to handle coroutines. Here are some things that make this piece of code weird and non-working:
  • _G.pullEventRaw_Backup is set to the first return value of an os.pullEventRaw call, there's no way it'll be a function you can call later in _G.os.pullEventRaw
  • _init_ is global
  • co_func is local and thus the coroutine created from func can never be referenced again, although you try to reference it in _G.os.pullEventRaw
  • func is global
  • received is local to the for loop inside func and thus func always returns nothing
Also, I have no idea what the table interface and the variable _PM_RUN_STATUS do. The way this code is now, I can't figure out what you're trying to achieve and can't really help.
Edited on 31 December 2015 - 08:09 PM
Creator #3
Posted 31 December 2015 - 09:32 PM
Why do you even overwrite os.pullEvent? You can just have a function main with the while true do loop.
cmdpwnd #4
Posted 01 January 2016 - 05:59 AM
I'm assuming that you know how to handle coroutines. Here are some things that make this piece of code weird and non-working:
  • _G.pullEventRaw_Backup is set to the first return value of an os.pullEventRaw call, there's no way it'll be a function you can call later in _G.os.pullEventRaw
  • _init_ is global
  • co_func is local and thus the coroutine created from func can never be referenced again, although you try to reference it in _G.os.pullEventRaw
  • func is global
  • received is local to the for loop inside func and thus func always returns nothing
Also, I have no idea what the table interface and the variable _PM_RUN_STATUS do. The way this code is now, I can't figure out what you're trying to achieve and can't really help.

Fixed.

_init_() is just a one time run function when the file itself is called on. PM_RUN_STATUS just lets that function know whether it should run or not.
tInterfaceWrap is a table that contains wrapped peripherals in the form: tInterfaceWrap[side] = peripheral.wrap(side) where side resolves to a string such as "top","bottom" etc.
cmdpwnd #5
Posted 01 January 2016 - 06:24 AM
Ok I fixed it. Thanks for the pointers, I was in such a hurry that I quickly threw it up here for some advice. I'll put the answer in a spoiler in the OP at the bottom.