展会信息港展会大全

Cocos2d-x 游戏资源(图片、XML、TXT等)打包加密 之 解密读取,cocos2d-xxml
来源:互联网   发布日期:2015-09-28 11:27:48   浏览:6918次  

导读: Cocos2d-x 游戏资源(图片、XML、TXT等)打包加密 之 解密读取,cocos2d-xxml 自上一篇 Unity3d 游戏资源打包加密(图片/XML/TXT等) C#编码 (一...

Cocos2d-x 游戏资源(图片、XML、TXT等)打包加密 之 解密读取,cocos2d-xxml

自上一篇

Unity3d 游戏资源打包加密(图片/XML/TXT等) C#编码 (一)介绍如何打包加密游戏资源已经好几月,却没有详细说明如何在游戏中去读取加密的资源,虽然聪明的程序员看一眼就知道如何逆向编码,但是还是详细说明一下,以作完结。

转自陈里陈外的博客 http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn

Cocos2d-X 资源加密与解密

加密只有一篇即可,解密分为两篇,Cocos2d-x 篇 和 Unity3d 篇。

首先,这一篇介绍Cocos2d-x 读取加密的资源。

转自陈里陈外的博客 http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn

以下内容需要对Cocos2d-x 稍微深入了解。

XML和文档的读取没啥好讲的,完全不涉及Cocos2d-X。这里不做介绍,如果想了解,请百度C++/C如何读取文件以及各大XML库如何读取XML文件。

下面开始我们的解密之旅。

转自陈里陈外的博客 http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn

Cocos2d-x中CCSprite的Create流程

首先我要提一下,在Cocos2d-X中显示一张图片,一般是直接在CCSprite中带图片路径参数去实例化。百分之九十的程序猿们都是这样用的,简单粗暴。但是一旦需要自己对图片进行加密之后,就不能在直接传入图片路径进行实例化,这下该如何是好……

转自陈里陈外的博客 http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn

嗯,Cocos2d-X的好处这个时候就提现出来了,开源免费,我们想看啥就看啥,想改啥就改啥。

作为一个合格的程序员一定要具备看源代码的能力。

下面来看一下CCSprite的部分源代码:

class CC_DLL Sprite : public Node, public TextureProtocol

