How to convert "js" to "jsc" for code security --(Tutorial by YUYE)

Cocos2d-x html5/ javascript binding is a very popular engine for developer to develop game quickly and convenient.

But the published javascript game is nearly naked for everybody can see it. the good way is to do Obfuscated code.

Cocos2d-x engine already had some tools to make your javascript code safe. so today let me show you How to convert
“js” to “jsc” for code security .

Firstly , we go to see some documents for jsc security‘s introduct:
You can always find the latest command for converting “js” to “jsc” in the documents under this path “/Users/UserName/cocos2d-x-2.2.2Test/tools/cocos2d-console/console/README.md”.

As the document “README.md” said:

# Will generate bytecode files
$ cocos2d jscompile -d output_dir -s cocos2dx_root/scripting/javascript/bindings/js -s cocos2dx_root/samples/Javascript/Shared/tests -o game.min.js -j compiler_config_sample.json -c


Wow! it’s scared。
Do not worried! I attend to tell you what happened in this command step by step.

h1.Step1:
I made a simple javascript “Hello World” Game called “MyGame” in 2.2.*V

If you do not know how to create a game. please read the tutorial “How to create a”Hello World" C++ project with cocos2d-x2.2.2 or 3.0“I
http://www.cocos2d-x.org/forums/6/topics/43031
and if want to create a 2.2.*V javascript game just using

python create_project.py -project MyGame -package com.MyCompany.AwesomeGame -language javascript

If you are using stable version 3.*

python create_project.py -n GameName -k com.cocos2dx.gameName -l javascript -p ../../projects

Step2:
We can find a “.json” format file under this path: “/Users/UserName/cocos2d-x-2.2.2/tools/cocos2d-console/console/bin/compiler_config_sample.json”

“compiler_config_sample.json” can help us to add/skip any javascript’s files. in this case we just need to modify this file like:

And copy it to this path:"/Users/UserName/cocos2d-x-2.2.2/projects/MyGame/Resources"
Step3:
Under this path:

 cd /Users/UserName/cocos2d-x-2.2.2Test/tools/cocos2d-console/console

We can find the "cocos2d.py " script. Here is a tools for convert “js” to “jsc” for code security by python . .
So if you had already prepared running python environment. you can just perform it like this way:

python cocos2d.py funtionName argumentName1 …

Now you can perform this command like this :
python cocos2d.py jscompile d /Users/UserName/cocos2d-x-2.2.2/projects/MyGame/Resources/ s /Users/UserName/cocos2d-x-2.2.2/projects/MyGame/Resources/ s /Users/UserName/cocos2d-x-2.2.2/projects/MyGame/Resources/src o game.cocos2d.js j /Users/UserName/cocos2d-x-2.2.2Test/projects/MyGame/Resources/compiler_config_sample.json c
Then you can remove the all files under the path :"/Users/UserName/cocos2d-x-2.2.2/projects/MyGame/Resources/src",and run your “Hello world game”. if it performed ok. congratulation! you did a great job for your code security.
h1.Now we can see what happan what happened in below’s command step by step.
In the command "$ python cocos2d.py jscompile … "we have 6 arguments.
**(1)” d output_dir“ **
In this argument required the output direction for converted file.
i.e:

 d /Users/UserName/cocos2d-x-2.2.2/projects/MyGame/Resources/

“s cocos2dx_root/scripting/javascript/bindings/js”
In this argument required the output direction for converted file.
i.e:

s /Users/UserName/cocos2d-x-2.2.2/projects/MyGame/Resources/

If you javascript code under many folders. then you can declare like this way
i.e:

s /Users/UserName/cocos2d-x-2.2.2/projects/MyGame/Resources/~~s /Users/UserName/cocos2d-x-2.2.2/projects/MyGame/Resources/src

"~~o game.min.js”
In this argument required the converted file’s name.
“~~j compiler_config_sample.json"
In this argument required the direction of compiler_config_sample.json.
i.e:

j /Users/UserName/cocos2d-x-2.2.2Test/projects/MyGame/Resources/compiler\_config\_sample.json

*(5)”c“*
In this argument required nothing.
if you added” c" it means to compress all js files into just a big file
i.e:

j /Users/UserName/cocos2d-x-2.2.2Test/projects/MyGame/Resources/compiler\_config\_sample.json

