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

Run program using special term API

Started by HPWebcamAble, 25 October 2015 - 08:09 PM
HPWebcamAble #1
Posted 25 October 2015 - 09:09 PM
So I'm remaking this code:
http://pastebin.com/9n34bACk


Here's what I have so far:
http://pastebin.com/iqxsiBm4


In the new version, when it sets the function's environment (when creating the coroutine), it passes a modified version of the term API that saves pixels to a buffer, so that the 'pause program' can draw over the screen, then redraw the original program's interface.

In other words, when you pause the program, it needs to know what the screen looked like when it was paused. And to do that, I've modified term.write() to add its text to a buffer, then write to the screen.



That works fine, but the program I'm testing with (my File Manager) has a call to paintutils. And for some reason, paintutils still has access to the original term functions.
I don't really want to pass a completely rewritten paintutils API (but I assume that would fix the problem, with paintutils anyway. Other APIs could have the same problem too)


Any ideas?
Edited on 25 October 2015 - 08:38 PM
KingofGamesYami #2
Posted 25 October 2015 - 09:35 PM
I'm not seeing where you modify the term API. Perhaps you could post that part?
HPWebcamAble #3
Posted 25 October 2015 - 09:39 PM
I'm not seeing where you modify the term API. Perhaps you could post that part?

Right, the code I had posted is the proof of concept.
Added my current code
Edited on 25 October 2015 - 08:39 PM
KingofGamesYami #4
Posted 25 October 2015 - 11:01 PM
Well, the problem is paintutils still has a pointer to the old term table. If you overwrote the original term functions (after backing them up!!), you could add checks to see if the sandboxed coroutine is calling the function or not.

Ex:


local tArgs = {...}
local oldTerm = {}
for k, v in pairs( term ) do
  oldTerm[ k ] = v
end

local program = coroutine.create( loadfile( tArgs[ 1 ] )

--#example of term.write
term.write = function( text )
  local x,y = term.getCursorPos()
  oldTerm.write(text)
  if coroutine.status( program ) == "running" then --#hey, our coroutine is running now, we should record the screen!!!
    for i = 1, #text do
      buffer[x+i-1][y] = {textCol = term.getTextColor(), backCol = term.getBackgroundColor(), text = text:sub(i,i)}
    end
  end
end
HPWebcamAble #5
Posted 26 October 2015 - 12:18 AM
I accidentally defined 'program' after redefining 'term.write'.

Don't do that. It doesn't work.


But after correcting that, it works flawlessly, thank you :)/>
KingofGamesYami #6
Posted 26 October 2015 - 12:31 AM
You're welcome, glad to hear it works ;)/> (That was the first time I've found a use for coroutine.status equaling running :D/>)
Bomb Bloke #7
Posted 26 October 2015 - 03:33 AM
You're setting yourself up for further problems by re-writing term. You'd be better off using term.current() to get the actual display object, creating a new version of that, then term.redirect()'ing to the result.
HPWebcamAble #8
Posted 26 October 2015 - 10:49 PM
You're setting yourself up for further problems by re-writing term. You'd be better off using term.current() to get the actual display object, creating a new version of that, then term.redirect()'ing to the result.

Well, using your method wasn't hard to set up, so I suppose I'll use that.

But what kind of problems are you referring to?
Bomb Bloke #9
Posted 27 October 2015 - 01:42 AM
Let's say someone tried to used your script through any multitasking OS - eg multishell, the one running on all advanced systems. Partway through execution, they switch tabs. Suddenly you'll be recording a different script.
HPWebcamAble #10
Posted 27 October 2015 - 01:51 AM
Let's say someone tried to used your script through any multitasking OS - eg multishell, the one running on all advanced systems. Partway through execution, they switch tabs. Suddenly you'll be recording a different script.

That explains one of the issues I encountered, though I didn't realize exactly what the problem was at the time ;)/>
Edited on 27 October 2015 - 12:51 AM