Jump to content




Redirect API?


51 replies to this topic

#21 nutcase84

  • Members
  • 711 posts
  • LocationIn My Lonely Little Computer Corner

Posted 12 February 2014 - 08:16 AM

 Bomb Bloke, on 11 February 2014 - 08:12 PM, said:

That doesn't really answer his question...

There are two ways to go about this. One is to have your API ignore any attempts by the running script to write stuff to the first line of the screen. This is fairly simple, but has a big downside in that you simply won't see the first line such scripts attempt to write - it'll be cropped out of view.

The other is to have your API capture ANY writes by the running script, and move them down one line. The upside is that the first line no longer gets cropped out of view - it's now just a line lower. The downside is that you've gotta rewrite most of the term API to pull it off. Optionally, you'd also report a smaller screen size to the scripts you run - this will allow those scripts which feel they "need" the whole display to throw up appropriate errors.

He's wondering which of these approaches you intend to take with your API.

Either way, you'll need to capture mouse input as well... The first method requires you to prevent the running script from "seeing" clicks in the top row. The second method requires you to report all clicks (except those to the top row) as being a line lower then they were actually located at. Either way you need to handle clicks to the menu bar itself.

While you could just rig co-routines to re-draw the menu bar whenever there's a chance, you never know how often you'll get those chances, let alone whether or not it needs to be redrawn. Expect a lot of flickering and an overall performance hit if you take that approach.

Yes, I need to move them down one line, not crop them. I couldn't figure out how to do it. All the text and stuff was messed up... so I was wondering if someone already made a API for this.

Edited by nutcase84, 12 February 2014 - 08:19 AM.


#22 CometWolf

  • Members
  • 1,283 posts

Posted 12 February 2014 - 10:51 AM

A really quick and easy way of doing this would be overwriting the setCursorPos, getSize and pullEvent functions. This does however mean that you'll have to use the backup methods.
_G.backup = {
  term = {
	setCursorPos = _G.term.setCursorPos,
	getSize = _G.term.getSize
  }
  os = {
	pullEventRaw = _G.os.pullEventRaw
  }
}
_G.term.setCursorPos = function(x,y)
  _G.backup.term.setCursorPos(x,math.max(2,y))
end
_G.term.getSize = function()
  local x,y = _G.term.getSize()
  return x,y-1
end
_G.os.pullEventRaw = function(filter)
	local tEvent = {_G.backup.os.pullEvent(filter)}
	if tEvent[1] == "mouse_click" then
	  tEvent[3] = tEvent[3]-1
	  --menu code here
	end
  return unpack(tEvent)
end

The API way of doing this would require a bit more work. So here's a quick attempt i just threw together.
http://pastebin.com/FH3eB0t8
os.loadAPI"redir"
--this is how to use it on a running program
redir.redirect(getfenv())
-- this is how to use it on a program you wish to run
local prog = loadfile"/progPath"
redir.redirect(prog)
prog()
Do note however that both of these approaches require you to program your menu into them, because of how pullEvent works.

Edited by CometWolf, 12 February 2014 - 10:53 AM.


#23 nutcase84

  • Members
  • 711 posts
  • LocationIn My Lonely Little Computer Corner

Posted 12 February 2014 - 11:29 AM

 CometWolf, on 12 February 2014 - 10:51 AM, said:

A really quick and easy way of doing this would be overwriting the setCursorPos, getSize and pullEvent functions. This does however mean that you'll have to use the backup methods.
_G.backup = {
  term = {
	setCursorPos = _G.term.setCursorPos,
	getSize = _G.term.getSize
  }
  os = {
	pullEventRaw = _G.os.pullEventRaw
  }
}
_G.term.setCursorPos = function(x,y)
  _G.backup.term.setCursorPos(x,math.max(2,y))
end
_G.term.getSize = function()
  local x,y = _G.term.getSize()
  return x,y-1
end
_G.os.pullEventRaw = function(filter)
	local tEvent = {_G.backup.os.pullEvent(filter)}
	if tEvent[1] == "mouse_click" then
	  tEvent[3] = tEvent[3]-1
	  --menu code here
	end
  return unpack(tEvent)
