Compare commits

...

2 Commits

Author SHA1 Message Date
eb60dac8cb added AssetPath.h 2025-05-10 01:08:28 +08:00
d40ae5e254 文件改名 2025-05-10 00:59:52 +08:00
10 changed files with 227 additions and 246 deletions

30
inc/hgl/asset/AssetPath.h Normal file
View File

@ -0,0 +1,30 @@
#pragma once
#include<hgl/type/String.h>
namespace hgl::asset
{
/**
*
*/
enum class Location
{
Asset=0,
Engine,
PlugIn,
ExtPack,
OS,
};//
/**
*
* @see doc/AssetPath.md
*/
class AssetPath
{
public:
};//class AssetPath
}//namespace hgl::asset

View File

@ -0,0 +1,59 @@
#pragma once
#include<hgl/type/String.h>
namespace hgl
{
namespace io
{
class InputStream;
}//namespace io
namespace asset
{
/**
*
*/
enum class AssetIndexType
{
Unknow=0, ///<未知类型
ID, ///<ID
Name, ///<字符串名称
ENUM_CLASS_RANGE(ID,Name)
};//enum class AssetIndexType
/**
*
*/
class AssetSource
{
U8String uri_short_name;
public:
const U8String &GetURI()const{return uri_short_name;}
public:
AssetSource(const U8String &);
virtual ~AssetSource();
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 * Open (const U8String &){return nullptr;}
virtual AssetSource * CreateSubSource (const U8String &){return nullptr;}
};//class AssetSource
AssetSource *CreateSourceByFilesystem(const U8String &sn,const OSString &pathname,const bool only_read);
AssetSource *GetSource(const U8String &uri_short_name);
io::InputStream *GetAssets(const U8String &uri);
}//namespace asset
}//namespace hgl

View File

@ -1,78 +0,0 @@
#ifndef HGL_ASSETS_SOURCE_INCLUDE
#define HGL_ASSETS_SOURCE_INCLUDE
#include<hgl/type/String.h>
namespace hgl
{
namespace io
{
class InputStream;
}//namespace io
namespace assets
{
/**
*
*/
enum class SourceType
{
Memory=0, ///<内存
Filesystem, ///<文件系统
Archive, ///<打包/压缩档
Database, ///<数据库
SaveDevice, ///<存储设备
StreamDevice, ///<流式设备(如摄像头、麦克风)
Network, ///<网络
ENUM_CLASS_RANGE(Memory,Network)
};//enum class SourceType
/**
*
*/
enum class IndexType
{
Anonymous=0, ///<匿名访问
Name, ///<字符串名称
ID, ///<ID
Position, ///<坐标访问(如图数据库)
ENUM_CLASS_RANGE(Anonymous,Position)
};//enum class IndexType
/**
*
*/
class AssetsSource
{
U8String uri_short_name;
public:
const U8String &GetURI()const{return uri_short_name;}
public:
AssetsSource(const U8String &);
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 * Open (const U8String &){return nullptr;}
virtual AssetsSource * CreateSubSource (const U8String &){return nullptr;}
};//class AssetsSource
AssetsSource *CreateSourceByFilesystem(const U8String &sn,const OSString &pathname,const bool only_read);
AssetsSource *GetSource(const U8String &uri_short_name);
io::InputStream *GetAssets(const U8String &uri);
}//namespace assets
}//namespace hgl
#endif//HGL_ASSETS_SOURCE_INCLUDE

View File

@ -1,24 +0,0 @@
#ifndef HGL_ASSETS_TYPE_INCLUDE
#define HGL_ASSETS_TYPE_INCLUDE
#include<hgl/TypeFunc.h>
namespace hgl
{
/**
*
*/
enum class AssetsType
{
Binary=0, ///<二进制数据
Text, ///<文本
Bitmap, ///<位图
Vector, ///<矢量图
MIDI, ///<MIDI音乐
Wave, ///<声音数据
Video, ///<视频数据
ENUM_CLASS_RANGE(Binary,Video)
};//enum class AssetsType
}//namespace hgl
#endif//HGL_ASSETS_TYPE_INCLUDE

78
src/AssetSource.cpp Normal file
View File

@ -0,0 +1,78 @@
#include<hgl/asset/AssetSource.h>
#include<hgl/type/Map.h>
namespace hgl::asset
{
namespace
{
Map<U8String,AssetSource *> 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

View File

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

View File

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

View File

@ -1,59 +0,0 @@
#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 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

View File

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