BehaviacFileManager.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // BehaviacFileManager.cpp
  3. // redream_runtime_mac
  4. //
  5. // Created by zhu on 2023/2/14.
  6. //
  7. #include "BehaviacFileManager.hpp"
  8. #include "cocos2d.h"
  9. namespace behaviac {
  10. BehaviacFileManager::BehaviacFileManager()
  11. {
  12. }
  13. BehaviacFileManager::~BehaviacFileManager()
  14. {
  15. }
  16. behaviac::IFile* BehaviacFileManager::FileOpen(const char* fileName, behaviac::CFileSystem::EOpenMode iOpenAccess)
  17. {
  18. std::string fileP = std::string(&fileName[1]);
  19. std::string fullPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(fileP.c_str());
  20. cocos2d::Data data = cocos2d::FileUtils::getInstance()->getDataFromFile(fullPath);
  21. if (data.isNull()) {
  22. return nullptr;
  23. }
  24. behaviac::IFile* file = BEHAVIAC_NEW CCBehaviacFile(fileName, data.getSize(), data.getBytes());
  25. if (!file) {
  26. return nullptr;
  27. }
  28. return file;
  29. }
  30. void BehaviacFileManager::FileClose(behaviac::IFile* file)
  31. {
  32. if (file) {
  33. delete file;
  34. }
  35. }
  36. bool BehaviacFileManager::FileExists(const char* fileName)
  37. {
  38. return cocos2d::FileUtils::getInstance()->isFileExist(fileName);
  39. }
  40. uint64_t BehaviacFileManager::FileGetSize(const char* fileName)
  41. {
  42. std::string fullPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(fileName);
  43. ssize_t fileSize = cocos2d::FileUtils::getInstance()->getFileSize(fullPath);
  44. return static_cast<uint32_t>(fileSize);
  45. }
  46. //MARK: - file
  47. CCBehaviacFile::CCBehaviacFile(const char* filePath, uint32_t size, const uint8_t* data)
  48. : _filePath(filePath){
  49. if (data) {
  50. _buffer.resize(size);
  51. memcpy(_buffer.data(), data, size);
  52. _bufferSize = size;
  53. }
  54. }
  55. CCBehaviacFile::~CCBehaviacFile(){
  56. _buffer.clear();
  57. _bufferSize = 0;
  58. }
  59. uint32_t CCBehaviacFile::Read(void* pBuffer, uint32_t numberOfBytesToRead){
  60. if (pBuffer && !_buffer.empty()) {
  61. numberOfBytesToRead = std::min(numberOfBytesToRead, _bufferSize - _currentPosition);
  62. memcpy(pBuffer, _buffer.data() + _currentPosition, numberOfBytesToRead);
  63. _currentPosition += numberOfBytesToRead;
  64. return true;
  65. }
  66. numberOfBytesToRead = 0;
  67. return false;
  68. }
  69. uint32_t CCBehaviacFile::Write(const void* pBuffer, uint32_t numberOfBytesToWrite){
  70. return 0;
  71. }
  72. int64_t CCBehaviacFile::Seek(int64_t distanceToMove, behaviac::CFileSystem::ESeekMode moveMethod){
  73. return 0;
  74. }
  75. uint64_t CCBehaviacFile::GetSize(){
  76. return _bufferSize;
  77. }
  78. };