end

The API way of doing this would require a bit more work. So here's a quick attempt i just threw together.
http://pastebin.com/FH3eB0t8
os.loadAPI"redir"
--this is how to use it on a running program
redir.redirect(getfenv())
-- this is how to use it on a program you wish to run
local prog = loadfile"/progPath"
redir.redirect(prog)
prog()
Do note however that both of these approaches require you to program your menu into them, because of how pullEvent works.

Can you launch a program from the menu code inside of the API?

#24 CometWolf

  • Members
  • 1,283 posts

Posted 12 February 2014 - 12:09 PM

Yeah i don't see why not. However, the original program wouldn't be closed and the menu would stop working, unless you use the API on the program that's loaded aswell. If you do that however, you'd end up with some slight recursion. I just realized there is a simple way to keep the menu code seperate from the API, using parallel.
os.loadAPI"redir"
local program = loadFile"/programPath"
redir.redirect(program)
parallel.waitForAny(program,
  function()
	--menu code here
  end
)

Edited by CometWolf, 12 February 2014 - 12:17 PM.


#25 nutcase84

  • Members
  • 711 posts
  • LocationIn My Lonely Little Computer Corner

Posted 12 February 2014 - 01:46 PM

 CometWolf, on 12 February 2014 - 12:09 PM, said:

Yeah i don't see why not. However, the original program wouldn't be closed and the menu would stop working, unless you use the API on the program that's loaded aswell. If you do that however, you'd end up with some slight recursion. I just realized there is a simple way to keep the menu code seperate from the API, using parallel.
os.loadAPI"redir"
local program = loadFile"/programPath"
redir.redirect(program)
parallel.waitForAny(program,
  function()
	--menu code here
  end
)

Thanks, I'll try that, it looks much more simple and understandable. XD

EDIT: Would I have to program my menu with something like term.setCursorPos(0) so I can put it on the top of the screen? Or will it work without anything fancy like that?

Edited by nutcase84, 12 February 2014 - 01:47 PM.


#26 CometWolf

  • Members
  • 1,283 posts

Posted 12 February 2014 - 02:07 PM

I haven't exactly tested this API extensivly, but you should be able to use any non-redirected program as normal.

#27 nutcase84

  • Members
  • 711 posts
  • LocationIn My Lonely Little Computer Corner

Posted 12 February 2014 - 04:48 PM

Spoiler

redir:69: Unable to redirect to a nil

Edited by nutcase84, 12 February 2014 - 04:48 PM.


#28 CometWolf

  • Members
  • 1,283 posts

Posted 12 February 2014 - 05:11 PM

i did a few changes to the redir api, so you can just pass it the program path directly now, however chances are you're getting the error because the file "paint test" dosen't exist". Keep in mind that this API requies absolute paths, and you can't pass an argument to the program either, as it does not use shell.run.

Edited by CometWolf, 12 February 2014 - 05:13 PM.


#29 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 12 February 2014 - 06:20 PM

 CometWolf, on 12 February 2014 - 10:51 AM, said:

A really quick and easy way of doing this would be overwriting the setCursorPos, getSize and pullEvent functions. This does however mean that you'll have to use the backup methods.
you don't have to... as I've said already, you can make a term object and just redirect to it, its what I do in CCTicTacToe to easier support normal and advanced computers.

 CometWolf, on 12 February 2014 - 05:11 PM, said:

and you can't pass an argument to the program either, as it does not use shell.run.
Well could can with one change, you realise that shell.run uses os.run, and os.run uses loadfile and then invokes the function... look at the code below to see how you can do it in yours

os.run

Edited by theoriginalbit, 12 February 2014 - 06:20 PM.


#30 Symmetryc

  • Members
  • 434 posts

Posted 12 February 2014 - 08:01 PM

Woops, didn't click the link above...
Spoiler

Edited by Symmetryc, 13 February 2014 - 05:57 AM.


#31 CometWolf

  • Members
  • 1,283 posts

Posted 13 February 2014 - 12:14 AM

 Symmetryc, on 12 February 2014 - 08:01 PM, said:

