I have a bunch of plist files in the assets/plist/ folder and I am trying to load these files to validate their hashes.

what happens is that the following code fails for me

const char *fullPath = cocos2d::CCFileUtils::sharedFileUtils()->fullPathForFilename(name).c_str();
std::ifstream ifs(fullPath, std::ios::binary);
std::vector<char> str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());

The returned char array is always empty.

Trying to open the same file with fopen also results in a null pointer for the file handle.

I have verified that the full path is assets/plists/file.plist and that the file.plist exists in the assets/plist folder.

What am I doing wrong here?

有帮助吗?

解决方案 2

FileUtils->getInstance()->getFileData was the way for me to go as well for reading asset resources. I wrapped this in a utility function when reading text files:

    #include "cocos2d.h"
    #include <iosfwd>
    #include <sstream>
    #include <memory>

    namespace FileUtil
    {
        using ResourceStream = std::basic_istringstream<char>;

        bool readResourceFile(std::shared_ptr<ResourceStream>& stream,const std::string& filename);

        bool readResourceFile(std::shared_ptr<ResourceStream>& stream,const std::string& filename)
        {
            // Note: Returned data allocated by "malloc" so must free when copy to string stream

            CCLOG("FileUtil::readResourceFile - Attempting to read resource file %s",filename.c_str());

            ssize_t size = 0;
            char* data = reinterpret_cast<char*>(FileUtils::getInstance()->getFileData(filename, "r", &size));
            if(!data || size == 0)
            {
                CCLOG("FileUtil::readResourceFile - unable to read filename %s - size was %lu",filename.c_str(),size);
                if(data)
                {
                    free(data);
                }
                return false;
            }

            CCLOG("FileUtil::readResourceFile - Read %lu bytes from resource file %s",size,filename.c_str());

            std::string stringData(data);
            // release since we've copied to string
            free(data);

            stream.reset(new std::istringstream(stringData));

            return true;
        }
    }

其他提示

thanks borrrden for the reference. That question you referred to did not answer my question completely, but led me to this.

For those who stumble upon this question, the assets folder is zipped inside the APK and unlike iOS it is not possible to read files directly from there. For a consistent solution that would work for both iOS and Android as well as for folders within Assets and otherwise, the code below uses CCFileUtils within the cocos2d-x framework to read the file.

unsigned long pSize = 0;
unsigned char* str = CCFileUtils::sharedFileUtils()->getFileData(name, "rb", &pSize);
std::string hash = GCGameUtils::sharedInstance()->hmacForKeyAndData(str, name, pSize);
delete[] str;

The cocos2d-x fileutils has this cool function already to getFileData!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top