这里没有采用通用的加密方法,使用了类似于图片加密的方法,对文件中的每个字符进行加密,读取的时候,根据加密的key值进行解密。
找到引擎加载Lua文件的接口cocos2dx_lua_loader
添加解密代码 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个),这样的话被破解的可能性应该会低一些。
总结以上是内存溢出为你收集整理的cocos2d-x 3.6 lua文件加密全部内容,希望文章能够帮你解决cocos2d-x 3.6 lua文件加密所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)