2004-06-03 20:38:14 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2004-05-22 01:46:16 +02:00
|
|
|
#include "BaseEntityCollection.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "graphics/ObjectManager.h"
|
|
|
|
#include "graphics/Model.h"
|
|
|
|
#include "ps/CLogger.h"
|
|
|
|
#include "ps/VFSUtil.h"
|
|
|
|
#include "ps/Player.h"
|
2004-05-22 01:46:16 +02:00
|
|
|
|
2004-08-15 22:57:31 +02:00
|
|
|
#define LOG_CATEGORY "entity"
|
|
|
|
|
2005-03-27 03:44:41 +02:00
|
|
|
void CBaseEntityCollection::LoadFile( const char* path )
|
|
|
|
{
|
2005-03-29 22:50:04 +02:00
|
|
|
// Build the entity name -> filename mapping. This is done so that
|
|
|
|
// the entity 'x' can be in units/x.xml, structures/x.xml, etc, and
|
|
|
|
// we don't have to search every directory for x.xml.
|
|
|
|
|
|
|
|
// Extract the filename out of the path+name+extension.
|
|
|
|
// Equivalent to /.*\/(.*)\.xml/, but not as pretty.
|
|
|
|
CStrW tag = CStr(path).AfterLast("/").BeforeLast(".xml");
|
|
|
|
m_templateFilenames[tag] = path;
|
2005-03-27 03:44:41 +02:00
|
|
|
}
|
2004-08-03 01:14:54 +02:00
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
static void LoadFileThunk( const char* path, const DirEnt* UNUSED(ent), void* context )
|
2005-03-27 03:44:41 +02:00
|
|
|
{
|
|
|
|
CBaseEntityCollection* this_ = (CBaseEntityCollection*)context;
|
|
|
|
this_->LoadFile(path);
|
|
|
|
}
|
2004-08-03 01:14:54 +02:00
|
|
|
|
2005-05-03 23:36:57 +02:00
|
|
|
int CBaseEntityCollection::loadTemplates()
|
2005-03-27 03:44:41 +02:00
|
|
|
{
|
|
|
|
// Load all files in entities/ and its subdirectories.
|
2006-04-14 08:32:05 +02:00
|
|
|
THROW_ERR( vfs_dir_enum( "entities/", VFS_DIR_RECURSIVE, "*.xml",
|
2005-08-09 17:55:44 +02:00
|
|
|
LoadFileThunk, this ) );
|
2005-05-03 23:36:57 +02:00
|
|
|
return 0;
|
2005-03-29 22:50:04 +02:00
|
|
|
}
|
2004-08-03 01:14:54 +02:00
|
|
|
|
2006-05-14 00:11:46 +02:00
|
|
|
CBaseEntity* CBaseEntityCollection::getTemplate( CStrW name, CPlayer* player )
|
2005-03-29 22:50:04 +02:00
|
|
|
{
|
2006-05-14 00:11:46 +02:00
|
|
|
// Find player ID
|
|
|
|
int id = ( player == 0 ? NULL_PLAYER : player->GetPlayerID() );
|
|
|
|
|
2005-03-29 22:50:04 +02:00
|
|
|
// Check whether this template has already been loaded
|
2006-05-14 00:11:46 +02:00
|
|
|
templateMap::iterator it = m_templates[id].find( name );
|
|
|
|
if( it != m_templates[id].end() )
|
2005-03-29 22:50:04 +02:00
|
|
|
return( it->second );
|
2004-10-07 21:23:35 +02:00
|
|
|
|
2005-03-29 22:50:04 +02:00
|
|
|
// Find the filename corresponding to this template
|
|
|
|
templateFilenameMap::iterator filename_it = m_templateFilenames.find( name );
|
|
|
|
if( filename_it == m_templateFilenames.end() )
|
|
|
|
return( NULL );
|
2004-11-11 08:09:32 +01:00
|
|
|
|
2005-03-29 22:50:04 +02:00
|
|
|
CStr path( filename_it->second );
|
2004-10-07 21:23:35 +02:00
|
|
|
|
2005-03-29 22:50:04 +02:00
|
|
|
// Try to load to the entity
|
2006-05-14 00:11:46 +02:00
|
|
|
CBaseEntity* newTemplate = new CBaseEntity( player );
|
2005-03-29 22:50:04 +02:00
|
|
|
if( !newTemplate->loadXML( path ) )
|
2004-11-11 08:09:32 +01:00
|
|
|
{
|
2005-03-29 22:50:04 +02:00
|
|
|
LOG(ERROR, LOG_CATEGORY, "CBaseEntityCollection::loadTemplates(): Couldn't load template \"%s\"", path.c_str());
|
|
|
|
return( NULL );
|
2004-10-07 21:23:35 +02:00
|
|
|
}
|
2004-05-22 01:46:16 +02:00
|
|
|
|
2005-03-29 22:50:04 +02:00
|
|
|
LOG(NORMAL, LOG_CATEGORY, "CBaseEntityCollection::loadTemplates(): Loaded template \"%s\"", path.c_str());
|
2006-05-14 00:11:46 +02:00
|
|
|
m_templates[id][name] = newTemplate;
|
2004-05-22 01:46:16 +02:00
|
|
|
|
2005-03-29 22:50:04 +02:00
|
|
|
// Load the entity's parent, if it has one
|
|
|
|
if( newTemplate->m_Base_Name.Length() )
|
|
|
|
{
|
2006-05-14 00:11:46 +02:00
|
|
|
CBaseEntity* base = getTemplate( newTemplate->m_Base_Name, player );
|
2005-03-29 22:50:04 +02:00
|
|
|
if( base )
|
2005-03-30 00:26:48 +02:00
|
|
|
{
|
2005-03-29 22:50:04 +02:00
|
|
|
newTemplate->m_base = base;
|
2005-03-30 00:26:48 +02:00
|
|
|
newTemplate->loadBase();
|
|
|
|
}
|
2005-03-29 22:50:04 +02:00
|
|
|
else
|
2005-03-30 04:06:00 +02:00
|
|
|
{
|
2005-03-29 22:50:04 +02:00
|
|
|
LOG( WARNING, LOG_CATEGORY, "Parent template \"%ls\" does not exist in template \"%ls\"", newTemplate->m_Base_Name.c_str(), newTemplate->m_Tag.c_str() );
|
|
|
|
// (The requested entity will still be returned, but with no parent.
|
|
|
|
// Is this a reasonable thing to do?)
|
2005-03-30 04:06:00 +02:00
|
|
|
}
|
2005-03-29 22:50:04 +02:00
|
|
|
}
|
2004-05-22 01:46:16 +02:00
|
|
|
|
2005-03-29 22:50:04 +02:00
|
|
|
return newTemplate;
|
2004-05-22 01:46:16 +02:00
|
|
|
}
|
2004-06-11 00:24:03 +02:00
|
|
|
|
2005-03-30 04:06:00 +02:00
|
|
|
void CBaseEntityCollection::getBaseEntityNames( std::vector<CStrW>& names )
|
2005-03-23 00:31:30 +01:00
|
|
|
{
|
2005-03-29 22:50:04 +02:00
|
|
|
for( templateFilenameMap::iterator it = m_templateFilenames.begin(); it != m_templateFilenames.end(); ++it )
|
2005-03-30 04:06:00 +02:00
|
|
|
if( ! (it->first.Length() > 8 && it->first.Left(8) == L"template"))
|
|
|
|
names.push_back( it->first );
|
2005-03-23 00:31:30 +01:00
|
|
|
}
|
|
|
|
|
2004-06-11 00:24:03 +02:00
|
|
|
CBaseEntityCollection::~CBaseEntityCollection()
|
|
|
|
{
|
2006-05-14 00:11:46 +02:00
|
|
|
for( int id = 0; id < PS_MAX_PLAYERS + 2; id++ )
|
|
|
|
for( templateMap::iterator it = m_templates[id].begin(); it != m_templates[id].end(); ++it )
|
|
|
|
delete( it->second );
|
2004-06-11 00:24:03 +02:00
|
|
|
}
|