Jump to content




Top Level Coroutine Override


  • You cannot reply to this topic
71 replies to this topic

#1 NeverCast

  • Members
  • 400 posts
  • LocationChristchurch, New Zealand

Posted 02 November 2012 - 03:02 PM

Hey Guys,

This has to be like my first topic or something, maybe even my first post, I'm not sure.
Anyway recently I wanted to design a networking API, but the problem was I couldn't run a top level coroutine, after talking to people and disputing with them on IRC for the last few days and being told it was very difficult or impossible I devised a way to launch functions at top level, by basically messing around with the shell and bios, getting them to quit and then launch my own code.

There is a lot of possibilities with this, and the full power could probably only be realised in the hands of an Advanced/Pro Lua Coder, but here is my attempt at some community contribution. I only started learning Lua a few weeks to a month ago. So I welcome any advice or input.

I've not made any test code for this, but it should all work how I've described it. It should let you spawn whatever top level programs you want to.

The example I have loads the shell per normal, but does not start rednet for redstone input. Just to show that it works.

You're welcome to use the code how you like, but I ask that you keep my credit on it.
You should use this as your startup file, it might work not as startup file, but I never tested it that way.

http://pastebin.com/2SQAzrWi

Enjoy, and please let me know what you think.

Edit: Re-uploaded to Pastebin via my account so I can start pushing updates and fixes.

#2 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 06 November 2012 - 08:31 AM

Cool hack!

#3 Sammich Lord

    IRC Addict

  • Members
  • 1,212 posts
  • LocationThe Sammich Kingdom

Posted 06 November 2012 - 11:50 AM

View PostCloudy, on 06 November 2012 - 08:31 AM, said:

Cool hack!
Defiantly!

#4 wraithbone

  • Members
  • 17 posts
  • LocationAuckland, New Zealand

Posted 06 November 2012 - 10:17 PM

This is really great, earlier this year I was trying to run a coroutine that did things while the shell ran the front end. But kept things going in the BG.
This is pretty much what I wanted. Awesome.

#5 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 07 November 2012 - 06:21 AM

I have no idea what this does, other than what the title says. What applications could this have?

#6 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 07 November 2012 - 07:39 AM

it allows you to run other functions/programs in parallel with the shell

#7 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 08 November 2012 - 06:23 AM

Well, color me noob, but I still don't know how I can use this :P/>

#8 Leo Verto

  • Members
  • 620 posts
  • LocationOver there

Posted 08 November 2012 - 06:35 AM

Awesome, now to find a tekkit server to test it :P/>

#9 wilcomega

  • Members
  • 466 posts
  • LocationHolland

Posted 08 November 2012 - 07:10 AM

hey, this is really really cool man. how can i print my own stuff instead of changing the default messages?

#10 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 08 November 2012 - 07:32 AM

it is perfect for a rednet networking system. every single wireless turtle/PC can perform its usual functions while also forwarding messages onwards

#11 kazagistar

  • Members
  • 365 posts

Posted 08 November 2012 - 08:48 AM

Wait, why do you need to inject yourself above the shell to run programs in parallel? Can't you just run programs in parallel with a new shell within the existing one?

#12 wilcomega

  • Members
  • 466 posts
  • LocationHolland

Posted 08 November 2012 - 08:59 AM

if you use this injection you can easly replace shell functions and os functions without replacing stuff twice. and if you run a shell in a shell it takes up more memory on your real computer

#13 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 08 November 2012 - 09:02 AM

View Postwilcomega, on 08 November 2012 - 08:59 AM, said:

if you use this injection you can easly replace shell functions and os functions without replacing stuff twice. and if you run a shell in a shell it takes up more memory on your real computer
you mean the couple KB of lua bytecode and function environments? pah.

#14 kazagistar

  • Members
  • 365 posts

Posted 08 November 2012 - 09:07 AM

I mean, this seems like optimizing for a non-issue (since each shell will use up maybe a millionth of your memory), but it is still pretty neat.

#15 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 08 November 2012 - 09:12 AM

View Postkazagistar, on 08 November 2012 - 09:07 AM, said:

