Cocos2d-x using Luajit to compile LUA scripts into bytecode, enabling encryption

Source: Internet
Author: User

The project requires LUA encryption scripts, information about the investigation, learning that LUA can use LUAC to compile bytecode scripts (bytecode) to enable encryption, and I tried. is indeed feasible.

The following is a native LUA compiled bytecode interpreter:

1. Create a file named 1.lua with only one word print ("Hello Lua"), creating a new empty Out.lua script file

2. Start-Execution--cmd

3, Luac-o Out.lua 1.lua

Note: luac -o [script name], with script path if necessary, such as:


After you enter, you can see the compiled bytecode again by opening Out.lua.


Then experiment and run this bytecode script to see that the LUA native interpreter can parse the LUAC compiled bytecode script directly. Very convenient!



Focus:

Having done the above series, I compiled the script in the project in this way. Then use in the COCOS2DX environment, found no. So I looked up the information again. Found that 2DX is using Luajit,lua native compiled bytecode and Luajit are incompatible. So the bytecode script compiled from the above method cannot be used in 2DX.

Solving the problem is actually very easy, is to compile the Lua script with the 2DX Luajit, the following is the Luajit compiled bytecode method:

1, there is a msvcbuild.bat batch file under the Cocos2d-x-2.2.3\scripting\lua\luajit\luajit-2.0.1\src folder. It is necessary to compile the Luajit.exe this thing first.

2. Open the command-line tool for Visual Studio. This only has to be installed vs will have, in the installation folder can be found.

3. Use the VS command-line tool CD to Luajit's src folder

4. Run the Msvcbuild.bat batch file and compile the Luajit.exe


After compiling, a series of files are generated under the SRC folder. There is a Luajit.exe


You can then use luajit.exe to compile the bytecode of the Lua script: luajit-b [script name] [compiled script name]. After running, a Jit.lua file that has been compiled into bytecode is generated under the SRC folder



The following will be compiled after the Jit.lua in 2DX test, based on Helloluaproject, put Jit.lua under the \samples\lua\hellolua\resources. Change the LUA call in AppDelegate.cpp to std::string path = Ccfileutils::sharedfileutils ()->fullpathforfilename ("Jit.lua");

The result of the execution is:


At this point, Luajit compiled bytecode encryption is complete!

Serious note: In the example, I put the script name before and after the compilation is not the same, is to let everyone see business alienation. When actually used in the project, it is best to compile the name of the script before and after compiling it, or the script may fail when it require each other.


One switch foot is too troublesome. Share a bat batch to batch convert all LUA files in a directory (only LUA files can be included in this directory)
@echo off
If exist out rd/s/q out
mkdir out
: input
Cls
Set input=:
set/p input= drag into the LUA directory you want to compile:
Set "Input=%input:" =% "
If "%input%" = = ":" Goto input
If not exist "%input%" goto input
For%%i in ("%input%") do if/i "%%~di" ==%%i goto input
pushd%cd%
cd/d "%input%" >nul 2>nul | | Exit
Set cur_dir=%cd%
popd
set/a num = 0
for/f "delims="%%i in (' dir/b/a-d/S "%input%" ') do (set/a num + = 1 & luajit-b%%~fsi Out/%%~nxi & Echo%%~n Xi
Echo Compilation script number:%num%
ATTRIB Out/*.* +r
Pause

---Copy the above code into the text with the suffix changed to bat and placed under COCOS2D-X-2.2.3\SCRIPTING\LUA\LUAJIT\LUAJIT-2.0.1\SRC


after compilation , all Lua scripts in the folder will be compiled into bytecode in bulk. and save it under the Luajit-2.0.1\src\out folder!


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Cocos2d-x using Luajit to compile LUA scripts into bytecode, enabling encryption

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.