您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-x项目移植到WP8系列之四:文件操作

2014-09-15 11:18 274 查看
原文链接: /article/5080067.html

读写文件Cocos已经用fopen fwrite来做好了,这里说的主要是文件和文件夹的创建、删除、判断是否存在等。

本来打算把把这部分代码放到C#工程来做,然后通过上一篇说到的C++和C#交互的那个通道来调用的,但是wp8里很多东西都被做成异步的形式了,文件的读写操作也被设计成了异步的形式,但是在C++这边发起调用的方法是需要同步调用的,这里如何转换是一个难题,由于对task PPL 那一套不熟悉,最后没办法只能放到C++工程来做。

另外,在task里那个捕获异常也是很痛苦的一件事情——一直没研究透彻task,所以代码实现得比较挫

bool WZFileUtilWP8::makeDirectory(const std::string& dirPath)
{

auto localFolder = Windows::Storage::ApplicationData::Current->LocalFolder;
Platform::String ^pStr_FileName = ref new Platform::String(WZCallCS_And_Back_CommonFunc::stringToWString(dirPath.c_str()).c_str());

create_task(localFolder->CreateFolderAsync(pStr_FileName, CreationCollisionOption::ReplaceExisting)).wait();

CCLog("craete folder: %s success\n", dirPath.c_str());
return true;

}

bool WZFileUtilWP8::removeDirectory(const std::string& dirName)
{
//return callCS->FunCallCS_RemoveDirectory(dirName.c_str());

//删除不存在的文件夹会抛出异常,但这异常在task里捕获真TM蛋疼
//现在通过先创建一个和要删除的文件夹同名的文件夹再删除来间接实现

auto localFolder = Windows::Storage::ApplicationData::Current->LocalFolder;
Platform::String ^pStr_dirName = ref new Platform::String(WZCallCS_And_Back_CommonFunc::stringToWString(dirName.c_str()).c_str());

this->makeDirectory(dirName);

create_task(localFolder->GetFolderAsync(pStr_dirName)).then([](StorageFolder^ folder)
{
folder->DeleteAsync();
}).wait();

CCLog("remove folder: %s success\n", dirName.c_str());
return true;
}

bool WZFileUtilWP8::isFullPathExist(const std::string &fullPath)
{
auto localFolder = Windows::Storage::ApplicationData::Current->LocalFolder;
Platform::String ^pStr_fulaPathName = ref new Platform::String(WZCallCS_And_Back_CommonFunc::stringToWString(fullPath.c_str()).c_str());

//还是异常捕获太蛋疼,直接创建吧,总是返回true
create_task(localFolder->CreateFileAsync(pStr_fulaPathName, CreationCollisionOption::OpenIfExists)).wait();

CCLog("file: %s is exist\n", fullPath.c_str());
return true;
}

//根据一个多级 目录 的字符串创建目录及文件
bool WZFileUtilWP8::makeFullPath(const std::string& fullTargetPath)
{

//wp8可以直接创建多级目录
//return callCS->FunCallCS_CreateFullPathFile(fullPath.c_str());

auto localFolder = Windows::Storage::ApplicationData::Current->LocalFolder;
Platform::String ^pStr_fullPathName = ref new Platform::String(WZCallCS_And_Back_CommonFunc::stringToWString(fullTargetPath.c_str()).c_str());
create_task(localFolder->CreateFileAsync(pStr_fullPathName, CreationCollisionOption::ReplaceExisting)).wait();

CCLog("create file: %s success\n", fullTargetPath.c_str());

return true;

}


原文链接: /article/5080067.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: