diff --git a/inc/hgl/assets/AssetsSource.h b/inc/hgl/assets/AssetsSource.h index 94b660a..80ec230 100644 --- a/inc/hgl/assets/AssetsSource.h +++ b/inc/hgl/assets/AssetsSource.h @@ -1,24 +1,87 @@ #ifndef HGL_ASSETS_SOURCE_INCLUDE #define HGL_ASSETS_SOURCE_INCLUDE +#include + namespace hgl { - /** - * 资产来源 - */ - enum class AssetsSource + namespace io { - Memory=0, ///<内存 - Filesystem, ///<文件系统 - Archive, ///<打包/压缩档 - Database, ///<数据库 - SaveDevice, ///<存储设备 - StreamDevice, ///<流式设备(如摄像头、麦克风) - Network, ///<网络 + class InputStream; + }//namespace io - BEGIN_RANGE =Memory, - END_RANGE =Network, - RANGE_SIZE =(END_RANGE-BEGIN_RANGE)+1 - };//enum class AssetsSource + namespace assets + { + /** + * 资产来源类型 + */ + enum class SourceType + { + Memory=0, ///<内存 + Filesystem, ///<文件系统 + Archive, ///<打包/压缩档 + Database, ///<数据库 + SaveDevice, ///<存储设备 + StreamDevice, ///<流式设备(如摄像头、麦克风) + Network, ///<网络 + + BEGIN_RANGE =Memory, + END_RANGE =Network, + RANGE_SIZE =(END_RANGE-BEGIN_RANGE)+1 + };//enum class SourceType + + /** + * 资产索引方法 + */ + enum class IndexType + { + itAnonymous=0, ///<匿名访问 + itName, ///<字符串名称 + itID, /// +#include + +namespace hgl +{ + namespace assets + { + namespace + { + Map assets_source_map; + }//namespace + + AssetsSource::AssetsSource(const UTF8String &sn) + { + uri_short_name=sn; + RegistryAssetsSource(sn,this); + } + + AssetsSource::~AssetsSource() + { + UnregistryAssetsSource(uri_short_name); + } + + bool RegistryAssetsSource(const UTF8String &uri_short_name,AssetsSource *as) + { + if(!as) + return(false); + + if(uri_short_name.IsEmpty()) + return(false); + + if(assets_source_map.KeyExist(uri_short_name)) + return(false); + + assets_source_map.Add(uri_short_name,as); + return(true); + } + + void UnregistryAssetsSource(const UTF8String &uri_short_name) + { + if(uri_short_name.IsEmpty()) + return; + + assets_source_map.DeleteByKey(uri_short_name); + } + + AssetsSource *GetAssetsSource(const UTF8String &uri_short_name) + { + if(uri_short_name.IsEmpty()) + return(nullptr); + + AssetsSource *as; + + if(assets_source_map.Get(uri_short_name,as)) + return as; + + return(nullptr); + } + + io::InputStream *GetAssets(const UTF8String &uri) + { + int pos=uri.FindChar(':'); + + if(pos<=0)return(nullptr); + + if(uri.Comp(pos,"://",3)) + return(nullptr); + + const UTF8String sn=uri.SubString(0,pos); + + AssetsSource *source=GetAssetsSource(sn); + + if(!source) + return(nullptr); + + const UTF8String surl=uri.SubString(pos+3); + + return source->OpenByName(surl); + } + }//namespace assets +}//namespace hgl \ No newline at end of file diff --git a/src/AssetsSourceFilesystem.cpp b/src/AssetsSourceFilesystem.cpp new file mode 100644 index 0000000..db36c94 --- /dev/null +++ b/src/AssetsSourceFilesystem.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +namespace hgl +{ + namespace assets + { + class AssetsSourceFilesytem:public AssetsSource + { + OSString root_path; + bool only_read; + + public: + + AssetsSourceFilesytem(const UTF8String &sn,const OSString &path,const bool or):AssetsSource(sn) + { + root_path=path; + only_read=or; + } + + bool hasNameAccess()const override{return true;} + + io::InputStream *OpenByName(const UTF8String &filename) + { + const OSString &fullname=filesystem::MergeFilename(root_path,ToOSString(filename)); + + if(!filesystem::FileCanRead(fullname)) + return(nullptr); + + io::FileInputStream *fis=new io::FileInputStream; + + if(fis->Open(fullname)) + return fis; + + delete fis; + return(nullptr); + } + };//class AssetsSourceFilesytem:public AssetsSource + + AssetsSource *CreateByFilesystem(const OSString &path,const bool only_read) + { + if(path.IsEmpty()) + return(nullptr); + + if(!filesystem::IsDirectory(path)) + return(nullptr); + + return(new AssetsSourceFilesytem(path,only_read)); + } + }//namespace assets +}//namespace hgl diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..998a778 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,7 @@ +file(GLOB ASSETS_MANAGE_HEADER ${CMASSETS_MANAGE_ROOT_INCLUDE_PATH}/hgl/assets/*.h) + +set(ASSETS_MANAGE_SOURCE AssetsSource.cpp + AssetsSourceFilesystem.cpp) + +add_cm_library(CMAssetsManage "CM" ${ASSETS_MANAGE_HEADER} + ${ASSETS_MANAGE_SOURCE})