Is there a way to use C++ library in Cocos JS?

So I have a game Cocos2d-x game written in C++. The majority of the game engine is decoupled from the framework which I compile as a separate library and linked in to the cocos2d-x app. I’ve been exploring the possibility of making a web build and I couldn’t find much on this topic. What I’m thinking of doing is to rewrite all the code coupled to Cocos2d-x in Javascript, and somehow load the decoupled c++ game engine library so I wouldn’t have to rewrite the whole engine in Javascript. Is this possible or is there another way?

Just to mention I’m not willing to compile the engine to JS using something like emscripten because I don’t want to expose the engine code. Thanks!

Do you want to build native applications on each platform or allow running the game in a web browser?
Browser - you’ll have to use something like Emscripten the same way Unity/Unreal/etc all do their WebGL builds.
Native - There’s already support for writing some or all game logic in Javascript and having the core engine (plus any of your own c++ code) running as a native c++ built app.

thanks, I want to convert a C++ project to a Javascript project to be used in browser

I was hoping I didn’t have to go through emscripten because I don’t want to expose the engine code because you can’t hide javascript. Basically I built a multiplayer engine and the single player is just client server running locally. I’m fine with rewriting client in javascript but I don’t want to expose the server code just because of single player.

The only way you can really hide your server code is if you run it on a server. Which makes sense for the setup you describe. Client written in javascript (using CocosCreator or another framework) can send requests to the server (websockets, http, socket.io, etc).

I’m not sure how you handled multi-player before? Maybe you had each client also act as a peer-to-peer server? Anyway it sounds like choosing c++/javascript or which engine to use isn’t the real issue.

My server runs independently from the game as it’s own process. It’s a dedicated authoritative server, and it’s a custom protocol that can run on either UDP or TCP.

So if I wish to do a javascript port I just have to rewrite the client in javascript and have it send packets via some web protocol to my C++ server. I’d would just add some sort of translation layer on top of my C++ server so it can handle web requests.

If I were to port only the multiplayer part of the game this wouldn’t be a problem because the client is passive as server is authoritative and I don’t mind the client code being exposed. My main concern is I’m not comfortable to to port the single player over because it basically needs to run client and server locally, which means I’ll have to expose my server code as well. Which is why I was looking for ways to run some C++ code from client side Cocos JS, which doesn’t seem possible unless I go through something like emscripten.

So I don’t think I’m going to end up doing a web port just yet.

BTW thanks for responding both times I posted here you helped me out! Here are some screen caps my game work in progress if you want to take a look. They are from single player but the it’s actually running local server and host.

Ah, now I understand a bit more. Yeah it really depends on your goals, I guess.

Just so you know. When I make a game for windows and produce a Win32 binary, that is essentially just as crackable as the same game built with Emscripten. If the cracker has all the code they can always with enough time and effort bypass and protections you may try to apply (e.g. DVD’s DRM master key).

There are many games that require an internet connection to play. The latest Mario Run as well as most of the top grossing free to play mobile games. This is the only good way to robustly try to defend against piracy or stealing of “source”.

Source code is much higher level than the output of Emscripten. Emscripten is essentially byte-code, but with a bit more information. The future WebAssembly will probably be slightly more obscure as it will be closer to native machine code.

Anyway, I digress.

If you’re really worried or paranoid about it then definitely wait on porting it, or definitely consider making the single player game behave like the multiplayer game, but with AI bots or run with only a single player input, etc, because then you get to finish your game, but allow people to play in the browser.

A final thought is that you could write everything in Javascript because the programming language is irrelevant or orthogonal to your problem.

While I’d never tell anyone not to worry about their game being cloned, or the binary being disassembled in order to re-use in another game (as clone or whatever). I do argue to people to think about how much effort they’re wasting trying to prevent bad actors instead of just making the game as fun as possible for those good actors we call Players (or Users if you’re a fan of Tron :smiley: )

yeah I believe in the notion that if your game is popular enough someone will reverse engineer it. I was shocked when I discovered people wrote WoW servers by reverse engineering their protocol just because they wanted to play an earlier build. The millions of lines of code put in just to reproduce a copy of another game is quite mind boggling to me.

The problem with cocos is though it makes it too easy to copy. Say if I release on Win32 and the web they essentially have everything they need, the code the audio the sprite sheets etc. They just need to compile the cocos engine and they have essentially have my entire development base!

I dont really mind people ripping the art or the audio, and when the source is the only thing you have that you have some protection over I don’t want it just to be in the wild. In my case if my game is purely single player I don’t think I would mind writing the whole thing in javascript. But I went authoritative server route so I want to at least protect the server code in some aspect.

Sounds good. Just don’t put the cart before the horse :smiley:

FYI:
You can also look into the encryption component of cocos2d-x. I think it’s xxTea
Encrypt/decrypt int using xxtea and save the unsigned char using UserDefaults? .
How to secure data from hacking? .
Suggest good encrypting & decrypting algorithms.
How to secure data from hacking? .

And if you use TexturePacker
https://www.codeandweb.com/texturepacker/contentprotection .
https://www.codeandweb.com/texturepacker/tutorials/texturepacker-content-protection .

1 Like