cocos2d-x 3.6 lua文件加密

前端之家收集整理的这篇文章主要介绍了cocos2d-x 3.6 lua文件加密前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

cocos2d-x 3.6 lua 文件加密

这里没有采用通用的加密方法,使用了类似于图片加密的方法,对文件中的每个字符进行加密,读取的时候,根据加密的key值进行解密。

  1. 找到引擎加载Lua文件的接口cocos2dx_lua_loader
  2. 添加解密代码
if (chunk != NULL&&chunkSize > 0)
{
    for (int i = 0; i < chunkSize; i++)
    {
        chunk[i] = chunk[i] - 99;//99是用来加密的key值
    }
}

更改后的源码

#include "Cocos2dxLuaLoader.h"
#include <string>
#include <algorithm>

#include "CCLuaStack.h"
#include "CCLuaEngine.h"

using namespace cocos2d;

extern "C"
{
    int cocos2dx_lua_loader(lua_State *L)
    {
        static const std::string BYTECODE_FILE_EXT    = ".luac";
        static const std::string NOT_BYTECODE_FILE_EXT = ".lua";

        std::string filename(luaL_checkstring(L,1));
        size_t pos = filename.rfind(BYTECODE_FILE_EXT);
        if (pos != std::string::npos)
        {
            filename = filename.substr(0,pos);
        }
        else
        {
            pos = filename.rfind(NOT_BYTECODE_FILE_EXT);
            if (pos == filename.length() - NOT_BYTECODE_FILE_EXT.length())
            {
                filename = filename.substr(0,pos);
            }
        }

        pos = filename.find_first_of(".");
        while (pos != std::string::npos)
        {
            filename.replace(pos,1,"/");
            pos = filename.find_first_of(".");
        }

        // search file in package.path
        unsigned char* chunk = nullptr;
        ssize_t chunkSize = 0;
        std::string chunkName;
        FileUtils* utils = FileUtils::getInstance();

        lua_getglobal(L,"package");
        lua_getfield(L,-1,"path");
        std::string searchpath(lua_tostring(L,-1));
        lua_pop(L,1);
        size_t begin = 0;
        size_t next = searchpath.find_first_of(";",0);

        do
        {
            if (next == std::string::npos)
                next = searchpath.length();
            std::string prefix = searchpath.substr(begin,next);
            if (prefix[0] == '.' && prefix[1] == '/')
            {
                prefix = prefix.substr(2);
            }

            pos = prefix.find("?.lua");
            chunkName = prefix.substr(0,pos) + filename + BYTECODE_FILE_EXT;
            if (utils->isFileExist(chunkName))
            {
                chunk = utils->getFileData(chunkName.c_str(),"rb",&chunkSize);
                if (chunk != NULL&&chunkSize > 0)
                {
                    for (int i = 0; i < chunkSize; i++)
                    {
                        chunk[i] = chunk[i] - 99;//99是用来加密的key值
                    }
                }
                break;
            }
            else
            {
                chunkName = prefix.substr(0,pos) + filename + NOT_BYTECODE_FILE_EXT;
                if (utils->isFileExist(chunkName))
                {
                    chunk = utils->getFileData(chunkName.c_str(),&chunkSize);
                    if (chunk != NULL&&chunkSize > 0)
                    {
                        for (int i = 0; i < chunkSize; i++)
                        {
                            chunk[i] = chunk[i] - 99;//99是用来加密的key值
                        }
                    }
                    break;
                }
            }

            begin = next + 1;
            next = searchpath.find_first_of(";",begin);
        } while (begin < (int)searchpath.length());

        if (chunk)
        {
            LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
            stack->luaLoadBuffer(L,(char*)chunk,(int)chunkSize,chunkName.c_str());
            free(chunk);
        }
        else
        {
            CCLOG("can not get file data of %s",chunkName.c_str());
            return 0;
        }

        return 1;
    }
}

注:测试ios,android,win32能正常使用。但是,不要对main.lua进行加密哦,他的载入,不是通过这里的代码的,在AppDelegate.cpp中能找到。

总结:

这里只是实现了简单的加密,真正的使用,不应该只是用一个Key,应该有多个key(我用的有将近20个),这样的话被破解的可能性应该会低一些。

原文链接:https://www.f2er.com/cocos2dx/340137.html

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