2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2012-04-25 16:18:04 +08:00
|
|
|
#define __CC_PLATFORM_FILEUTILS_CPP__
|
2012-06-19 17:22:55 +08:00
|
|
|
#include "platform/CCFileUtilsCommon_cpp.h"
|
2012-10-01 17:55:21 +08:00
|
|
|
#include <Shlobj.h>
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCDirector.h"
|
2012-09-05 14:38:39 +08:00
|
|
|
#include "CCApplication.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
// record the resource path
|
|
|
|
static char s_pszResourcePath[MAX_PATH] = {0};
|
|
|
|
|
|
|
|
static void _CheckPath()
|
|
|
|
{
|
|
|
|
if (! s_pszResourcePath[0])
|
|
|
|
{
|
2012-08-24 18:01:16 +08:00
|
|
|
WCHAR wszPath[MAX_PATH] = {0};
|
2012-10-01 17:55:21 +08:00
|
|
|
int nNum = WideCharToMultiByte(CP_ACP, 0, wszPath,
|
|
|
|
GetCurrentDirectoryW(sizeof(wszPath), wszPath),
|
2012-04-19 14:35:52 +08:00
|
|
|
s_pszResourcePath, MAX_PATH, NULL, NULL);
|
2012-10-01 17:55:21 +08:00
|
|
|
s_pszResourcePath[nNum] = '\\';
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-08 16:22:57 +08:00
|
|
|
static CCFileUtils* s_pFileUtils = NULL;
|
|
|
|
|
|
|
|
CCFileUtils* CCFileUtils::sharedFileUtils()
|
|
|
|
{
|
|
|
|
if (s_pFileUtils == NULL)
|
|
|
|
{
|
|
|
|
s_pFileUtils = new CCFileUtils();
|
2013-01-24 13:56:50 +08:00
|
|
|
s_pFileUtils->init();
|
2012-08-24 18:01:16 +08:00
|
|
|
_CheckPath();
|
2012-06-08 16:22:57 +08:00
|
|
|
}
|
|
|
|
return s_pFileUtils;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCFileUtils::purgeFileUtils()
|
|
|
|
{
|
2012-06-11 10:59:57 +08:00
|
|
|
if (s_pFileUtils != NULL)
|
|
|
|
{
|
|
|
|
s_pFileUtils->purgeCachedEntries();
|
2013-01-24 13:56:50 +08:00
|
|
|
CC_SAFE_RELEASE(s_pFileUtils->m_pFilenameLookupDict);
|
|
|
|
CC_SAFE_RELEASE(s_pFileUtils->m_pSearchPathArray);
|
|
|
|
CC_SAFE_RELEASE(s_pFileUtils->m_pSearchResolutionsOrderArray);
|
2012-06-11 10:59:57 +08:00
|
|
|
}
|
2012-10-01 17:55:21 +08:00
|
|
|
|
2012-06-08 16:22:57 +08:00
|
|
|
CC_SAFE_DELETE(s_pFileUtils);
|
|
|
|
}
|
|
|
|
|
2012-06-11 10:59:57 +08:00
|
|
|
void CCFileUtils::purgeCachedEntries()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-01-24 13:56:50 +08:00
|
|
|
bool CCFileUtils::init()
|
|
|
|
{
|
|
|
|
m_pSearchPathArray = new CCArray();
|
|
|
|
m_pSearchPathArray->addObject(CCString::create(""));
|
|
|
|
|
|
|
|
m_pSearchResolutionsOrderArray = new CCArray();
|
|
|
|
m_pSearchResolutionsOrderArray->addObject(CCString::create(""));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-08 18:39:33 +08:00
|
|
|
const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
|
2013-01-18 21:54:48 +08:00
|
|
|
{
|
2013-01-25 22:30:16 +08:00
|
|
|
return CCString::create(fullPathForFilename(pszRelativePath))->getCString();
|
2013-01-18 21:54:48 +08:00
|
|
|
}
|
|
|
|
|
2013-01-24 13:56:50 +08:00
|
|
|
std::string CCFileUtils::getPathForFilename(const std::string& filename, const std::string& resourceDirectory, const std::string& searchPath)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
const std::string& resourceRootPath = CCApplication::sharedApplication()->getResourceRootPath();
|
|
|
|
|
|
|
|
if (filename.length() > 0
|
|
|
|
&& ('/' == filename[0] || '\\' == filename[0]))
|
|
|
|
{
|
|
|
|
// path start with '/' or '\', is absolute path without driver name
|
|
|
|
char szDriver[3] = {s_pszResourcePath[0], s_pszResourcePath[1], 0};
|
|
|
|
ret = szDriver;
|
|
|
|
ret += "/";
|
2013-01-18 21:54:48 +08:00
|
|
|
}
|
2013-01-24 13:56:50 +08:00
|
|
|
else if (resourceRootPath.length() > 0)
|
|
|
|
{
|
|
|
|
ret = resourceRootPath;
|
|
|
|
if (ret[ret.length()-1] != '\\' && ret[ret.length()-1] != '/')
|
|
|
|
{
|
|
|
|
ret += "/";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = s_pszResourcePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string file = filename;
|
|
|
|
std::string file_path = "";
|
|
|
|
size_t pos = filename.find_last_of("/");
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
file_path = filename.substr(0, pos+1);
|
|
|
|
file = filename.substr(pos+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// searchPath + file_path + resourceDirectory
|
|
|
|
std::string path = searchPath;
|
|
|
|
if (path.size() > 0 && path[path.length()-1] != '/')
|
|
|
|
{
|
|
|
|
path += "/";
|
|
|
|
}
|
|
|
|
path += file_path;
|
|
|
|
path += resourceDirectory;
|
|
|
|
|
|
|
|
if (path.size() > 0 && path[path.length()-1] != '/')
|
|
|
|
{
|
|
|
|
path += "/";
|
|
|
|
}
|
|
|
|
path += file;
|
|
|
|
ret += path;
|
|
|
|
|
|
|
|
return ret;
|
2013-01-18 21:54:48 +08:00
|
|
|
}
|
|
|
|
|
2013-01-25 22:30:16 +08:00
|
|
|
std::string CCFileUtils::fullPathForFilename(const char* pszFileName)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-01-24 13:56:50 +08:00
|
|
|
bool bFound = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-01-24 13:56:50 +08:00
|
|
|
std::string newFileName = getNewFilename(pszFileName);
|
|
|
|
std::string fullpath;
|
2012-08-24 18:01:16 +08:00
|
|
|
|
2013-01-24 13:56:50 +08:00
|
|
|
do
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-01-24 13:56:50 +08:00
|
|
|
if ((newFileName.length() > 1 && newFileName[1] == ':'))
|
2012-08-24 18:01:16 +08:00
|
|
|
{
|
2013-01-25 22:30:16 +08:00
|
|
|
// path start with "x:", is absolute path, return directly
|
|
|
|
return newFileName;
|
2012-08-24 18:01:16 +08:00
|
|
|
}
|
2013-01-24 13:56:50 +08:00
|
|
|
|
|
|
|
CCObject* pSearchObj = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pSearchPathArray, pSearchObj)
|
|
|
|
{
|
|
|
|
CCString* pSearchPath = (CCString*)pSearchObj;
|
|
|
|
|
|
|
|
CCObject* pResourceDirObj = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pSearchResolutionsOrderArray, pResourceDirObj)
|
|
|
|
{
|
|
|
|
CCString* pResourceDirectory = (CCString*)pResourceDirObj;
|
|
|
|
// Search in subdirectories
|
|
|
|
fullpath = this->getPathForFilename(newFileName, pResourceDirectory->getCString(), pSearchPath->getCString());
|
|
|
|
|
|
|
|
if (GetFileAttributesA(fullpath.c_str()) != -1)
|
|
|
|
{
|
|
|
|
bFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bFound)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}while(false);
|
2012-10-01 17:55:21 +08:00
|
|
|
|
2013-01-25 22:30:16 +08:00
|
|
|
return bFound ? fullpath : newFileName;
|
2013-01-18 21:54:48 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
const char *CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const char *pszRelativeFile)
|
|
|
|
{
|
|
|
|
std::string relativeFile = pszRelativeFile;
|
2012-08-24 18:01:16 +08:00
|
|
|
CCString *pRet = CCString::create("");
|
2012-04-19 14:35:52 +08:00
|
|
|
pRet->m_sString = relativeFile.substr(0, relativeFile.find_last_of("/\\") + 1);
|
2013-01-24 13:56:50 +08:00
|
|
|
pRet->m_sString += getNewFilename(pszFilename);
|
2012-04-19 14:35:52 +08:00
|
|
|
return pRet->m_sString.c_str();
|
|
|
|
}
|
|
|
|
|
2012-06-19 16:31:26 +08:00
|
|
|
unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long* pSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-19 16:31:26 +08:00
|
|
|
unsigned char* pBuffer = NULL;
|
|
|
|
CCAssert(pszFileName != NULL && pSize != NULL && pszMode != NULL, "Invaild parameters.");
|
|
|
|
*pSize = 0;
|
2012-10-01 17:55:21 +08:00
|
|
|
do
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// read the file from hardware
|
|
|
|
FILE *fp = fopen(pszFileName, pszMode);
|
|
|
|
CC_BREAK_IF(!fp);
|
|
|
|
|
|
|
|
fseek(fp,0,SEEK_END);
|
|
|
|
*pSize = ftell(fp);
|
|
|
|
fseek(fp,0,SEEK_SET);
|
|
|
|
pBuffer = new unsigned char[*pSize];
|
|
|
|
*pSize = fread(pBuffer,sizeof(unsigned char), *pSize,fp);
|
|
|
|
fclose(fp);
|
|
|
|
} while (0);
|
|
|
|
|
2012-06-15 16:47:30 +08:00
|
|
|
if (! pBuffer && isPopupNotify())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
std::string title = "Notification";
|
|
|
|
std::string msg = "Get data from file(";
|
|
|
|
msg.append(pszFileName).append(") failed!");
|
|
|
|
|
|
|
|
CCMessageBox(msg.c_str(), title.c_str());
|
|
|
|
}
|
|
|
|
return pBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
string CCFileUtils::getWriteablePath()
|
|
|
|
{
|
2012-10-01 17:55:21 +08:00
|
|
|
// Get full path of executable, e.g. c:\Program Files (x86)\My Game Folder\MyGame.exe
|
|
|
|
char full_path[_MAX_PATH + 1];
|
|
|
|
::GetModuleFileNameA(NULL, full_path, _MAX_PATH + 1);
|
|
|
|
|
2012-10-08 23:22:27 +08:00
|
|
|
// Debug app uses executable directory; Non-debug app uses local app data directory
|
2012-10-17 15:08:25 +08:00
|
|
|
#ifndef _DEBUG
|
2012-10-08 23:22:27 +08:00
|
|
|
// Get filename of executable only, e.g. MyGame.exe
|
|
|
|
char *base_name = strrchr(full_path, '\\');
|
2012-10-01 17:55:21 +08:00
|
|
|
|
2012-10-08 23:22:27 +08:00
|
|
|
if(base_name)
|
2012-10-01 17:55:21 +08:00
|
|
|
{
|
2012-10-08 23:22:27 +08:00
|
|
|
char app_data_path[_MAX_PATH + 1];
|
|
|
|
|
|
|
|
// Get local app data directory, e.g. C:\Documents and Settings\username\Local Settings\Application Data
|
|
|
|
if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, app_data_path)))
|
|
|
|
{
|
|
|
|
string ret((char*)app_data_path);
|
2012-10-01 17:55:21 +08:00
|
|
|
|
2012-10-08 23:22:27 +08:00
|
|
|
// Adding executable filename, e.g. C:\Documents and Settings\username\Local Settings\Application Data\MyGame.exe
|
|
|
|
ret += base_name;
|
2012-10-01 17:55:21 +08:00
|
|
|
|
2012-10-08 23:22:27 +08:00
|
|
|
// Remove ".exe" extension, e.g. C:\Documents and Settings\username\Local Settings\Application Data\MyGame
|
|
|
|
ret = ret.substr(0, ret.rfind("."));
|
2012-10-01 17:55:21 +08:00
|
|
|
|
2012-10-08 23:22:27 +08:00
|
|
|
ret += "\\";
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-10-08 23:22:27 +08:00
|
|
|
// Create directory
|
|
|
|
if (SUCCEEDED(SHCreateDirectoryExA(NULL, ret.c_str(), NULL)))
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2012-10-01 17:55:21 +08:00
|
|
|
}
|
|
|
|
}
|
2012-10-17 15:08:25 +08:00
|
|
|
#endif // not defined _DEBUG
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-10-01 17:55:21 +08:00
|
|
|
// If fetching of local app data directory fails, use the executable one
|
|
|
|
string ret((char*)full_path);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-10-01 17:55:21 +08:00
|
|
|
// remove xxx.exe
|
|
|
|
ret = ret.substr(0, ret.rfind("\\") + 1);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-10-01 17:55:21 +08:00
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|