I mean, this seems like optimizing for a non-issue (since each shell will use up maybe a millionth of your memory), but it is still pretty neat.
i usually only optimize cpu and not memory >_>

#16 kazagistar

  • Members
  • 365 posts

Posted 08 November 2012 - 09:17 AM

View PostPixelToast, on 08 November 2012 - 09:12 AM, said:

i usually only optimize cpu and not memory >_>

I optimize CPU bound code for CPU and memory bound code for memory (which almost never happens in CC, it is all interrupt/hardware bound stuff in general) and the rest for understandability and functionality.

#17 NeverCast

  • Members
  • 400 posts
  • LocationChristchurch, New Zealand

Posted 07 January 2013 - 07:48 PM

This was actually for an Adhoc network api I was writing, and RedNet was getting in the way at the time, so I needed a way to get rid of it :P

Also I didn't want people using a shell on my Mainframe computers.

Wow it's so good to see all the great replies, Thanks guys :)

#18 bbqroast

  • Members
  • 124 posts

Posted 15 January 2013 - 08:56 AM

The amount of control we get over the upper level stuff in CC has always saddened me. I can't work it out, but does it require a change to any of the ROM files?

#19 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 15 January 2013 - 06:28 PM

man I took a look through the code and I gotta say I'm super confused, on line 36 you make the table prevState and nowhere else in the program do you add any values to it... how do you then call functions and values from that table throughout the program?? :huh:

I must admit the getParentShell is quite smart though. I have tried replacing the printError function in my programs but it refuses to work on ccEmu, not sure about real CC :(

the below code still uses the normal printError
local pE=printError
rawset(_G,printError,function(...)
print("functional")
sleep(5)
printError=pE
return printError(...)
end)
printError('me')

EDIT, derpity derp derp.... no quotes -_- works if you use
local pE=printError
local tO={}
for i=1,5 do
if rawget(getfenv(i),"printError")~=nil and not tO[getfenv(i)] then
  tO[getfenv(i)]=true
  rawset(getfenv(i),"printError",function(...)
   print("functional")
   sleep(5)
   printError=pE
   return printError(...)
  end)
end
end

you can then replace the print and sleep with whatever you need

#20 NeverCast

  • Members
  • 400 posts
  • LocationChristchurch, New Zealand

Posted 15 January 2013 - 09:12 PM

View Postbbqroast, on 15 January 2013 - 08:56 AM, said:

The amount of control we get over the upper level stuff in CC has always saddened me. I can't work it out, but does it require a change to any of the ROM files?

Nope that's the point, you don't have to change ROM files, I've recently been looking in to something more advanced than this for overriding the bios.

View PostKaoS, on 15 January 2013 - 06:28 PM, said:

man I took a look through the code and I gotta say I'm super confused, on line 36 you make the table prevState and nowhere else in the program do you add any values to it... how do you then call functions and values from that table throughout the program?? :huh:

I must admit the getParentShell is quite smart though. I have tried replacing the printError function in my programs but it refuses to work on ccEmu, not sure about real CC :(

the below code still uses the normal printError
local pE=printError
rawset(_G,printError,function(...)
print("functional")
sleep(5)
printError=pE
return printError(...)
end)
printError('me')

EDIT, derpity derp derp.... no quotes -_- works if you use
local pE=printError
local tO={}
for i=1,5 do
if rawget(getfenv(i),"printError")~=nil and not tO[getfenv(i)] then
  tO[getfenv(i)]=true
  rawset(getfenv(i),"printError",function(...)
   print("functional")
   sleep(5)
   printError=pE
   return printError(...)
  end)
end
end

you can then replace the print and sleep with whatever you need

I'm sorry you don't understand it :P
I wish I'd know about rawset back when I wrote this code, fortunately there was no metatables that are using __newindex so it didn't make much difference.
I'm glad people are still looking at this post. I look forward to it one day being useful.

I will likely use it in my operating system, just to get rid of Rednet and the Shell running in the background. Free up some stuff and make sure they don't interfere. I tend to do things outside of the book so the less other things running the better.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users