As seasoned web developer I understand that javascript code cannot be secure, but only minified/obfuscated.
However, many game engines are confident enough to allow their engine code and the customer's game code to be downloaded and visible in the client/browser.
-
Attempting to understand how Unity3D does it, I came across the following breakdown of the files that are downloaded by the client/browser:
A MyProject.asm.framework.unityweb file containing the asm.js runtime and JavaScript plugins.`
A MyProject.asm.code.unityweb file containing the asm.js module for your player.
A MyProject.asm.memory.unityweb file containing a binary image to initialize the heap memory for your player.
A MyProject.data.unityweb file containing the Asset data and Scenes.
Source: https://docs.unity3d.com/Manual/webgl-building.html
-
Then I saw they are using emscripten to compile their C/C++ code to Javascript:
To run in WebGL, all code needs to be JavaScript. We use the emscripten compiler toolchain to cross-compile the Unity runtime code (written in C and C++) into asm.js JavaScript. asm.js is a very optimizable subset of JavaScript which allows JavaScript engines to AOT-compile asm.js code into very efficient native code.
Source: https://docs.unity3d.com/Manual/webgl-gettingstarted.html
-
Questions:
- Is it Emscripten and/or asm.js that is securing the Javascript code somehow? How?
- Is it even secure? If I try really hard I could "steal" something from this code and write a hack/cheat for a game: https://files.unity3d.com/jonas/AngryBots/Release/AngryBots.js
- If I've already written a javascript game, what can I do now to do what Emscripten does for Unity3D games? (assuming #2 is true)