#include "Cocos2dxLuaLoader.h" #include <string> #include <algorithm> #include<iostream> using namespace cocos2d; extern "C" { //单个字符异或运算 char MakecodeChar(char c,int key){ return c=c^key; } //单个字符解密 char CutcodeChar(char c,int key){ return c^key; } //加密 void Makecode(char *pstr,pkey[i%5]); } //解密 void Cutecode(char *pstr,pkey[i%5]); } int cocos2dx_lua_loader(lua_State *L) { std::string filename(luaL_checkstring(L,1)); size_t pos = filename.rfind(".lua"); if (pos != std::string::npos) { filename = filename.substr(0,pos); } pos = filename.find_first_of("."); while (pos != std::string::npos) { filename.replace(pos,"/"); pos = filename.find_first_of("."); } filename.append(".lua"); Data data = FileUtils::getInstance()->getDataFromFile(filename); if (!data.isNull()) { //====code decode start================================== log("===encode filename:%s===",filename.c_str()); //如果filename == 'main.lua',则解密 char *fileContent = (char*)data.getBytes(); int key[]={1,6};//加密字符 char *fileContentDecoded = NULL; if (strcmp(filename.c_str(),"ReadyScene.lua")==0 || strcmp(filename.c_str(),"GameScene.lua")==0 || strcmp(filename.c_str(),"PutHeadScene.lua")==0 || strcmp(filename.c_str(),"TutorialsScene.lua")==0 || strcmp(filename.c_str(),"WordsCategoryScene.lua")==0 || strcmp(filename.c_str(),"DictHelper.lua")==0 || strcmp(filename.c_str(),"LJ.lua")==0 || strcmp(filename.c_str(),"ResultScene.lua")==0 || strcmp(filename.c_str(),"DQueue.lua")==0 || strcmp(filename.c_str(),"UIHelper.lua")==0 ) { if (data.getSize() < strlen(fileContent)) { fileContentDecoded = (char *) malloc ((data.getSize() + 1) * sizeof(char));//增加一位 memset(fileContentDecoded,data.getSize() + 1); fileContentDecoded[data.getSize()] = '';//最后一位置为结束位 strncpy(fileContentDecoded,fileContent,data.getSize()); fileContent = NULL; } else { fileContentDecoded = fileContent; } Cutecode(fileContentDecoded,key);//解密 } else { fileContentDecoded = fileContent; } //====code decode end================================== if (luaL_loadbuffer(L,fileContentDecoded,data.getSize(),filename.c_str()) != 0) { luaL_error(L,"error loading module %s from file %s : %s", lua_tostring(L,1),filename.c_str(),lua_tostring(L,-1)); } } else { log("can not get file data of %s",filename.c_str()); } return 1; } } |