genesis-3d_engine/Engine/addons/resource/resource.cc
zhongdaohuan 6e8fbca745 genesis-3d engine version 1.3.
match the genesis editor version 1.3.0.653.
2014-05-05 14:50:33 +08:00

241 lines
5.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/****************************************************************************
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.
****************************************************************************/
#include "resource/resource_stdneb.h"
#include "exception/exceptions.h"
#include "resource/resource.h"
#include "util/delegate.h"
#include "resource/resourcemanager.h"
namespace Resources
{
__ImplementClass(Resources::Resource, 'RSRC', Core::RefCounted);
__ImplementClass(Resources::ResourceLoader, 'RSRL', Core::RefCounted);
__ImplementClass(Resources::ResourceSaver, 'RSRS', Core::RefCounted);
__ImplementClass(Resources::ResourceNotifier, 'RSRN', Core::RefCounted);
ResourceId Resource::EmptyResID;
//------------------------------------------------------------------------
Resource::Resource()
: mState(UnLoaded)
, mAsynProcessingIndex(0)
, mManuLoad(false)
, isAsynLoading(false)
{
// empty
}
//------------------------------------------------------------------------
Resource::~Resource()
{
n_assert( mNotifierList.IsEmpty() ) ;
}
//------------------------------------------------------------------------
void
Resource::AttachNotifier(const WeakPtr<ResourceNotifier>& notifier )
{
if ( notifier.isvalid() )
{
notifier->Attach( this );
mNotifierList.Append( notifier );
}
}
//------------------------------------------------------------------------
void
Resource::DeattachNotifier(const WeakPtr<ResourceNotifier>& notifier)
{
IndexT findIndex = mNotifierList.FindIndex( notifier );
if ( findIndex != InvalidIndex )
{
notifier->Deattach(this);
mNotifierList.EraseIndex(findIndex);
}
}
//------------------------------------------------------------------------
bool
Resource::Load( const GPtr<ResourceLoader>& pLoader )
{
bool bLoadOK = false;
if ( pLoader.isvalid() )
{
try
{
bLoadOK = pLoader->LoadResource( this );
}
catch(const Exceptions::MemoryException& )
{
n_warning("Resource is too large, lack of memory!\n");
bLoadOK = false;
}
catch(const Exceptions::Exception& e)
{
n_warning("Resource load error: %s", e.what());
bLoadOK = false;
}
catch (...)
{
bLoadOK = false;
}
}
if ( bLoadOK )
{
// <20><><EFBFBD><EFBFBD><EFBFBD>ݸı<C4B1><E4A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC>ػ<EFBFBD><D8BB><EFBFBD><EFBFBD>ٴμ<D9B4><CEBC><EFBFBD>
SetState(Loaded);
}
else
{
n_warning("Resource::Load %s Falied:\n" ,mResourceID.AsString().AsCharPtr() );
//<2F><><EFBFBD><EFBFBD>ʧЧ<CAA7>ˣ<EFBFBD><CBA3>Ѿ<EFBFBD><D1BE>е<EFBFBD>ȫ<EFBFBD><C8AB>ж<EFBFBD>ء<EFBFBD>
UnLoadImpl();
SetState(Failed );
}
return bLoadOK;
}
//------------------------------------------------------------------------
bool
Resource::Save( const GPtr<ResourceSaver>& pSaver )
{
bool bSaveOK = false;
if ( pSaver.isvalid() && mState==Loaded )
{
try
{
bSaveOK = pSaver->SaveResource(this);
}
catch (...)
{
bSaveOK = false;
}
}
if ( !bSaveOK )
{
n_warning("Resource::Save %s Falied:\n" ,mResourceID.AsString().AsCharPtr() );
}
return bSaveOK;
}
//------------------------------------------------------------------------
void
Resource::UnLoad()
{
if ( GetState() != UnLoaded )
{
UnLoadImpl();
SetState( UnLoaded );
}
}
//------------------------------------------------------------------------
bool
Resource::SwapLoad(const GPtr<Resource>& tempRes )
{
bool bSwapOK = false;
if ( tempRes.isvalid() && tempRes->GetState() == Loaded )
{
try
{
bSwapOK = SwapLoadImpl( tempRes );
}
catch (...)
{
bSwapOK = false;
}
}
//<2F><><EFBFBD><EFBFBD>״̬<D7B4>л<EFBFBD>
if ( bSwapOK )
{
// <20><><EFBFBD><EFBFBD><EFBFBD>ݸı<C4B1><E4A3AC><EFBFBD><EFBFBD>״̬<D7B4>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<C4B1><E4A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>֪ͨ<CDA8><D6AA>
SetState(Loaded);
}
else
{
n_warning("Resource::SwapLoad %s Falied:\n" ,mResourceID.AsString().AsCharPtr() );
// ִ<><D6B4><EFBFBD>˽<EFBFBD><CBBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><DDBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD>Ϊ<EFBFBD>Ѿ<EFBFBD><D1BE>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB>ʧЧ<CAA7><D0A7>
UnLoadImpl();
SetState( Failed );
}
return bSwapOK;
}
//------------------------------------------------------------------------
void
Resource::SetState( State newState)
{
State oldState = mState;
mState = newState;
// if(newState != Resources::Resource::Loaded)
// return;
IndexT size = mNotifierList.Size();
for ( IndexT index = 0; index < size; ++index )
{
n_assert( mNotifierList[index].isvalid() );
mNotifierList[index]->Notify( this, oldState );
}
}
//------------------------------------------------------------------------
bool
Resource::SwapLoadImpl( const GPtr<Resource>& tempRes)
{
// should impl in Derived Resource
return false;
}
//------------------------------------------------------------------------
void Resource::UnLoadImpl(void)
{
// should impl in Derived Resource
}
//------------------------------------------------------------------------
SizeT Resource::CalculateRuntimeSize(void) const
{
return sizeof(Resource);
}
//------------------------------------------------------------------------
void Resource::InitLoadParam( const GPtr<Resource>& res )
{
mState = res->GetState();
mResourceID = res->GetResourceId();
}
}