// // BehaviacFileManager.cpp // redream_runtime_mac // // Created by zhu on 2023/2/14. // #include "BehaviacFileManager.hpp" #include "cocos2d.h" namespace behaviac { BehaviacFileManager::BehaviacFileManager() { } BehaviacFileManager::~BehaviacFileManager() { } behaviac::IFile* BehaviacFileManager::FileOpen(const char* fileName, behaviac::CFileSystem::EOpenMode iOpenAccess) { std::string fileP = std::string(&fileName[1]); std::string fullPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(fileP.c_str()); cocos2d::Data data = cocos2d::FileUtils::getInstance()->getDataFromFile(fullPath); if (data.isNull()) { return nullptr; } behaviac::IFile* file = BEHAVIAC_NEW CCBehaviacFile(fileName, data.getSize(), data.getBytes()); if (!file) { return nullptr; } return file; } void BehaviacFileManager::FileClose(behaviac::IFile* file) { if (file) { delete file; } } bool BehaviacFileManager::FileExists(const char* fileName) { return cocos2d::FileUtils::getInstance()->isFileExist(fileName); } uint64_t BehaviacFileManager::FileGetSize(const char* fileName) { std::string fullPath = cocos2d::FileUtils::getInstance()->fullPathForFilename(fileName); ssize_t fileSize = cocos2d::FileUtils::getInstance()->getFileSize(fullPath); return static_cast(fileSize); } //MARK: - file CCBehaviacFile::CCBehaviacFile(const char* filePath, uint32_t size, const uint8_t* data) : _filePath(filePath){ if (data) { _buffer.resize(size); memcpy(_buffer.data(), data, size); _bufferSize = size; } } CCBehaviacFile::~CCBehaviacFile(){ _buffer.clear(); _bufferSize = 0; } uint32_t CCBehaviacFile::Read(void* pBuffer, uint32_t numberOfBytesToRead){ if (pBuffer && !_buffer.empty()) { numberOfBytesToRead = std::min(numberOfBytesToRead, _bufferSize - _currentPosition); memcpy(pBuffer, _buffer.data() + _currentPosition, numberOfBytesToRead); _currentPosition += numberOfBytesToRead; return true; } numberOfBytesToRead = 0; return false; } uint32_t CCBehaviacFile::Write(const void* pBuffer, uint32_t numberOfBytesToWrite){ return 0; } int64_t CCBehaviacFile::Seek(int64_t distanceToMove, behaviac::CFileSystem::ESeekMode moveMethod){ return 0; } uint64_t CCBehaviacFile::GetSize(){ return _bufferSize; } };