2006-06-06 08:31:17 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2006-06-10 01:07:11 +02:00
|
|
|
#include "TechnologyCollection.h"
|
2006-06-06 08:31:17 +02:00
|
|
|
#include "ps/CLogger.h"
|
|
|
|
#include "ps/VFSUtil.h"
|
2006-07-18 06:17:46 +02:00
|
|
|
#include "ps/Player.h"
|
2006-06-06 08:31:17 +02:00
|
|
|
|
|
|
|
#define LOG_CATEGORY "tech"
|
|
|
|
|
2006-06-10 01:07:11 +02:00
|
|
|
void CTechnologyCollection::LoadFile( const char* path )
|
2006-06-06 08:31:17 +02:00
|
|
|
{
|
|
|
|
//Make tech file reading
|
|
|
|
CStrW tag = CStr(path).AfterLast("/").BeforeLast(".xml");
|
2006-07-18 06:17:46 +02:00
|
|
|
m_techFilenames[tag] = path;
|
2006-06-06 08:31:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void LoadTechThunk( const char* path, const DirEnt* UNUSED(ent), void* context )
|
|
|
|
{
|
2006-06-10 01:07:11 +02:00
|
|
|
CTechnologyCollection* this_ = (CTechnologyCollection*)context;
|
2006-06-06 08:31:17 +02:00
|
|
|
this_->LoadFile(path);
|
|
|
|
}
|
|
|
|
|
2006-06-10 01:07:11 +02:00
|
|
|
int CTechnologyCollection::loadTechnologies()
|
2006-06-06 08:31:17 +02:00
|
|
|
{
|
|
|
|
// Load all files in techs/ and subdirectories.
|
2006-06-10 01:07:11 +02:00
|
|
|
THROW_ERR( vfs_dir_enum( "technologies/", VFS_DIR_RECURSIVE, "*.xml",
|
2006-06-06 08:31:17 +02:00
|
|
|
LoadTechThunk, this ) );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 16:37:58 +02:00
|
|
|
CTechnology* CTechnologyCollection::getTechnology( const CStrW& name, CPlayer* player )
|
2006-06-06 08:31:17 +02:00
|
|
|
{
|
2006-07-18 06:17:46 +02:00
|
|
|
// Find player ID
|
|
|
|
debug_assert( player != 0 );
|
|
|
|
int id = player->GetPlayerID();
|
|
|
|
|
2006-06-06 08:31:17 +02:00
|
|
|
// Check whether this template has already been loaded.
|
|
|
|
//If previously loaded, all slots will be found, so any entry works.
|
2006-07-18 06:17:46 +02:00
|
|
|
TechMap::iterator it = m_techs[id].find( name );
|
|
|
|
if( it != m_techs[id].end() )
|
2006-06-06 08:31:17 +02:00
|
|
|
return( it->second );
|
|
|
|
|
|
|
|
// Find the filename corresponding to this template
|
2006-07-18 06:17:46 +02:00
|
|
|
TechFilenameMap::iterator filename_it = m_techFilenames.find( name );
|
|
|
|
if( filename_it == m_techFilenames.end() )
|
2006-06-06 08:31:17 +02:00
|
|
|
return( NULL );
|
|
|
|
|
|
|
|
CStr path( filename_it->second );
|
|
|
|
|
|
|
|
//Try to load to the tech
|
2006-07-21 00:43:06 +02:00
|
|
|
CTechnology* newTemplate = new CTechnology( name, player );
|
2006-06-06 08:31:17 +02:00
|
|
|
if( !newTemplate->loadXML( path ) )
|
|
|
|
{
|
2006-07-18 06:17:46 +02:00
|
|
|
LOG(ERROR, LOG_CATEGORY, "CTechnologyCollection::getTechnology(): Couldn't load tech \"%s\"", path.c_str());
|
2006-06-06 08:31:17 +02:00
|
|
|
delete newTemplate;
|
|
|
|
return( NULL );
|
|
|
|
|
|
|
|
}
|
2006-07-18 06:17:46 +02:00
|
|
|
m_techs[id][name] = newTemplate;
|
2006-06-06 08:31:17 +02:00
|
|
|
|
2006-07-18 06:17:46 +02:00
|
|
|
LOG(NORMAL, LOG_CATEGORY, "CTechnologyCollection::getTechnology(): Loaded tech \"%s\"", path.c_str());
|
2006-06-06 08:31:17 +02:00
|
|
|
return newTemplate;
|
|
|
|
}
|
|
|
|
|
2006-06-10 01:07:11 +02:00
|
|
|
CTechnologyCollection::~CTechnologyCollection()
|
2006-06-06 08:31:17 +02:00
|
|
|
{
|
2006-07-18 06:17:46 +02:00
|
|
|
for( uint id=0; id<PS_MAX_PLAYERS+1; id++ )
|
|
|
|
for( TechMap::iterator it = m_techs[id].begin(); it != m_techs[id].end(); ++it )
|
|
|
|
delete( it->second );
|
2006-06-06 08:31:17 +02:00
|
|
|
}
|