If you don not want to compress all js files into just a big file. you can input the command like this way:

python cocos2d.py jscompile -d /Users/UserName/cocos2d-x-2.2.2/projects/MyGame/Resources/ -s /Users/UserName/cocos2d-x-2.2.2/projects/MyGame/Resources/ -s /Users/UserName/cocos2d-x-2.2.2/projects/MyGame/Resources/src 

Just ignore the j xxx o xxx c

h1. In the end:
You can using command

 python cocos2d.py jscompile —help

Usage: cocos2d.py jscompile~~s src_dir ~~d dst_dir
Options:
h, —help show this help message and exit
s SRC_DIR_ARR, —src=SRC_DIR_ARR
source directory of js files needed to be compiled,
supports mutiple source directory
d DST_DIR, —dst=DST_DIR
destination directory of js bytecode files to be
stored
c, —use_closure_compiler
Whether to use closure compiler to compress all js
files into just a big file
o COMPRESSED_FILENAME, —output_compressed_filename=COMPRESSED_FILENAME
Only available when ’c’ option was True
j COMPILER_CONFIG, —compiler_config=COMPILER_CONFIG
The configuration for closure compiler by using JSON,
please refer to compiler_config_sample.json


1.png (28.1 KB)


2.png (89.1 KB)


3.png (104.6 KB)


4.png (118.2 KB)


5.png (20.8 KB)

1 Like

Please use markdown syntax for this tutorial. Thanks.

e, I’ll fix it. I have no idear why it became like this.
James Chen wrote:

Please use markdown syntax for this tutorial. Thanks.

Because we switched our syntax system for forum to markdown

yuye liu wrote:

e, I’ll fix it. I have no idear why it became like this.
James Chen wrote:

Please use markdown syntax for this tutorial. Thanks.

ok, I got it
Huabin LING wrote:

Because we switched our syntax system for forum to markdown

yuye liu wrote:

e, I’ll fix it. I have no idear why it became like this.
James Chen wrote:

Please use markdown syntax for this tutorial. Thanks.

How to solve the problem

JS: assets/cocos2d-jsb.js:39:InternalError: bad script XDR magic number

http://www.cocoachina.com/bbs/read.php?tid=183538

But there is no “cocos2d.py” script file in cocos2d-x-3.10 so how can we convert js to jsc?

you can use cocos command - cocos jscompile, check the help

below is the code i used to comvert all js files into 1 .jsc file with some extra exclude which you might no need

cocos jscompile -s ./AllJSFilesFolder -d ./ProjectDestination/src -c -o ProjectName.js -m "--jscomp_warning internetExplorerChecks --language_in=ECMASCRIPT5"

cocos jscompile -s ./AllJSFilesFolder -d ./ProjectDestination/src -c -o ProjectName.js

Hi Zinitter,
I tried your solution it’s working for js file very well but when i am trying to convert game.min.js file into jsc then it’s giving errors.
For example:-
When i run command in cmd then it prints like
“Error running command, return code: 13.Check the log file at C:\Users\stpl/.cocos\cocos.log”

After opening cocos.log file, below error occurs.
publish\html5\game.min.js:4332: ERROR - Parse error. syntax error
d.setEnable(!1):d.setEnable(!0)});this.addChild(a,5)}}),PickDemo=GameDemo.extend({ctor:function(){this._super();var a=new cc.DrawNode,b=!1;this.headerLabel.setString(“How to Pick a Card?”);this.headerLabel.setString(“How to Pick a Card?”);this.headerLabel.x-=5;this.headerLabel.y+=5;this.video=a.playVideo(res.video.pick,cc.p(0,400scaleFactor),cc.size(844scaleFactor,500scaleFactor),cc.size(650scaleFactor,400*scaleFactor));this.video.id=“demoVideo”;this.video.load();this.video.onloadeddata=function(){b=
^
Please look into this and revert me back asap.

Are you publishing to website version? i remember last time i tried for website it doesnt work with .jsc
I using .jsc with native apps

I believe game.min.js is already the minified version and you cant use the closure compiler.

Between you can check the log file on the “C:\Users\stpl/.cocos\cocos.log” to know what errors you get and find the fix

So is there any approach to obfuscate game.min.js file? If yes, then post here.