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

前端之家收集整理的这篇文章主要介绍了[cocos2dx_Lua]在发布时加密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,framework -o $OUT_PATH/ -e xxtea_chunk -es $MYSIG -ek $MYKEY

参数详解

compile_scripts.sh和compile_scripts.PHP在quick-cocos2dx引擎安装目录下

$options = array(
    array('h','help',false,'show help'),array('i','src',1,null,'source files directory'),array('o','output','output filename | output directory'),array('p','prefix','','package prefix name'),array('x','excludes','excluded packages'),array('m','compile','zip','compile mode'),array('e','encrypt','encrypt mode'),array('ek','key','encrypt key'),array('es','sign','encrypt sign'),array('ex','extname','lua','encrypted file extension name (default is "lua"),only valid for xxtea_chunk'),array('c','config','load options from config file'),array('q','quiet','quiet'),array('jit','jit','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,"/");
    }
    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;
}
最后需要注意的是:编译发布程序的时候要记得把源代码文件夹从项目中移除(不是删除,只是不包含进项目里),不然一切都白费了
原文链接:https://www.f2er.com/cocos2dx/338417.html

猜你在找的Cocos2d-x相关文章