2010-10-08 17:53:04 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-05-06 15:31:51 +08:00
|
|
|
#include "windows.h"
|
2011-11-28 17:28:43 +08:00
|
|
|
#include "CCDirector.h"
|
2011-05-06 15:31:51 +08:00
|
|
|
|
2012-04-08 14:28:07 +08:00
|
|
|
#define CC_RETINA_DISPLAY_FILENAME_SUFFIX "-hd"
|
2012-03-28 14:29:23 +08:00
|
|
|
#define CC_IPAD_FILENAME_SUFFIX "-ipad"
|
|
|
|
#define CC_IPAD_DISPLAY_RETINA_SUPPFIX "-ipadhd"
|
2012-04-08 14:28:07 +08:00
|
|
|
|
2011-05-06 15:31:51 +08:00
|
|
|
using namespace std;
|
|
|
|
|
2012-04-19 11:46:08 +08:00
|
|
|
NS_CC_BEGIN
|
2010-10-08 17:53:04 +08:00
|
|
|
|
|
|
|
// record the resource path
|
|
|
|
static char s_pszResourcePath[MAX_PATH] = {0};
|
|
|
|
|
2012-04-19 11:46:08 +08:00
|
|
|
static void _CheckPath()
|
2010-10-08 17:53:04 +08:00
|
|
|
{
|
|
|
|
if (! s_pszResourcePath[0])
|
|
|
|
{
|
|
|
|
WCHAR wszPath[MAX_PATH];
|
2010-10-09 17:41:42 +08:00
|
|
|
int nNum = WideCharToMultiByte(CP_ACP, 0, wszPath,
|
2011-05-19 21:11:29 +08:00
|
|
|
GetCurrentDirectoryW(sizeof(wszPath), wszPath),
|
2010-10-08 17:53:04 +08:00
|
|
|
s_pszResourcePath, MAX_PATH, NULL, NULL);
|
2011-05-13 14:13:30 +08:00
|
|
|
s_pszResourcePath[nNum] = '\\';
|
2010-10-08 17:53:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCFileUtils::setResourcePath(const char *pszResourcePath)
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert(pszResourcePath != NULL, "[FileUtils setResourcePath] -- wrong resource path");
|
|
|
|
CCAssert(strlen(pszResourcePath) <= MAX_PATH, "[FileUtils setResourcePath] -- resource path too long");
|
2010-10-08 17:53:04 +08:00
|
|
|
|
|
|
|
strcpy(s_pszResourcePath, pszResourcePath);
|
|
|
|
}
|
|
|
|
|
2012-04-08 14:52:47 +08:00
|
|
|
const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath, ccResolutionType *pResolutionType)
|
2010-10-08 17:53:04 +08:00
|
|
|
{
|
|
|
|
_CheckPath();
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCString * pRet = new CCString();
|
2010-10-08 17:53:04 +08:00
|
|
|
pRet->autorelease();
|
|
|
|
if ((strlen(pszRelativePath) > 1 && pszRelativePath[1] == ':'))
|
|
|
|
{
|
2011-05-19 21:11:29 +08:00
|
|
|
// path start with "x:", is absolute path
|
2010-10-08 17:53:04 +08:00
|
|
|
pRet->m_sString = pszRelativePath;
|
|
|
|
}
|
2011-05-19 21:11:29 +08:00
|
|
|
else if (strlen(pszRelativePath) > 0
|
|
|
|
&& ('/' == pszRelativePath[0] || '\\' == pszRelativePath[0]))
|
2010-10-08 17:53:04 +08:00
|
|
|
{
|
2011-05-19 21:11:29 +08:00
|
|
|
// path start with '/' or '\', is absolute path without driver name
|
2010-10-08 17:53:04 +08:00
|
|
|
char szDriver[3] = {s_pszResourcePath[0], s_pszResourcePath[1], 0};
|
|
|
|
pRet->m_sString = szDriver;
|
|
|
|
pRet->m_sString += pszRelativePath;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pRet->m_sString = s_pszResourcePath;
|
|
|
|
pRet->m_sString += pszRelativePath;
|
|
|
|
}
|
2012-04-08 14:28:07 +08:00
|
|
|
|
2012-03-28 14:29:23 +08:00
|
|
|
// is ipad?
|
|
|
|
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
bool isIpad = (winSize.width == 1024 || winSize.height == 768);
|
|
|
|
|
|
|
|
std::string hiRes = pRet->m_sString.c_str();
|
|
|
|
std::string::size_type pos = hiRes.find_last_of("/\\");
|
|
|
|
std::string::size_type dotPos = hiRes.find_last_of(".");
|
2012-04-08 14:52:47 +08:00
|
|
|
*pResolutionType = kCCResolutioniPhone;
|
2012-03-28 14:29:23 +08:00
|
|
|
|
|
|
|
if (isIpad)
|
|
|
|
{
|
|
|
|
if (CC_CONTENT_SCALE_FACTOR() == 1.0f)
|
|
|
|
{
|
|
|
|
// ipad
|
|
|
|
|
|
|
|
if (std::string::npos != dotPos && dotPos > pos)
|
|
|
|
{
|
|
|
|
hiRes.insert(dotPos, CC_IPAD_FILENAME_SUFFIX);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hiRes.append(CC_IPAD_FILENAME_SUFFIX);
|
|
|
|
}
|
2012-04-08 14:52:47 +08:00
|
|
|
|
|
|
|
*pResolutionType = kCCResolutioniPad;
|
2012-03-28 14:29:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// ipad retina
|
|
|
|
|
|
|
|
if (std::string::npos != dotPos && dotPos > pos)
|
|
|
|
{
|
|
|
|
hiRes.insert(dotPos, CC_IPAD_DISPLAY_RETINA_SUPPFIX);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hiRes.append(CC_IPAD_DISPLAY_RETINA_SUPPFIX);
|
|
|
|
}
|
2012-04-08 14:52:47 +08:00
|
|
|
|
|
|
|
*pResolutionType = kCCResolutioniPadRetinaDisplay;
|
2012-03-28 14:29:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (CC_CONTENT_SCALE_FACTOR() != 1.0f)
|
2011-02-18 18:30:22 +08:00
|
|
|
{
|
2012-03-28 14:29:23 +08:00
|
|
|
// iphone retina
|
|
|
|
|
|
|
|
if (std::string::npos != dotPos && dotPos > pos)
|
|
|
|
{
|
|
|
|
hiRes.insert(dotPos, CC_RETINA_DISPLAY_FILENAME_SUFFIX);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hiRes.append(CC_RETINA_DISPLAY_FILENAME_SUFFIX);
|
|
|
|
}
|
2012-04-08 14:52:47 +08:00
|
|
|
|
|
|
|
*pResolutionType = kCCResolutioniPhoneRetinaDisplay;
|
2011-02-18 18:30:22 +08:00
|
|
|
}
|
2012-03-28 14:29:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DWORD attrib = GetFileAttributesA(hiRes.c_str());
|
|
|
|
if (attrib != INVALID_FILE_ATTRIBUTES && ! (FILE_ATTRIBUTE_DIRECTORY & attrib))
|
|
|
|
{
|
|
|
|
pRet->m_sString.swap(hiRes);
|
2011-02-18 18:30:22 +08:00
|
|
|
}
|
2012-04-08 14:28:07 +08:00
|
|
|
|
2010-10-08 17:53:04 +08:00
|
|
|
return pRet->m_sString.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const char *pszRelativeFile)
|
|
|
|
{
|
|
|
|
_CheckPath();
|
2011-04-26 14:53:57 +08:00
|
|
|
// std::string relativeFile = fullPathFromRelativePath(pszRelativeFile);
|
|
|
|
std::string relativeFile = pszRelativeFile;
|
2011-03-07 17:11:57 +08:00
|
|
|
CCString *pRet = new CCString();
|
2010-10-08 17:53:04 +08:00
|
|
|
pRet->autorelease();
|
2011-05-16 17:13:15 +08:00
|
|
|
pRet->m_sString = relativeFile.substr(0, relativeFile.find_last_of("/\\") + 1);
|
2010-10-08 17:53:04 +08:00
|
|
|
pRet->m_sString += pszFilename;
|
|
|
|
return pRet->m_sString.c_str();
|
|
|
|
}
|
|
|
|
|
2011-03-26 13:52:33 +08:00
|
|
|
unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize)
|
|
|
|
{
|
2011-03-29 15:05:53 +08:00
|
|
|
unsigned char * pBuffer = NULL;
|
2011-03-26 13:52:33 +08:00
|
|
|
|
2011-04-26 14:53:57 +08:00
|
|
|
do
|
|
|
|
{
|
2011-03-26 13:52:33 +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);
|
2012-03-05 09:55:50 +08:00
|
|
|
pBuffer = new unsigned char[*pSize];
|
2011-03-29 15:05:53 +08:00
|
|
|
*pSize = fread(pBuffer,sizeof(unsigned char), *pSize,fp);
|
2011-04-26 14:53:57 +08:00
|
|
|
fclose(fp);
|
2011-03-26 13:52:33 +08:00
|
|
|
} while (0);
|
|
|
|
|
2011-03-29 15:05:53 +08:00
|
|
|
if (! pBuffer && getIsPopupNotify())
|
2011-03-29 11:41:44 +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());
|
|
|
|
}
|
2011-03-29 15:05:53 +08:00
|
|
|
return pBuffer;
|
2011-03-26 13:52:33 +08:00
|
|
|
}
|
|
|
|
|
2011-04-25 09:40:10 +08:00
|
|
|
void CCFileUtils::setResource(const char* pszZipFileName)
|
2011-03-26 13:52:33 +08:00
|
|
|
{
|
2011-06-10 17:51:37 +08:00
|
|
|
CC_UNUSED_PARAM(pszZipFileName);
|
2011-03-26 13:52:33 +08:00
|
|
|
CCAssert(0, "Have not implement!");
|
|
|
|
}
|
|
|
|
|
2011-05-06 17:46:19 +08:00
|
|
|
string CCFileUtils::getWriteablePath()
|
2011-05-06 15:31:51 +08:00
|
|
|
{
|
|
|
|
// return the path that the exe file saved in
|
|
|
|
|
|
|
|
char full_path[_MAX_PATH + 1];
|
|
|
|
::GetModuleFileNameA(NULL, full_path, _MAX_PATH + 1);
|
|
|
|
|
|
|
|
string ret((char*)full_path);
|
|
|
|
|
|
|
|
// remove xxx.exe
|
|
|
|
ret = ret.substr(0, ret.rfind("\\") + 1);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-04-19 11:46:08 +08:00
|
|
|
NS_CC_END
|