From 6a065c913e9308cc72e1ad0723b6167048f439b6 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 8 Aug 2019 08:56:51 +0800 Subject: *misc --- .../asura-base/FileSystem/Binding/_data_buffer.cpp | 6 +- .../asura-base/FileSystem/Binding/_file.cpp | 6 +- .../asura-base/FileSystem/Binding/_file_data.cpp | 6 +- .../FileSystem/Binding/_file_manager.cpp | 6 +- .../asura-base/FileSystem/Binding/_io_task.cpp | 6 +- .../modules/asura-base/FileSystem/Compressor.cpp | 10 +- Source/modules/asura-base/FileSystem/Compressor.h | 28 +- .../modules/asura-base/FileSystem/DataBuffer.cpp | 6 +- Source/modules/asura-base/FileSystem/DataBuffer.h | 132 +++--- Source/modules/asura-base/FileSystem/DecodedData.h | 52 ++- Source/modules/asura-base/FileSystem/File.cpp | 484 ++++++++++----------- Source/modules/asura-base/FileSystem/File.h | 229 +++++----- Source/modules/asura-base/FileSystem/FileData.cpp | 104 +++-- Source/modules/asura-base/FileSystem/FileData.h | 84 ++-- .../modules/asura-base/FileSystem/FileManager.cpp | 320 +++++++------- Source/modules/asura-base/FileSystem/FileManager.h | 142 +++--- Source/modules/asura-base/FileSystem/IOBatchTask.h | 36 +- Source/modules/asura-base/FileSystem/IOTask.cpp | 86 ++-- Source/modules/asura-base/FileSystem/IOTask.h | 64 ++- Source/modules/asura-base/FileSystem/Renewable.h | 30 +- 20 files changed, 882 insertions(+), 955 deletions(-) (limited to 'Source/modules/asura-base/FileSystem') diff --git a/Source/modules/asura-base/FileSystem/Binding/_data_buffer.cpp b/Source/modules/asura-base/FileSystem/Binding/_data_buffer.cpp index ff1f0dc..3f5ee57 100644 --- a/Source/modules/asura-base/FileSystem/Binding/_data_buffer.cpp +++ b/Source/modules/asura-base/FileSystem/Binding/_data_buffer.cpp @@ -2,10 +2,8 @@ using namespace Luax; -namespace AsuraEngine -{ - namespace FileSystem - { +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) LUAX_REGISTRY(DataBuffer) { diff --git a/Source/modules/asura-base/FileSystem/Binding/_file.cpp b/Source/modules/asura-base/FileSystem/Binding/_file.cpp index d19c02a..f2a45f9 100644 --- a/Source/modules/asura-base/FileSystem/Binding/_file.cpp +++ b/Source/modules/asura-base/FileSystem/Binding/_file.cpp @@ -1,9 +1,7 @@ #include "../file.h" -namespace AsuraEngine -{ - namespace FileSystem - { +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) LUAX_REGISTRY(File) { diff --git a/Source/modules/asura-base/FileSystem/Binding/_file_data.cpp b/Source/modules/asura-base/FileSystem/Binding/_file_data.cpp index ca872d2..3ecf2cf 100644 --- a/Source/modules/asura-base/FileSystem/Binding/_file_data.cpp +++ b/Source/modules/asura-base/FileSystem/Binding/_file_data.cpp @@ -2,10 +2,8 @@ using namespace std; -namespace AsuraEngine -{ - namespace FileSystem - { +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) LUAX_REGISTRY(FileData) { diff --git a/Source/modules/asura-base/FileSystem/Binding/_file_manager.cpp b/Source/modules/asura-base/FileSystem/Binding/_file_manager.cpp index 7781d99..6852c37 100644 --- a/Source/modules/asura-base/FileSystem/Binding/_file_manager.cpp +++ b/Source/modules/asura-base/FileSystem/Binding/_file_manager.cpp @@ -2,10 +2,8 @@ using namespace Luax; -namespace AsuraEngine -{ - namespace FileSystem - { +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) #define PREPARE(l) \ LUAX_STATE(l); \ diff --git a/Source/modules/asura-base/FileSystem/Binding/_io_task.cpp b/Source/modules/asura-base/FileSystem/Binding/_io_task.cpp index a3d82d5..82bb2ed 100644 --- a/Source/modules/asura-base/FileSystem/Binding/_io_task.cpp +++ b/Source/modules/asura-base/FileSystem/Binding/_io_task.cpp @@ -2,10 +2,8 @@ using namespace std; -namespace AsuraEngine -{ - namespace FileSystem - { +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) LUAX_REGISTRY(IOTask) { diff --git a/Source/modules/asura-base/FileSystem/Compressor.cpp b/Source/modules/asura-base/FileSystem/Compressor.cpp index eaa59a2..192ef7d 100644 --- a/Source/modules/asura-base/FileSystem/Compressor.cpp +++ b/Source/modules/asura-base/FileSystem/Compressor.cpp @@ -1,11 +1,9 @@ #include "Compressor.h" -namespace AsuraEngine -{ - namespace FileSystem - { +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) - } -} \ No newline at end of file +namespace_end +namespace_end \ No newline at end of file diff --git a/Source/modules/asura-base/FileSystem/Compressor.h b/Source/modules/asura-base/FileSystem/Compressor.h index 9fd0808..f06c5f4 100644 --- a/Source/modules/asura-base/FileSystem/Compressor.h +++ b/Source/modules/asura-base/FileSystem/Compressor.h @@ -3,26 +3,24 @@ #include "../Scripting/Scripting.h" -namespace AsuraEngine -{ - namespace FileSystem - { +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) - class Compressor ASURA_FINAL - : public AEScripting::Portable - { - public: +class Compressor ASURA_FINAL + : public AEScripting::Portable +{ +public: - LUAX_DECL_SINGLETON(Compressor); + LUAX_DECL_SINGLETON(Compressor); - private: +private: - LUAX_DECL_METHOD(_Compress); - LUAX_DECL_METHOD(_Decompress); + LUAX_DECL_METHOD(_Compress); + LUAX_DECL_METHOD(_Decompress); - }; +}; - } -} +namespace_end +namespace_end #endif \ No newline at end of file diff --git a/Source/modules/asura-base/FileSystem/DataBuffer.cpp b/Source/modules/asura-base/FileSystem/DataBuffer.cpp index 71431e1..cd6191b 100644 --- a/Source/modules/asura-base/FileSystem/DataBuffer.cpp +++ b/Source/modules/asura-base/FileSystem/DataBuffer.cpp @@ -4,10 +4,8 @@ using namespace AEThreading; -namespace AsuraEngine -{ - namespace FileSystem - { +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) DataBuffer::DataBuffer(DataBuffer& src) : m_Size(0) diff --git a/Source/modules/asura-base/FileSystem/DataBuffer.h b/Source/modules/asura-base/FileSystem/DataBuffer.h index ace372e..da302ce 100644 --- a/Source/modules/asura-base/FileSystem/DataBuffer.h +++ b/Source/modules/asura-base/FileSystem/DataBuffer.h @@ -6,81 +6,79 @@ #include "../Scripting/Scripting.h" #include "../Threads/Mutex.h" -namespace AsuraEngine +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) + +/// +/// 对内存数据的封装,所有的数据使用Data buffer包装,不直接使用const void*。通过resource manager读取。 +/// +class DataBuffer ASURA_FINAL + : public AEScripting::Portable { - namespace FileSystem - { - - /// - /// 对内存数据的封装,所有的数据使用Data buffer包装,不直接使用const void*。通过resource manager读取。 - /// - class DataBuffer ASURA_FINAL - : public AEScripting::Portable - { - public: - - LUAX_DECL_FACTORY(DataBuffer); - - DataBuffer(DataBuffer& src); - DataBuffer(std::size_t capacity); - DataBuffer(const void* bytes, std::size_t size); - ~DataBuffer(); - - byte* GetData(); - size_t GetSize(); - size_t GetCapacity(); - - /// - /// 修改容量,并清空 - /// - void Refactor(size_t capacity); - - /// - /// 拷贝数据源到此缓冲 - /// - void Load(DataBuffer& db); - void Load(const void* bytes, std::size_t size); - - /// - /// 绑定bytes,并拥有所有权,capacity为size - /// - void Move(void* bytes, std::size_t size); - - /// - /// 清理缓冲 - /// - void Clear(); - - void Lock(); - void Unlock(); +public: + + LUAX_DECL_FACTORY(DataBuffer); + + DataBuffer(DataBuffer& src); + DataBuffer(std::size_t capacity); + DataBuffer(const void* bytes, std::size_t size); + ~DataBuffer(); + + byte* GetData(); + size_t GetSize(); + size_t GetCapacity(); + + /// + /// 修改容量,并清空 + /// + void Refactor(size_t capacity); + + /// + /// 拷贝数据源到此缓冲 + /// + void Load(DataBuffer& db); + void Load(const void* bytes, std::size_t size); + + /// + /// 绑定bytes,并拥有所有权,capacity为size + /// + void Move(void* bytes, std::size_t size); + + /// + /// 清理缓冲 + /// + void Clear(); + + void Lock(); + void Unlock(); - private: +private: - LUAX_DECL_METHOD(_New); - LUAX_DECL_METHOD(_GetData); - LUAX_DECL_METHOD(_GetSize); - LUAX_DECL_METHOD(_GetCapacity); - LUAX_DECL_METHOD(_Refactor); - LUAX_DECL_METHOD(_Load); - LUAX_DECL_METHOD(_Clear); + LUAX_DECL_METHOD(_New); + LUAX_DECL_METHOD(_GetData); + LUAX_DECL_METHOD(_GetSize); + LUAX_DECL_METHOD(_GetCapacity); + LUAX_DECL_METHOD(_Refactor); + LUAX_DECL_METHOD(_Load); + LUAX_DECL_METHOD(_Clear); - /// - /// Buffer首地址和里面数据的长度 - /// - byte* m_Bytes; - size_t m_Size; + /// + /// Buffer首地址和里面数据的长度 + /// + byte* m_Bytes; + size_t m_Size; - /// - /// Buffer容量。 - /// - size_t m_Capacity; + /// + /// Buffer容量。 + /// + size_t m_Capacity; - AEThreading::Mutex m_Mutex; + AEThreading::Mutex m_Mutex; - }; +}; - } -} +namespace_end +namespace_end namespace AEFileSystem = AsuraEngine::FileSystem; diff --git a/Source/modules/asura-base/FileSystem/DecodedData.h b/Source/modules/asura-base/FileSystem/DecodedData.h index 1744233..f83d1c3 100644 --- a/Source/modules/asura-base/FileSystem/DecodedData.h +++ b/Source/modules/asura-base/FileSystem/DecodedData.h @@ -9,33 +9,31 @@ #include "DataBuffer.h" -namespace AsuraEngine +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) + +/// +/// 可以在另一个线程构建的data继承此类。如图片数据、音频数据等,可以在另一个线程中解析原 +/// 文件,生成内部数据格式,如像素等。 +/// +ASURA_ABSTRACT class DecodedData { - namespace FileSystem - { - - /// - /// 可以在另一个线程构建的data继承此类。如图片数据、音频数据等,可以在另一个线程中解析原 - /// 文件,生成内部数据格式,如像素等。 - /// - ASURA_ABSTRACT class DecodedData - { - public: - - /// - /// 从内存中构建data,可以放在另一个线程里面,从资源管理系统里面加载。 - /// - DecodedData() {}; - virtual ~DecodedData() {}; - - /// - /// 解码内存中的数据并以某种格式保存。 - /// - virtual void Decode(DataBuffer& buffer) = 0; - - }; - - } -} +public: + + /// + /// 从内存中构建data,可以放在另一个线程里面,从资源管理系统里面加载。 + /// + DecodedData() {}; + virtual ~DecodedData() {}; + + /// + /// 解码内存中的数据并以某种格式保存。 + /// + virtual void Decode(DataBuffer& buffer) = 0; + +}; + +namespace_end +namespace_end #endif \ No newline at end of file diff --git a/Source/modules/asura-base/FileSystem/File.cpp b/Source/modules/asura-base/FileSystem/File.cpp index ec8a9bf..30a75e0 100644 --- a/Source/modules/asura-base/FileSystem/File.cpp +++ b/Source/modules/asura-base/FileSystem/File.cpp @@ -4,291 +4,289 @@ #include "File.h" -namespace AsuraEngine +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) + +File::File(const std::string& filename) + : m_FileName(filename) + , m_FileHandle(nullptr) + , m_Mode(FILE_MODE_CLOSED) + , m_BufferMode(BUFFER_MODE_NONE) + , m_BufferSize(0) { - namespace FileSystem + size_t dot = filename.rfind('.'); + if (dot != std::string::npos) { + m_Extension = filename.substr(dot + 1); + m_Name = filename.substr(0, dot); + } + else + m_Name = filename; +} - File::File(const std::string& filename) - : m_FileName(filename) - , m_FileHandle(nullptr) - , m_Mode(FILE_MODE_CLOSED) - , m_BufferMode(BUFFER_MODE_NONE) - , m_BufferSize(0) - { - size_t dot = filename.rfind('.'); - if (dot != std::string::npos) - { - m_Extension = filename.substr(dot + 1); - m_Name = filename.substr(0, dot); - } - else - m_Name = filename; - } +File::~File() +{ + if (m_Mode != FILE_MODE_CLOSED) + Close(); +} - File::~File() - { - if (m_Mode != FILE_MODE_CLOSED) - Close(); - } +bool File::Open(FileMode mode) +{ + if (!PHYSFS_isInit()) + throw Exception("Physfs is NOT initialized."); - bool File::Open(FileMode mode) - { - if (!PHYSFS_isInit()) - throw Exception("Physfs is NOT initialized."); - - if (mode == FILE_MODE_CLOSED) - return false; - - if (mode == FILE_MODE_READ && !PHYSFS_exists(m_FileName.c_str())) - throw Exception("Could NOT open file %s. Does not exist.", m_FileName.c_str()); - - if (mode == FILE_MODE_APPEND || mode == FILE_MODE_WRITE) - { - if (!PHYSFS_getWriteDir()) - { - throw Exception("Could NOT set write directory."); - } - } - - // 已经在之前打开过,就不再创建新的handle了 - if (m_FileHandle != nullptr) - return true; - - PHYSFS_getLastErrorCode(); - - PHYSFS_File* handle = nullptr; - - switch (mode) - { - case FILE_MODE_READ: - handle = PHYSFS_openRead(m_FileName.c_str()); - break; - case FILE_MODE_APPEND: - handle = PHYSFS_openAppend(m_FileName.c_str()); - break; - case FILE_MODE_WRITE: - handle = PHYSFS_openWrite(m_FileName.c_str()); - break; - } - - if (handle == nullptr) - { - const char *err = PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()); - if (err == nullptr) - err = "unknown error"; - throw Exception("Could not open file %s (%s)", m_FileName.c_str(), err); - } - - m_FileHandle = handle; - m_Mode = mode; - - if (m_FileHandle && !SetBuffer(m_BufferMode,m_BufferSize)) - { - m_BufferMode = BUFFER_MODE_NONE; - m_BufferSize = 0; - } - - return m_FileHandle != nullptr; - } + if (mode == FILE_MODE_CLOSED) + return false; - bool File::Close() - { - if (m_FileHandle == nullptr || !PHYSFS_close(m_FileHandle)) - return false; - m_Mode = FILE_MODE_CLOSED; - m_FileHandle = nullptr; - return true; - } + if (mode == FILE_MODE_READ && !PHYSFS_exists(m_FileName.c_str())) + throw Exception("Could NOT open file %s. Does not exist.", m_FileName.c_str()); - bool File::IsOpen() + if (mode == FILE_MODE_APPEND || mode == FILE_MODE_WRITE) + { + if (!PHYSFS_getWriteDir()) { - return m_Mode != FILE_MODE_CLOSED && m_FileHandle != nullptr; + throw Exception("Could NOT set write directory."); } + } - size_t File::GetSize() - { - if (m_FileHandle == nullptr) - { - Open(FILE_MODE_READ); - size_t size = PHYSFS_fileLength(m_FileHandle); - Close(); - return size; - } - return PHYSFS_fileLength(m_FileHandle); - } + // 已经在之前打开过,就不再创建新的handle了 + if (m_FileHandle != nullptr) + return true; - size_t File::Read(ASURA_OUT DataBuffer* dst, size_t length) - { - ASSERT(dst); + PHYSFS_getLastErrorCode(); - if (dst->GetCapacity() < length) - throw Exception("Data buffer is too small compares to read length."); + PHYSFS_File* handle = nullptr; - if (!m_FileHandle || m_Mode != FILE_MODE_READ) - throw Exception("File \"%s\" is not opened for reading", m_FileName); + switch (mode) + { + case FILE_MODE_READ: + handle = PHYSFS_openRead(m_FileName.c_str()); + break; + case FILE_MODE_APPEND: + handle = PHYSFS_openAppend(m_FileName.c_str()); + break; + case FILE_MODE_WRITE: + handle = PHYSFS_openWrite(m_FileName.c_str()); + break; + } - size_t max = PHYSFS_fileLength(m_FileHandle); - length = (length > max) ? max : length; + if (handle == nullptr) + { + const char *err = PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()); + if (err == nullptr) + err = "unknown error"; + throw Exception("Could not open file %s (%s)", m_FileName.c_str(), err); + } - if (length < 0) - throw Exception("Invalid read size."); + m_FileHandle = handle; + m_Mode = mode; - dst->Lock(); - size_t size = PHYSFS_readBytes(m_FileHandle, dst->GetData(), length); - dst->Unlock(); - return size; - } + if (m_FileHandle && !SetBuffer(m_BufferMode,m_BufferSize)) + { + m_BufferMode = BUFFER_MODE_NONE; + m_BufferSize = 0; + } - size_t File::ReadAll(ASURA_OUT DataBuffer* dst) - { - ASSERT(dst); + return m_FileHandle != nullptr; +} - if (!m_FileHandle || m_Mode != FILE_MODE_READ) - throw Exception("File \"%s\" is not opened for reading", m_FileName); +bool File::Close() +{ + if (m_FileHandle == nullptr || !PHYSFS_close(m_FileHandle)) + return false; + m_Mode = FILE_MODE_CLOSED; + m_FileHandle = nullptr; + return true; +} + +bool File::IsOpen() +{ + return m_Mode != FILE_MODE_CLOSED && m_FileHandle != nullptr; +} + +size_t File::GetSize() +{ + if (m_FileHandle == nullptr) + { + Open(FILE_MODE_READ); + size_t size = PHYSFS_fileLength(m_FileHandle); + Close(); + return size; + } + return PHYSFS_fileLength(m_FileHandle); +} - size_t length = PHYSFS_fileLength(m_FileHandle); +size_t File::Read(ASURA_OUT DataBuffer* dst, size_t length) +{ + ASSERT(dst); - if (dst->GetCapacity() < length) - throw Exception("Data buffer is too small compares to file length."); + if (dst->GetCapacity() < length) + throw Exception("Data buffer is too small compares to read length."); - dst->Lock(); - byte* data = dst->GetData(); - size_t size = PHYSFS_readBytes(m_FileHandle, data, length); - dst->Move(data, length); - dst->Unlock(); - return size; - } + if (!m_FileHandle || m_Mode != FILE_MODE_READ) + throw Exception("File \"%s\" is not opened for reading", m_FileName); + + size_t max = PHYSFS_fileLength(m_FileHandle); + length = (length > max) ? max : length; + + if (length < 0) + throw Exception("Invalid read size."); + + dst->Lock(); + size_t size = PHYSFS_readBytes(m_FileHandle, dst->GetData(), length); + dst->Unlock(); + return size; +} + +size_t File::ReadAll(ASURA_OUT DataBuffer* dst) +{ + ASSERT(dst); + + if (!m_FileHandle || m_Mode != FILE_MODE_READ) + throw Exception("File \"%s\" is not opened for reading", m_FileName); + + size_t length = PHYSFS_fileLength(m_FileHandle); + + if (dst->GetCapacity() < length) + throw Exception("Data buffer is too small compares to file length."); + + dst->Lock(); + byte* data = dst->GetData(); + size_t size = PHYSFS_readBytes(m_FileHandle, data, length); + dst->Move(data, length); + dst->Unlock(); + return size; +} #ifdef ASURA_WINDOWS - inline bool test_eof(File *that, PHYSFS_File *) - { - int64 pos = that->Tell(); - int64 size = that->GetSize(); - return pos == -1 || size == -1 || pos >= size; - } +inline bool test_eof(File *that, PHYSFS_File *) +{ + int64 pos = that->Tell(); + int64 size = that->GetSize(); + return pos == -1 || size == -1 || pos >= size; +} #else - inline bool test_eof(File *, PHYSFS_File *file) - { - return PHYSFS_eof(file); - } +inline bool test_eof(File *, PHYSFS_File *file) +{ + return PHYSFS_eof(file); +} #endif - bool File::IsEOF() - { - return m_FileHandle == nullptr || test_eof(this, m_FileHandle); - } +bool File::IsEOF() +{ + return m_FileHandle == nullptr || test_eof(this, m_FileHandle); +} - size_t File::Tell() - { - if (!m_FileHandle) - return - 1; +size_t File::Tell() +{ + if (!m_FileHandle) + return - 1; - return PHYSFS_tell(m_FileHandle); - } + return PHYSFS_tell(m_FileHandle); +} - bool File::Seek(size_t pos) - { - return m_FileHandle != nullptr && PHYSFS_seek(m_FileHandle, pos) != 0; - } +bool File::Seek(size_t pos) +{ + return m_FileHandle != nullptr && PHYSFS_seek(m_FileHandle, pos) != 0; +} - bool File::Write(ASURA_REF DataBuffer* src) - { - if (!m_FileHandle || (m_Mode != FILE_MODE_APPEND && m_Mode != FILE_MODE_WRITE)) - throw Exception("File is not opened for writing."); +bool File::Write(ASURA_REF DataBuffer* src) +{ + if (!m_FileHandle || (m_Mode != FILE_MODE_APPEND && m_Mode != FILE_MODE_WRITE)) + throw Exception("File is not opened for writing."); - byte* data = src->GetData(); - int size = src->GetSize(); + byte* data = src->GetData(); + int size = src->GetSize(); - if (size < 0) - throw Exception("Invalid write size."); + if (size < 0) + throw Exception("Invalid write size."); - size_t written = PHYSFS_writeBytes(m_FileHandle, data, size); + size_t written = PHYSFS_writeBytes(m_FileHandle, data, size); - if (written != src->GetSize()) - return false; + if (written != src->GetSize()) + return false; - // 处理行缓冲 - if (m_BufferSize == BUFFER_MODE_LINE && m_BufferSize > size) - { - if (memchr(data, '\n', size) != nullptr) - Flush(); - } + // 处理行缓冲 + if (m_BufferSize == BUFFER_MODE_LINE && m_BufferSize > size) + { + if (memchr(data, '\n', size) != nullptr) + Flush(); + } - return true; - } + return true; +} - bool File::Flush() - { - if (!m_FileHandle || (m_Mode != FILE_MODE_WRITE && m_Mode != FILE_MODE_APPEND)) - throw Exception("File is not opened for writing."); +bool File::Flush() +{ + if (!m_FileHandle || (m_Mode != FILE_MODE_WRITE && m_Mode != FILE_MODE_APPEND)) + throw Exception("File is not opened for writing."); - return PHYSFS_flush(m_FileHandle) != 0; - } + return PHYSFS_flush(m_FileHandle) != 0; +} - bool File::SetBuffer(BufferMode mode, size_t size) - { - if (size < 0) - return false; - - // If the file isn't open, we'll make sure the buffer values are set in - // File::open. - if (!IsOpen()) - { - m_BufferMode = mode; - m_BufferSize = size; - return true; - } - - int ret = 1; - - switch (mode) - { - case BUFFER_MODE_NONE: - default: - ret = PHYSFS_setBuffer(m_FileHandle, 0); - size = 0; - break; - case BUFFER_MODE_LINE: - case BUFFER_MODE_FULL: - ret = PHYSFS_setBuffer(m_FileHandle, size); - break; - } - - if (ret == 0) - return false; - - m_BufferMode = mode; - m_BufferSize = size; - - return true; - } +bool File::SetBuffer(BufferMode mode, size_t size) +{ + if (size < 0) + return false; - File::BufferMode File::GetBuffer(ASURA_OUT size_t& size) - { - size = m_BufferSize; - return m_BufferMode; - } + // If the file isn't open, we'll make sure the buffer values are set in + // File::open. + if (!IsOpen()) + { + m_BufferMode = mode; + m_BufferSize = size; + return true; + } - const std::string& File::GetFileName() - { - return m_FileName; - } + int ret = 1; - const std::string& File::GetName() - { - return m_Name; - } + switch (mode) + { + case BUFFER_MODE_NONE: + default: + ret = PHYSFS_setBuffer(m_FileHandle, 0); + size = 0; + break; + case BUFFER_MODE_LINE: + case BUFFER_MODE_FULL: + ret = PHYSFS_setBuffer(m_FileHandle, size); + break; + } - const std::string& File::GetExtension() - { - return m_Extension; - } + if (ret == 0) + return false; - File::FileMode File::GetMode() - { - return m_Mode; - } + m_BufferMode = mode; + m_BufferSize = size; - } -} \ No newline at end of file + return true; +} + +File::BufferMode File::GetBuffer(ASURA_OUT size_t& size) +{ + size = m_BufferSize; + return m_BufferMode; +} + +const std::string& File::GetFileName() +{ + return m_FileName; +} + +const std::string& File::GetName() +{ + return m_Name; +} + +const std::string& File::GetExtension() +{ + return m_Extension; +} + +File::FileMode File::GetMode() +{ + return m_Mode; +} + +namespace_end +namespace_end diff --git a/Source/modules/asura-base/FileSystem/File.h b/Source/modules/asura-base/FileSystem/File.h index be772e0..3bb6067 100644 --- a/Source/modules/asura-base/FileSystem/File.h +++ b/Source/modules/asura-base/FileSystem/File.h @@ -8,139 +8,104 @@ #include "FileData.h" -namespace AsuraEngine +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) + +/// +/// 以流的形式打开文件,可以指定读写起点、大小。在期望使用流读取时使用本类,否则使用Filesystem.read()直接读取文件全部 +/// 内容,并返回一个FileData对象。 +/// +class File ASURA_FINAL : public AEScripting::Portable { - namespace FileSystem - { +public: + + LUAX_DECL_FACTORY(File); - /// - /// 以流的形式打开文件,可以指定读写起点、大小。在期望使用流读取时使用本类,否则使用Filesystem.read()直接读取文件全部 - /// 内容,并返回一个FileData对象。 - /// - class File ASURA_FINAL - : public AEScripting::Portable - { - public: - - LUAX_DECL_FACTORY(File); - - /// - /// 文件读写模式 - /// - enum FileMode - { - FILE_MODE_CLOSED, - FILE_MODE_READ, - FILE_MODE_WRITE, - FILE_MODE_APPEND, - }; - - /// - /// 文件写入时缓冲区行为 - /// - enum BufferMode - { - BUFFER_MODE_NONE, ///< 不使用缓冲,立即写入文件 - BUFFER_MODE_LINE, ///< 行缓冲,遇到换行符或者达到缓冲区大小时写入文件 - BUFFER_MODE_FULL, ///< 完全缓冲,缓冲区满时写入文件 - }; - - File(const std::string& filename); - ~File(); - - bool Open(FileMode mode); - bool Close(); - bool IsOpen(); - FileMode GetMode(); - size_t GetSize(); - - /// - /// 读取到data buffer里,并返回读入的内容 - /// - size_t Read(ASURA_OUT DataBuffer* dst, size_t length); - size_t ReadAll(ASURA_OUT DataBuffer* dst); - size_t ReadAsync(ASURA_OUT DataBuffer* dst); - - /// - /// 是否读到了文件结尾 - /// - bool IsEOF(); - - /// - /// 将data buffer中的内容写入,并返回是否成功 - /// - bool Write(ASURA_REF DataBuffer* src); - - /// - /// 异步写文件,将写文件task加入thread的队列。 - /// - bool WriteAsync(ASURA_REF DataBuffer* src, AEThreading::Thread* thread); - - /// - /// 如果开启了缓冲,强制清空缓冲区,写入文件。 - /// - bool Flush(); - - /// - /// 返回当前读写位置 - /// - size_t Tell(); - - /// - /// 跳到对应位置 - /// - bool Seek(size_t pos); - - /// - /// 设置缓冲区大小和模式 - /// - bool SetBuffer(BufferMode mode, size_t size); - - /// - /// 获取缓冲区大小和模式 - /// - BufferMode GetBuffer(ASURA_OUT size_t& size); - - const std::string& GetFileName(); - const std::string& GetName(); - const std::string& GetExtension(); - - private: - - PHYSFS_File* m_FileHandle; ///< physfs 文件 - std::string m_FileName; ///< 文件名 - std::string m_Extension; ///< 不包含点的扩展名 - std::string m_Name; ///< 不包含点和扩展名的文件名 - FileMode m_Mode; ///< 文件打开模式 - BufferMode m_BufferMode; ///< 写入缓冲区模式 - size_t m_BufferSize; ///< 写入缓冲区大小 - - LUAX_DECL_ENUM(FileMode); - LUAX_DECL_ENUM(BufferMode); - - LUAX_DECL_METHOD(_New); - LUAX_DECL_METHOD(_Open); - LUAX_DECL_METHOD(_Close); - LUAX_DECL_METHOD(_IsOpen); - LUAX_DECL_METHOD(_GetMode); - LUAX_DECL_METHOD(_GetSize); - LUAX_DECL_METHOD(_Read); - LUAX_DECL_METHOD(_Write); - LUAX_DECL_METHOD(_ReadAsync); - LUAX_DECL_METHOD(_WriteAsync); - LUAX_DECL_METHOD(_IsEOF); - LUAX_DECL_METHOD(_Flush); - LUAX_DECL_METHOD(_Tell); - LUAX_DECL_METHOD(_Seek); - LUAX_DECL_METHOD(_SetBuffer); - LUAX_DECL_METHOD(_GetBuffer); - LUAX_DECL_METHOD(_GetFileName); - LUAX_DECL_METHOD(_GetExtension); - LUAX_DECL_METHOD(_GetName); - - }; - - } -} + /// 文件读写模式 + enum FileMode + { + FILE_MODE_CLOSED, + FILE_MODE_READ, + FILE_MODE_WRITE, + FILE_MODE_APPEND, + }; + + /// 文件写入时缓冲区行为 + enum BufferMode + { + BUFFER_MODE_NONE, ///< 不使用缓冲,立即写入文件 + BUFFER_MODE_LINE, ///< 行缓冲,遇到换行符或者达到缓冲区大小时写入文件 + BUFFER_MODE_FULL, ///< 完全缓冲,缓冲区满时写入文件 + }; + + File(const std::string& filename); + ~File(); + + bool Open(FileMode mode); + bool Close(); + bool IsOpen(); + FileMode GetMode(); + size_t GetSize(); + + size_t Read(ASURA_OUT DataBuffer* dst, size_t length); + size_t ReadAll(ASURA_OUT DataBuffer* dst); + size_t ReadAsync(ASURA_OUT DataBuffer* dst); + + bool IsEOF(); + + bool Write(ASURA_REF DataBuffer* src); + bool WriteAsync(ASURA_REF DataBuffer* src, AEThreading::Thread* thread); + + bool Flush(); + + size_t Tell(); + + bool Seek(size_t pos); + + bool SetBuffer(BufferMode mode, size_t size); + + BufferMode GetBuffer(ASURA_OUT size_t& size); + + const std::string& GetFileName(); + const std::string& GetName(); + const std::string& GetExtension(); + +private: + + PHYSFS_File* m_FileHandle; ///< physfs 文件 + std::string m_FileName; ///< 文件名 + std::string m_Extension; ///< 不包含点的扩展名 + std::string m_Name; ///< 不包含点和扩展名的文件名 + FileMode m_Mode; ///< 文件打开模式 + BufferMode m_BufferMode; ///< 写入缓冲区模式 + size_t m_BufferSize; ///< 写入缓冲区大小 + + LUAX_DECL_ENUM(FileMode); + LUAX_DECL_ENUM(BufferMode); + + LUAX_DECL_METHOD(_New); + LUAX_DECL_METHOD(_Open); + LUAX_DECL_METHOD(_Close); + LUAX_DECL_METHOD(_IsOpen); + LUAX_DECL_METHOD(_GetMode); + LUAX_DECL_METHOD(_GetSize); + LUAX_DECL_METHOD(_Read); + LUAX_DECL_METHOD(_Write); + LUAX_DECL_METHOD(_ReadAsync); + LUAX_DECL_METHOD(_WriteAsync); + LUAX_DECL_METHOD(_IsEOF); + LUAX_DECL_METHOD(_Flush); + LUAX_DECL_METHOD(_Tell); + LUAX_DECL_METHOD(_Seek); + LUAX_DECL_METHOD(_SetBuffer); + LUAX_DECL_METHOD(_GetBuffer); + LUAX_DECL_METHOD(_GetFileName); + LUAX_DECL_METHOD(_GetExtension); + LUAX_DECL_METHOD(_GetName); + +}; + +namespace_end +namespace_end #endif \ No newline at end of file diff --git a/Source/modules/asura-base/FileSystem/FileData.cpp b/Source/modules/asura-base/FileSystem/FileData.cpp index b29a95b..6caedad 100644 --- a/Source/modules/asura-base/FileSystem/FileData.cpp +++ b/Source/modules/asura-base/FileSystem/FileData.cpp @@ -1,59 +1,57 @@ #include "FileData.h" -namespace AsuraEngine +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) + +FileData::FileData(const std::string& filename) + : m_Data(nullptr) + , m_FileName(filename) { - namespace FileSystem + size_t dot = filename.rfind('.'); + if (dot != std::string::npos) { + m_Extension = filename.substr(dot + 1); + m_Name = filename.substr(0, dot); + } + else + m_Name = filename; +} - FileData::FileData(const std::string& filename) - : m_Data(nullptr) - , m_FileName(filename) - { - size_t dot = filename.rfind('.'); - if (dot != std::string::npos) - { - m_Extension = filename.substr(dot + 1); - m_Name = filename.substr(0, dot); - } - else - m_Name = filename; - } - - FileData::~FileData() - { - if (m_Data) - m_Data->Release(); - } - - const std::string& FileData::GetFileName() - { - return m_FileName; - } - - const std::string& FileData::GetExtension() - { - return m_Extension; - } - - const std::string& FileData::GetName() - { - return m_Name; - } - - void FileData::BindData(ASURA_MOVE DataBuffer* buffer) - { - if (!buffer) - return; - if (m_Data) - m_Data->Release(); - m_Data = buffer; - m_Data->Retain(); - } - - DataBuffer* FileData::GetDataBuffer() - { - return m_Data; - } +FileData::~FileData() +{ + if (m_Data) + m_Data->Release(); +} - } -} \ No newline at end of file +const std::string& FileData::GetFileName() +{ + return m_FileName; +} + +const std::string& FileData::GetExtension() +{ + return m_Extension; +} + +const std::string& FileData::GetName() +{ + return m_Name; +} + +void FileData::BindData(ASURA_MOVE DataBuffer* buffer) +{ + if (!buffer) + return; + if (m_Data) + m_Data->Release(); + m_Data = buffer; + m_Data->Retain(); +} + +DataBuffer* FileData::GetDataBuffer() +{ + return m_Data; +} + +namespace_end +namespace_end \ No newline at end of file diff --git a/Source/modules/asura-base/FileSystem/FileData.h b/Source/modules/asura-base/FileSystem/FileData.h index d0acd26..e79d42c 100644 --- a/Source/modules/asura-base/FileSystem/FileData.h +++ b/Source/modules/asura-base/FileSystem/FileData.h @@ -7,63 +7,61 @@ #include "DataBuffer.h" -namespace AsuraEngine -{ - namespace FileSystem - { +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) - class Filesystem; +class Filesystem; - /// - /// 当从filesystem直接读取整个文件时,返回FileData对象描述文件内容和其他信息。由Filesystem创建。 - /// - class FileData ASURA_FINAL - : public AEScripting::Portable - { - public: +/// +/// 当从filesystem直接读取整个文件时,返回FileData对象描述文件内容和其他信息。由Filesystem创建。 +/// +class FileData ASURA_FINAL + : public AEScripting::Portable +{ +public: - LUAX_DECL_FACTORY(FileData); + LUAX_DECL_FACTORY(FileData); - ~FileData(); + ~FileData(); - /// - /// 返回文件内容,可以通过Databuffer获得内容和大小。由于内部接口都是以Data buffer作为参数,所以这里也返回data buffer。 - /// - DataBuffer* GetDataBuffer(); + /// + /// 返回文件内容,可以通过Databuffer获得内容和大小。由于内部接口都是以Data buffer作为参数,所以这里也返回data buffer。 + /// + DataBuffer* GetDataBuffer(); - const std::string& GetFileName(); - const std::string& GetExtension(); - const std::string& GetName(); + const std::string& GetFileName(); + const std::string& GetExtension(); + const std::string& GetName(); - private: +private: - friend class FileManager; + friend class FileManager; - FileData(const std::string& name); + FileData(const std::string& name); - /// - /// 绑定data buffer。 - /// - void BindData(ASURA_MOVE DataBuffer* buffer); + /// + /// 绑定data buffer。 + /// + void BindData(ASURA_MOVE DataBuffer* buffer); - /// - /// Data buffer不会再filedata析构时销毁,当lua引用计数为0时由lua调用GC销毁。创建mData时会添加一个成员引用。 - /// - ASURA_REF DataBuffer* m_Data; - Luax::LuaxMemberRef m_DataRef; + /// + /// Data buffer不会再filedata析构时销毁,当lua引用计数为0时由lua调用GC销毁。创建mData时会添加一个成员引用。 + /// + ASURA_REF DataBuffer* m_Data; + Luax::LuaxMemberRef m_DataRef; - std::string m_FileName; ///< 包含扩展名的文件名 - std::string m_Extension; ///< 不包含点的扩展名 - std::string m_Name; ///< 不包含点和后缀的文件名 + std::string m_FileName; ///< 包含扩展名的文件名 + std::string m_Extension; ///< 不包含点的扩展名 + std::string m_Name; ///< 不包含点和后缀的文件名 - LUAX_DECL_METHOD(_GetDataBuffer); - LUAX_DECL_METHOD(_GetFileName); - LUAX_DECL_METHOD(_GetExtension); - LUAX_DECL_METHOD(_GetName); + LUAX_DECL_METHOD(_GetDataBuffer); + LUAX_DECL_METHOD(_GetFileName); + LUAX_DECL_METHOD(_GetExtension); + LUAX_DECL_METHOD(_GetName); - }; +}; - } -} +namespace_end +namespace_end #endif \ No newline at end of file diff --git a/Source/modules/asura-base/FileSystem/FileManager.cpp b/Source/modules/asura-base/FileSystem/FileManager.cpp index bdb4069..c845c9c 100644 --- a/Source/modules/asura-base/FileSystem/FileManager.cpp +++ b/Source/modules/asura-base/FileSystem/FileManager.cpp @@ -8,191 +8,189 @@ using namespace std; -namespace AsuraEngine -{ - namespace FileSystem - { +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) #ifdef ASURA_WINDOWS - #include - #include +#include +#include #else - #include - #include +#include +#include #endif - FileManager::~FileManager() - { - if (m_Inited) //PHYSFS_isInit - PHYSFS_deinit(); - } +FileManager::~FileManager() +{ + if (m_Inited) //PHYSFS_isInit + PHYSFS_deinit(); +} - void FileManager::Init(const char* arg0) - { - if (!PHYSFS_init(arg0)) - throw Exception("Failed to initialize filesystem: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); +void FileManager::Init(const char* arg0) +{ + if (!PHYSFS_init(arg0)) + throw Exception("Failed to initialize filesystem: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())); - m_Inited = true; - } + m_Inited = true; +} - bool FileManager::Mount(const std::string& locpath, const std::string& montpoint/* = "/"*/, bool prepend /*= false*/) - { - if (!m_Inited) - return false; +bool FileManager::Mount(const std::string& locpath, const std::string& montpoint/* = "/"*/, bool prepend /*= false*/) +{ + if (!m_Inited) + return false; - return PHYSFS_mount(locpath.c_str(), montpoint.c_str(), !prepend); - } + return PHYSFS_mount(locpath.c_str(), montpoint.c_str(), !prepend); +} - bool FileManager::Mount(DataBuffer* db, const std::string& archivename, const std::string& mountpoint /*= "/"*/, bool prepend /*= false*/) - { - if (!m_Inited) - return false; - if (PHYSFS_mountMemory(db->GetData(), db->GetSize(), nullptr, archivename.c_str(), mountpoint.c_str(), !prepend)) - { - m_MountData[archivename] = db; - return true; - } - return false; - } +bool FileManager::Mount(DataBuffer* db, const std::string& archivename, const std::string& mountpoint /*= "/"*/, bool prepend /*= false*/) +{ + if (!m_Inited) + return false; + if (PHYSFS_mountMemory(db->GetData(), db->GetSize(), nullptr, archivename.c_str(), mountpoint.c_str(), !prepend)) + { + m_MountData[archivename] = db; + return true; + } + return false; +} - bool FileManager::Unmount(const std::string& locpath) - { - if (!m_Inited) - return false; - - // 如果是归档,从映射中删除它 - auto datait = m_MountData.find(locpath); - if (datait != m_MountData.end() && PHYSFS_unmount(locpath.c_str()) != 0) - { - m_MountData.erase(datait); - return true; - } - - return PHYSFS_unmount(locpath.c_str()); - } +bool FileManager::Unmount(const std::string& locpath) +{ + if (!m_Inited) + return false; - bool FileManager::Unmount(DataBuffer* db) - { - for (const auto& dp : m_MountData) - { - if (dp.second == db) - { - std::string archive = dp.first; - return Unmount(archive); - } - } - } + // 如果是归档,从映射中删除它 + auto datait = m_MountData.find(locpath); + if (datait != m_MountData.end() && PHYSFS_unmount(locpath.c_str()) != 0) + { + m_MountData.erase(datait); + return true; + } - bool FileManager::GetMountPoint(const std::string& locpath, ASURA_OUT std::string& mountpoint) - { - if (!m_Inited) - return false; - const char* point = PHYSFS_getMountPoint(locpath.c_str()); - if (point != nullptr) - { - mountpoint = point; - return true; - } - return false; - } + return PHYSFS_unmount(locpath.c_str()); +} - void FileManager::SetWriteDirectory(const std::string locpath) +bool FileManager::Unmount(DataBuffer* db) +{ + for (const auto& dp : m_MountData) + { + if (dp.second == db) { - if (!m_Inited) - return; - if (!PHYSFS_setWriteDir(locpath.c_str())) - throw Exception("Failed to set write directory %s", locpath.c_str()); + std::string archive = dp.first; + return Unmount(archive); } + } +} - std::string FileManager::GetWriteDirectory() - { - return PHYSFS_getWriteDir(); - } +bool FileManager::GetMountPoint(const std::string& locpath, ASURA_OUT std::string& mountpoint) +{ + if (!m_Inited) + return false; + const char* point = PHYSFS_getMountPoint(locpath.c_str()); + if (point != nullptr) + { + mountpoint = point; + return true; + } + return false; +} - File* FileManager::NewFile(const std::string& name) - { - return new File(name); - } +void FileManager::SetWriteDirectory(const std::string locpath) +{ + if (!m_Inited) + return; + if (!PHYSFS_setWriteDir(locpath.c_str())) + throw Exception("Failed to set write directory %s", locpath.c_str()); +} - bool FileManager::NewDirectory(const std::string& path) - { - if (!m_Inited) - return false; - if (!PHYSFS_getWriteDir()) - return false; - if (!PHYSFS_mkdir(path.c_str())) - return false; - return true; - } +std::string FileManager::GetWriteDirectory() +{ + return PHYSFS_getWriteDir(); +} - bool FileManager::Write(const std::string& name, ASURA_REF DataBuffer* buffer) - { - File file(name); - file.Open(File::FILE_MODE_WRITE); - if (!file.Write(buffer)) - throw Exception("Data could not be written."); - } +File* FileManager::NewFile(const std::string& name) +{ + return new File(name); +} - bool FileManager::Append(const std::string& name, ASURA_REF DataBuffer* buffer) - { - File file(name); - file.Open(File::FILE_MODE_APPEND); - if (!file.Write(buffer)) - throw Exception("Data could not be append."); - } +bool FileManager::NewDirectory(const std::string& path) +{ + if (!m_Inited) + return false; + if (!PHYSFS_getWriteDir()) + return false; + if (!PHYSFS_mkdir(path.c_str())) + return false; + return true; +} + +bool FileManager::Write(const std::string& name, ASURA_REF DataBuffer* buffer) +{ + File file(name); + file.Open(File::FILE_MODE_WRITE); + if (!file.Write(buffer)) + throw Exception("Data could not be written."); +} - FileData* FileManager::Read(const std::string& name) - { - File file = File(name); - file.Open(File::FILE_MODE_READ); - int size = file.GetSize(); - DataBuffer* db = new DataBuffer(size); - if (db) - { - file.ReadAll(db); - FileData* fd = new FileData(name); - fd->BindData(db); - return fd; - } - return nullptr; - } +bool FileManager::Append(const std::string& name, ASURA_REF DataBuffer* buffer) +{ + File file(name); + file.Open(File::FILE_MODE_APPEND); + if (!file.Write(buffer)) + throw Exception("Data could not be append."); +} - bool FileManager::Remove(const std::string& path) - { - if (!m_Inited) - return false; - if (PHYSFS_getWriteDir() == 0) - return false; +FileData* FileManager::Read(const std::string& name) +{ + File file = File(name); + file.Open(File::FILE_MODE_READ); + int size = file.GetSize(); + DataBuffer* db = new DataBuffer(size); + if (db) + { + file.ReadAll(db); + FileData* fd = new FileData(name); + fd->BindData(db); + return fd; + } + return nullptr; +} - if (!PHYSFS_delete(path.c_str())) - return false; +bool FileManager::Remove(const std::string& path) +{ + if (!m_Inited) + return false; + if (PHYSFS_getWriteDir() == 0) + return false; - return true; - } + if (!PHYSFS_delete(path.c_str())) + return false; - bool FileManager::GetFileInfo(const std::string& filepath, ASURA_OUT FileInfo* info) - { - if (!m_Inited) - return false; - - PHYSFS_Stat stat = {}; - if (!PHYSFS_stat(filepath.c_str(), &stat)) - return false; - - info->size = (int64)stat.filesize; - info->modtime = (int64)stat.modtime; - - if (stat.filetype == PHYSFS_FILETYPE_REGULAR) - info->type = FILE_TYPE_FILE; - else if (stat.filetype == PHYSFS_FILETYPE_DIRECTORY) - info->type = FILE_TYPE_DIRECTORY; - else if (stat.filetype == PHYSFS_FILETYPE_SYMLINK) - info->type = FILE_TYPE_SYMLINK; - else - info->type = FILE_TYPE_OTHER; - - return true; - } + return true; +} - } -} \ No newline at end of file +bool FileManager::GetFileInfo(const std::string& filepath, ASURA_OUT FileInfo* info) +{ + if (!m_Inited) + return false; + + PHYSFS_Stat stat = {}; + if (!PHYSFS_stat(filepath.c_str(), &stat)) + return false; + + info->size = (int64)stat.filesize; + info->modtime = (int64)stat.modtime; + + if (stat.filetype == PHYSFS_FILETYPE_REGULAR) + info->type = FILE_TYPE_FILE; + else if (stat.filetype == PHYSFS_FILETYPE_DIRECTORY) + info->type = FILE_TYPE_DIRECTORY; + else if (stat.filetype == PHYSFS_FILETYPE_SYMLINK) + info->type = FILE_TYPE_SYMLINK; + else + info->type = FILE_TYPE_OTHER; + + return true; +} + +namespace_end +namespace_end \ No newline at end of file diff --git a/Source/modules/asura-base/FileSystem/FileManager.h b/Source/modules/asura-base/FileSystem/FileManager.h index ac97da3..2820321 100644 --- a/Source/modules/asura-base/FileSystem/FileManager.h +++ b/Source/modules/asura-base/FileSystem/FileManager.h @@ -11,101 +11,99 @@ #include "FileData.h" #include "File.h" -namespace AsuraEngine -{ - namespace FileSystem - { - - enum FileType - { - FILE_TYPE_FILE, ///< 文件 - FILE_TYPE_DIRECTORY, ///< 文件夹 - FILE_TYPE_SYMLINK, ///< 链接 - FILE_TYPE_OTHER, ///< 其他 - }; +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) - struct FileInfo - { - int64 size; - int64 modtime; - FileType type; - }; +enum FileType +{ + FILE_TYPE_FILE, ///< 文件 + FILE_TYPE_DIRECTORY, ///< 文件夹 + FILE_TYPE_SYMLINK, ///< 链接 + FILE_TYPE_OTHER, ///< 其他 +}; - /// - /// 资源管理,负责加载、存储资源,指定根目录等。无论编辑器还是运行时,都需要限制访问的机制,将用户的操作限制在游戏目录 - /// 下,file system就是做这件事的。FileManager是运行时和编辑器共用的类,AssetDatabase是用来管理资源的类,在framework - /// 里实现,单纯是逻辑处理,读写还是用FileManager实现,AssetDatabase提供根据文件内容创建对应资源的方法。 - /// - class FileManager ASURA_FINAL - : public Singleton - , public AEScripting::Portable - { - public: +struct FileInfo +{ + int64 size; + int64 modtime; + FileType type; +}; + +/// +/// 资源管理,负责加载、存储资源,指定根目录等。无论编辑器还是运行时,都需要限制访问的机制,将用户的操作限制在游戏目录 +/// 下,file system就是做这件事的。FileManager是运行时和编辑器共用的类,AssetDatabase是用来管理资源的类,在framework +/// 里实现,单纯是逻辑处理,读写还是用FileManager实现,AssetDatabase提供根据文件内容创建对应资源的方法。 +/// +class FileManager ASURA_FINAL + : public Singleton + , public AEScripting::Portable +{ +public: - LUAX_DECL_SINGLETON(FileManager); + LUAX_DECL_SINGLETON(FileManager); - ~FileManager(); + ~FileManager(); - void Init(const char* arg0); + void Init(const char* arg0); - /// - /// 当前可执行文件的所在文件夹 - /// - std::string GetWorkingDirectory(); + /// + /// 当前可执行文件的所在文件夹 + /// + std::string GetWorkingDirectory(); - bool Mount(const std::string& locpath, const std::string& montpoint = "/", bool prepend = false); - bool Mount(DataBuffer* db, const std::string& archivename, const std::string& mountpoint = "/", bool prepend = false); + bool Mount(const std::string& locpath, const std::string& montpoint = "/", bool prepend = false); + bool Mount(DataBuffer* db, const std::string& archivename, const std::string& mountpoint = "/", bool prepend = false); - bool Unmount(const std::string& locpath); - bool Unmount(DataBuffer* db); + bool Unmount(const std::string& locpath); + bool Unmount(DataBuffer* db); - bool GetMountPoint(const std::string& locpath, ASURA_OUT std::string& mountpoint); + bool GetMountPoint(const std::string& locpath, ASURA_OUT std::string& mountpoint); - void SetWriteDirectory(const std::string locpath); - std::string GetWriteDirectory(); - File* NewFile(const std::string& name); - bool NewDirectory(const std::string& path); - bool Write(const std::string& path, ASURA_REF DataBuffer* buffer); - bool Append(const std::string& path, ASURA_REF DataBuffer* buffer); - bool Remove(const std::string& path); + void SetWriteDirectory(const std::string locpath); + std::string GetWriteDirectory(); + File* NewFile(const std::string& name); + bool NewDirectory(const std::string& path); + bool Write(const std::string& path, ASURA_REF DataBuffer* buffer); + bool Append(const std::string& path, ASURA_REF DataBuffer* buffer); + bool Remove(const std::string& path); - FileData* Read(const std::string& path); - bool GetFileInfo(const std::string& path, ASURA_OUT FileInfo* info); + FileData* Read(const std::string& path); + bool GetFileInfo(const std::string& path, ASURA_OUT FileInfo* info); - bool GetDirectoryItems(const std::string& path, ASURA_OUT std::vector& items) { return false; }; + bool GetDirectoryItems(const std::string& path, ASURA_OUT std::vector& items) { return false; }; - private: +private: - typedef std::map MountDataMap; + typedef std::map MountDataMap; - bool m_Inited; ///< 是否初始化成功 - std::string m_Cwd; ///< 当前执行文件的工作目录 - MountDataMap m_MountData; ///< 从路径到压缩文档的映射 + bool m_Inited; ///< 是否初始化成功 + std::string m_Cwd; ///< 当前执行文件的工作目录 + MountDataMap m_MountData; ///< 从路径到压缩文档的映射 - LUAX_DECL_METHOD(_Init); - LUAX_DECL_METHOD(_Mount); - LUAX_DECL_METHOD(_Unmount); - LUAX_DECL_METHOD(_GetMountPoint); + LUAX_DECL_METHOD(_Init); + LUAX_DECL_METHOD(_Mount); + LUAX_DECL_METHOD(_Unmount); + LUAX_DECL_METHOD(_GetMountPoint); - LUAX_DECL_METHOD(_SetWriteDirectory); - LUAX_DECL_METHOD(_GetWriteDirectory); - LUAX_DECL_METHOD(_CreateFile); - LUAX_DECL_METHOD(_CreateDirectory); + LUAX_DECL_METHOD(_SetWriteDirectory); + LUAX_DECL_METHOD(_GetWriteDirectory); + LUAX_DECL_METHOD(_CreateFile); + LUAX_DECL_METHOD(_CreateDirectory); - LUAX_DECL_METHOD(_Write); - LUAX_DECL_METHOD(_Append); - LUAX_DECL_METHOD(_Remove); + LUAX_DECL_METHOD(_Write); + LUAX_DECL_METHOD(_Append); + LUAX_DECL_METHOD(_Remove); - LUAX_DECL_METHOD(_Read); + LUAX_DECL_METHOD(_Read); - LUAX_DECL_METHOD(_GetFileInfo); + LUAX_DECL_METHOD(_GetFileInfo); - LUAX_DECL_METHOD(_GetDirectoryItems); + LUAX_DECL_METHOD(_GetDirectoryItems); - }; +}; - } -} +namespace_end +namespace_end namespace AEFileSystem = AsuraEngine::FileSystem; diff --git a/Source/modules/asura-base/FileSystem/IOBatchTask.h b/Source/modules/asura-base/FileSystem/IOBatchTask.h index 8d73e93..d82d83d 100644 --- a/Source/modules/asura-base/FileSystem/IOBatchTask.h +++ b/Source/modules/asura-base/FileSystem/IOBatchTask.h @@ -3,29 +3,27 @@ #include "IOTask.h" -namespace AsuraEngine -{ - namespace FileSystem - { +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) - /// - /// 批量处理读或者写。一次性提交一个table,依次处理后返回结果。 - /// - class IOBatchTask ASURA_FINAL : public AEThreading::Task - { - public: +/// +/// 批量处理读或者写。一次性提交一个table,依次处理后返回结果。 +/// +class IOBatchTask ASURA_FINAL : public AEThreading::Task +{ +public: - private: +private: - /// - /// 任务表,每一条的结构如下: - /// { path = "", } - /// - Luax::LuaxMemberRef m_Tasks; + /// + /// 任务表,每一条的结构如下: + /// { path = "", } + /// + Luax::LuaxMemberRef m_Tasks; - }; +}; - } -} +namespace_end +namespace_end #endif \ No newline at end of file diff --git a/Source/modules/asura-base/FileSystem/IOTask.cpp b/Source/modules/asura-base/FileSystem/IOTask.cpp index 9152a6e..493ee9f 100644 --- a/Source/modules/asura-base/FileSystem/IOTask.cpp +++ b/Source/modules/asura-base/FileSystem/IOTask.cpp @@ -6,56 +6,54 @@ using namespace AEScripting; using namespace Luax; -namespace AsuraEngine +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) + +IOTask::IOTask(const std::string& path, DataBuffer* buffer, IOTaskType type) + : m_Path(path) + , m_Buffer(buffer) { - namespace FileSystem - { + if (buffer) + buffer->Retain(); +} - IOTask::IOTask(const std::string& path, DataBuffer* buffer, IOTaskType type) - : m_Path(path) - , m_Buffer(buffer) - { - if (buffer) - buffer->Retain(); - } +IOTask::~IOTask() +{ + if (m_Buffer) + m_Buffer->Release(); +} - IOTask::~IOTask() - { - if (m_Buffer) - m_Buffer->Release(); - } +bool IOTask::Execute() +{ + File file(m_Path); + if (m_Type == IOTASK_TYPE_WRITE) + { - bool IOTask::Execute() - { - File file(m_Path); - if (m_Type == IOTASK_TYPE_WRITE) - { - - } - // 从path读取内容保存在mBuffer中 - else if (m_Type == IOTASK_TYPE_READ) - { - if (!m_Buffer) - return false; - file.Open(File::FILE_MODE_READ); - file.ReadAll(m_Buffer); - file.Close(); - } - return true; - } + } + // 从path读取内容保存在mBuffer中 + else if (m_Type == IOTASK_TYPE_READ) + { + if (!m_Buffer) + return false; + file.Open(File::FILE_MODE_READ); + file.ReadAll(m_Buffer); + file.Close(); + } + return true; +} - void IOTask::Invoke(lua_State* invokeThreaad) +void IOTask::Invoke(lua_State* invokeThreaad) +{ + if (m_Callback) + { + LuaxScopedState state(invokeThreaad); + if (this->PushLuaxMemberRef(state, m_Callback)) { - if (m_Callback) - { - LuaxScopedState state(invokeThreaad); - if (this->PushLuaxMemberRef(state, m_Callback)) - { - this->PushLuaxMemberRef(state, m_BufferRef); - state.Call(1, 0); - } - } + this->PushLuaxMemberRef(state, m_BufferRef); + state.Call(1, 0); } - } } + +namespace_end +namespace_end \ No newline at end of file diff --git a/Source/modules/asura-base/FileSystem/IOTask.h b/Source/modules/asura-base/FileSystem/IOTask.h index da54fdc..c2a22ba 100644 --- a/Source/modules/asura-base/FileSystem/IOTask.h +++ b/Source/modules/asura-base/FileSystem/IOTask.h @@ -8,49 +8,47 @@ #include "DataBuffer.h" -namespace AsuraEngine -{ - namespace FileSystem - { - - enum IOTaskType - { - IOTASK_TYPE_READ, - IOTASK_TYPE_WRITE, - IOTASK_TYPE_APPEND, - }; +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) - /// - /// 读取文件任务。 - /// - class IOTask ASURA_FINAL - : public AEScripting::Portable - { - public: +enum IOTaskType +{ + IOTASK_TYPE_READ, + IOTASK_TYPE_WRITE, + IOTASK_TYPE_APPEND, +}; + +/// +/// 读取文件任务。 +/// +class IOTask ASURA_FINAL + : public AEScripting::Portable +{ +public: - LUAX_DECL_FACTORY(IOTask); + LUAX_DECL_FACTORY(IOTask); - IOTask(const std::string& path, DataBuffer* buffer, IOTaskType type); - ~IOTask(); + IOTask(const std::string& path, DataBuffer* buffer, IOTaskType type); + ~IOTask(); - bool Execute() override ; - void Invoke(lua_State* invokeThreaad) override; + bool Execute() override ; + void Invoke(lua_State* invokeThreaad) override; - private: +private: - LUAX_DECL_ENUM(IOTaskType); + LUAX_DECL_ENUM(IOTaskType); - LUAX_DECL_METHOD(_New); + LUAX_DECL_METHOD(_New); - std::string m_Path; - IOTaskType m_Type; + std::string m_Path; + IOTaskType m_Type; - DataBuffer* m_Buffer; - Luax::LuaxMemberRef m_BufferRef; + DataBuffer* m_Buffer; + Luax::LuaxMemberRef m_BufferRef; - }; +}; - } -} +namespace_end +namespace_end #endif \ No newline at end of file diff --git a/Source/modules/asura-base/FileSystem/Renewable.h b/Source/modules/asura-base/FileSystem/Renewable.h index ff44303..1be3a88 100644 --- a/Source/modules/asura-base/FileSystem/Renewable.h +++ b/Source/modules/asura-base/FileSystem/Renewable.h @@ -3,24 +3,22 @@ #include "DecodedData.h" -namespace AsuraEngine -{ - namespace FileSystem - { +namespace_begin(AsuraEngine) +namespace_begin(FileSystem) - /// - /// 可以重新构建的数据结构。比如图片、音频这种,从解析后数据可以直接构建,可以在编辑器内重 - /// 新构建,不会修改handle值,改变不具备破坏性,适用于不改变handle的资源。 - /// - ASURA_ABSTRACT class Renewable - { - public: - Renewable() {}; - virtual ~Renewable() {}; - }; +/// +/// 可以重新构建的数据结构。比如图片、音频这种,从解析后数据可以直接构建,可以在编辑器内重 +/// 新构建,不会修改handle值,改变不具备破坏性,适用于不改变handle的资源。 +/// +ASURA_ABSTRACT class Renewable +{ +public: + Renewable() {}; + virtual ~Renewable() {}; +}; - } -} +namespace_end +namespace_end namespace AEFileSystem = AsuraEngine::FileSystem; -- cgit v1.1-26-g67d0