genesis-3d_engine/Engine/addons/resource/resource.h

361 lines
9.2 KiB
C
Raw Normal View History

/****************************************************************************
Copyright (c) 2011-2013,WebJet Business Division,CYOU
http://www.genesis-3d.com.cn
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __resource_H__
#define __resource_H__
#include "core/refcounted.h"
#include "util/stringatom.h"
#include "io/stream.h"
#include "core/weakptr.h"
#include "resource/resourcebase.h"
#include "resourcepriority.h"
namespace Resources
{
typedef Util::StringAtom ResourceId;
struct ReferenceResource
{
Resources::ResourceId id;
Resources::ResourceType type;
inline ReferenceResource()
:type(RR_Unknown)
{
}
inline ReferenceResource(const Resources::ResourceId& id, Resources::ResourceType type)
{
this->id = id;
this->type = type;
}
};
class Resource;
class ResourceNotifier;
class ResourceServer;
//-------------------------------<2D><>Դ<EFBFBD><D4B4>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-----------------------------------------
class ResourceLoaderSaver
{
public:
void SetStream(const GPtr<IO::Stream>& pStream )
{
mStream = pStream;
}
const GPtr<IO::Stream>& GetStream(void) const
{
return mStream;
}
protected:
ResourceLoaderSaver() { };
GPtr<IO::Stream> mStream; // Ҫ<><D2AA>ȡ/д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
};
// <20><>Stream<61>ж<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>л<EFBFBD><D0BB><EFBFBD>Դ
class ResourceLoader: public Core::RefCounted, public ResourceLoaderSaver
{
__DeclareClass(ResourceLoader);
public:
ResourceLoader(){ };
virtual ~ResourceLoader(){ };
virtual bool LoadResource(Resource* res ){ return false; };
};
// <20><><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD>л<EFBFBD><D0BB><EFBFBD>Stream<61><6D>
class ResourceSaver: public Core::RefCounted, public ResourceLoaderSaver
{
__DeclareClass(ResourceSaver);
public:
ResourceSaver(){ };
virtual ~ResourceSaver() { };
virtual bool SaveResource( Resource* res ) { return false; };
};
//------------------------------<2D><>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD>------------------------------------------
class Resource : public Core::RefCounted
{
__DeclareClass(Resource);
public:
enum State
{
UnLoaded,
Loaded,
Failed
};
/// constructor
Resource();
/// destructor
virtual ~Resource();
/**
* GetState <EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD>״̬
* @param: void
* @return: Resources::Resource::State
* @see:
* @remark:
*/
State GetState(void) const;
/**
* SetManuLoad <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param: bool bManu
* @return: void
* @see:
* @remark: <EFBFBD>ֶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>ж<EFBFBD>ء<EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Resource::Unload<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ң<EFBFBD>ж<EFBFBD><EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
void SetManuLoad(bool bManuLoad );
/**
* GetManuLoad <EFBFBD>ж<EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param: void
* @return: bool
* @see:
* @remark:
*/
bool IsManuLoad(void) const;
/**
* IsAsynProcessing <EFBFBD><EFBFBD>Դ<EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return: bool
* @see:
* @remark:
*/
bool IsAsynProcessing() const;
/**
* SetResourceId set the resource identifier
* @param: const ResourceId & id
* @return: void
* @see:
* @remark: !!!!!! nerver change resourceid after resource create!!!!!!!!!!
*/
void SetResourceId(const ResourceId& id);
/**
* GetResourceId get the resource identifier
* @return: const ResourceId&
* @see:
* @remark:
*/
const ResourceId& GetResourceId() const;
/**
* AttachNotifier <EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD><EFBFBD>֪ͨ<EFBFBD>ص<EFBFBD>
* @param: const WeakPtr<ResourceNotifier> & notifier
* @return: void
* @see:
* @remark: ResourceNotifier<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ⲿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
void AttachNotifier(const WeakPtr<ResourceNotifier>& notifier );
/**
* DeattachNotifier ɾ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD><EFBFBD>֪ͨ<EFBFBD>ص<EFBFBD>
* @param: const WeakPtr<ResourceNotifier> & notifier
* @return: void
* @see:
* @remark: ResourceNotifier<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ⲿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
void DeattachNotifier(const WeakPtr<ResourceNotifier>& notifier);
/**
* Load <EFBFBD><EFBFBD><EFBFBD><EFBFBD>/<EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD> ResourceLoader->Resource
* @param: const GPtr<ResourceLoader> & loader
* @return: bool <EFBFBD><EFBFBD><EFBFBD><EFBFBD>/<EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߲<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>true<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>򣬷<EFBFBD><EFBFBD><EFBFBD>false
* @see:
* @remark: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>μ<EFBFBD><EFBFBD>ع<EFBFBD><EFBFBD>̳ɹ<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ResourceLoader::GetEndClipIndex<EFBFBD><EFBFBD>MaxResClipCount<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ״̬ΪLoaded<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԴΪUnloaded/Falied/LoadedParty->LoadedParty
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>μ<EFBFBD><EFBFBD>ع<EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪFailed<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Loader<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܻ<EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD>ʧЧ<EFBFBD><EFBFBD>
*/
virtual bool Load( const GPtr<ResourceLoader>& loader );
/**
* Save <EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD>Resource->ResourceSaver
* @param: const GPtr<ResourceSaver> & saver
* @return: bool <EFBFBD>Ƿ<EFBFBD>д<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD>
* @see:
* @remark: <EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ResouceSaver
*/
virtual bool Save( const GPtr<ResourceSaver>& saver );
/**
* UnLoad ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD>ݡ<EFBFBD><EFBFBD><EFBFBD>Դ״̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪUnLoad
* @return: void
* @see:
* @remark:
*/
virtual void UnLoad(void);
/**
* SwapLoad <EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٵĻ<EFBFBD>ȡ<EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param: const GPtr<Resource> & tempResData tempResDataΪ<EFBFBD><EFBFBD>,UnLoaded<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Falied״̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD>RTTI<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>;
* @return: bool <EFBFBD>Ƿ񽻻<EFBFBD><EFBFBD>ɹ<EFBFBD>
* @see: ResourceServer ʹ<EFBFBD>øú<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>ǰ<EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD>м<EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>Դ֮<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٵĽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><EFBFBD><EFBFBD>
* @remark:
*/
virtual bool SwapLoad(const GPtr<Resource>& tempResData );
/**
* CalculateRuntimeSize <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱռ<EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD>С. <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ʵļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param: void
* @return: SizeT
* @see:
* @remark: this is not fast
*/
virtual SizeT CalculateRuntimeSize(void) const;
/**
* SetState <EFBFBD>޸<EFBFBD>״̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>֪ͨNotifier
* @param: State newState
* @return: void
* @see:
* @remark: !!!!!!!!!!Ӧ<EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>Load,UnLoad,SwapLoad<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD>޸ġ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
void SetState( State newState);
bool IsAsynLoading() const
{
return isAsynLoading;
}
void SetAsynLoading(bool b)
{
isAsynLoading = b;
}
static ResourceId EmptyResID;
protected:
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>ı<EFBFBD>־
void AddAsynProcessingIndex(void);
void DecAsynProcessingIndex(void);
// <20><><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><C3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><DDBD><EFBFBD>
virtual bool SwapLoadImpl( const GPtr<Resource>& tempRes );
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ز<EFBFBD><D8B2><EFBFBD>
virtual void UnLoadImpl(void);
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>resource<63>õ<EFBFBD>һЩ<D2BB><D0A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ij<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>resourceserver<65><72><EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD><DAB8><EFBFBD>ʱ<EFBFBD><CAB1>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD>ʼֵ
virtual void InitLoadParam( const GPtr<Resource>& res );
protected:
State mState;
ResourceId mResourceID;
IndexT mAsynProcessingIndex;
bool mManuLoad;
bool isAsynLoading;
Util::Array< WeakPtr<ResourceNotifier> > mNotifierList;
friend class ResourceServer;
};
//------------------------------<2D><>Դ״̬<D7B4><EFBFBD><E4BBAF><EFBFBD><EFBFBD>֪ͨ<CDA8>ص<EFBFBD>------------------------------------------
class ResourceNotifier: public Core::RefCounted
{
__DeclareClass(ResourceNotifier);
public:
ResourceNotifier()
:mAttachCount(0) {
};
virtual ~ResourceNotifier() { };
// is notifier attach to a resource
bool IsAttached(void) const{
n_assert(mAttachCount >= 0 );
return mAttachCount != 0;
}
protected:
virtual void Notify(Resource* res, Resource::State preState) { };
void Attach(Resource* res){
++mAttachCount;
}
void Deattach(Resource* res){
--mAttachCount;
}
IndexT mAttachCount;
friend class Resource;
};
//------------------------------------------------------------------------
inline
Resource::State
Resource::GetState(void) const
{
return mState;
}
//------------------------------------------------------------------------
inline
void
Resource::SetResourceId(const ResourceId& id)
{
this->mResourceID = id;
}
//------------------------------------------------------------------------------
inline
const ResourceId&
Resource::GetResourceId() const
{
return this->mResourceID;
}
//------------------------------------------------------------------------
inline
bool
Resource::IsAsynProcessing() const
{
return mAsynProcessingIndex > 0;
}
//------------------------------------------------------------------------
inline
void
Resource::AddAsynProcessingIndex(void)
{
++mAsynProcessingIndex;
}
//------------------------------------------------------------------------
inline
void
Resource::DecAsynProcessingIndex(void)
{
--mAsynProcessingIndex;
}
//------------------------------------------------------------------------
inline
void
Resource::SetManuLoad(bool bManuLoad )
{
mManuLoad = bManuLoad;
}
//------------------------------------------------------------------------
inline
bool
Resource::IsManuLoad(void) const
{
return mManuLoad;
}
} // namespace Resources
#endif // __resource_H__