Cocos2d-x 3.X resource and script decryption, cocos2d-x3.x

Source: Internet
Author: User

Cocos2d-x 3.X resource and script decryption, cocos2d-x3.x

You don't need to talk about encryption. You can refer to the 2.x encryption method in the previous article. Only the decryption rules can be guaranteed;

Now we will focus on 3. X decryption:

In the new 3. X engine, most of the methods for obtaining resources are officially integrated, and finally a getdata is merged;

You can see from the source code, and stack call:

CCFileUtils. cpp:

Data FileUtils::getDataFromFile(const std::string& filename){    return getData(filename, false);}

GetDataFromFile currently only calls getData (filename, false );

Data getData(const std::string& filename, bool forString)

This function is a non-class member static function.

ForString is used to identify whether a text file is used. If so, the buffer needs to have one more byte.

This is not important, because the final buffer we process is completely obtained.

So directly change the code:

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";        std::string lastname = FileUtils::getInstance()->fullPathForFilename(filename);    lastname = lastname.substr(lastname.length()-5,lastname.length());        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,0,SEEK_END);        size = ftell(fp);        fseek(fp,0,SEEK_SET);                if (forString)        {            buffer = (unsigned char*)malloc(sizeof(unsigned char) * (size + 1));            buffer[size] = '\0';        }        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] = '\0';        }    } 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    {        if(lastname == "_jm.d")        {            for (int i = 0; i<readsize; i++) {                buffer[i]=MD5(buffer[i]);            }            buffer[readsize]=buffer[readsize]-MD5size;        }        ret.fastSet(buffer, readsize);    }        return ret;}

The red code is our custom encrypted file decryption, No matter what encryption you use or modify the address scrambling code, as long as the encryption and decryption formats are the same;

OK. The decryption is complete, but you still need to determine the resource type. In 2.x, The EImageFormat judgment is processed and the resource type can be defined.

However, it is recommended that you do not change the source code. version 3.2 and later are very simple and powerful.

3. After Format is integrated in. X, another way of defining the resource type is also available: _ fileType = detectFormat (unpackedData, unpackedLen );

The decrypted resource type does not work in 3.x. Format: UNKOWN;

In this case, we can solve this problem by using the overload function:

Reload initWithImageData, in CCImage. cpp in CCImage. h

. H Add:

/* Jmflag encrypted ID */bool initWithImageData (const unsigned char * data, ssize_t dataLen, bool jmflag );

. Cpp Add:

bool Image::initWithImageData(const unsigned char * data, ssize_t dataLen,bool jmflag){    bool ret = false;        do    {        CC_BREAK_IF(! data || dataLen <= 0);                unsigned char* unpackedData = nullptr;        ssize_t unpackedLen = 0;                //detecgt and unzip the compress file        if (ZipUtils::isCCZBuffer(data, dataLen))        {            unpackedLen = ZipUtils::inflateCCZBuffer(data, dataLen, &unpackedData);        }        else if (ZipUtils::isGZipBuffer(data, dataLen))        {            unpackedLen = ZipUtils::inflateMemory(const_cast<unsigned char*>(data), dataLen, &unpackedData);        }        else        {            unpackedData = const_cast<unsigned char*>(data);            unpackedLen = dataLen;        }                if(jmflag == true)        {            _fileType=Format::PNG;        }                ret = initWithPngData(unpackedData, unpackedLen);                if(unpackedData != data)        {            free(unpackedData);        }    } while (0);        return ret;}

In addition, modify the call method in the initWithImageFile function.

CCImage. cpp:

Data data = FileUtils::getInstance()->getDataFromFile(_filePath);    if (!data.isNull())    {        std::string lastname = _filePath;        lastname = lastname.substr(lastname.length()-5,lastname.length());        if(lastname=="_jm.d")        {            ret = initWithImageData(data.getBytes(), data.getSize(),true);        }        else        {            ret = initWithImageData(data.getBytes(), data.getSize());        }    }

Okay, you can decrypt the custom type of resources, but this only processes the encryption of the PNG Image type. Other image types can be processed in this way.

Well, some people ask, what about the decryption of JS and Lua scripts?

Here

ScirptingCore. cpp:

  // Check whether '.jsc' files exist to avoid outputing log which says 'couldn't find .jsc file'.    CCLOG("byteCodePath > %s",byteCodePath.c_str());    if (futil->isFileExist(byteCodePath))    {        Data data = futil->getDataFromFile(byteCodePath);        if (!data.isNull())        {            script = JS_DecodeScript(cx, data.getBytes(), static_cast<uint32_t>(data.getSize()), nullptr, nullptr);        }    }

Data data = futil-> getDataFromFile (byteCodePath );

For the loading and reading of the script language, we have modified the getDataFromFile method!

That means we don't have to deal with it. We just need to consider encryption.

 

PS ad time:

My game Official Website: www.tapheros.com

I am currently recording cocos2dx-3.X series of practical project video, can be completely said to be a commercial project video to explain, let you fall in love with more interesting game development;

Our actual video will be available in 9 seconds!

Can let everyone more quickly grasp the development of cocos2dx-Js game practice! Now playing games with scripts has become a big trend;

In addition, today's mobile game industry is developing rapidly. If you don't need a quick scripting language to develop games, you won't be Out.

In addition, we hope everyone can support our game development group: [41131516]

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.