resourcemanager.cpp

来自「天之炼狱1服务器端源文件游戏服务端不完整」· C++ 代码 · 共 182 行

CPP
182
字号
//--------------------------------------------------------------------------------// // Filename    : ResourceManager.cpp// Written By  : Reiot// //--------------------------------------------------------------------------------// include files#include "ResourceManager.h"#include "Assert.h"#include "Socket.h"#include "SocketOutputStream.h"//--------------------------------------------------------------------------------// constructor//--------------------------------------------------------------------------------ResourceManager::ResourceManager () 	throw (){}//--------------------------------------------------------------------------------// destructor//--------------------------------------------------------------------------------ResourceManager::~ResourceManager () 	throw (){	while ( !m_Resources.empty() ) 	{		Resource * pResource = m_Resources.front();		SAFE_DELETE(pResource);		m_Resources.pop_front();	}}	//--------------------------------------------------------------------------------// load from resource file//--------------------------------------------------------------------------------void ResourceManager::load ( const string & filename ) 	throw ( Error ){	__BEGIN_TRY	ifstream ifile( filename.c_str() , ios::in | ios::binary | ios::nocreate );	if ( !ifile ) {		StringStream msg;		msg << "cannot open " << filename << " with read mode";		throw Error(msg.toString());	}	//--------------------------------------------------------------------------------	// read #Resources	//--------------------------------------------------------------------------------	WORD nResources;	ifile.read( (char *)&nResources, szWORD );	if ( nResources > maxResources )		throw Error("too many resources");	//--------------------------------------------------------------------------------	// read each recource	//--------------------------------------------------------------------------------	for ( int i = 0 ; i < nResources ; i ++ ) {		Resource * pResource = new Resource();		pResource->load( ifile );		push_back( pResource );	}	ifile.close();	__END_CATCH}//--------------------------------------------------------------------------------// save to resource file//--------------------------------------------------------------------------------void ResourceManager::save ( const string & filename ) const 	throw ( Error ){	__BEGIN_TRY	ofstream ofile( filename.c_str() , ios::out | ios::noreplace );	if ( !ofile ) {		StringStream msg;		msg << "cannot open " << filename << " with write mode";		throw Error(msg.toString());	}	//--------------------------------------------------------------------------------	// write #Resource	//--------------------------------------------------------------------------------	WORD nResources = m_Resources.size();	if ( nResources > maxResources )		throw Error("too many resources");	ofile.write( (const char *)&nResources , szWORD );	//--------------------------------------------------------------------------------	// write each resource	//--------------------------------------------------------------------------------	for ( list< Resource * >::const_iterator itr = m_Resources.begin() ; itr != m_Resources.end() ; itr ++ ) {		(*itr)->save( ofile );	}	ofile.close();	__END_CATCH}//--------------------------------------------------------------------------------// 促款罐绰 颇老狼 吝汗 咯何 殿阑 眉农秦辑 促款肺靛 矫埃苞 侩樊阑 弥利拳窍档废 茄促.// 老窜篮 角青 颇老狼 吝汗 咯何父阑 眉农茄促.//--------------------------------------------------------------------------------void ResourceManager::optimize ()	throw ( Error ){	__BEGIN_TRY/*	// 第俊辑何磐 眉农茄促.	bool exefileFound = false;	list< Resource * >::reverse_iterator before = m_Resources.rend();	list< Resource * >::reverse_iterator current = m_Resources.rbegin();	while ( current != m_Resources.rend() ) {		// 颇老疙阑 罐酒柯促.		string filename = (*current)->getFilename();		// 颇老 荤捞令甫 肋扼郴绊 鉴荐窍霸 颇老疙父阑 历厘秦敌促.		uint i = filename.find_first_of(' ');		filename = filename.substr(0,i);		// 捞 颇老疙救俊 darkeden.exe 啊 器窃瞪 版快, 吝汗 眉农俊 甸绢埃促.		if ( filename == "DarkEden.exe" ) {			if ( exefileFound == false ) {				exefileFound = true;			} else {				// 畴靛甫 昏力茄促.				if ( before == m_Resources.rend() ) {					m_Resources.erase( current );					current = m_Resources.rbegin();				} else {					m_Resources.erase( current );					current = before;					current ++;				}			}		}	}*/	__END_CATCH}//--------------------------------------------------------------------------------// get debug string//--------------------------------------------------------------------------------string ResourceManager::toString () const 	throw (){	StringStream msg;	msg << "ResourceManager(\n";			for ( list< Resource * >::const_iterator itr = m_Resources.begin() ; itr != m_Resources.end() ; itr ++ ) 	{		msg << (*itr)->toString();	}	msg << ")";	return msg.toString();}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?