2013-02-01 11:20:46 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-02-01 11:20:46 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2014-01-31 08:51:43 +08:00
|
|
|
|
2014-09-10 08:41:49 +08:00
|
|
|
#include "platform/CCPlatformConfig.h"
|
2014-01-31 08:51:43 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
|
|
|
|
|
2014-09-10 08:53:08 +08:00
|
|
|
#include "CCFileUtils-win32.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCCommon.h"
|
2013-02-01 11:20:46 +08:00
|
|
|
#include <Shlobj.h>
|
2015-02-06 20:12:02 +08:00
|
|
|
#include <cstdlib>
|
2013-02-01 11:20:46 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-08-02 17:31:19 +08:00
|
|
|
#define CC_MAX_PATH 512
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2013-08-02 17:31:19 +08:00
|
|
|
// The root path of resources, the character encoding is UTF-8.
|
|
|
|
// UTF-8 is the only encoding supported by cocos2d-x API.
|
|
|
|
static std::string s_resourcePath = "";
|
2013-02-01 11:20:46 +08:00
|
|
|
|
2013-08-02 17:31:19 +08:00
|
|
|
// D:\aaa\bbb\ccc\ddd\abc.txt --> D:/aaa/bbb/ccc/ddd/abc.txt
|
|
|
|
static inline std::string convertPathFormatToUnixStyle(const std::string& path)
|
2013-07-29 18:27:26 +08:00
|
|
|
{
|
|
|
|
std::string ret = path;
|
|
|
|
int len = ret.length();
|
|
|
|
for (int i = 0; i < len; ++i)
|
|
|
|
{
|
2013-08-02 17:31:19 +08:00
|
|
|
if (ret[i] == '\\')
|
2013-07-29 18:27:26 +08:00
|
|
|
{
|
2013-08-02 17:31:19 +08:00
|
|
|
ret[i] = '/';
|
2013-07-29 18:27:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-08-02 17:31:19 +08:00
|
|
|
static void _checkPath()
|
2013-07-29 18:27:26 +08:00
|
|
|
{
|
2013-08-02 17:31:19 +08:00
|
|
|
if (0 == s_resourcePath.length())
|
2013-07-29 18:27:26 +08:00
|
|
|
{
|
2015-02-06 20:12:02 +08:00
|
|
|
WCHAR *pUtf16ExePath = nullptr;
|
|
|
|
_get_wpgmptr(&pUtf16ExePath);
|
|
|
|
|
|
|
|
// We need only directory part without exe
|
|
|
|
WCHAR *pUtf16DirEnd = wcsrchr(pUtf16ExePath, L'\\');
|
|
|
|
|
|
|
|
char utf8ExeDir[CC_MAX_PATH] = { 0 };
|
|
|
|
int nNum = WideCharToMultiByte(CP_UTF8, 0, pUtf16ExePath, pUtf16DirEnd-pUtf16ExePath+1, utf8ExeDir, sizeof(utf8ExeDir), nullptr, nullptr);
|
|
|
|
|
|
|
|
s_resourcePath = convertPathFormatToUnixStyle(utf8ExeDir);
|
2013-07-29 18:27:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-12 12:03:39 +08:00
|
|
|
FileUtils* FileUtils::getInstance()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
if (s_sharedFileUtils == nullptr)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
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;
|
2014-07-10 00:45:27 +08:00
|
|
|
s_sharedFileUtils = nullptr;
|
2013-07-23 20:44:08 +08:00
|
|
|
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-08-02 17:31:19 +08:00
|
|
|
_defaultResRootPath = s_resourcePath;
|
2013-06-20 14:13:12 +08:00
|
|
|
return FileUtils::init();
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2014-04-02 15:35:09 +08:00
|
|
|
bool FileUtilsWin32::isFileExistInternal(const std::string& strFilePath) const
|
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
|
|
|
}
|
2013-07-29 18:27:26 +08:00
|
|
|
|
2013-08-02 17:31:19 +08:00
|
|
|
WCHAR utf16Buf[CC_MAX_PATH] = {0};
|
2014-03-15 01:05:04 +08:00
|
|
|
MultiByteToWideChar(CP_UTF8, 0, strPath.c_str(), -1, utf16Buf, sizeof(utf16Buf)/sizeof(utf16Buf[0]));
|
2013-07-29 18:27:26 +08:00
|
|
|
|
2014-04-08 15:24:47 +08:00
|
|
|
DWORD attr = GetFileAttributesW(utf16Buf);
|
|
|
|
if(attr == INVALID_FILE_ATTRIBUTES || (attr & FILE_ATTRIBUTE_DIRECTORY))
|
|
|
|
return false; // not a file
|
|
|
|
return true;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 13:52:23 +08:00
|
|
|
bool FileUtilsWin32::isAbsolutePath(const std::string& strPath) const
|
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-12-18 14:58:17 +08:00
|
|
|
static Data getData(const std::string& filename, bool forString)
|
|
|
|
{
|
2014-04-10 11:07:00 +08:00
|
|
|
if (filename.empty())
|
|
|
|
{
|
|
|
|
return Data::Null;
|
|
|
|
}
|
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
unsigned char *buffer = nullptr;
|
2014-04-10 11:07:00 +08:00
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
size_t size = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// read the file from hardware
|
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename);
|
|
|
|
|
|
|
|
WCHAR wszBuf[CC_MAX_PATH] = {0};
|
2014-03-15 01:05:04 +08:00
|
|
|
MultiByteToWideChar(CP_UTF8, 0, fullPath.c_str(), -1, wszBuf, sizeof(wszBuf)/sizeof(wszBuf[0]));
|
2013-12-18 14:58:17 +08:00
|
|
|
|
2015-01-04 16:43:10 +08:00
|
|
|
HANDLE fileHandle = ::CreateFileW(wszBuf, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, nullptr);
|
2013-12-18 14:58:17 +08:00
|
|
|
CC_BREAK_IF(fileHandle == INVALID_HANDLE_VALUE);
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
size = ::GetFileSize(fileHandle, nullptr);
|
2013-12-18 14:58:17 +08:00
|
|
|
|
|
|
|
if (forString)
|
|
|
|
{
|
|
|
|
buffer = (unsigned char*) malloc(size + 1);
|
|
|
|
buffer[size] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer = (unsigned char*) malloc(size);
|
|
|
|
}
|
|
|
|
DWORD sizeRead = 0;
|
|
|
|
BOOL successed = FALSE;
|
2014-07-10 00:45:27 +08:00
|
|
|
successed = ::ReadFile(fileHandle, buffer, size, &sizeRead, nullptr);
|
2013-12-18 14:58:17 +08:00
|
|
|
::CloseHandle(fileHandle);
|
|
|
|
|
|
|
|
if (!successed)
|
|
|
|
{
|
2015-01-05 13:49:47 +08:00
|
|
|
// should determine buffer value, or it will cause memory leak
|
|
|
|
if (buffer)
|
|
|
|
{
|
|
|
|
free(buffer);
|
|
|
|
buffer = nullptr;
|
|
|
|
}
|
2013-12-18 14:58:17 +08:00
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
Data ret;
|
|
|
|
|
|
|
|
if (buffer == nullptr || size == 0)
|
|
|
|
{
|
|
|
|
std::string msg = "Get data from file(";
|
|
|
|
// Gets error code.
|
|
|
|
DWORD errorCode = ::GetLastError();
|
|
|
|
char errorCodeBuffer[20] = {0};
|
|
|
|
snprintf(errorCodeBuffer, sizeof(errorCodeBuffer), "%d", errorCode);
|
|
|
|
|
|
|
|
msg = msg + filename + ") failed, error code is " + errorCodeBuffer;
|
|
|
|
CCLOG("%s", msg.c_str());
|
2015-01-07 15:02:27 +08:00
|
|
|
|
|
|
|
if (buffer)
|
|
|
|
free(buffer);
|
2013-12-18 14:58:17 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret.fastSet(buffer, size);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-27 14:38:22 +08:00
|
|
|
std::string FileUtilsWin32::getStringFromFile(const std::string& filename)
|
2013-12-18 14:58:17 +08:00
|
|
|
{
|
|
|
|
Data data = getData(filename, true);
|
2014-02-10 18:15:30 +08:00
|
|
|
if (data.isNull())
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
2014-04-10 11:07:00 +08:00
|
|
|
|
2013-12-18 14:58:17 +08:00
|
|
|
std::string ret((const char*)data.getBytes());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-27 14:38:22 +08:00
|
|
|
Data FileUtilsWin32::getDataFromFile(const std::string& filename)
|
2013-12-18 14:58:17 +08:00
|
|
|
{
|
|
|
|
return getData(filename, false);
|
|
|
|
}
|
|
|
|
|
2013-12-27 14:38:22 +08:00
|
|
|
unsigned char* FileUtilsWin32::getFileData(const std::string& filename, const char* mode, ssize_t* size)
|
2013-07-29 18:27:26 +08:00
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
unsigned char * pBuffer = nullptr;
|
2013-07-29 18:27:26 +08:00
|
|
|
*size = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// read the file from hardware
|
|
|
|
std::string fullPath = fullPathForFilename(filename);
|
|
|
|
|
2013-08-02 17:31:19 +08:00
|
|
|
WCHAR wszBuf[CC_MAX_PATH] = {0};
|
2014-03-15 01:05:04 +08:00
|
|
|
MultiByteToWideChar(CP_UTF8, 0, fullPath.c_str(), -1, wszBuf, sizeof(wszBuf)/sizeof(wszBuf[0]));
|
2013-07-29 18:27:26 +08:00
|
|
|
|
2015-01-04 16:43:10 +08:00
|
|
|
HANDLE fileHandle = ::CreateFileW(wszBuf, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, nullptr);
|
2013-07-29 18:27:26 +08:00
|
|
|
CC_BREAK_IF(fileHandle == INVALID_HANDLE_VALUE);
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
*size = ::GetFileSize(fileHandle, nullptr);
|
2013-07-29 18:27:26 +08:00
|
|
|
|
2013-11-14 17:14:35 +08:00
|
|
|
pBuffer = (unsigned char*) malloc(*size);
|
2013-07-29 18:27:26 +08:00
|
|
|
DWORD sizeRead = 0;
|
|
|
|
BOOL successed = FALSE;
|
2014-07-10 00:45:27 +08:00
|
|
|
successed = ::ReadFile(fileHandle, pBuffer, *size, &sizeRead, nullptr);
|
2013-07-29 18:27:26 +08:00
|
|
|
::CloseHandle(fileHandle);
|
|
|
|
|
|
|
|
if (!successed)
|
|
|
|
{
|
2013-11-14 17:14:35 +08:00
|
|
|
free(pBuffer);
|
2013-12-18 14:58:17 +08:00
|
|
|
pBuffer = nullptr;
|
2013-07-29 18:27:26 +08:00
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
if (! pBuffer)
|
|
|
|
{
|
|
|
|
std::string msg = "Get data from file(";
|
|
|
|
// Gets error code.
|
|
|
|
DWORD errorCode = ::GetLastError();
|
|
|
|
char errorCodeBuffer[20] = {0};
|
|
|
|
snprintf(errorCodeBuffer, sizeof(errorCodeBuffer), "%d", errorCode);
|
|
|
|
|
|
|
|
msg = msg + filename + ") failed, error code is " + errorCodeBuffer;
|
|
|
|
CCLOG("%s", msg.c_str());
|
|
|
|
}
|
|
|
|
return pBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FileUtilsWin32::getPathForFilename(const std::string& filename, const std::string& resolutionDirectory, const std::string& searchPath)
|
|
|
|
{
|
|
|
|
std::string unixFileName = convertPathFormatToUnixStyle(filename);
|
2013-08-02 17:31:19 +08:00
|
|
|
std::string unixResolutionDirectory = convertPathFormatToUnixStyle(resolutionDirectory);
|
2013-07-29 18:27:26 +08:00
|
|
|
std::string unixSearchPath = convertPathFormatToUnixStyle(searchPath);
|
|
|
|
|
2013-08-02 17:31:19 +08:00
|
|
|
return FileUtils::getPathForFilename(unixFileName, unixResolutionDirectory, unixSearchPath);
|
2013-07-29 18:27:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string FileUtilsWin32::getFullPathForDirectoryAndFilename(const std::string& strDirectory, const std::string& strFilename)
|
|
|
|
{
|
2013-08-02 17:31:19 +08:00
|
|
|
std::string unixDirectory = convertPathFormatToUnixStyle(strDirectory);
|
|
|
|
std::string unixFilename = convertPathFormatToUnixStyle(strFilename);
|
2013-07-29 18:27:26 +08:00
|
|
|
|
2013-08-02 17:31:19 +08:00
|
|
|
return FileUtils::getFullPathForDirectoryAndFilename(unixDirectory, unixFilename);
|
2013-07-29 18:27:26 +08:00
|
|
|
}
|
|
|
|
|
2013-09-07 13:52:23 +08:00
|
|
|
string FileUtilsWin32::getWritablePath() const
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
// Get full path of executable, e.g. c:\Program Files (x86)\My Game Folder\MyGame.exe
|
2013-08-02 17:31:19 +08:00
|
|
|
char full_path[CC_MAX_PATH + 1];
|
2014-07-10 00:45:27 +08:00
|
|
|
::GetModuleFileNameA(nullptr, full_path, CC_MAX_PATH + 1);
|
2013-02-01 11:20:46 +08:00
|
|
|
|
|
|
|
// Debug app uses executable directory; Non-debug app uses local app data directory
|
2014-08-29 17:27:55 +08:00
|
|
|
//#ifndef _DEBUG
|
2013-02-01 11:20:46 +08:00
|
|
|
// Get filename of executable only, e.g. MyGame.exe
|
|
|
|
char *base_name = strrchr(full_path, '\\');
|
|
|
|
|
|
|
|
if(base_name)
|
|
|
|
{
|
2013-08-02 17:31:19 +08:00
|
|
|
char app_data_path[CC_MAX_PATH + 1];
|
2013-02-01 11:20:46 +08:00
|
|
|
|
|
|
|
// Get local app data directory, e.g. C:\Documents and Settings\username\Local Settings\Application Data
|
2014-07-10 00:45:27 +08:00
|
|
|
if (SUCCEEDED(SHGetFolderPathA(nullptr, CSIDL_LOCAL_APPDATA, nullptr, SHGFP_TYPE_CURRENT, app_data_path)))
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
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
|
2014-07-10 00:45:27 +08:00
|
|
|
if (SUCCEEDED(SHCreateDirectoryExA(nullptr, ret.c_str(), nullptr)))
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2014-03-18 10:51:07 +08:00
|
|
|
return convertPathFormatToUnixStyle(ret);
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-29 17:27:55 +08:00
|
|
|
//#endif // not defined _DEBUG
|
2013-02-01 11:20:46 +08:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2013-08-02 17:31:19 +08:00
|
|
|
ret = convertPathFormatToUnixStyle(ret);
|
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
2014-01-31 08:51:43 +08:00
|
|
|
|
|
|
|
#endif // CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
|
|
|
|
|