2013-02-01 11:20:46 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCFileUtilsWin32.h"
|
|
|
|
#include "platform/CCCommon.h"
|
|
|
|
#include <Shlobj.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
static char s_pszResourcePath[MAX_PATH] = {0};
|
|
|
|
|
|
|
|
static void _checkPath()
|
|
|
|
{
|
|
|
|
if (! s_pszResourcePath[0])
|
|
|
|
{
|
|
|
|
WCHAR wszPath[MAX_PATH] = {0};
|
|
|
|
int nNum = WideCharToMultiByte(CP_ACP, 0, wszPath,
|
|
|
|
GetCurrentDirectoryW(sizeof(wszPath), wszPath),
|
|
|
|
s_pszResourcePath, MAX_PATH, NULL, NULL);
|
|
|
|
s_pszResourcePath[nNum] = '\\';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-12 12:03:39 +08:00
|
|
|
FileUtils* FileUtils::getInstance()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
if (s_sharedFileUtils == NULL)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
s_sharedFileUtils = new FileUtilsWin32();
|
2013-07-23 20:44:08 +08:00
|
|
|
if(!s_sharedFileUtils->init())
|
|
|
|
{
|
|
|
|
delete s_sharedFileUtils;
|
|
|
|
s_sharedFileUtils = NULL;
|
|
|
|
CCLOG("ERROR: Could not init CCFileUtilsWin32");
|
|
|
|
}
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
return s_sharedFileUtils;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
FileUtilsWin32::FileUtilsWin32()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool FileUtilsWin32::init()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
_checkPath();
|
2013-06-15 14:03:30 +08:00
|
|
|
_defaultResRootPath = s_pszResourcePath;
|
2013-06-20 14:13:12 +08:00
|
|
|
return FileUtils::init();
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool FileUtilsWin32::isFileExist(const std::string& strFilePath)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-04-25 21:51:13 +08:00
|
|
|
if (0 == strFilePath.length())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-01 22:19:58 +08:00
|
|
|
std::string strPath = strFilePath;
|
|
|
|
if (!isAbsolutePath(strPath))
|
|
|
|
{ // Not absolute path, add the default root path at the beginning.
|
2013-06-15 14:03:30 +08:00
|
|
|
strPath.insert(0, _defaultResRootPath);
|
2013-02-01 22:19:58 +08:00
|
|
|
}
|
|
|
|
return GetFileAttributesA(strPath.c_str()) != -1 ? true : false;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool FileUtilsWin32::isAbsolutePath(const std::string& strPath)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
if ( strPath.length() > 2
|
|
|
|
&& ( (strPath[0] >= 'a' && strPath[0] <= 'z') || (strPath[0] >= 'A' && strPath[0] <= 'Z') )
|
|
|
|
&& strPath[1] == ':')
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
string FileUtilsWin32::getWritablePath()
|
2013-02-01 11:20:46 +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);
|
|
|
|
|
|
|
|
// Debug app uses executable directory; Non-debug app uses local app data directory
|
|
|
|
#ifndef _DEBUG
|
|
|
|
// Get filename of executable only, e.g. MyGame.exe
|
|
|
|
char *base_name = strrchr(full_path, '\\');
|
|
|
|
|
|
|
|
if(base_name)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Adding executable filename, e.g. C:\Documents and Settings\username\Local Settings\Application Data\MyGame.exe
|
|
|
|
ret += base_name;
|
|
|
|
|
|
|
|
// Remove ".exe" extension, e.g. C:\Documents and Settings\username\Local Settings\Application Data\MyGame
|
|
|
|
ret = ret.substr(0, ret.rfind("."));
|
|
|
|
|
|
|
|
ret += "\\";
|
|
|
|
|
|
|
|
// Create directory
|
|
|
|
if (SUCCEEDED(SHCreateDirectoryExA(NULL, ret.c_str(), NULL)))
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // not defined _DEBUG
|
|
|
|
|
|
|
|
// If fetching of local app data directory fails, use the executable one
|
|
|
|
string ret((char*)full_path);
|
|
|
|
|
|
|
|
// remove xxx.exe
|
|
|
|
ret = ret.substr(0, ret.rfind("\\") + 1);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|