diff --git a/cocos/audio/include/AudioDecoder.h b/cocos/audio/include/AudioDecoder.h index eb4fb2fc49..dceae61ed9 100644 --- a/cocos/audio/include/AudioDecoder.h +++ b/cocos/audio/include/AudioDecoder.h @@ -27,6 +27,7 @@ #include #include +#include "platform/CCFileStream.h" namespace cocos2d { enum class AUDIO_SOURCE_FORMAT : uint16_t diff --git a/cocos/audio/include/AudioDecoderMp3.h b/cocos/audio/include/AudioDecoderMp3.h index b25dc68dd7..c7ee95caf0 100644 --- a/cocos/audio/include/AudioDecoderMp3.h +++ b/cocos/audio/include/AudioDecoderMp3.h @@ -26,7 +26,6 @@ #pragma once #include "audio/include/AudioDecoder.h" -#include "platform/PXFileStream.h" #if !defined(CC_USE_MPG123) #define CC_USE_MPG123 0 @@ -81,7 +80,7 @@ protected: static bool lazyInit(); static void destroy(); - PXFileStream _fileStream; + FileStream _fileStream; mp3dec_handle_t _handle; friend class AudioDecoderManager; diff --git a/cocos/audio/include/AudioDecoderOgg.h b/cocos/audio/include/AudioDecoderOgg.h index 6bd6d1a6dc..dcc53999b9 100644 --- a/cocos/audio/include/AudioDecoderOgg.h +++ b/cocos/audio/include/AudioDecoderOgg.h @@ -29,8 +29,6 @@ #include "vorbis/vorbisfile.h" -#include "platform/PXFileStream.h" - namespace cocos2d { /** @@ -70,7 +68,7 @@ protected: AudioDecoderOgg(); ~AudioDecoderOgg(); - PXFileStream _fileStream; + FileStream _fileStream; OggVorbis_File _vf; friend class AudioDecoderManager; diff --git a/cocos/audio/include/AudioDecoderWav.h b/cocos/audio/include/AudioDecoderWav.h index ff853d182f..b285cec851 100644 --- a/cocos/audio/include/AudioDecoderWav.h +++ b/cocos/audio/include/AudioDecoderWav.h @@ -28,7 +28,6 @@ #pragma once #include "audio/include/AudioDecoder.h" -#include "platform/PXFileStream.h" #if !defined(MAKE_FOURCC) #define MAKE_FOURCC(a,b,c,d) ((uint32_t)((a) | ((b) << 8) | ((c) << 16) | (((uint32_t)(d)) << 24))) @@ -107,7 +106,7 @@ struct WAV_FILE WAV_FILE_HEADER FileHeader; AUDIO_SOURCE_FORMAT SourceFormat; uint32_t PcmDataOffset; - cocos2d::PXFileStream Stream; + cocos2d::FileStream Stream; }; diff --git a/cocos/audio/src/AudioDecoderMp3.cpp b/cocos/audio/src/AudioDecoderMp3.cpp index d60a78992a..07b20a3390 100644 --- a/cocos/audio/src/AudioDecoderMp3.cpp +++ b/cocos/audio/src/AudioDecoderMp3.cpp @@ -56,28 +56,28 @@ namespace cocos2d { #if !CC_USE_MPG123 static size_t minimp3_read_r(void* buf, size_t size, void* user_data) { - return ((PXFileStream*)user_data)->read(buf, size); + return ((FileStream*)user_data)->read(buf, size); } static int minimp3_seek_r(uint64_t position, void* user_data) { - return ((PXFileStream*)user_data)->seek(position, SEEK_SET) >= 0 ? 0 : -1; + return ((FileStream*)user_data)->seek(position, SEEK_SET) >= 0 ? 0 : -1; } #else static bool __mp3Inited = false; static ssize_t mpg123_read_r(void * handle, void * buffer, size_t count) { - return ((PXFileStream*)handle)->read(buffer, count); + return ((FileStream*)handle)->read(buffer, count); } static off_t mpg123_lseek_r(void * handle, off_t offset, int whence) { - return ((PXFileStream*)handle)->seek(offset, whence); + return ((FileStream*)handle)->seek(offset, whence); } void mpg123_close_r(void* handle) { - ((PXFileStream*)handle)->close(); + ((FileStream*)handle)->close(); } #endif bool AudioDecoderMp3::lazyInit() @@ -128,7 +128,7 @@ namespace cocos2d { #if !CC_USE_MPG123 do { - if (!_fileStream.open(fullPath)) + if (!_fileStream.open(fullPath, FileStream::Mode::READ)) { ALOGE("Trouble with minimp3(1): %s\n", strerror(errno)); break; diff --git a/cocos/audio/src/AudioDecoderOgg.cpp b/cocos/audio/src/AudioDecoderOgg.cpp index 357502addf..7581af0ceb 100644 --- a/cocos/audio/src/AudioDecoderOgg.cpp +++ b/cocos/audio/src/AudioDecoderOgg.cpp @@ -39,22 +39,22 @@ namespace cocos2d { static size_t ov_fread_r(void* buffer, size_t element_size, size_t element_count, void* handle) { - return ((PXFileStream*)handle)->read(buffer, static_cast(element_size * element_count)); + return ((FileStream*)handle)->read(buffer, static_cast(element_size * element_count)); } static int ov_fseek_r(void * handle, ogg_int64_t offset, int whence) { - auto n = ((PXFileStream*)handle)->seek(offset, whence); + auto n = ((FileStream*)handle)->seek(offset, whence); return n >= 0 ? 0 : -1; } static long ov_ftell_r(void * handle) { - return ((PXFileStream*)handle)->seek(0, SEEK_CUR); + return ((FileStream*)handle)->seek(0, SEEK_CUR); } static int ov_fclose_r(void* handle) { - ((PXFileStream*)handle)->close(); + ((FileStream*)handle)->close(); return 0; } @@ -69,7 +69,7 @@ namespace cocos2d { bool AudioDecoderOgg::open(const std::string& fullPath) { - if (!_fileStream.open(fullPath)) + if (!_fileStream.open(fullPath, FileStream::Mode::READ)) { ALOGE("Trouble with ogg(1): %s\n", strerror(errno)); return false; diff --git a/cocos/audio/src/AudioDecoderWav.cpp b/cocos/audio/src/AudioDecoderWav.cpp index abbeacbf4b..3501adad58 100644 --- a/cocos/audio/src/AudioDecoderWav.cpp +++ b/cocos/audio/src/AudioDecoderWav.cpp @@ -76,7 +76,7 @@ namespace cocos2d { } static bool wav_open(const std::string& fullPath, WAV_FILE* wavf) { - bool succeed = wavf->Stream.open(fullPath); + bool succeed = wavf->Stream.open(fullPath, FileStream::Mode::READ); if (!succeed) return false; diff --git a/cocos/network/CCDownloader-curl.cpp b/cocos/network/CCDownloader-curl.cpp index 242d924e5a..27276f7445 100644 --- a/cocos/network/CCDownloader-curl.cpp +++ b/cocos/network/CCDownloader-curl.cpp @@ -35,7 +35,7 @@ #include "base/CCScheduler.h" #include "platform/CCFileUtils.h" #include "network/CCDownloader.h" -#include "platform/PXFileStream.h" +#include "platform/CCFileStream.h" #include "md5/md5.h" #include "yasio/xxsocket.hpp" @@ -151,7 +151,7 @@ namespace cocos2d { namespace network { } } // open file - _fs.open(_tempFileName, PXFileStream::kModeAppend); + _fs.open(_tempFileName, FileStream::Mode::APPEND); if (!_fs) { _errCode = DownloadTask::ERROR_OPEN_FILE_FAILED; @@ -163,7 +163,7 @@ namespace cocos2d { namespace network { // init md5 state _checksumFileName = filename + ".chksum"; - _fsMd5.open(_checksumFileName.c_str(), PXFileStream::kModeWrite); + _fsMd5.open(_checksumFileName.c_str(), FileStream::Mode::WRITE); if (_fsMd5.seek(0, SEEK_END) != sizeof(md5_state_s)) { md5_init(&_md5State); } @@ -305,10 +305,10 @@ namespace cocos2d { namespace network { string _tempFileName; std::string _checksumFileName; vector _buf; - PXFileStream _fs; + FileStream _fs; // calculate md5 in downloading time support - PXFileStream _fsMd5; // store md5 state realtime + FileStream _fsMd5; // store md5 state realtime md5_state_s _md5State; @@ -901,8 +901,8 @@ public: if (checkState & kCheckSumStateSucceed) // No need download { - PXFileStream fsOrigin; - if (fsOrigin.open(coTask._fileName)) { + FileStream fsOrigin; + if (fsOrigin.open(coTask._fileName, FileStream::Mode::READ)) { task.progressInfo.totalBytesExpected = fsOrigin.seek(0, SEEK_END); task.progressInfo.bytesReceived = task.progressInfo.totalBytesExpected; task.progressInfo.totalBytesReceived = task.progressInfo.totalBytesExpected; diff --git a/cocos/platform/PXFileStream.cpp b/cocos/platform/CCFileStream.cpp similarity index 88% rename from cocos/platform/PXFileStream.cpp rename to cocos/platform/CCFileStream.cpp index 7e0e479dc4..b4166cf14f 100644 --- a/cocos/platform/PXFileStream.cpp +++ b/cocos/platform/CCFileStream.cpp @@ -1,5 +1,6 @@ // Copyright (c) 2018-2019 HALX99. -#include "platform/PXFileStream.h" +// Copyright (c) 2020 c4games.com +#include "platform/CCFileStream.h" #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID #include "base/ZipUtils.h" @@ -36,16 +37,16 @@ struct PXIoF { int(*close)(PXFileHandle& handle); }; -static int pfs_posix_open(const std::string& path, int mode, PXFileHandle& handle) +static int pfs_posix_open(const std::string& path, FileStream::Mode mode, PXFileHandle& handle) { switch (mode) { - case PXFileStream::kModeReadOnly: + case FileStream::Mode::READ: handle._fd = posix_open(path.c_str(), O_READ_FLAGS); break; - case PXFileStream::kModeWrite: + case FileStream::Mode::WRITE: handle._fd = posix_open(path.c_str(), O_WRITE_FLAGS); break; - case PXFileStream::kModeAppend: + case FileStream::Mode::APPEND: handle._fd = posix_open(path.c_str(), O_APPEND_FLAGS); break; default: @@ -102,16 +103,16 @@ static PXIoF pfs_obb_iof = { }; #endif -PXFileStream::PXFileStream() : _iof(&pfs_posix_iof) +FileStream::FileStream() : _iof(&pfs_posix_iof) { } -PXFileStream::~PXFileStream() +FileStream::~FileStream() { this->close(); } -bool PXFileStream::open(const std::string& path, int mode) +bool FileStream::open(const std::string& path, FileStream::Mode mode) { #if CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID return pfs_posix_open(path, mode, _handle) != -1; @@ -152,7 +153,7 @@ bool PXFileStream::open(const std::string& path, int mode) #endif } -PXFileStream::operator bool() const +FileStream::operator bool() const { #if CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID return _handle._fd != -1; @@ -161,22 +162,22 @@ PXFileStream::operator bool() const #endif } -int PXFileStream::close() +int FileStream::close() { return _iof->close(_handle); } -int PXFileStream::seek(long offset, int origin) +int FileStream::seek(long offset, int origin) { return _iof->seek(_handle, offset, origin); } -int PXFileStream::read(void* buf, unsigned int size) +int FileStream::read(void* buf, unsigned int size) { return _iof->read(_handle, buf, size); } -int PXFileStream::write(const void* buf, unsigned int size) +int FileStream::write(const void* buf, unsigned int size) { return posix_write(_handle._fd, buf, size); } diff --git a/cocos/platform/PXFileStream.h b/cocos/platform/CCFileStream.h similarity index 75% rename from cocos/platform/PXFileStream.h rename to cocos/platform/CCFileStream.h index d82b4cce5f..7f817eb213 100644 --- a/cocos/platform/PXFileStream.h +++ b/cocos/platform/CCFileStream.h @@ -1,4 +1,5 @@ -// Copyright (c) 2018-2019 HALX99, TODO: may rename this file to PXFileStream +// Copyright (c) 2018-2019 HALX99 +// Copyright (c) 2020 c4games.com #pragma once #include "platform/CCPlatformConfig.h" @@ -36,18 +37,18 @@ union PXFileHandle { struct PXIoF; -class CC_DLL PXFileStream { +class CC_DLL FileStream { public: - PXFileStream(); - ~PXFileStream(); + FileStream(); + ~FileStream(); - enum Mode { - kModeReadOnly, - kModeWrite, - kModeAppend, + enum class Mode { + READ, + WRITE, + APPEND, }; - bool open(const std::string& path, int mode = kModeReadOnly); + bool open(const std::string& path, Mode mode); int close(); int seek(long offset, int origin); diff --git a/cocos/platform/CMakeLists.txt b/cocos/platform/CMakeLists.txt index f053f23d50..e89f47f0d1 100644 --- a/cocos/platform/CMakeLists.txt +++ b/cocos/platform/CMakeLists.txt @@ -144,7 +144,7 @@ set(COCOS_PLATFORM_HEADER platform/CCPlatformMacros.h platform/CCSAXParser.h platform/CCStdC.h - platform/PXFileStream.h + platform/CCFileStream.h ) set(COCOS_PLATFORM_SRC @@ -153,5 +153,5 @@ set(COCOS_PLATFORM_SRC platform/CCGLView.cpp platform/CCFileUtils.cpp platform/CCImage.cpp - platform/PXFileStream.cpp + platform/CCFileStream.cpp )