So that you don't have to mess around with restoring or possible screw ups or anything since it's all controlled.
This is exactly what the api i threw together does >.>

 theoriginalbit, on 12 February 2014 - 06:20 PM, said:

you don't have to... as I've said already, you can make a term object and just redirect to it, its what I do in CCTicTacToe to easier support normal and advanced computers.
You're missing the point, that was a suggestion for a quick and easy way to do it. Also as far as i know, that wouldn't allow us to change os.pullEvent.

Edited by CometWolf, 13 February 2014 - 12:15 AM.


#32 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 13 February 2014 - 12:32 AM

 CometWolf, on 13 February 2014 - 12:14 AM, said:

You're missing the point, that was a suggestion for a quick and easy way to do it. Also as far as i know, that wouldn't allow us to change os.pullEvent.
the better one to change isn't even os.pullEvent, you're better adding the override to coroutine.yield

#33 CometWolf

  • Members
  • 1,283 posts

Posted 13 February 2014 - 02:29 AM

Oh herp derp, it's an suggestion so he would understand a simple way of doing it. pullEvent made more sense, since basically no one use coroutine yield instead of pullEvent, or even know they are the same. And there's no way you could override yield with redirect...

Edited by CometWolf, 13 February 2014 - 02:42 AM.


#34 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 13 February 2014 - 03:20 AM

and what about the people that use os.pullEventRaw or coroutine.yield in their programs, you're better to override the top-level function that way it effects all the lower ones too!

#35 CometWolf

  • Members
  • 1,283 posts

Posted 13 February 2014 - 04:08 AM

I used raw in my example. Which should be plenty sufficent, because like i said, barely anyone use yield in favor of pullEvent.

#36 Symmetryc

  • Members
  • 434 posts

Posted 13 February 2014 - 05:56 AM

 CometWolf, on 13 February 2014 - 12:14 AM, said:

 Symmetryc, on 12 February 2014 - 08:01 PM, said:

So that you don't have to mess around with restoring or possible screw ups or anything since it's all controlled.
This is exactly what the api i threw together does >.>
>.< Didn't see that sorry. Although you should really localize your variables.

#37 CometWolf

  • Members
  • 1,283 posts

Posted 13 February 2014 - 06:05 AM

It's just one variable, the redirect table. I wasn't sure if indexing to a local table from a program that dosen't have acess to it would work or not.

#38 nutcase84

  • Members
  • 711 posts
  • LocationIn My Lonely Little Computer Corner

Posted 13 February 2014 - 08:44 AM

Yay! It works! Kinda anyway!
Posted Image
Now I need to get to work making it work better! Thanks guys!

EDIT: I tried to run Firewolf, and this error message pops up.
Posted Image
:(

Edited by nutcase84, 13 February 2014 - 08:51 AM.


#39 CometWolf

  • Members
  • 1,283 posts

Posted 13 February 2014 - 09:22 AM

Ah yes...
local firewolfLocation = "/" .. shell.getRunningProgram()
Any program loaded with dofile/loadfile won't have acess to the shell table, and thus not it's functions either. Lemme get right on that, since i've been meaning to implement the argument possibility bit suggested i'll just do them both. Wasn't planning on putting much work into this, but it seems it could come in handy, so i might aswell.

Edited by CometWolf, 13 February 2014 - 09:23 AM.


#40 nutcase84

  • Members
  • 711 posts
  • LocationIn My Lonely Little Computer Corner

Posted 13 February 2014 - 10:47 AM

 CometWolf, on 13 February 2014 - 09:22 AM, said:

Ah yes...
local firewolfLocation = "/" .. shell.getRunningProgram()
Any program loaded with dofile/loadfile won't have acess to the shell table, and thus not it's functions either. Lemme get right on that, since i've been meaning to implement the argument possibility bit suggested i'll just do them both. Wasn't planning on putting much work into this, but it seems it could come in handy, so i might aswell.

Thanks! You are going into the credits of my OS my friend!

EDIT: 700th post? Yay!

Edited by nutcase84, 13 February 2014 - 11:12 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users