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

LuahhhJ

Started by Lignum, 19 May 2014 - 05:44 PM
Lignum #1
Posted 19 May 2014 - 07:44 PM
Currently, I'm working on a ComputerCraft emulator which uses LuaJ which has been driving me insane lately. So I decided to ask for help here.

I'm trying to reproduce os.loadAPI's functionality but I've had more trouble than I should have with it.
After hours I've come to the conclusion that this piece of code makes the most sense:

public void prepareAPI(IAPI api) {
	   String apiFile = Storage.getComputerCraftDir() + File.separator + "rom" + File.separator + "apis" + File.separator + api.getName();
	   System.out.println("Loading API " + apiFile);

	   LuaTable apiTable = LuaTable.tableOf();
	   LuaValue dofileEnv = m_dofile.getfenv();
	   m_dofile.setfenv(apiTable);
	   m_dofile.call(LuaString.valueOf(apiFile));
	   m_dofile.setfenv(dofileEnv);

	   m_globals.set(api.getName(), apiTable);
}
However, this seems to store the API functions inside the global table instead of the API table as I have specified with setfenv.
Which means that term.setBackgroundColour(colours.orange) would be setBackgroundColour(orange) which is not at all what I intended.

I would gladly appreciate any help with this.
apemanzilla #2
Posted 19 May 2014 - 07:46 PM
os.loadAPI is defined in Lua in the bios.
Lignum #3
Posted 19 May 2014 - 07:50 PM
os.loadAPI is defined in Lua in the bios.
It is? That just saved me a lot of work. Thank you!
apemanzilla #4
Posted 19 May 2014 - 08:13 PM
os.loadAPI is defined in Lua in the bios.
It is? That just saved me a lot of work. Thank you!
You're welcome :3
Check the BIOS and APIs before defining them in Java, it'll save you a lot of work.
Lignum #5
Posted 19 May 2014 - 08:21 PM
You're welcome :3
Check the BIOS and APIs before defining them in Java, it'll save you a lot of work.
Yes, I thought I had to register the Java side and then load the Lua side, without knowing the BIOS did it for me. Anyway, I'll be sure to search the existing files for such things in the future.
GravityScore #6
Posted 20 May 2014 - 07:41 AM
If you're using Java, you don't need to recreate CC from scratch… Just import the computercraft.jar and let it do all the work. You only have to retrieve what's on the computer and display it, and also pass it mouse clicks and keyboard presses. Much easier than re-writing the whole mod.
Lignum #7
Posted 20 May 2014 - 01:34 PM
If you're using Java, you don't need to recreate CC from scratch… Just import the computercraft.jar and let it do all the work. You only have to retrieve what's on the computer and display it, and also pass it mouse clicks and keyboard presses. Much easier than re-writing the whole mod.
I'll give it a try. Thanks for the advice!
EDIT: Worked like a charm!
Edited on 20 May 2014 - 06:59 PM