Cocos2d-x uses luajit to compile Lua scripts into bytecode for encryption

Source: Internet
Author: User

The project requires that the Lua script be encrypted. After checking the relevant information, we know that Lua can use luac to compile the script into a bytecode to implement encryption. I tried it, it is feasible.
The following describes how to use the native Lua interpreter to compile bytecode:
1. Create a new file named 1. Lua with only one print ("Hello Lua") and create an empty out. Lua script file.
2. Start -- run -- cmd
3. luac-O out. Lua 1.lua
Note: luac-O [Script Name After compilation] [script name], with the script path when necessary, such:

 

Press enter and open out. Lua to see the compiled bytecode.

Run the bytecode script. You can see that Lua's native interpreter can directly parse the bytecode script compiled by luac, which is very convenient!

Important:
After completing the above series, I used this method to compile the scripts in the project and use them in the cocos2dx environment! So I checked the information and found that 2dx uses luajit. the bytecode compiled by Lua is incompatible with luajit, therefore, the bytecode script compiled according to the above method cannot be used in 2dx.
To solve this problem, it is actually very easy to compile the Lua script with the luajit of 2dx. The following describes how to compile bytecode with luajit:
1. There is a msvcbuild.batbatch file under the cocos2d-x-2.2.3 \ Scripting \ Lua \ luajit \ LuaJIT-2.0.1 \ srcdirectory, first compile the luajit.exe.
2. Open the command line tool of Visual Studio, which will be available if vs is installed. It can be found in the installation directory.
3. Use the vs command line tool CD to the src directory of luajit
4. Execute msvcbuild.batbatch file to compile luajit.exe

 

After compilation, a series of files will be generated in the srcdirectory, with a luajit.exe

You can use luajit.exe to compile the bytecode Of The Lua Script: luajit-B [Script Name] [compiled Script Name]. After execution, a JIT compiled into bytecode will be generated in the src directory. lua File

 

The compiled JIT. put Lua in the 2dx pilot test, based on the hellolua project, put JIT. put Lua under \ samples \ Lua \ hellolua \ resources and modify appdelegate. the Lua call in CPP is STD: String Path = ccfileutils: sharedfileutils ()-> fullpathforfilename ("JIT. lua ");
The running result is:

 

So far, luajit compilation bytecode encryption has been completed!

Note: In this example, the names of the scripts before and after compilation are different to show the differences, it is recommended that the script name be consistent before and after compilation. Otherwise, problems may occur when the script require each other!

 

One conversion script is too troublesome. You can share a bat batch to batch convert all the Lua files in a folder (this folder can only contain Lua files)

1 @ echo off 2 if exist out RD/S/Q out 3 mkdir out 4: Input 5 CLS 6 set input =: 7 set/p input = drag the Lua folder to be compiled: 8 set "input = % input:" = % "9 if" % input % "= ": "Goto input10 if not exist" % input % "Goto input11 for % I in (" % input % ") do if/I" % ~ Di "= % I goto input12 pushd % Cd % 13 CD/D" % input % "> NUL 2> NUL | exit14 set cur_dir = % Cd % 15 popd16 set/ a num = 017 for/F "delims =" % I in ('dir/B/a-D/S "% input % "') do (Set/a num + = 1 & luajit-B % ~ FSI out/% ~ Nxi & Echo % ~ Nxi) 18 echo compiled scripts: % num % 19 attrib out/*. * + R20 pause

 

--- Copy the above Code to the text, suffix to bat, put under the cocos2d-x-2.2.3 \ Scripting \ Lua \ luajit \ LuaJIT-2.0.1 \ SRC

After compilation, all Lua scripts in the folder will be batch compiled into bytecode and saved in the LuaJIT-2.0.1 \ SRC \ out directory!

Note: In the cocos2d-x-3.1.1 version, the path for luajit is: cocos2d-x-3.1.1 \ external \ Lua \ luajit \ SRC.

 

 

Http://www.cocoachina.com/bbs/read.php? Tid = 205802

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.