[android] fixed #688: support reading file other than apk

This commit is contained in:
minggo 2011-08-25 11:24:31 +08:00
parent 55dd2545d7
commit e26eaae5e1
3 changed files with 31 additions and 38 deletions

View File

@ -76,6 +76,9 @@ public:
/**
@brief Set the ResourcePath,we will find resource in this path
@param pszResourcePath The absolute resource path
@warning Don't call this function in android and iOS, it has not effect.
In android, if you want to read file other than apk, you shoud use invoke getFileData(), and pass the
absolute path.
*/
static void setResourcePath(const char *pszResourcePath);

View File

@ -27,48 +27,32 @@ NS_CC_BEGIN;
#include "CCCommon.h"
#include "jni/SystemInfoJni.h"
#define MAX_PATH 256
// record the resource path
static string s_strResourcePath = "";
/*
* This function is implemented for jni to set apk path.
*/
void CCFileUtils::setResourcePath(const char* pszResourcePath)
{
CCAssert(pszResourcePath != NULL, "[FileUtils setRelativePath] -- wrong relative path");
if (! pszResourcePath)
string tmp(pszResourcePath);
if ((! pszResourcePath) || tmp.find(".apk") == string::npos)
{
return;
}
s_strResourcePath = pszResourcePath;
/*
* If the path is set by user, and not end with "/", append it
*/
if (s_strResourcePath.find(".apk") == string::npos
&& s_strResourcePath.find_last_of("/") != s_strResourcePath.length() - 1)
{
s_strResourcePath += "/";
}
s_strResourcePath = pszResourcePath;
}
const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath)
{
if (s_strResourcePath.find(".apk") != string::npos)
{
return pszRelativePath;
}
else
{
CCString *pRet = new CCString();
pRet->autorelease();
pRet->m_sString = s_strResourcePath + pszRelativePath;
return pRet->m_sString.c_str();
}
return pszRelativePath;
}
const char *CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const char *pszRelativeFile)
const char* CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const char *pszRelativeFile)
{
std::string relativeFile = pszRelativeFile;
CCString *pRet = new CCString();
@ -79,11 +63,16 @@ const char *CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const
}
unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize)
{
string fullPath = pszFileName;
{
unsigned char * pData = 0;
string fullPath(pszFileName);
if (s_strResourcePath.find(".apk") != string::npos)
if ((! pszFileName) || (! pszMode))
{
return 0;
}
if (pszFileName[0] != '/')
{
// read from apk
fullPath.insert(0, "assets/");
@ -97,12 +86,18 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz
FILE *fp = fopen(pszFileName, pszMode);
CC_BREAK_IF(!fp);
unsigned long size;
fseek(fp,0,SEEK_END);
*pSize = ftell(fp);
size = ftell(fp);
fseek(fp,0,SEEK_SET);
pData = new unsigned char[*pSize];
*pSize = fread(pData,sizeof(unsigned char), *pSize,fp);
pData = new unsigned char[size];
size = fread(pData,sizeof(unsigned char), size,fp);
fclose(fp);
if (pSize)
{
*pSize = size;
}
} while (0);
}
@ -113,6 +108,7 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz
msg.append(fullPath.c_str()).append(") failed!");
CCMessageBox(msg.c_str(), title.c_str());
}
return pData;
}

View File

@ -237,15 +237,9 @@ static void static_addValueToCCDict(id key, id value, CCDictionary<std::string,
namespace cocos2d {
// record the resource path
static char s_pszResourcePath[MAX_PATH] = {0};
void CCFileUtils::setResourcePath(const char *pszResourcePath)
{
// NSAssert(pszResourcePath != NULL, "[FileUtils setResourcePath] -- wrong resource path");
// NSAssert(strlen(pszResourcePath) <= MAX_PATH, "[FileUtils setResourcePath] -- resource path too long");
strcpy(s_pszResourcePath, pszResourcePath);
assert(0);
}
int CCFileUtils::ccLoadFileIntoMemory(const char *filename, unsigned char **out)