(Transfer) Quick-Cocos2D-X picture resource encryption (continued)

Summary: Supplement to the resource encryption method implemented on quick-cocos2d-x

First part:

quick-cocos2d-x image resource encryption

Let quick-cocos2d-x support encrypted plist files

Through the modification of the previous article, we have been able to use encrypted image resources in common formats (png, jpg, etc.) on the Windows platform and Android. However, on the player on the MAC and on the IOS, another code is called when loading the resource, so we need to make additional modifications.

First, modify the Player code on the Mac. What we are going to modify this time is the CCImage.mm file in the lib\cocos2d-x\cocos2dx\platform\mac directory. At the beginning of the _initWithFile function, you can see the following code:

NSString *fullPath=[NSString stringWithUTF8String:path]; jpg = [[NSImage alloc]initWithContentsOfFile:fullPath];

We use the following code instead: p>

unsignedlong fileSize=0; unsignedchar* pFileData= cocos2d::CZHelperFunc::getFileData(path, "rb",&fileSize); NSData*adata=[ [NSDataalloc]initWithBytes:pFileDatalength:fileSize]; delete[ ]pFileData; jpg=[[NSImagealloc]initWithData:adata] ;< /span> 

And in the CCImage::initWithImageFile function, I saw a familiar statement:

 unsignedchar*pFileData=CCFileUtils::sharedFileUtils()->getFileData (strTemp.c_str(), "rb", &fileSize); 

I should be very skilled in modifying this:

unsignedchar* pFileData= CZHelperFunc::getFileDat a(strTemp.c_str(), "rb", &fileSize) ;

In addition, the inclusion of the HelperFunc.h header file is definitely necessary, and the subsequent modifications are the same, so I won’t explain it every time.

Ok, now MAC supports encrypted image files. If you want to support encrypted plist files, you must also change the CCFileUtilsMac.mm file in the same directory. Find the CCFileUtilsMac::createCCDictionaryWithContentsOfFile function, the two sentences that need to be modified are:

NSString* pPath=[NSString stringWithUTF8String:fullPath.c_str()]; NSDictionary* pDict= [NSDictionary dictionaryWithContentsOfFile:pPath];

You need to write a little more code than before:

span>

