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

Computercraft to Opencomputers Compatibility

Started by Terra1, 18 April 2015 - 08:33 PM
Terra1 #1
Posted 18 April 2015 - 10:33 PM
how do i make a program (such as a game) cross compatible between computercraft and opencomputers???
Creator #2
Posted 18 April 2015 - 10:40 PM
CC uses lua 5.1 while OC uses lua 5.2. The difference is that 5.2 does not have setfenv and getfenv, shich is sad :(/>. Also, they use different apis for everything. CC uses term and i guess OC uses something else. I do not think it is possible.
Lignum #3
Posted 18 April 2015 - 10:59 PM
CC uses lua 5.1 while OC uses lua 5.2. The difference is that 5.2 does not have setfenv and getfenv, shich is sad :(/>. Also, they use different apis for everything. CC uses term and i guess OC uses something else. I do not think it is possible.

Of course it's possible. You'd need to write an abstraction layer, which should actually be quite easy. It just takes a lot of effort.
Creator #4
Posted 18 April 2015 - 11:09 PM
I wanted to write that, but I did not really feel like doing it ;)/> The abstraction layer would look like this


if cc then setfenv(loadstring(yourProg),ccEnv)
esle load(yourprog, some args I don't know, OC env)
This is pseudocode. Reproducing it may be dangerous
Lupus590 #5
Posted 18 April 2015 - 11:23 PM
Abstraction layer is the best way to go.

This is the same way games allow you to use OpenGl or DirectX, the game developers have their own graphics API which abstracts the actual graphics API calls.

In terms of OC and CC, your abstraction layer would test for which system you are running on
this is done the same way we check for API's in CC
if CC then
  --#link layer calls to CC
  func = CC.func
else
  --#link layer calls to OC
  OC.func
end
Edited on 18 April 2015 - 09:25 PM
Lignum #6
Posted 19 April 2015 - 12:03 AM
I'd go for something like this:

local TerminalCCImpl = {}
local TerminalOCImpl = {}

function TerminalCCImpl.write(text)
   term.write(text)
end

function TerminalOCImpl.write(text)
   --# dunno, never used oc
end

Terminal = CC and TerminalCCImpl or TerminalOCImpl

--# Repeat for all the other functions...

--# This allows code like this to be written:
Terminal.write("Hello World!")
Creator #7
Posted 19 April 2015 - 12:11 AM
That's the general idea, but instead of doing the functions separately, you canput them in a table and do this

term = cc and ccterm or octerm

where ccterm is the table that holds the cc functions
Lignum #8
Posted 19 April 2015 - 12:14 AM
That's the general idea, but instead of doing the functions separately, you canput them in a table and do this

term = cc and ccterm or octerm

where ccterm is the table that holds the cc functions

That's what my code is doing…
Lupus590 #9
Posted 19 April 2015 - 12:18 AM
I think we have established that a full compatibility layer would be a large project

Also, due to the differences in lua version, there almost certainly will be some compatibility problems in some areas.
AgentE382 #10
Posted 19 April 2015 - 12:21 AM
I wrote a Lua 5.1 compatibility layer for Lua 5.2 (as much as can be done). There are some issues (which is why I haven't released it yet… I'm still testing it…), but I could clean it up a little and PM it to you if you want. That'd let you mostly write your code in Lua 5.1

But, I have no CC compatibility layer for OC. I started working on it, but ran out of free time.
Lignum #11
Posted 19 April 2015 - 12:26 AM
CC1.74pr20 already has a Lua 5.2 compatibility layer in-built. And since CC will be upgraded to Lua 5.2 soon anyway, Lua version differences shouldn't be a problem.
AgentE382 #12
Posted 19 April 2015 - 05:52 PM
Whuuuutttt????? No way… Super sweet :-D

I'm gonna have to check that out!
Terra1 #13
Posted 21 April 2015 - 04:20 AM
Another related question
Is it possible to make OC network cards compatible with CC wireless modems or even moarperipherals bitnet towers???
flaghacker #14
Posted 21 April 2015 - 06:03 AM
Another related question
Is it possible to make OC network cards compatible with CC wireless modems or even moarperipherals bitnet towers???

I don't know how OC's network cards work, but if you can use tgem to send a string to another computer and there is an event system, then you can write a compatebility layer.
Lupus590 #15
Posted 21 April 2015 - 07:55 AM
I believe OC has CC rednet compatibility built in.

check the trailer video

Edit: trailer says yes

Also wiki says:
"Switch and Access Point blocks act as CC peripherals that emulate the CC modem API."
http://ocdoc.cil.li/crossmod_interoperation
Edited on 21 April 2015 - 06:02 AM
Engineer #16
Posted 21 April 2015 - 10:37 AM
If you really are excited to write an abstraction layer, you might as well do it for all the components of OC and CC. I know this is not an easy task, but it is worth it in the end so that both CC and OC can form a community where programs and games get shared among each other. Sure, it is a really long shot, but this is actually something that is worth calling an OS, since the operating system can manage the "hardware" of both devices which should be the main goal of an OS.

I know that this answer isn't helpful as I am just saying to write an abstraction layer, but I have yet to see a good abstraction layer in this topic. Since I am real Java lunatic lately I dont find Lua efficient enough to do it, because it does not provide some sort of interface by default which makes sure you can call the correct method on both abstraction layers.

However, a first start is that you need to know on which platform you are so you can return the correct implementation for that platform, and frankly I dont know how to do it since I dont know OC at all. You could check for the global API's and try to determine whether it is CC or OC.
Both implementations of the abstraction should contain both the same functions, nothing more or nothing less. Otherwise the abstraction is useless!

Well, after me ranting a bit… Good luck! :P/>
Lignum #17
Posted 21 April 2015 - 05:58 PM
However, a first start is that you need to know on which platform you are so you can return the correct implementation for that platform, and frankly I dont know how to do it since I dont know OC at all. You could check for the global API's and try to determine whether it is CC or OC.

Regarding that, CC1.74pr20 defines the global _CC_VERSION.
So you could do…

if __CC_VERSION then
   --# cc
else
   --# opencomps
end

While this is a very reliable method, it won't work with versions prior to CC1.74pr20, of course.