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

[Advanced] Musings about hidden APIs and events

Started by kazagistar, 01 May 2012 - 08:47 AM
kazagistar #1
Posted 01 May 2012 - 10:47 AM
I was working on designing an OS, one that would not be multithreaded, but would allow paralell processes by using a central event dispatch to decide what proccess to continue running. The idea is to enable, say, someone to be typing on the terminal, communication about stuff over the rednet, and to be moving as a turtle at the exact same time, all from the same proccess. Why? It's fun. :)/>/>

So, I noticed that the computercraft api has a number of undocumented events. In addition to the standard "key", "rednet_message", "terminate", "timer", and "alarm", there are a number of undocumented events used in the built in apis. Also, there are just a number of plain undocumented apis whose existance is simply mentioned.

One is the "turtle_response" event. It seems like the native turtles libraries are wrapped away. The original libraries do not wait for a turtle to arrive, they just contine proccessing. The wrapper then just waits for a "turtle_response" and returns if it managed to move, but this could be bypassed in theory if the native apis could be accessed somehow, and the turtle could do stuff while it moves and mines and such, and then move again once it gets a "turtle_response". Unfortunately, my Lua-fu is too limited to know how to extract a wrapped function via introspection or something.

The rednet api is completely filled with this sort of thing, to the point where it is hard to understand. Apparently, periphrials send events "periphrial" and "periphrial_detach", and the whole of rednet is just a hacked-on implementation on top of the rs library, using "redstone", "close", and "send" events, and the whole "rednet_message" is implemented on top.

… and of course, I have no idea how much else exists that is like this in other areas that we don't have access too, or that I didn't see.

I am just wondering how much of this "hidden API" stuff can be used. Off the top of my head, it might be possible to reimplement a "cracked" rednet api that reads all messages, even ones not sent to it. Any ideas?
Xtansia #2
Posted 01 May 2012 - 11:07 AM
The reason you can't access most of the apis like turtle/rs and such is because they are implemented in java not lua.
Cloudy #3
Posted 01 May 2012 - 11:58 AM
The way rednet works for cables, is it runs the rednet process in another coroutine. It then monitors a bundled cable. rednet wireless though is handled java side.

Also, the reasons that turtles work the way they do, is because each computer runs in a coroutine - if the functions waited, it would wait for every single computer - not just the turtle.
Hawk777 #4
Posted 05 May 2012 - 08:34 AM
You don't need any fancy introspection or anything to access the native Turtle API. Just take a look at the source code in mods/ComputerCraft/rom/apis/turtle/turtle: it exposes the original native API as a variable called "turtle.native"! What's more, it appears that every single function in the ordinary Turtle API actually just calls a function in the native API with identical name and parameters that returns an activity ID, then waits for the "turtle_response" message with matching ID. Untested, but you ought to just be able to do something like "activityID = turtle.native.forward()" to have the turtle move asynchronously, then later in your main message handling loop when you see a message of type "turtle_response" with first parameter equal to activityID, you know the forward motion has finished and the second event parameter indicates success/failure.
Lyqyd #5
Posted 06 May 2012 - 06:22 AM
The goals you mention in your first post (well, not the during turtle movment part) are possible; I'm almost done writing up a set of APIs and programs that do just that. Unfortunately, mine hooks in via a slight modification to read(), so it isn't deployable anywhere one doesn't have access to the ROM files for the system. I haven't quite found a way to effectively override read() globally on a given computer yet.