unsignedlong fileSize = 0; unsignedchar* pFileData= CZHelperFunc::getFileData(fullPath.c_str(), "rb",&fileSize); NSData *data = [[[NSData alloc]initWithBytes:pFileDatalength:fileSize]autorelease]; delete[]pFileData; NSPropertyListFormat format; NSString *error;  NSMutableDictionary *pDict = (NSMutableDictionary*)[ sNSPropertyListSerialization propertyListFromData:data mutability option:NSPropertyListMutableContainersAndLeaves  span> 

After changing the MAC, it is easier to modify the IOS, because the modification methods are similar, but the files are different. The files to be modified are the CCImage.mm and CCFileUtilsIOS.mm files in the lib\cocos2d-x\cocos2dx\platform\ios directory. The functions to be modified in these two files are the same, even the code that needs to be modified are almost the same. The only thing that needs to be noticed is that UIImage should be used in IOS instead of NSImage in Mac. No more code posted here.

After so many modifications, it is basically commonly used All picture files are already supported, but some special format files may also be used, by the way.

Support PVR files, need to modify lib\cocos2d -x\cocos2dx\textures CCTexturePVR.cpp file, find the sentence where CCFileUtils::sharedFileUtils()->getFileData is located, and modify it, no need to say more.

Support TGA files, modify lib\cocos2d-x\cocos2dx TGAlib.cpp under \support\image_support, similar to the above, is also a sentence.

Also in There is also a file CCTextureCache.cpp in the lib\cocos2d-x\cocos2dx\textures directory. I didn’t take a closer look. It should have something to do with the loading of TMX MAP. It’s just a sentence, so let’s change it together, ha ha.

After the above modification, it should be able to meet The vast majority of encryption needs are gone. A cursory glance at the code, there should be only PKM files that cannot be supported, because it uses JNI to call Android’s API to decode the file, which is not very easy to modify.

The above is my personal modification method, there must be a better modification method, hope Can share and communicate with everyone. If there is something wrong, please correct me!

(

2014.2 .24 update:

supports pvr.ccz file, need to modify lib\cocos2d The ccInflateCCZFile function in the -x\cocos2dx\support\zip_support\ZipUtils.cpp file is also a sentence for reading the content of the file, applicable under all platforms.

Thank you to the friend “Xiaosheng” in the group for providing this information!

Original address: https://my.oschina.net/SunLightJuly/blog/189971

First article:

quick-cocos2d-x image resource encryption

let quick-cocos2d-x support encrypted plist files

Through the modification of the previous article, we have been able to play on the Windows platform player and Android Common formats using encryption Type (png, jpg, etc.) image resources. However, on the player on the MAC and on the IOS, another code is called when loading the resource, so we need to make additional modifications.

First, modify the Player code on the Mac. What we are going to modify this time is the CCImage.mm file in the lib\cocos2d-x\cocos2dx\platform\mac directory. At the beginning of the _initWithFile function, you can see the following code:

NSString *fullPath=[NSString stringWithUTF8String:path]; jpg = [[NSImage alloc]initWithContentsOfFile:fullPath];

We use the following code instead: p>

unsignedlong fileSize=0; unsignedchar* pFileData= cocos2d::CZHelperFunc::getFileData(path, "rb",&fileSize); NSData*adata=[ [NSDataalloc]initWithBytes:pFileDatalength:fileSize]; delete[ ]pFileData; jpg=[[NSImagealloc]initWithData:adata] ;< /span>

And in the CCImage::initWithImageFile function, I saw a familiar statement:

unsigned< span class="hljs-keyword" style="color: #e3ceab">char* pFileData= CCFileUtils::sharedFileUtils()->getFileData(strTemp .c_str(), "rb",&fileSize);

I should be familiar with modifying this:

unsignedchar* pFileData= CZHelperFunc::getFileData(strTemp. c_str(), "rb",&fileSize);

In addition, the inclusion of the HelperFunc.h header file is definitely necessary, and the subsequent modifications are the same, so I won’t explain it every time.

Ok, now MAC supports encrypted image files. If you want to support encrypted plist files, you must also change the CCFileUtilsMac.mm file in the same directory. Find the CCFileUtilsMac::createCCDictionaryWithContentsOfFile function, the two sentences that need to be modified are:

NSString* pPath=[NSString stringWithUTF8String:fullPath.c_str()]; NSDictionary* pDict= [NSDictionary dictionaryWithContentsOfFile:pPath];

You need to write a little more code than before:

span>

unsignedlong fileSize = 0; unsignedchar* pFileData= CZHelperFunc::getFileData(fullPath.c_str(), "rb",&fileSize); NSData *data = [[[NSData alloc]initWithBytes:pFileDatalength:fileSize]autorelease]; delete[]pFileData; NSPropertyListFormat format; NSString *error; NSMutableDictionary *pDict = (NSMutableDictionary*)[ NSMutableDictionary*)[ NSMutableDictionary*)[  -built_in" style="color: #cc9393">NSPropertyListSerializationpropertyListFromData:data mutabilityOption:NSPropertyListMutableContainersAndLeaves  >  span>

After changing the MAC, it is easier to modify the IOS, because the modification methods are similar, but the files are different. The files to be modified are the CCImage.mm and CCFileUtilsIOS.mm files in the lib\cocos2d-x\cocos2dx\platform\ios directory. The functions to be modified in these two files are the same, even the code that needs to be modified are almost the same. The only thing that needs to be noticed is that UIImage should be used in IOS instead of NSImage in Mac. No more code posted here.

After so many modifications, it is basically commonly used All picture files are already supported, but some special format files may also be used, by the way.

Support PVR files, need to modify lib\cocos2d -x\cocos2dx\textures CCTexturePVR.cpp file, find the sentence where CCFileUtils::sharedFileUtils()->getFileData is located, and modify it, no need to say more.

Support TGA files, modify lib\cocos2d-x\cocos2dx TGAlib.cpp under \support\image_support, similar to the above, is also a sentence.

Also in There is also a file CCTextureCache.cpp in the lib\cocos2d-x\cocos2dx\textures directory. I didn’t take a closer look. It should have something to do with the loading of TMX MAP. It’s just a sentence, so let’s change it together, ha ha.

After the above modification, it should be able to meet The vast majority of encryption needs are gone. A cursory glance at the code, there should be only PKM files that cannot be supported, because it uses JNI to call Android’s API to decode the file, which is not very easy to modify.

The above is my personal modification method, there must be a better modification method, hope Can share and communicate with everyone. If there is something wrong, please correct me!

(

2014.2 .24 update:

supports pvr.ccz file, need to modify lib\cocos2d The ccInflateCCZFile function in the -x\cocos2dx\support\zip_support\ZipUtils.cpp file is also a sentence for reading the content of the file, applicable under all platforms.

Thank you to the friend “Xiaosheng” in the group for providing this information!

First part:

quick-cocos2d-x image resource encryption

Let quick-cocos2d-x support encrypted plist files

Through the modification of the previous article, we have been able to use encrypted image resources in common formats (png, jpg, etc.) on Windows platform players and Android. However, in On the player on MAC and on IOS, another code is called when loading resources, so we need to make additional modifications.

First, modify the Player code on the Mac. What we are going to modify this time is the CCImage.mm file in the lib\cocos2d-x\cocos2dx\platform\mac directory. At the beginning of the _initWithFile function, you can see the following code:

NSString *fullPath=[NSString stringWithUTF8String:path]; jpg = [[NSImage alloc]initWithContentsOfFile:fullPath];

We use the following code instead: p>

unsignedlong fileSize=0; unsignedchar * PFileData = cocos2d::CZHelperFunc::getFileData(path, " rb", &fileSize); NSData*adata=[[NSDataalloc]initWithBytes:pFileDatalength:fileSize]; delete[]pFileData; jpg = [[NSImagealloc]initWithData:adata]; < /span>

And in the CCImage::initWithImageFile function, I saw a familiar statement:

unsignedchar* pFileData= CCFileUtils::sharedFileUtils()->getFileData(strTemp.c_str( ), "rb",&fileSize);< /span>

I should be familiar with modifying this:

unsignedchar* pFileData= CZHelperFunc::getFileData(strTemp.c_str(),< span class="hljs-string" style="color: #cc9393">"rb",&fileSize); 

In addition, the inclusion of the HelperFunc.h header file is definitely necessary, and the subsequent modifications are the same, so I won’t explain it every time.

Ok, now MAC supports encrypted image files. If you want to support encrypted plist files, you must also change the CCFileUtilsMac.mm file in the same directory. Find the CCFileUtilsMac::createCCDictionaryWithContentsOfFile function, the two sentences that need to be modified are:

NSString* pPath=[NSString stringWithUTF8String:fullPath.c_str()]; NSDictionary* pDict= [NSDictionary dictionaryWithContentsOfFile:pPath];

You need to write a little more code than before:

span>

unsignedlong fileSize=< span class="hljs-number" style="color: #8cd0d3">0; unsignedchar* pFileData= CZHelperFunc::getFileData(fullPath.c_str(), "rb",&fileSize); NSData*data=[[[NSDataalloc] initWithBytes:pFileData length:fileSize] autorelease]; delete[]pFileData; NSPropertyListFormat format; NSString*error; NSMutableDictionary*pDict= (NSMutableDictionary*)[ NSPropertyListSerialization propertyListFromData:data                            mutabilityOption:NSPropertyListMutableContainersAndLeaves                            format:&format                            errorDescription:&error];

 

改好了MAC,修改IOS就比较容易了,因为修改方法很类似,只是文件不同。要修改的是lib\cocos2d-x\cocos2dx\platform\ios目录下的CCImage.mm和CCFileUtilsIOS.mm文件。这两个文件里要修改的函数都是一样的,连需要修改的代码都几乎一样,唯一需要稍为注意的是在IOS里要用UIImage来代替Mac里的NSImage。这里就不再贴出代码了。

经过这么多修改,基本上常用的图片文件都已经支持,不过,有些特殊格式的文件也是有可能用到的,顺便提一下吧。

支持PVR文件,要修改lib\cocos2d-x\cocos2dx\textures目录下的CCTexturePVR.cpp文件,找到CCFileUtils::sharedFileUtils()->getFileData所在的那一句进行修改就是了,不需要多说了。

支持TGA文件,修改lib\cocos2d-x\cocos2dx\support\image_support下的TGAlib.cpp,类似上面,也是一条语句的事。

另外在lib\cocos2d-x\cocos2dx\textures目录下还有个文件CCTextureCache.cpp,我没细看,应该是跟TMX MAP的载入有点关系。里面也只是一条语句的事情,一起改掉吧,呵呵。

经过以上修改后,应该能满足绝大多数的加密需求了。粗粗的浏览了一下代码,应该只有PKM文件无法支持,因为它是直接通过JNI调用Android的API来对文件解码的,不是太好修改了。

以上是我个人的修改方法,肯定还有更好的改法,希望能和大家相互分享和交流。如果有错误的地方,也请大家多多指正!

(

2014.2.24更新:

支持pvr.ccz文件,需要修改lib\cocos2d-x\cocos2dx\support\zip_support\ZipUtils.cpp文件里的ccInflateCCZFile函数,也是一条读文件内容的语句,各平台下都适用。

感谢群里的朋友“蚀生”提供这一信息!

 

原文地址:https://my.oschina.net/SunLightJuly/blog/189971

Leave a Comment

Your email address will not be published.