BehaviacFileManager.cpp 2.5 KB

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