File API callback for crypting content

Hi,

Does the recent version of cocos2d-x provide the ability to have callbacks for the file API?

E.g. setting a function, which is called when loading content/assets, so the content can be decrypted/unpacked, before the content is used.

That way, the engine code does not need to be changed.

If not provided yet, is it on the ToDo list?

Thanks!

This is a good idea, I think. Perhaps you should make a GitHub issue out of it? :smile:

I digged through the API docs and found out, that it’s already supported:

static void setDelegate ( FileUtils * delegate )
static
You can inherit from platform dependent implementation of FileUtils, such as FileUtilsAndroid, and use this function to set delegate, then FileUtils will invoke delegate’s implementation.

Fox example, your resources are encrypted, so you need to decrypt it after reading data from resources, then you can implement all getXXX functions, and engine will invoke your own getXX functions when reading data of resources.

If you don’t want to system default implementation after setting delegate, you can just pass nullptr to this function.

Warning
It will delete previous delegate

The issue with this is, that a simple decryption hast to be implemented for every platform.

But isn’t that a typo?

If you don’t want to system default implementation after setting delegate, you can just pass nullptr to this function.

It should read:

If you want to use the system default implementation after setting delegate, you can just pass nullptr to this function.

Because it will create the platform specific instance, if the pointer is zero. E.g.: Apple:

FileUtils* FileUtils::getInstance()
{
    if (s_sharedFileUtils == nullptr)
    {
        s_sharedFileUtils = new (std::nothrow) FileUtilsApple();
        if(!s_sharedFileUtils->init())
        {
          delete s_sharedFileUtils;
          s_sharedFileUtils = nullptr;
          CCLOG("ERROR: Could not init CCFileUtilsApple");
        }
    }
    return s_sharedFileUtils;
}