123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- //
- // 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<uint32_t>(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;
- }
- };
|