[cocos2dx_Lua]在发布时加密lua源文件

发表于2018-05-10
评论0 1.7k浏览
为了防止自己的源码会被人剽窃,很多开发者在开发过程中会对一些关键部分进行加密处理,本篇文章要和大家介绍的就是在使用cocos2dx引擎做开发在发布时加密lua源文件的方法。

加密shell脚本
#!/bin/bash  
CURRENT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"  
CMP_PATH=$CURRENT_PATH/quick/bin  
SRC_PATH=$CURRENT_PATH/../src  
OUT_PATH=$CURRENT_PATH/../res/sc  
MYSIG="BJGAME"  
MYKEY="W@Z#X^^"  
cd $CMP_PATH  
# ./compile_scripts.sh  -i $SRC_PATH -x main,config,cocos,framework -o $OUT_PATH/game.tg -e xxtea_zip -es $MYSIG -ek $MYKEY  
#android的可能有问题  
./compile_scripts.sh  -m files -i $SRC_PATH -x main,config,cocos,framework -o $OUT_PATH/ -e xxtea_chunk -es $MYSIG -ek $MYKEY  

参数详解

compile_scripts.sh和compile_scripts.php在quick-cocos2dx引擎安装目录下
$options = array(  
    array('h',   'help',       0,      false,       'show help'),  
    array('i',   'src',        1,      null,        'source files directory'),  
    array('o',   'output',     1,      null,        'output filename | output directory'),  
    array('p',   'prefix',     1,      '',          'package prefix name'),  
    array('x',   'excludes',   1,      null,        'excluded packages'),  
    array('m',   'compile',    1,      'zip',       'compile mode'),  
    array('e',   'encrypt',    1,      null,        'encrypt mode'),  
    array('ek',  'key',        1,      null,        'encrypt key'),  
    array('es',  'sign',       1,      null,        'encrypt sign'),  
    array('ex',  'extname',    1,      'lua',       'encrypted file extension name (default is "lua"), only valid for xxtea_chunk'),  
    array('c',   'config',     1,      null,        'load options from config file'),  
    array('q',   'quiet',      0,      false,       'quiet'),  
    array('jit', 'jit',        0,      false,       'using luajit compile framework'),  
);  
    -h 帮助  
    -i 源码目录,input  
    -o 输出文件或目录,output  
    -p 包前缀  
    -x 不包括文件夹或文件,如果你有些源文件不想被编译进去的话,将会用到这个参数  
    -m 编译模式:zip(zip压缩格式)、files(离散文件)  
    -e 加密模式:xxtea_zip(对zip包进行 xxtea 加密)、xxtea_chunk(对.luac 进行加密)  
    -ek 加密密钥  
    -es 加密签名  
    -ex 编译后的文件的后缀名  
    -c 从一个文件加载参数列表  
    -q 生成过程不输出信息  
compile mode:  
    -m zip (default)    package all scripts bytecodes to a ZIP archive file.  
    -m c                package all scripts bytecodes to a C source file.  
    -m files            save bytecodes to separate files. -o specifies output dir.  
encrypt mode:  
    -e xxtea_zip        encrypt ZIP archive file with XXTEA algorithm,  
    -e xxtea_chunk      encrypt every bytecodes chunk with XXTEA algorithm.  
                        * default encrypt sign is "XXTEA"  
                        * output file extension name is "bytes"  

修改AppDelegate.cpp
bool AppDelegate::applicationDidFinishLaunching()  
{  
    ...      
    //protofuf lua  
    luaopen_protobuf_c(L);  
    LuaStack* stack = engine->getLuaStack();  
    stack->setXXTEAKeyAndSign("W@Z#X^^", strlen("W@Z#X^^"), "BJGAME", strlen("BJGAME"));  
    string path = FileUtils::getInstance()->fullPathForFilename("src/main.lua");  
    CCLOG("path ==%s",path.c_str());  
    size_t pos;  
    while ((pos = path.find_first_of("\\")) != std::string::npos)  
    {  
        path.replace(pos, 1, "/");  
    }  
    size_t p = path.find_last_of("/\\");  
    if (p != path.npos)  
    {  
        const string dir = path.substr(0, p);  
        stack->addSearchPath(dir.c_str());  
        p = dir.find_last_of("/\\");  
        if (p != dir.npos)  
        {  
            stack->addSearchPath(dir.substr(0, p).c_str());  
        }  
    }  
    string env = "__LUA_STARTUP_FILE__=\"";  
    env.append(path);  
    env.append("\"");  
    stack->executeString(env.c_str());  
    CCLOG("------------------------------------------------");  
    CCLOG("LOAD LUA FILE: %s", path.c_str());  
    CCLOG("------------------------------------------------");  
//#ifdef DEBUG  
//      
    engine->executeScriptFile(path.c_str());  
//  
//#else  
//    //    stack->executeScriptFile(path.c_str());  
//    stack->loadChunksFromZIP("res/sc/game.tg");  
//    string script_main = "";  
//    script_main += "require(\"main\")";  
//    //    CCLOG("Boot script: \n%s", script_main.c_str());  
//    engine->executeString(script_main.c_str());  
//  
//      
//#endif  
    return true;  
}  
最后需要注意的是:编译发布程序的时候要记得把源代码文件夹从项目中移除(不是删除,只是不包含进项目里),不然一切都白费了
来自:

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引