123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- 必须使用使用Cocos2d-x源代码的形式打包
- 步骤:
- 1: 添加附件中的 XCForwarder.h 和 XCForwarder.m 到 cocos2d_libs.xcodeproj 中
- 2: 找到 CCFileUtils.cpp 文件
- 3: 在 #include "CCFileUtils.h" 下面添加 #include "XCForwarder.h"
- 4: 修改static Data getData(const std::string& filename, bool forString)为
- static Data getData(const std::string& filename, bool forString)
- {
- if (filename.empty())
- {
- return Data::Null;
- }
-
- Data ret;
- unsigned char* buffer = nullptr;
- ssize_t size = 0;
- size_t readsize;
- const char* mode = nullptr;
- if (forString)
- mode = "rt";
- else
- mode = "rb";
-
- do
- {
- // Read the file from hardware
- std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename);
- #ifdef __XCForwarder_H__
- buffer = XCForwarderLoadData(fullPath.c_str(), mode, &readsize);
- #else
- FILE *fp = fopen(fullPath.c_str(), mode);
- CC_BREAK_IF(!fp);
- fseek(fp,0,SEEK_END);
- size = ftell(fp);
- fseek(fp,0,SEEK_SET);
-
- if (forString)
- {
- buffer = (unsigned char*)malloc(sizeof(unsigned char) * (size + 1));
- buffer[size] = '\0';
- }
- else
- {
- buffer = (unsigned char*)malloc(sizeof(unsigned char) * size);
- }
-
- readsize = fread(buffer, sizeof(unsigned char), size, fp);
- fclose(fp);
-
- if (forString && readsize < size)
- {
- buffer[readsize] = '\0';
- }
- #endif
-
- } while (0);
-
- if (nullptr == buffer || 0 == readsize)
- {
- std::string msg = "Get data from file(";
- msg.append(filename).append(") failed!");
- CCLOG("%s", msg.c_str());
- }
- else
- {
- ret.fastSet(buffer, readsize);
- }
-
- return ret;
- }
- 5: 修改unsigned char* FileUtils::getFileData(const std::string& filename, const char* mode, ssize_t *size)为
- unsigned char* FileUtils::getFileData(const std::string& filename, const char* mode, ssize_t *size)
- {
- unsigned char * buffer = nullptr;
- CCASSERT(!filename.empty() && size != nullptr && mode != nullptr, "Invalid parameters.");
- *size = 0;
- do
- {
- // read the file from hardware
- const std::string fullPath = fullPathForFilename(filename);
- #ifdef __XCForwarder_H__
- buffer = XCForwarderLoadData(fullPath.c_str(), mode, size);
- #else
- FILE *fp = fopen(fullPath.c_str(), mode);
- CC_BREAK_IF(!fp);
-
- fseek(fp,0,SEEK_END);
- *size = ftell(fp);
- fseek(fp,0,SEEK_SET);
- buffer = (unsigned char*)malloc(*size);
- *size = fread(buffer,sizeof(unsigned char), *size,fp);
- fclose(fp);
- #endif
-
- } while (0);
-
- if (! buffer)
- {
- std::string msg = "Get data from file(";
- msg.append(filename).append(") failed!");
-
- CCLOG("%s", msg.c_str());
- }
- return buffer;
- }
-
-
-
|