{

public:

static const int INDEX_NOT_INITIALIZED = -1; /// Sprite invalid index on the SpriteBatchNode

/// @{

/// @name Creators

/**

* Creates an empty sprite without texture. You can call setTexture method subsequently.

*

* @return An autoreleased sprite object.

*/

static Sprite* create();

/**

* Creates a sprite with an image filename.

*

* After creation, the rect of sprite will be the size of the image,

* and the offset will be (0,0).

*

* @paramfilename A path to image file, e.g., "scene1/monster.png"

* @returnAn autoreleased sprite object.

*/

static Sprite* create(const std::string& filename);

/**

* Creates a sprite with an image filename and a rect.

*

* @paramfilename A path to image file, e.g., "scene1/monster.png"

* @paramrectA subrect of the image file

* @returnAn autoreleased sprite object

*/

static Sprite* create(const std::string& filename, const Rect& rect);

/**

* Creates a sprite with a Texture2D object.

*

* After creation, the rect will be the size of the texture, and the offset will be (0,0).

*

* @paramtextureA pointer to a Texture2D object.

* @returnAn autoreleased sprite object

*/

static Sprite* createWithTexture(Texture2D *texture);

/**

* Creates a sprite with a texture and a rect.

*

* After creation, the offset will be (0,0).

*

* @paramtextureA pointer to an existing Texture2D object.

*You can use a Texture2D object for many sprites.

* @paramrectOnly the contents inside the rect of this texture will be applied for this sprite.

* @paramrotatedWhether or not the rect is rotated

* @returnAn autoreleased sprite object

*/

static Sprite* createWithTexture(Texture2D *texture, const Rect& rect, bool rotated=false);

转自陈里陈外的博客 http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn

从源代码中看到,CCSprite不只有传入文件路径这一种实例化的方式,还可以传入一个Texture2D 来实例化。

Texture2D是什么?

学习过OpenGL的朋友就知道,Texture在GL中就是存储纹理数据的一块内存,至于Texture2D,无非就是一块内存。

那内存里面放着什么,肯定就是纹理数据了。

那纹理数据从哪来,肯定是读取文件来的。

那文件是什么,一块数据。

至此流程明了,这一实例化流程和我们的目的一致。

即:用一块数据来实例化一个Texture2D,然后实例化CCSprite。

根据上面的流程,我们下一步要做的就是,实例化一个 Texture2D 。

我们来看Texture2D的源代码

// implementation Texture2D (Image)

bool Texture2D::initWithImage(Image *image)

{

return initWithImage(image, g_defaultAlphaPixelFormat);

}

转自陈里陈外的博客 http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn

然后我们需要的就是 实例化一个Image。

在CCImage中,先来看这个函数

bool Image::initWithImageFile(const std::string& path)

{

bool ret = false;

_filePath = FileUtils::getInstance()->fullPathForFilename(path);

#ifdef EMSCRIPTEN

// Emscripten includes a re-implementation of SDL that uses HTML5 canvas

// operations underneath. Consequently, loading images via IMG_Load (an SDL

// API) will be a lot faster than running libpng et al as compiled with

// Emscripten.

SDL_Surface *iSurf = IMG_Load(fullPath.c_str());

int size = 4 * (iSurf->w * iSurf->h);

ret = initWithRawData((const unsigned char*)iSurf->pixels, size, iSurf->w, iSurf->h, 8, true);

unsigned int *tmp = (unsigned int *)_data;

int nrPixels = iSurf->w * iSurf->h;

for(int i = 0; i < nrPixels; i++)

{

unsigned char *p = _data + i * 4;

tmp[i] = CC_RGB_PREMULTIPLY_ALPHA( p[0], p[1], p[2], p[3] );

}

SDL_FreeSurface(iSurf);

#else

Data data = FileUtils::getInstance()->getDataFromFile(_filePath);

if (!data.isNull())

{

ret = initWithImageData(data.getBytes(), data.getSize());

}

#endif // EMSCRIPTEN

return ret;

}

这个函数的功能就是,根据传进来的参数-图片路径,读取这个图片路径的文件,获取数据,然后根据这块数据,来实例化 Texture2D 。

好的,终于到了最后一步,直接和内存打交道了。

转自陈里陈外的博客 http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn

下面整理一下CCSprite的实例化流程:

好了,首先我们来按照上面的流程,读取一张未加密图片,例化一个CCSprite 。

读取未加密图片

一贯作风,还是拿HelloWorld 作为例子,以下代码

Image* image = new Image();

Data data = FileUtils::getInstance()->getDataFromFile("Deemo.jpg");

image->initWithImageData(data.getBytes(), data.getSize());

Texture2D* texutre=new Texture2D();

texutre->initWithImage(image);

Sprite* sprite=Sprite::createWithTexture(texutre);

// position the sprite on the center of the screen

sprite->setPosition(Vec2(visibleSize / 2) + origin);

// add the sprite as a child to this layer

this->addChild(sprite);

运行成功

转自陈里陈外的博客 http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn

对图片打包加密

首先我们使用之前编写的资源打包加密工具对图片进行打包加密。

http://download.csdn.net/detail/cp790621656/8393457

在配置文件中添加要打包的文件夹的名字,一行一个。然后运行程序打包,我这里打包 Deemo 这个文件夹,生成了Deemo.UPK 。

转自陈里陈外的博客 http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn

对加密的资源进行解密

上篇博客虽然介绍了打包加密的原理以及贴上了代码,但可能还是不够明了,我这里贴上UPK文件的内存结构图,这样大家对UPK就能一目了然。

好了,知道了UPK的内存结构,我们就可以开始在Cocos2d-X中去读取我们想要的数据了。

转自陈里陈外的博客 http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn

首先我们读取出文件头

void HelloWorld::InitUPKFileSystem()

{

std::string fullPath = FileUtils::getInstance()->fullPathForFilename("Deemo.UPK");

std::ifstream fin(fullPath.c_str(),std::ios::binary);

if (!fin)

{

CCLOG("File Open Error");

return;

}

//首先读取文件数量4byte;

char fileCountArr[4];

fin.read(fileCountArr,4);

int fileCount=0;

memcpy(&fileCount,fileCountArr,4);

//然后循环读取文件信息;

for (int index=0;index<fileCount;index++)

{

OneFileInfor oneFileInfor;

//读取m_id;

char idArr[4];

int id=0;

fin.read(idArr,4);

memcpy(&id,idArr,4);

oneFileInfor.m_id=id;

//读取m_StartPos;

char startPosArr[4];

int startPos=0;

fin.read(startPosArr,4);

memcpy(&startPos,startPosArr,4);

oneFileInfor.m_StartPos=startPos;

//读取m_Size;

char sizeArr[4];

int size=0;

fin.read(sizeArr,4);

memcpy(&size,sizeArr,4);

oneFileInfor.m_Size=size;

//读取m_Path;

char pathArr[256];

std::string path;

fin.read(pathArr,256);

path=pathArr;

oneFileInfor.m_Path=path;

m_allFileInforVec.push_back(oneFileInfor);

}

fin.close();

}

然后根据文件路径,查找到对应的OneFileInfor,然后根据m_StartPos进行偏移读取m_Size大小的数据即可。

cocos2d::Data HelloWorld::GetDataFromUPK(const char* filepath)

{

Data ret;

//根据文件路径找到文件信息;

for (int index=0;index<m_allFileInforVec.size();index++)

{

OneFileInfor oneFileInfor=m_allFileInforVec[index];

std::string path=filepath;

if (oneFileInfor.m_Path==path)

{

//找到了文件,开始读取文件数据;

std::string fullPath = FileUtils::getInstance()->fullPathForFilename("Deemo.UPK");

std::ifstream fin(fullPath.c_str(),std::ios::binary);

if (!fin)

{

CCLOG("File Open Error");

return ret;

}

char* buffer=NULL;

buffer=(char*)malloc(oneFileInfor.m_Size);

fin.seekg(oneFileInfor.m_StartPos,std::ios::beg); //ios::cur从当前位置偏移;ios::beg从文件开头偏移;

fin.read(buffer,oneFileInfor.m_Size);

ret.fastSet((unsigned char *)buffer,oneFileInfor.m_Size);

fin.close();

break;

}

}

return ret;

}

成功运行转自陈里陈外的博客 http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn

工程下载:转自陈里陈外的博客 http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn

http://pan.baidu.com/s/1pJkf2mJ

http://download.csdn.net/detail/cp790621656/8609965

转自陈里陈外的博客 http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn

http://www.bkjia.com/Androidjc/986890.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/986890.htmlTechArticleCocos2d-x 游戏资源(图片、XML、TXT等)打包加密 之 解密读取,cocos2d-xxml 自上一篇 Unity3d 游戏资源打包加密(图片/XML/TXT等) C#编码 (一) 介绍如何...

赞助本站

人工智能实验室

相关热词: android开发 android教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港