FileManager......好多問題沒有解決......

#include "cocos2d.h"

#include <string>

class FileManeger
{
public:
    static std::string getFilePath(const std::string &aPath, bool aIsNewVersion);
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
    static bool exportOutData(const std::string &aPath);
#endif
};

std::string FileManeger::getFilePath(const std::string &aPath, bool aIsNewVersion)
{
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
     std::string fullPath = cocos2d::FileUtils::getInstance()->getWritablePath() + aPath;
    bool isExist = cocos2d::FileUtils::getInstance()->isFileExist(fullPath);
    if (!isExist || aIsNewVersion)
    {
        exportOutData(aPath);
    }
    return fullPath;
#else
    return cocos2d::FileUtils::getInstance()->fullPathForFilename(aPath);
#endif
}

#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
bool FileManeger::exportOutData(const std::string &aPath)
{
    auto datas = cocos2d::FileUtils::getInstance()->getDataFromFile(aPath);
    if (!datas.isNull())
    {
        std::string fullPath = cocos2d::FileUtils::getInstance()->getWritablePath() + aPath;
        std::string::size_type pos = fullPath.find_last_of("/");
        std::string::size_type len = fullPath.length();
        std::string folderPath = fullPath;
        if (std::string::npos != pos)
        {
            if ((len - 1) != pos)
            {
                folderPath = folderPath.substr(0, pos);
            }
        }
        else
        {
            assert(false);   //file path error
            return false;
        }

        if (cocos2d::FileUtils::getInstance()->createDirectory(folderPath))
        {
            FILE *fp = fopen(fullPath.c_str(), "w+");
            if (fp)
            {
                if(1 == fwrite(datas.getBytes(), sizeof(char), datas.getSize(), fp))
                {
                    fclose(fp);
                    return true;
                }
                fclose(fp);
            }
        }
    }

    return false;
}
#endif

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章