接入.txt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 必须使用使用Cocos2d-x源代码的形式打包
  2. 步骤:
  3. 1: 添加附件中的 XCForwarder.h 和 XCForwarder.m 到 cocos2d_libs.xcodeproj 中
  4. 2: 找到 CCFileUtils.cpp 文件
  5. 3: 在 #include "CCFileUtils.h" 下面添加 #include "XCForwarder.h"
  6. 4: 修改static Data getData(const std::string& filename, bool forString)为
  7. static Data getData(const std::string& filename, bool forString)
  8. {
  9. if (filename.empty())
  10. {
  11. return Data::Null;
  12. }
  13. Data ret;
  14. unsigned char* buffer = nullptr;
  15. ssize_t size = 0;
  16. size_t readsize;
  17. const char* mode = nullptr;
  18. if (forString)
  19. mode = "rt";
  20. else
  21. mode = "rb";
  22. do
  23. {
  24. // Read the file from hardware
  25. std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename);
  26. #ifdef __XCForwarder_H__
  27. buffer = XCForwarderLoadData(fullPath.c_str(), mode, &readsize);
  28. #else
  29. FILE *fp = fopen(fullPath.c_str(), mode);
  30. CC_BREAK_IF(!fp);
  31. fseek(fp,0,SEEK_END);
  32. size = ftell(fp);
  33. fseek(fp,0,SEEK_SET);
  34. if (forString)
  35. {
  36. buffer = (unsigned char*)malloc(sizeof(unsigned char) * (size + 1));
  37. buffer[size] = '\0';
  38. }
  39. else
  40. {
  41. buffer = (unsigned char*)malloc(sizeof(unsigned char) * size);
  42. }
  43. readsize = fread(buffer, sizeof(unsigned char), size, fp);
  44. fclose(fp);
  45. if (forString && readsize < size)
  46. {
  47. buffer[readsize] = '\0';
  48. }
  49. #endif
  50. } while (0);
  51. if (nullptr == buffer || 0 == readsize)
  52. {
  53. std::string msg = "Get data from file(";
  54. msg.append(filename).append(") failed!");
  55. CCLOG("%s", msg.c_str());
  56. }
  57. else
  58. {
  59. ret.fastSet(buffer, readsize);
  60. }
  61. return ret;
  62. }
  63. 5: 修改unsigned char* FileUtils::getFileData(const std::string& filename, const char* mode, ssize_t *size)为
  64. unsigned char* FileUtils::getFileData(const std::string& filename, const char* mode, ssize_t *size)
  65. {
  66. unsigned char * buffer = nullptr;
  67. CCASSERT(!filename.empty() && size != nullptr && mode != nullptr, "Invalid parameters.");
  68. *size = 0;
  69. do
  70. {
  71. // read the file from hardware
  72. const std::string fullPath = fullPathForFilename(filename);
  73. #ifdef __XCForwarder_H__
  74. buffer = XCForwarderLoadData(fullPath.c_str(), mode, size);
  75. #else
  76. FILE *fp = fopen(fullPath.c_str(), mode);
  77. CC_BREAK_IF(!fp);
  78. fseek(fp,0,SEEK_END);
  79. *size = ftell(fp);
  80. fseek(fp,0,SEEK_SET);
  81. buffer = (unsigned char*)malloc(*size);
  82. *size = fread(buffer,sizeof(unsigned char), *size,fp);
  83. fclose(fp);
  84. #endif
  85. } while (0);
  86. if (! buffer)
  87. {
  88. std::string msg = "Get data from file(";
  89. msg.append(filename).append(") failed!");
  90. CCLOG("%s", msg.c_str());
  91. }
  92. return buffer;
  93. }