
2.需要工具:quick-lua中已经集成图片加密工具,但是我没有用quick,所以单独把这个加密文件夹拎出来了。点击下载加密工具。 http://pan.baidu.com/s/1pLyI6NL
二、修改CCfileUtils.h和cpp文件 1.找到frameworks\cocos2d-x\cocos\platform\CCfileUtils.h,添加一个结构体ResEncryptData:
class CC_DLL fileUtils{public: //=====添加代码 static struct ResEncryptData{ ResEncryptData(){ allowNoEncrpt = true; key = "key123456"; sign = "sign520"; } std::string key; std::string sign; bool allowNoEncrpt; }encryptData; static unsigned char* decryptBuffer(unsigned char* buf,unsigned long size,unsigned long *newSize); //=====添加结束 /** * Gets the instance of fileUtils. */ static fileUtils* getInstance(); /** * Destroys the instance of fileUtils. */ static voID destroyInstance();2. 找到 frameworks\cocos2d-x\cocos\platform\ CCfileUtils.cpp,添加对应的实现代码: //头文件声明=====#include "xxtea/xxtea.h"#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) #include "xxtea/xxtea.cpp"#endifunsigned char* fileUtils::decryptBuffer(unsigned char* buf,unsigned long *newSize){ unsigned char *m_xxteaKey = (unsigned char *)fileUtils::encryptData.key.c_str(); unsigned char *m_xxteaSign = (unsigned char *)fileUtils::encryptData.sign.c_str(); xxtea_long m_xxteaSignLen = fileUtils::encryptData.sign.length(); xxtea_long m_xxteaKeyLen = fileUtils::encryptData.key.length(); if (NulL==buf) return NulL; unsigned char* buffer = NulL; bool isXXTEA = true; for (unsigned int i = 0; isXXTEA && i < m_xxteaSignLen && i < size; ++i) { if(buf[i] != m_xxteaSign[i]){ isXXTEA = false; break; } } if(m_xxteaSignLen == 0){ isXXTEA = false; } if (isXXTEA) { // decrypt XXTEA xxtea_long len = 0; buffer = xxtea_decrypt(buf + m_xxteaSignLen,(xxtea_long)size - (xxtea_long)m_xxteaSignLen,(unsigned char*)m_xxteaKey,(xxtea_long)m_xxteaKeyLen,&len); delete []buf; buf = NulL; size = len; } else { if(fileUtils::getInstance()->encryptData.allowNoEncrpt) {buffer = buf;} } *newSize = size; return buffer;} 3.在上面那个文件中: frameworks\cocos2d-x\cocos\platform\ CCfileUtils.cpp,修改getData函数:
static Data getData(const std::string& filename,bool forString){ if (filename.empty()) { return Data::Null; } Data ret; unsigned char* buffer = nullptr; size_t size = 0; size_t readsize; const char* mode = nullptr; if (forString) mode = "rt"; else mode = "rb"; do { // Read the file from harDWare std::string fullPath = fileUtils::getInstance()->fullPathForfilename(filename); file *fp = fopen(fullPath.c_str(),mode); CC_BREAK_IF(!fp); fseek(fp,SEEK_END); size = ftell(fp); fseek(fp,SEEK_SET); if (forString) { buffer = (unsigned char*)malloc(sizeof(unsigned char) * (size + 1)); buffer[size] = 'frameworks\cocos2d-x\cocos\platform\'; } else { buffer = (unsigned char*)malloc(sizeof(unsigned char) * size); } readsize = fread(buffer,sizeof(unsigned char),size,fp); fclose(fp); if (forString && readsize < size) { buffer[readsize] = 'CCfileUtils.cpp,修改getfileData函数:'; } } while (0); if (nullptr == buffer || 0 == readsize) { std::string msg = "Get data from file("; msg.append(filename).append(") Failed!"); cclOG("%s",msg.c_str()); } else { //ret.fastSet(buffer,readsize); //======新的改动 unsigned long newSize = 0; unsigned char *newBuffer = fileUtils::decryptBuffer(buffer,readsize,&newSize); ret.fastSet(newBuffer,newSize); } 4.在上面那个文件中:
unsigned char* fileUtils::getfileData(const std::string& filename,const char* mode,ssize_t *size){ unsigned char * buffer = nullptr; CCASSERT(!filename.empty() && size != nullptr && mode != nullptr,"InvalID parameters."); *size = 0; do { // read the file from harDWare const std::string fullPath = fullPathForfilename(filename); file *fp = fopen(fullPath.c_str(),mode); CC_BREAK_IF(!fp); fseek(fp,SEEK_END); *size = ftell(fp); fseek(fp,SEEK_SET); buffer = (unsigned char*)malloc(*size); *size = fread(buffer,*size,fp); fclose(fp); } while (0); if (!buffer) { std::string msg = "Get data from file("; msg.append(filename).append(") Failed!"); cclOG("%s",msg.c_str()); }else{ //新的改动========= unsigned long newSize = 0; unsigned char *newBuffer = fileUtils::decryptBuffer(buffer,&newSize); *size = newSize; buffer = newBuffer; } return buffer;}1.找到 frameworks\cocos2d-x\cocos\platform\win32\
三、修改CCfileUtils-win32.cpp文件 CCfileUtils-win32.cpp,添加对应的代码:
static Data getData(const std::string& filename,bool forString){ if (filename.empty()) { return Data::Null; } unsigned char *buffer = nullptr; size_t size = 0; do { // read the file from harDWare std::string fullPath = fileUtils::getInstance()->fullPathForfilename(filename); WCHAR wszBuf[CC_MAX_PATH] = {0}; MultiBytetoWIDeChar(CP_UTF8,fullPath.c_str(),-1,wszBuf,sizeof(wszBuf)/sizeof(wszBuf[0])); HANDLE fileHandle = ::CreatefileW(wszBuf,GENERIC_READ,file_SHARE_READ|file_SHARE_WRITE,NulL,OPEN_EXISTING,nullptr); CC_BREAK_IF(fileHandle == INVALID_HANDLE_VALUE); size = ::GetfileSize(fileHandle,nullptr); if (forString) { buffer = (unsigned char*) malloc(size + 1); buffer[size] = '1.找到'; } else { buffer = (unsigned char*) malloc(size); } DWORD sizeRead = 0; BOol successed = FALSE; successed = ::Readfile(fileHandle,buffer,&sizeRead,nullptr); ::CloseHandle(fileHandle); if (!successed) { // should determine buffer value,or it will cause memory leak if (buffer) { free(buffer); buffer = nullptr; } } } while (0); Data ret; if (buffer == nullptr || size == 0) { std::string msg = "Get data from file("; // Gets error code. DWORD errorCode = ::GetLastError(); char errorCodeBuffer[20] = {0}; snprintf(errorCodeBuffer,sizeof(errorCodeBuffer),"%d",errorCode); msg = msg + filename + ") Failed,error code is " + errorCodeBuffer; cclOG("%s",msg.c_str()); if (buffer) free(buffer); } else { //新的改动 unsigned long newSize = 0; unsigned char *newBuffer = fileUtils::decryptBuffer(buffer,&newSize); //ret.fastSet(buffer,size); ret.fastSet(newBuffer,newSize); } return ret;}unsigned char* fileUtilsWin32::getfileData(const std::string& filename,ssize_t* size){ unsigned char * pBuffer = nullptr; *size = 0; do { // read the file from harDWare std::string fullPath = fullPathForfilename(filename); WCHAR wszBuf[CC_MAX_PATH] = {0}; MultiBytetoWIDeChar(CP_UTF8,nullptr); CC_BREAK_IF(fileHandle == INVALID_HANDLE_VALUE); *size = ::GetfileSize(fileHandle,nullptr); pBuffer = (unsigned char*) malloc(*size); DWORD sizeRead = 0; BOol successed = FALSE; successed = ::Readfile(fileHandle,pBuffer,nullptr); ::CloseHandle(fileHandle); if (!successed) { free(pBuffer); pBuffer = nullptr; } } while (0); if (! pBuffer) { std::string msg = "Get data from file("; // Gets error code. DWORD errorCode = ::GetLastError(); char errorCodeBuffer[20] = {0}; snprintf(errorCodeBuffer,msg.c_str()); }else{ //新的改动 unsigned long newSize = 0; unsigned char *newBuffer = fileUtils::decryptBuffer(pBuffer,&newSize); *size = newSize; pBuffer = newBuffer; } return pBuffer;} frameworks\cocos2d-x\cocos\platform\androID\ CCfileUtils-androID.cpp,添加对应的代码:
四、修改CCfileUtils-androID.cpp文件
Data fileUtilsAndroID::getData(const std::string& filename,bool forString){ if (filename.empty()) { return Data::Null; } unsigned char* data = nullptr; ssize_t size = 0; string fullPath = fullPathForfilename(filename); cocosplay::updateAssets(fullPath); if (fullPath[0] != '/') { string relativePath = string(); size_t position = fullPath.find("assets/"); if (0 == position) { // "assets/" is at the beginning of the path and we don't want it relativePath += fullPath.substr(strlen("assets/")); } else { relativePath += fullPath; } cclOGINFO("relative path = %s",relativePath.c_str()); if (nullptr == fileUtilsAndroID::assetmanager) { LOGD("... fileUtilsAndroID::assetmanager is nullptr"); return Data::Null; } // read asset data AAsset* asset = AAssetManager_open(fileUtilsAndroID::assetmanager,relativePath.c_str(),AASSET_MODE_UNKNowN); if (nullptr == asset) { LOGD("asset is nullptr"); return Data::Null; } off_t fileSize = AAsset_getLength(asset); if (forString) { data = (unsigned char*) malloc(fileSize + 1); data[fileSize] = '1.找到'; } else { data = (unsigned char*) malloc(fileSize); } int bytesread = AAsset_read(asset,(voID*)data,fileSize); size = bytesread; AAsset_close(asset); } else { do { // read rrom other path than user set it //cclOG("GETTING file absolute DATA: %s",filename); const char* mode = nullptr; if (forString) mode = "rt"; else mode = "rb"; file *fp = fopen(fullPath.c_str(),mode); CC_BREAK_IF(!fp); long fileSize; fseek(fp,SEEK_END); fileSize = ftell(fp); fseek(fp,SEEK_SET); if (forString) { data = (unsigned char*) malloc(fileSize + 1); data[fileSize] = 'frameworks\cocos2d-x\cocos\platform\apple\'; } else { data = (unsigned char*) malloc(fileSize); } fileSize = fread(data,fileSize,fp); fclose(fp); size = fileSize; } while (0); } Data ret; if (data == nullptr || size == 0) { std::string msg = "Get data from file("; msg.append(filename).append(") Failed!"); cclOG("%s",msg.c_str()); } else { //新的改动========== unsigned long newSize = 0; unsigned char *newBuffer = fileUtils::decryptBuffer(data,&newSize); // ret.fastSet(data,newSize); cocosplay::notifyfileLoaded(fullPath); } return ret;}unsigned char* fileUtilsAndroID::getfileData(const std::string& filename,ssize_t * size){ unsigned char * data = 0; if ( filename.empty() || (! mode) ) { return 0; } string fullPath = fullPathForfilename(filename); cocosplay::updateAssets(fullPath); if (fullPath[0] != '/') { string relativePath = string(); size_t position = fullPath.find("assets/"); if (0 == position) { // "assets/" is at the beginning of the path and we don't want it relativePath += fullPath.substr(strlen("assets/")); } else { relativePath += fullPath; } LOGD("relative path = %s",relativePath.c_str()); if (nullptr == fileUtilsAndroID::assetmanager) { LOGD("... fileUtilsAndroID::assetmanager is nullptr"); return nullptr; } // read asset data AAsset* asset = AAssetManager_open(fileUtilsAndroID::assetmanager,AASSET_MODE_UNKNowN); if (nullptr == asset) { LOGD("asset is nullptr"); return nullptr; } off_t fileSize = AAsset_getLength(asset); data = (unsigned char*) malloc(fileSize); int bytesread = AAsset_read(asset,fileSize); if (size) { *size = bytesread; } AAsset_close(asset); } else { do { // read rrom other path than user set it //cclOG("GETTING file absolute DATA: %s",filename); file *fp = fopen(fullPath.c_str(),SEEK_SET); data = (unsigned char*) malloc(fileSize); fileSize = fread(data,fp); fclose(fp); if (size) { *size = fileSize; } } while (0); } if (! data) { std::string msg = "Get data from file("; msg.append(filename).append(") Failed!"); cclOG("%s",msg.c_str()); } else { //新的改动 unsigned long newSize = 0; unsigned char *newBuffer = fileUtils::decryptBuffer(data,&newSize); *size = newSize; data = newBuffer; cocosplay::notifyfileLoaded(fullPath); } return data;} CCfileUtils-apple.cpp,修改对应的代码:getValueMapFromfile。以便读取pList文件,是因为ios读取pList文件方式和androID和win32不同。 ValueMap fileUtilsApple::getValueMapFromfile(const std::string& filename){ //====新的改动 std::string fullPath = fullPathForfilename(filename); Data d = fileUtils::getDataFromfile(filename); unsigned long fileSize = d.getSize(); unsigned char* pfileData = d.getBytes(); NSData *data = [[[NSData alloc] initWithBytes:pfileData length:fileSize] autorelease]; nspropertyListFormat format; Nsstring *error; NSMutableDictionary *dict = (NSMutableDictionary *)[ nspropertyListSerialization propertyListFromData:data mutabilityOption:nspropertyListMutableContainersAndLeaves format:&format errorDescription:&error]; //====改动结束 //std::string fullPath = fullPathForfilename(filename); //Nsstring* path = [Nsstring stringWithUTF8String:fullPath.c_str()]; //NSDictionary* dict = [NSDictionary dictionaryWithContentsOffile:path]; ValueMap ret; if (dict != nil) { for (ID key in [dict allKeys]) { ID value = [dict objectForKey:key]; addValuetoDict(key,value,ret); } } return ret;} 五、修改CCfileUtils-apple.mm文件 2.注意key和sign要和你的AppDelegate中设置的key和sign一样:
LuaStack* stack = LuaEngine::getInstance()->getLuaStack();stack->setXXTEAKeyAndSign(key,strlen(key),sign,strlen(sign));
六、大功告成! 1.重新编译c++代码,把res文件夹替换成加密后的文件夹,即可正常运行。 总结
以上是内存溢出为你收集整理的cocos2dx-lua 3.4 之 图片资源加密!全部内容,希望文章能够帮你解决cocos2dx-lua 3.4 之 图片资源加密!所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)