2006-04-23 16:14:18 -07:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
2006-06-09 16:07:11 -07:00
|
|
|
#include "FormationCollection.h"
|
2006-06-01 19:10:27 -07:00
|
|
|
#include "graphics/ObjectManager.h"
|
|
|
|
|
#include "graphics/Model.h"
|
|
|
|
|
#include "ps/CLogger.h"
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
#define LOG_CATEGORY "formation"
|
|
|
|
|
|
|
|
|
|
|
2007-12-20 12:21:45 -08:00
|
|
|
void CFormationCollection::LoadFile( const VfsPath& pathname )
|
2006-04-23 16:14:18 -07: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.
|
|
|
|
|
|
2008-01-07 12:03:19 -08:00
|
|
|
const CStrW basename(fs::basename(pathname));
|
2007-12-20 12:21:45 -08:00
|
|
|
m_templateFilenames[basename] = pathname.string();
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
2007-12-20 12:21:45 -08:00
|
|
|
static LibError LoadFormationThunk( const VfsPath& path, const FileInfo& UNUSED(fileInfo), uintptr_t cbData )
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2007-09-25 02:39:20 -07:00
|
|
|
CFormationCollection* this_ = (CFormationCollection*)cbData;
|
2006-04-23 16:14:18 -07:00
|
|
|
this_->LoadFile(path);
|
2007-12-20 12:21:45 -08:00
|
|
|
return INFO::CB_CONTINUE;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
2007-05-02 05:07:08 -07:00
|
|
|
int CFormationCollection::LoadTemplates()
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2006-06-09 16:07:11 -07:00
|
|
|
// Load all files in formations and subdirectories.
|
2007-12-20 12:21:45 -08:00
|
|
|
THROW_ERR( fs_ForEachFile(g_VFS, "formations/", LoadFormationThunk, (uintptr_t)this, "*.xml", DIR_RECURSIVE));
|
2006-04-23 16:14:18 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-02 05:07:08 -07:00
|
|
|
CFormation* CFormationCollection::GetTemplate( const CStrW& name )
|
2006-04-23 16:14:18 -07: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-09 16:07:11 -07:00
|
|
|
CFormation* newTemplate = new CFormation();
|
2007-05-02 05:07:08 -07:00
|
|
|
if( !newTemplate->LoadXml( path ) )
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2007-12-29 08:22:23 -08:00
|
|
|
LOG(CLogger::Error, LOG_CATEGORY, "CFormationCollection::LoadTemplates(): Couldn't load template \"%s\"", path.c_str());
|
2006-06-05 23:31:17 -07:00
|
|
|
delete newTemplate;
|
2006-04-23 16:14:18 -07:00
|
|
|
return( NULL );
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-29 08:22:23 -08:00
|
|
|
LOG(CLogger::Normal, LOG_CATEGORY, "CFormationCollection::LoadTemplates(): Loaded template \"%s\"", path.c_str());
|
2006-04-23 16:14:18 -07:00
|
|
|
m_templates[name] = newTemplate;
|
|
|
|
|
|
|
|
|
|
return newTemplate;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-02 05:07:08 -07:00
|
|
|
void CFormationCollection::GetFormationNames( std::vector<CStrW>& names )
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
for( templateFilenameMap::iterator it = m_templateFilenames.begin(); it != m_templateFilenames.end(); ++it )
|
2007-02-01 06:46:14 -08:00
|
|
|
if( ! (it->first.length() > 8 && it->first.Left(8) == L"template"))
|
2006-04-23 16:14:18 -07:00
|
|
|
names.push_back( it->first );
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-09 16:07:11 -07:00
|
|
|
CFormationCollection::~CFormationCollection()
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
for( templateMap::iterator it = m_templates.begin(); it != m_templates.end(); ++it )
|
|
|
|
|
delete( it->second );
|
|
|
|
|
}
|