2006-04-24 01:14:18 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2006-06-10 01:07:11 +02:00
|
|
|
#include "FormationCollection.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"
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
#define LOG_CATEGORY "formation"
|
|
|
|
|
|
|
|
|
2006-06-10 01:07:11 +02:00
|
|
|
void CFormationCollection::LoadFile( const char* path )
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
// Build the formation name -> filename mapping. This is done so that
|
|
|
|
// the formation '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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void LoadFormationThunk( const char* path, const DirEnt* UNUSED(ent), void* context )
|
|
|
|
{
|
2006-06-10 01:07:11 +02:00
|
|
|
CFormationCollection* this_ = (CFormationCollection*)context;
|
2006-04-24 01:14:18 +02:00
|
|
|
this_->LoadFile(path);
|
|
|
|
}
|
|
|
|
|
2006-06-10 01:07:11 +02:00
|
|
|
int CFormationCollection::loadTemplates()
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2006-06-10 01:07:11 +02:00
|
|
|
// Load all files in formations and subdirectories.
|
|
|
|
THROW_ERR( vfs_dir_enum( "formations", VFS_DIR_RECURSIVE, "*.xml",
|
2006-04-24 01:14:18 +02:00
|
|
|
LoadFormationThunk, this ) );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 16:37:58 +02:00
|
|
|
CFormation* CFormationCollection::getTemplate( const CStrW& name )
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
// Check whether this template has already been loaded
|
|
|
|
templateMap::iterator it = m_templates.find( name );
|
|
|
|
if( it != m_templates.end() )
|
|
|
|
return( it->second );
|
|
|
|
|
|
|
|
// Find the filename corresponding to this template
|
|
|
|
templateFilenameMap::iterator filename_it = m_templateFilenames.find( name );
|
|
|
|
if( filename_it == m_templateFilenames.end() )
|
|
|
|
return( NULL );
|
|
|
|
|
|
|
|
CStr path( filename_it->second );
|
|
|
|
|
|
|
|
//Try to load to the formation
|
2006-06-10 01:07:11 +02:00
|
|
|
CFormation* newTemplate = new CFormation();
|
2006-04-24 01:14:18 +02:00
|
|
|
if( !newTemplate->loadXML( path ) )
|
|
|
|
{
|
2006-06-10 01:07:11 +02:00
|
|
|
LOG(ERROR, LOG_CATEGORY, "CFormationCollection::loadTemplates(): Couldn't load template \"%s\"", path.c_str());
|
2006-06-06 08:31:17 +02:00
|
|
|
delete newTemplate;
|
2006-04-24 01:14:18 +02:00
|
|
|
return( NULL );
|
|
|
|
}
|
|
|
|
|
2006-06-10 01:07:11 +02:00
|
|
|
LOG(NORMAL, LOG_CATEGORY, "CFormationCollection::loadTemplates(): Loaded template \"%s\"", path.c_str());
|
2006-04-24 01:14:18 +02:00
|
|
|
m_templates[name] = newTemplate;
|
|
|
|
|
|
|
|
return newTemplate;
|
|
|
|
}
|
|
|
|
|
2006-06-10 01:07:11 +02:00
|
|
|
void CFormationCollection::getFormationNames( std::vector<CStrW>& names )
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
for( templateFilenameMap::iterator it = m_templateFilenames.begin(); it != m_templateFilenames.end(); ++it )
|
|
|
|
if( ! (it->first.Length() > 8 && it->first.Left(8) == L"template"))
|
|
|
|
names.push_back( it->first );
|
|
|
|
}
|
|
|
|
|
2006-06-10 01:07:11 +02:00
|
|
|
CFormationCollection::~CFormationCollection()
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
for( templateMap::iterator it = m_templates.begin(); it != m_templates.end(); ++it )
|
|
|
|
delete( it->second );
|
|
|
|
}
|