2013-10-14 14:56:57 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
2017-02-14 14:36:57 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2013-10-14 14:56:57 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2014-05-17 05:36:00 +08:00
|
|
|
|
|
|
|
#include "base/ccUtils.h"
|
2014-07-14 20:42:08 +08:00
|
|
|
|
2016-07-28 05:49:41 +08:00
|
|
|
#include <cmath>
|
2014-07-14 20:42:08 +08:00
|
|
|
#include <stdlib.h>
|
2017-04-01 11:43:17 +08:00
|
|
|
#include "md5/md5.h"
|
2014-07-14 20:42:08 +08:00
|
|
|
|
2014-05-27 10:44:23 +08:00
|
|
|
#include "base/CCDirector.h"
|
2015-05-18 01:48:49 +08:00
|
|
|
#include "base/CCAsyncTaskPool.h"
|
2015-07-15 12:09:08 +08:00
|
|
|
#include "base/CCEventDispatcher.h"
|
2015-09-21 15:31:01 +08:00
|
|
|
#include "base/base64.h"
|
2014-05-27 10:44:23 +08:00
|
|
|
#include "renderer/CCCustomCommand.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
2016-06-06 22:32:50 +08:00
|
|
|
#include "renderer/CCTextureCache.h"
|
2016-05-07 22:10:53 +08:00
|
|
|
|
2014-05-27 10:44:23 +08:00
|
|
|
#include "platform/CCImage.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
2015-09-21 15:31:01 +08:00
|
|
|
#include "2d/CCSprite.h"
|
2016-05-07 22:10:53 +08:00
|
|
|
#include "2d/CCRenderTexture.h"
|
2013-10-14 14:56:57 +08:00
|
|
|
|
2014-05-14 16:25:47 +08:00
|
|
|
NS_CC_BEGIN
|
2013-10-14 14:56:57 +08:00
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
int ccNextPOT(int x)
|
2013-10-14 14:56:57 +08:00
|
|
|
{
|
|
|
|
x = x - 1;
|
|
|
|
x = x | (x >> 1);
|
|
|
|
x = x | (x >> 2);
|
|
|
|
x = x | (x >> 4);
|
|
|
|
x = x | (x >> 8);
|
|
|
|
x = x | (x >>16);
|
|
|
|
return x + 1;
|
|
|
|
}
|
2014-05-14 16:25:47 +08:00
|
|
|
|
2014-05-27 14:29:52 +08:00
|
|
|
namespace utils
|
2014-05-27 10:44:23 +08:00
|
|
|
{
|
2014-05-27 14:29:52 +08:00
|
|
|
/**
|
2015-05-18 01:48:49 +08:00
|
|
|
* Capture screen implementation, don't use it directly.
|
|
|
|
*/
|
2014-05-27 10:44:23 +08:00
|
|
|
void onCaptureScreen(const std::function<void(bool, const std::string&)>& afterCaptured, const std::string& filename)
|
|
|
|
{
|
2015-05-18 01:48:49 +08:00
|
|
|
static bool startedCapture = false;
|
|
|
|
|
|
|
|
if (startedCapture)
|
|
|
|
{
|
|
|
|
CCLOG("Screen capture is already working");
|
|
|
|
if (afterCaptured)
|
|
|
|
{
|
|
|
|
afterCaptured(false, filename);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
startedCapture = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-27 10:44:23 +08:00
|
|
|
auto glView = Director::getInstance()->getOpenGLView();
|
|
|
|
auto frameSize = glView->getFrameSize();
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
|
|
|
|
frameSize = frameSize * glView->getFrameZoomFactor() * glView->getRetinaFactor();
|
|
|
|
#endif
|
2015-05-18 01:48:49 +08:00
|
|
|
|
2014-05-27 10:44:23 +08:00
|
|
|
int width = static_cast<int>(frameSize.width);
|
|
|
|
int height = static_cast<int>(frameSize.height);
|
2015-05-18 01:48:49 +08:00
|
|
|
|
2014-05-27 10:44:23 +08:00
|
|
|
bool succeed = false;
|
|
|
|
std::string outputFile = "";
|
2015-05-18 01:48:49 +08:00
|
|
|
|
2014-05-27 10:44:23 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
std::shared_ptr<GLubyte> buffer(new GLubyte[width * height * 4], [](GLubyte* p){ CC_SAFE_DELETE_ARRAY(p); });
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2015-05-18 01:48:49 +08:00
|
|
|
|
2014-05-27 10:44:23 +08:00
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT, 1);
|
|
|
|
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer.get());
|
2015-05-18 01:48:49 +08:00
|
|
|
|
2014-05-27 10:44:23 +08:00
|
|
|
std::shared_ptr<GLubyte> flippedBuffer(new GLubyte[width * height * 4], [](GLubyte* p) { CC_SAFE_DELETE_ARRAY(p); });
|
|
|
|
if (!flippedBuffer)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-07-01 14:48:43 +08:00
|
|
|
|
2014-05-27 10:44:23 +08:00
|
|
|
for (int row = 0; row < height; ++row)
|
|
|
|
{
|
|
|
|
memcpy(flippedBuffer.get() + (height - row - 1) * width * 4, buffer.get() + row * width * 4, width * 4);
|
|
|
|
}
|
2014-07-01 14:48:43 +08:00
|
|
|
|
2015-05-18 01:48:49 +08:00
|
|
|
Image* image = new (std::nothrow) Image;
|
2014-05-27 10:44:23 +08:00
|
|
|
if (image)
|
|
|
|
{
|
|
|
|
image->initWithRawData(flippedBuffer.get(), width * height * 4, width, height, 8);
|
|
|
|
if (FileUtils::getInstance()->isAbsolutePath(filename))
|
|
|
|
{
|
|
|
|
outputFile = filename;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCASSERT(filename.find("/") == std::string::npos, "The existence of a relative path is not guaranteed!");
|
|
|
|
outputFile = FileUtils::getInstance()->getWritablePath() + filename;
|
|
|
|
}
|
2015-05-18 01:48:49 +08:00
|
|
|
|
|
|
|
// Save image in AsyncTaskPool::TaskType::TASK_IO thread, and call afterCaptured in mainThread
|
|
|
|
static bool succeedSaveToFile = false;
|
2016-11-16 09:48:37 +08:00
|
|
|
std::function<void(void*)> mainThread = [afterCaptured, outputFile](void* /*param*/)
|
2015-05-18 01:48:49 +08:00
|
|
|
{
|
|
|
|
if (afterCaptured)
|
|
|
|
{
|
|
|
|
afterCaptured(succeedSaveToFile, outputFile);
|
|
|
|
}
|
|
|
|
startedCapture = false;
|
|
|
|
};
|
|
|
|
|
2017-05-08 13:39:17 +08:00
|
|
|
AsyncTaskPool::getInstance()->enqueue(AsyncTaskPool::TaskType::TASK_IO, std::move(mainThread), nullptr, [image, outputFile]()
|
2015-05-18 01:48:49 +08:00
|
|
|
{
|
|
|
|
succeedSaveToFile = image->saveToFile(outputFile);
|
|
|
|
delete image;
|
|
|
|
});
|
2014-05-27 10:44:23 +08:00
|
|
|
}
|
2015-07-10 11:21:03 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("Malloc Image memory failed!");
|
|
|
|
if (afterCaptured)
|
|
|
|
{
|
|
|
|
afterCaptured(succeed, outputFile);
|
|
|
|
}
|
|
|
|
startedCapture = false;
|
2014-05-27 10:44:23 +08:00
|
|
|
}
|
2015-05-18 01:48:49 +08:00
|
|
|
} while (0);
|
2014-05-27 10:44:23 +08:00
|
|
|
}
|
2015-05-18 01:48:49 +08:00
|
|
|
|
2014-05-27 14:29:52 +08:00
|
|
|
/*
|
|
|
|
* Capture screen interface
|
|
|
|
*/
|
2015-07-15 12:09:08 +08:00
|
|
|
static EventListenerCustom* s_captureScreenListener;
|
|
|
|
static CustomCommand s_captureScreenCommand;
|
2014-05-27 14:29:52 +08:00
|
|
|
void captureScreen(const std::function<void(bool, const std::string&)>& afterCaptured, const std::string& filename)
|
|
|
|
{
|
2015-07-15 12:09:08 +08:00
|
|
|
if (s_captureScreenListener)
|
|
|
|
{
|
2015-09-22 16:08:23 +08:00
|
|
|
CCLOG("Warning: CaptureScreen has been called already, don't call more than once in one frame.");
|
2015-07-15 12:09:08 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
s_captureScreenCommand.init(std::numeric_limits<float>::max());
|
|
|
|
s_captureScreenCommand.func = std::bind(onCaptureScreen, afterCaptured, filename);
|
2016-11-16 09:48:37 +08:00
|
|
|
s_captureScreenListener = Director::getInstance()->getEventDispatcher()->addCustomEventListener(Director::EVENT_AFTER_DRAW, [](EventCustom* /*event*/) {
|
2015-07-15 12:09:08 +08:00
|
|
|
auto director = Director::getInstance();
|
|
|
|
director->getEventDispatcher()->removeEventListener((EventListener*)(s_captureScreenListener));
|
|
|
|
s_captureScreenListener = nullptr;
|
|
|
|
director->getRenderer()->addCommand(&s_captureScreenCommand);
|
|
|
|
director->getRenderer()->render();
|
|
|
|
});
|
2014-05-27 14:29:52 +08:00
|
|
|
}
|
2015-07-15 12:09:08 +08:00
|
|
|
|
2016-05-07 22:10:53 +08:00
|
|
|
Image* captureNode(Node* startNode, float scale)
|
|
|
|
{ // The best snapshot API, support Scene and any Node
|
|
|
|
auto& size = startNode->getContentSize();
|
|
|
|
|
|
|
|
Director::getInstance()->setNextDeltaTimeZero(true);
|
|
|
|
|
|
|
|
RenderTexture* finalRtx = nullptr;
|
|
|
|
|
|
|
|
auto rtx = RenderTexture::create(size.width, size.height, Texture2D::PixelFormat::RGBA8888, GL_DEPTH24_STENCIL8);
|
|
|
|
// rtx->setKeepMatrix(true);
|
|
|
|
Point savedPos = startNode->getPosition();
|
|
|
|
Point anchor;
|
|
|
|
if (!startNode->isIgnoreAnchorPointForPosition()) {
|
|
|
|
anchor = startNode->getAnchorPoint();
|
|
|
|
}
|
|
|
|
startNode->setPosition(Point(size.width * anchor.x, size.height * anchor.y));
|
|
|
|
rtx->begin();
|
|
|
|
startNode->visit();
|
|
|
|
rtx->end();
|
|
|
|
startNode->setPosition(savedPos);
|
|
|
|
|
2016-07-28 05:49:41 +08:00
|
|
|
if (std::abs(scale - 1.0f) < 1e-6f/* no scale */)
|
2016-05-07 22:10:53 +08:00
|
|
|
finalRtx = rtx;
|
|
|
|
else {
|
|
|
|
/* scale */
|
|
|
|
auto finalRect = Rect(0, 0, size.width, size.height);
|
|
|
|
Sprite *sprite = Sprite::createWithTexture(rtx->getSprite()->getTexture(), finalRect);
|
|
|
|
sprite->setAnchorPoint(Point(0, 0));
|
|
|
|
sprite->setFlippedY(true);
|
|
|
|
|
|
|
|
finalRtx = RenderTexture::create(size.width * scale, size.height * scale, Texture2D::PixelFormat::RGBA8888, GL_DEPTH24_STENCIL8);
|
|
|
|
|
|
|
|
sprite->setScale(scale); // or use finalRtx->setKeepMatrix(true);
|
|
|
|
finalRtx->begin();
|
|
|
|
sprite->visit();
|
|
|
|
finalRtx->end();
|
|
|
|
}
|
|
|
|
|
|
|
|
Director::getInstance()->getRenderer()->render();
|
|
|
|
|
|
|
|
return finalRtx->newImage();
|
|
|
|
}
|
|
|
|
|
2014-06-26 14:05:30 +08:00
|
|
|
std::vector<Node*> findChildren(const Node &node, const std::string &name)
|
|
|
|
{
|
|
|
|
std::vector<Node*> vec;
|
|
|
|
|
|
|
|
node.enumerateChildren(name, [&vec](Node* nodeFound) -> bool {
|
|
|
|
vec.push_back(nodeFound);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return vec;
|
|
|
|
}
|
2014-07-14 20:42:08 +08:00
|
|
|
|
|
|
|
#define MAX_ITOA_BUFFER_SIZE 256
|
|
|
|
double atof(const char* str)
|
|
|
|
{
|
|
|
|
if (str == nullptr)
|
|
|
|
{
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf[MAX_ITOA_BUFFER_SIZE];
|
|
|
|
strncpy(buf, str, MAX_ITOA_BUFFER_SIZE);
|
|
|
|
|
|
|
|
// strip string, only remain 7 numbers after '.'
|
|
|
|
char* dot = strchr(buf, '.');
|
|
|
|
if (dot != nullptr && dot - buf + 8 < MAX_ITOA_BUFFER_SIZE)
|
|
|
|
{
|
|
|
|
dot[8] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return ::atof(buf);
|
|
|
|
}
|
2014-08-04 11:45:50 +08:00
|
|
|
|
|
|
|
double gettime()
|
|
|
|
{
|
2014-08-04 11:48:23 +08:00
|
|
|
struct timeval tv;
|
2014-08-04 15:18:01 +08:00
|
|
|
gettimeofday(&tv, nullptr);
|
2014-08-04 11:45:50 +08:00
|
|
|
|
2014-08-04 11:48:23 +08:00
|
|
|
return (double)tv.tv_sec + (double)tv.tv_usec/1000000;
|
2014-08-04 11:45:50 +08:00
|
|
|
}
|
2014-08-04 15:18:01 +08:00
|
|
|
|
2015-06-29 11:52:53 +08:00
|
|
|
long long getTimeInMilliseconds()
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
2015-07-06 14:34:32 +08:00
|
|
|
gettimeofday (&tv, nullptr);
|
2016-07-01 14:32:43 +08:00
|
|
|
return (long long)tv.tv_sec * 1000 + tv.tv_usec / 1000;
|
2015-06-29 11:52:53 +08:00
|
|
|
}
|
|
|
|
|
2014-09-18 15:27:11 +08:00
|
|
|
Rect getCascadeBoundingBox(Node *node)
|
|
|
|
{
|
|
|
|
Rect cbb;
|
|
|
|
Size contentSize = node->getContentSize();
|
|
|
|
|
2015-09-22 16:08:23 +08:00
|
|
|
// check all children bounding box, get maximize box
|
2014-09-18 15:27:11 +08:00
|
|
|
Node* child = nullptr;
|
|
|
|
bool merge = false;
|
|
|
|
for(auto object : node->getChildren())
|
|
|
|
{
|
|
|
|
child = dynamic_cast<Node*>(object);
|
|
|
|
if (!child->isVisible()) continue;
|
|
|
|
|
2014-09-18 15:40:28 +08:00
|
|
|
const Rect box = getCascadeBoundingBox(child);
|
2014-09-18 15:27:11 +08:00
|
|
|
if (box.size.width <= 0 || box.size.height <= 0) continue;
|
|
|
|
|
|
|
|
if (!merge)
|
|
|
|
{
|
|
|
|
cbb = box;
|
|
|
|
merge = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cbb.merge(box);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// merge content size
|
|
|
|
if (contentSize.width > 0 && contentSize.height > 0)
|
|
|
|
{
|
|
|
|
const Rect box = RectApplyAffineTransform(Rect(0, 0, contentSize.width, contentSize.height), node->getNodeToWorldAffineTransform());
|
|
|
|
if (!merge)
|
|
|
|
{
|
|
|
|
cbb = box;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cbb.merge(box);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cbb;
|
|
|
|
}
|
2015-09-21 15:31:01 +08:00
|
|
|
|
2016-06-06 22:32:50 +08:00
|
|
|
Sprite* createSpriteFromBase64Cached(const char* base64String, const char* key)
|
|
|
|
{
|
|
|
|
Texture2D* texture = Director::getInstance()->getTextureCache()->getTextureForKey(key);
|
|
|
|
|
|
|
|
if (texture == nullptr)
|
|
|
|
{
|
|
|
|
unsigned char* decoded;
|
|
|
|
int length = base64Decode((const unsigned char*)base64String, (unsigned int)strlen(base64String), &decoded);
|
|
|
|
|
|
|
|
Image *image = new (std::nothrow) Image();
|
|
|
|
bool imageResult = image->initWithImageData(decoded, length);
|
|
|
|
CCASSERT(imageResult, "Failed to create image from base64!");
|
|
|
|
free(decoded);
|
|
|
|
|
2016-06-17 13:13:35 +08:00
|
|
|
if (!imageResult) {
|
|
|
|
CC_SAFE_RELEASE_NULL(image);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-06-06 22:32:50 +08:00
|
|
|
texture = Director::getInstance()->getTextureCache()->addImage(image, key);
|
|
|
|
image->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite* sprite = Sprite::createWithTexture(texture);
|
|
|
|
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
|
2015-09-21 15:31:01 +08:00
|
|
|
Sprite* createSpriteFromBase64(const char* base64String)
|
|
|
|
{
|
|
|
|
unsigned char* decoded;
|
2016-06-06 22:32:50 +08:00
|
|
|
int length = base64Decode((const unsigned char*)base64String, (unsigned int)strlen(base64String), &decoded);
|
2015-09-21 15:31:01 +08:00
|
|
|
|
2015-12-16 14:02:55 +08:00
|
|
|
Image *image = new (std::nothrow) Image();
|
2015-09-21 15:31:01 +08:00
|
|
|
bool imageResult = image->initWithImageData(decoded, length);
|
|
|
|
CCASSERT(imageResult, "Failed to create image from base64!");
|
|
|
|
free(decoded);
|
|
|
|
|
2016-06-17 13:13:35 +08:00
|
|
|
if (!imageResult) {
|
|
|
|
CC_SAFE_RELEASE_NULL(image);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-12-16 14:02:55 +08:00
|
|
|
Texture2D *texture = new (std::nothrow) Texture2D();
|
2015-09-21 15:31:01 +08:00
|
|
|
texture->initWithImage(image);
|
|
|
|
texture->setAliasTexParameters();
|
|
|
|
image->release();
|
|
|
|
|
|
|
|
Sprite* sprite = Sprite::createWithTexture(texture);
|
|
|
|
texture->release();
|
2016-06-06 22:32:50 +08:00
|
|
|
|
2015-09-21 15:31:01 +08:00
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
|
2016-07-14 13:52:30 +08:00
|
|
|
Node* findChild(Node* levelRoot, const std::string& name)
|
2016-05-04 09:46:00 +08:00
|
|
|
{
|
2016-07-14 13:52:30 +08:00
|
|
|
if (levelRoot == nullptr || name.empty())
|
2016-05-04 09:46:00 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Find this node
|
2016-07-14 13:52:30 +08:00
|
|
|
auto target = levelRoot->getChildByName(name);
|
|
|
|
if (target != nullptr)
|
|
|
|
return target;
|
2016-05-04 09:46:00 +08:00
|
|
|
|
|
|
|
// Find recursively
|
|
|
|
for (auto& child : levelRoot->getChildren())
|
|
|
|
{
|
2016-07-14 13:52:30 +08:00
|
|
|
target = findChild(child, name);
|
2016-05-04 09:46:00 +08:00
|
|
|
if (target != nullptr)
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* findChild(Node* levelRoot, int tag)
|
|
|
|
{
|
2016-07-14 13:52:30 +08:00
|
|
|
if (levelRoot == nullptr || tag == Node::INVALID_TAG)
|
2016-05-04 09:46:00 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Find this node
|
2016-07-14 13:52:30 +08:00
|
|
|
auto target = levelRoot->getChildByTag(tag);
|
|
|
|
if (target != nullptr)
|
|
|
|
return target;
|
2016-05-04 09:46:00 +08:00
|
|
|
|
|
|
|
// Find recursively
|
|
|
|
for (auto& child : levelRoot->getChildren())
|
|
|
|
{
|
2016-07-14 13:52:30 +08:00
|
|
|
target = findChild(child, tag);
|
2016-05-04 09:46:00 +08:00
|
|
|
if (target != nullptr)
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-03-16 13:47:45 +08:00
|
|
|
std::string getFileMD5Hash(const std::string &filename)
|
2017-05-04 09:21:22 +08:00
|
|
|
{
|
|
|
|
Data data;
|
|
|
|
FileUtils::getInstance()->getContents(filename, &data);
|
|
|
|
|
|
|
|
return getDataMD5Hash(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getDataMD5Hash(const Data &data)
|
2017-03-16 13:47:45 +08:00
|
|
|
{
|
2017-04-01 11:43:17 +08:00
|
|
|
static const unsigned int MD5_DIGEST_LENGTH = 16;
|
|
|
|
|
2017-05-04 09:21:22 +08:00
|
|
|
if (data.isNull())
|
|
|
|
{
|
|
|
|
return std::string();
|
|
|
|
}
|
2017-03-16 13:47:45 +08:00
|
|
|
|
2017-04-01 11:43:17 +08:00
|
|
|
md5_state_t state;
|
|
|
|
md5_byte_t digest[MD5_DIGEST_LENGTH];
|
2017-05-04 09:21:22 +08:00
|
|
|
char hexOutput[(MD5_DIGEST_LENGTH << 1) + 1] = { 0 };
|
2017-04-01 11:43:17 +08:00
|
|
|
|
|
|
|
md5_init(&state);
|
2017-05-04 09:21:22 +08:00
|
|
|
md5_append(&state, (const md5_byte_t *)data.getBytes(), (int)data.getSize());
|
2017-04-01 11:43:17 +08:00
|
|
|
md5_finish(&state, digest);
|
|
|
|
|
|
|
|
for (int di = 0; di < 16; ++di)
|
|
|
|
sprintf(hexOutput + di * 2, "%02x", digest[di]);
|
2017-03-16 13:47:45 +08:00
|
|
|
|
2017-04-01 11:43:17 +08:00
|
|
|
return hexOutput;
|
2017-03-16 13:47:45 +08:00
|
|
|
}
|
|
|
|
|
2014-05-27 10:44:23 +08:00
|
|
|
}
|
|
|
|
|
2014-05-26 16:15:30 +08:00
|
|
|
NS_CC_END
|