add AssetsSourceFilesytem
This commit is contained in:
@@ -1,12 +1,21 @@
|
||||
#ifndef HGL_ASSETS_SOURCE_INCLUDE
|
||||
#define HGL_ASSETS_SOURCE_INCLUDE
|
||||
|
||||
#include<hgl/type/BaseString.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
namespace io
|
||||
{
|
||||
class InputStream;
|
||||
}//namespace io
|
||||
|
||||
namespace assets
|
||||
{
|
||||
/**
|
||||
* 资产来源
|
||||
* 资产来源类型
|
||||
*/
|
||||
enum class AssetsSource
|
||||
enum class SourceType
|
||||
{
|
||||
Memory=0, ///<内存
|
||||
Filesystem, ///<文件系统
|
||||
@@ -19,6 +28,60 @@ namespace hgl
|
||||
BEGIN_RANGE =Memory,
|
||||
END_RANGE =Network,
|
||||
RANGE_SIZE =(END_RANGE-BEGIN_RANGE)+1
|
||||
};//enum class AssetsSource
|
||||
};//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
|
||||
#endif//HGL_ASSETS_SOURCE_INCLUDE
|
||||
|
81
src/AssetsSource.cpp
Normal file
81
src/AssetsSource.cpp
Normal 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
|
53
src/AssetsSourceFilesystem.cpp
Normal file
53
src/AssetsSourceFilesystem.cpp
Normal 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
7
src/CMakeLists.txt
Normal 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})
|
Reference in New Issue
Block a user