From d40ae5e2548645c34460d372f2d6c9385645ce77 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sat, 10 May 2025 00:59:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AssetsSource.h => asset/AssetSource.h} | 38 +++------ inc/hgl/assets/AssetsType.h | 24 ------ src/AssetSource.cpp | 78 ++++++++++++++++++ ...urceAndroid.cpp => AssetSourceAndroid.cpp} | 0 src/AssetSourceFilesystem.cpp | 56 +++++++++++++ src/AssetsSource.cpp | 81 ------------------- src/AssetsSourceFilesystem.cpp | 59 -------------- src/CMakeLists.txt | 8 +- 8 files changed, 148 insertions(+), 196 deletions(-) rename inc/hgl/{assets/AssetsSource.h => asset/AssetSource.h} (52%) delete mode 100644 inc/hgl/assets/AssetsType.h create mode 100644 src/AssetSource.cpp rename src/{AssetsSourceAndroid.cpp => AssetSourceAndroid.cpp} (100%) create mode 100644 src/AssetSourceFilesystem.cpp delete mode 100644 src/AssetsSource.cpp delete mode 100644 src/AssetsSourceFilesystem.cpp diff --git a/inc/hgl/assets/AssetsSource.h b/inc/hgl/asset/AssetSource.h similarity index 52% rename from inc/hgl/assets/AssetsSource.h rename to inc/hgl/asset/AssetSource.h index e28ace5..5095dd9 100644 --- a/inc/hgl/assets/AssetsSource.h +++ b/inc/hgl/asset/AssetSource.h @@ -1,5 +1,4 @@ -#ifndef HGL_ASSETS_SOURCE_INCLUDE -#define HGL_ASSETS_SOURCE_INCLUDE +#pragma once #include @@ -10,24 +9,8 @@ namespace hgl class InputStream; }//namespace io - namespace assets + namespace asset { - /** - * 资产来源类型 - */ - enum class SourceType - { - Memory=0, ///<内存 - Filesystem, ///<文件系统 - Archive, ///<打包/压缩档 - Database, ///<数据库 - SaveDevice, ///<存储设备 - StreamDevice, ///<流式设备(如摄像头、麦克风) - Network, ///<网络 - - ENUM_CLASS_RANGE(Memory,Network) - };//enum class SourceType - /** * 资产索引方法 */ @@ -44,7 +27,7 @@ namespace hgl /** * 资产来源 */ - class AssetsSource + class AssetSource { U8String uri_short_name; @@ -54,8 +37,8 @@ namespace hgl public: - AssetsSource(const U8String &); - virtual ~AssetsSource(); + AssetSource(const U8String &); + virtual ~AssetSource(); virtual bool hasAnonymousAccess ()const{return false;} virtual bool hasNameAccess ()const{return false;} @@ -65,14 +48,13 @@ namespace hgl public: virtual io::InputStream * Open (const U8String &){return nullptr;} - virtual AssetsSource * CreateSubSource (const U8String &){return nullptr;} - };//class AssetsSource + virtual AssetSource * CreateSubSource (const U8String &){return nullptr;} + };//class AssetSource - AssetsSource *CreateSourceByFilesystem(const U8String &sn,const OSString &pathname,const bool only_read); + AssetSource *CreateSourceByFilesystem(const U8String &sn,const OSString &pathname,const bool only_read); - AssetsSource *GetSource(const U8String &uri_short_name); + AssetSource *GetSource(const U8String &uri_short_name); io::InputStream *GetAssets(const U8String &uri); - }//namespace assets + }//namespace asset }//namespace hgl -#endif//HGL_ASSETS_SOURCE_INCLUDE diff --git a/inc/hgl/assets/AssetsType.h b/inc/hgl/assets/AssetsType.h deleted file mode 100644 index 9582c43..0000000 --- a/inc/hgl/assets/AssetsType.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef HGL_ASSETS_TYPE_INCLUDE -#define HGL_ASSETS_TYPE_INCLUDE - -#include -namespace hgl -{ - /** - * 资产类型 - */ - enum class AssetsType - { - Binary=0, ///<二进制数据 - - Text, ///<文本 - Bitmap, ///<位图 - Vector, ///<矢量图 - MIDI, /// +#include + +namespace hgl::asset +{ + namespace + { + Map assets_source_map; + }//namespace + + bool RegistryAssetsSource(const U8String &uri_short_name,AssetSource *as) + { + if(!as) + return(false); + + if(uri_short_name.IsEmpty()) + return(false); + + if(assets_source_map.ContainsKey(uri_short_name)) + return(false); + + assets_source_map.Add(uri_short_name,as); + return(true); + } + + void UnregistryAssetsSource(const U8String &uri_short_name) + { + if(uri_short_name.IsEmpty()) + return; + + assets_source_map.DeleteByKey(uri_short_name); + } + + AssetSource *GetSource(const U8String &uri_short_name) + { + if(uri_short_name.IsEmpty()) + return(nullptr); + + AssetSource *as; + + if(assets_source_map.Get(uri_short_name,as)) + return as; + + return(nullptr); + } + + io::InputStream *GetAssets(const U8String &uri) + { + int pos=uri.FindChar(':'); + + if(pos<=0)return(nullptr); + + if(uri.Comp(pos,u8"://",3)) + return(nullptr); + + const U8String sn=uri.SubString(0,pos); + + AssetSource *source=GetSource(uri); + + if(!source) + return(nullptr); + + const U8String surl=uri.SubString(pos+3); + + return source->Open(surl); + } + + AssetSource::AssetSource(const U8String &sn) + { + uri_short_name=sn; + RegistryAssetsSource(sn,this); + } + + AssetSource::~AssetSource() + { + UnregistryAssetsSource(uri_short_name); + } +}//namespace hgl::asset diff --git a/src/AssetsSourceAndroid.cpp b/src/AssetSourceAndroid.cpp similarity index 100% rename from src/AssetsSourceAndroid.cpp rename to src/AssetSourceAndroid.cpp diff --git a/src/AssetSourceFilesystem.cpp b/src/AssetSourceFilesystem.cpp new file mode 100644 index 0000000..a93c3c6 --- /dev/null +++ b/src/AssetSourceFilesystem.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include + +namespace hgl::asset +{ + class AssetsSourceFilesytem:public AssetSource + { + OSString root_path; + bool only_read; + + public: + + AssetsSourceFilesytem(const U8String &sn,const OSString &path,const bool _or):AssetSource(sn) + { + root_path=path; + only_read=_or; + } + + bool hasNameAccess()const override{return true;} + + io::InputStream *Open(const U8String &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 AssetSource + + AssetSource *CreateSourceByFilesystem(const U8String &uri,const OSString &path,const bool only_read) + { + if(!uri.IsEmpty()) + { + if(GetSource(uri)) + return(nullptr); + } + + if(path.IsEmpty()) + return(nullptr); + + if(!filesystem::IsDirectory(path)) + return(nullptr); + + return(new AssetsSourceFilesytem(uri,path,only_read)); + } +}//namespace hgl::asset diff --git a/src/AssetsSource.cpp b/src/AssetsSource.cpp deleted file mode 100644 index b76d483..0000000 --- a/src/AssetsSource.cpp +++ /dev/null @@ -1,81 +0,0 @@ -#include -#include - -namespace hgl -{ - namespace assets - { - namespace - { - Map assets_source_map; - }//namespace - - bool RegistryAssetsSource(const U8String &uri_short_name,AssetsSource *as) - { - if(!as) - return(false); - - if(uri_short_name.IsEmpty()) - return(false); - - if(assets_source_map.ContainsKey(uri_short_name)) - return(false); - - assets_source_map.Add(uri_short_name,as); - return(true); - } - - void UnregistryAssetsSource(const U8String &uri_short_name) - { - if(uri_short_name.IsEmpty()) - return; - - assets_source_map.DeleteByKey(uri_short_name); - } - - AssetsSource *GetSource(const U8String &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 U8String &uri) - { - int pos=uri.FindChar(':'); - - if(pos<=0)return(nullptr); - - if(uri.Comp(pos,u8"://",3)) - return(nullptr); - - const U8String sn=uri.SubString(0,pos); - - AssetsSource *source=GetSource(uri); - - if(!source) - return(nullptr); - - const U8String surl=uri.SubString(pos+3); - - return source->Open(surl); - } - - AssetsSource::AssetsSource(const U8String &sn) - { - uri_short_name=sn; - RegistryAssetsSource(sn,this); - } - - AssetsSource::~AssetsSource() - { - UnregistryAssetsSource(uri_short_name); - } - }//namespace assets -}//namespace hgl \ No newline at end of file diff --git a/src/AssetsSourceFilesystem.cpp b/src/AssetsSourceFilesystem.cpp deleted file mode 100644 index 35ec159..0000000 --- a/src/AssetsSourceFilesystem.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include -#include - -namespace hgl -{ - namespace assets - { - class AssetsSourceFilesytem:public AssetsSource - { - OSString root_path; - bool only_read; - - public: - - AssetsSourceFilesytem(const U8String &sn,const OSString &path,const bool _or):AssetsSource(sn) - { - root_path=path; - only_read=_or; - } - - bool hasNameAccess()const override{return true;} - - io::InputStream *Open(const U8String &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 *CreateSourceByFilesystem(const U8String &uri,const OSString &path,const bool only_read) - { - if(!uri.IsEmpty()) - { - if(GetSource(uri)) - return(nullptr); - } - - if(path.IsEmpty()) - return(nullptr); - - if(!filesystem::IsDirectory(path)) - return(nullptr); - - return(new AssetsSourceFilesytem(uri,path,only_read)); - } - }//namespace assets -}//namespace hgl diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 34e0697..7525cbf 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,11 +1,11 @@ -file(GLOB ASSETS_MANAGE_HEADER ${CMASSETS_MANAGE_ROOT_INCLUDE_PATH}/hgl/assets/*.h) +file(GLOB ASSETS_MANAGE_HEADER ${CMASSETS_MANAGE_ROOT_INCLUDE_PATH}/hgl/asset/*.h) -set(ASSETS_MANAGE_SOURCE AssetsSource.cpp - AssetsSourceFilesystem.cpp) +set(ASSETS_MANAGE_SOURCE AssetSource.cpp + AssetSourceFilesystem.cpp) IF(ANDROID) set(ASSETS_MANAGE_SOURCE ${ASSETS_MANAGE_SOURCE} - AssetsSourceAndroid.cpp) + AssetSourceAndroid.cpp) ENDIF() add_cm_library(CMAssetsManage "CM" ${ASSETS_MANAGE_HEADER}