add AssetsSourceFilesytem

This commit is contained in:
2020-01-13 11:18:25 +08:00
parent ef66ff499e
commit a60bc74929
4 changed files with 219 additions and 15 deletions

View File

@@ -1,24 +1,87 @@
#ifndef HGL_ASSETS_SOURCE_INCLUDE #ifndef HGL_ASSETS_SOURCE_INCLUDE
#define HGL_ASSETS_SOURCE_INCLUDE #define HGL_ASSETS_SOURCE_INCLUDE
#include<hgl/type/BaseString.h>
namespace hgl namespace hgl
{ {
/** namespace io
* 资产来源
*/
enum class AssetsSource
{ {
Memory=0, ///<内存 class InputStream;
Filesystem, ///<文件系统 }//namespace io
Archive, ///<打包/压缩档
Database, ///<数据库
SaveDevice, ///<存储设备
StreamDevice, ///<流式设备(如摄像头、麦克风)
Network, ///<网络
BEGIN_RANGE =Memory, namespace assets
END_RANGE =Network, {
RANGE_SIZE =(END_RANGE-BEGIN_RANGE)+1 /**
};//enum class AssetsSource * 资产来源类型
*/
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, ///<ID
itPosition, ///<坐标访问(如图数据库)
BEGIN_RANGE =itAnonymous,
END_RANGE =itPosition,
RANGE_SIZE =(END_RANGE-BEGIN_RANGE)+1
};//enum class IndexType
/**
* 资产来源
*/
class AssetsSource
{
protected:
UTF8String uri_short_name;
public:
const UTF8String &GetURI()const{return uri_short_name;}
public:
AssetsSource(const UTF8String &);
virtual ~AssetsSource();
virtual bool hasAnonymousAccess ()const{return false;}
virtual bool hasNameAccess ()const{return false;}
virtual bool hasIDAccess ()const{return false;}
virtual bool hasPositionAccess ()const{return false;}
public:
virtual io::InputStream * OpenByName (const UTF8String &){return nullptr;}
virtual AssetsSource * CreateSubSource (const UTF8String &){return nullptr;}
};//class AssetsSource
AssetsSource *CreateByFilesystem(const UTF8String &sn,const OSString &pathname,const bool only_read);
bool RegistryAssetsSource(const UTF8String &uri_short_name,AssetsSource *);
void UnregistryAssetsSource(const UTF8String &uri_short_name);
AssetsSource *GetAssetsSource(const UTF8String &uri_short_name);
io::InputStream *GetAssets(const UTF8String &uri);
}//namespace assets
}//namespace hgl }//namespace hgl
#endif//HGL_ASSETS_SOURCE_INCLUDE #endif//HGL_ASSETS_SOURCE_INCLUDE

81
src/AssetsSource.cpp Normal file
View File

@@ -0,0 +1,81 @@
#include<hgl/assets/AssetsSource.h>
#include<hgl/type/Map.h>
namespace hgl
{
namespace assets
{
namespace
{
Map<UTF8String,AssetsSource *> 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

View File

@@ -0,0 +1,53 @@
#include<hgl/assets/AssetsSource.h>
#include<hgl/filesystem/FileSystem.h>
#include<hgl/io/FileInputStream.h>
#include<hgl/CodePage.h>
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

7
src/CMakeLists.txt Normal file
View File

@@ -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})