1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- //
- // BehaviacFileManager.cpp
- // redream_runtime_mac
- //
- // Created by zhu on 2023/2/14.
- //
- #include "BehaviacFileManager.hpp"
- #include "cocos2d.h"
- namespace behaviac {
- BehaviacFileManager::BehaviacFileManager()
- : _rootPath("Rebolt_BT/")
- {
- }
- BehaviacFileManager::~BehaviacFileManager()
- {
- }
- void BehaviacFileManager::setRootPath(std::string path){
- _rootPath = path;
- }
- behaviac::IFile* BehaviacFileManager::FileOpen(const char* fileName, behaviac::CFileSystem::EOpenMode iOpenAccess)
- {
- std::string fileP = _rootPath + 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;
- }
- };
|