0ad/source/simulation/FormationCollection.cpp
janwas 8667ea74c8 fixes and improvements
- directoryPosix: replace most methods with boost filesystem (but not
all: the latter cannot efficiently enumerate files AND query their
size/mtime)
- AllocatorChecker: better name for member functions
- file: move the File class here
- trace: bugfix
- io: move UnalignedWriter to write_buffer.cpp (basically the same
thing)
- vfs: remove unnecessary "vfs" warts from variable names
- vfs_tree: VfsFile now stores single Name/Size/MTime fields instead of
the FileInfo record (less clunky)
- vfs_path: use boost filesystem's version of the basename/extension
functions
- lf_alloc: remove (no longer necessary, won't be finished - not worth
the trouble)
- path_util: remove path_foreach_component (replaced by better path
traversal logic) and PathPackage (obsoleted by fs::path)

! resource loading code now receives VfsPath as its filename. there is
also OsPath (native absolute path) and Path (relative to binaries/data)

- tex is now independent of file loading code; it just en/decodes
in-memory buffers
- wdll_ver: clean up, use smart pointer to simplify bailout code
- wsdl: remove nonexistent failure path from calc_gamma (cruised by here
because SDL_SetGamme is failing once after a cold boot at work)
- wsnd: simplify OpenAL DLL search, use boost::filesystem
- wutil: Wow64 redirection is now packaged in a (RAII) class

This was SVN commit r5525.
2007-12-22 18:15:52 +00:00

75 lines
2.3 KiB
C++

#include "precompiled.h"
#include "FormationCollection.h"
#include "graphics/ObjectManager.h"
#include "graphics/Model.h"
#include "ps/CLogger.h"
#define LOG_CATEGORY "formation"
void CFormationCollection::LoadFile( const VfsPath& pathname )
{
// 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.
const CStrW basename(fs::basename((const fs::path&)pathname));
m_templateFilenames[basename] = pathname.string();
}
static LibError LoadFormationThunk( const VfsPath& path, const FileInfo& UNUSED(fileInfo), uintptr_t cbData )
{
CFormationCollection* this_ = (CFormationCollection*)cbData;
this_->LoadFile(path);
return INFO::CB_CONTINUE;
}
int CFormationCollection::LoadTemplates()
{
// Load all files in formations and subdirectories.
THROW_ERR( fs_ForEachFile(g_VFS, "formations/", LoadFormationThunk, (uintptr_t)this, "*.xml", DIR_RECURSIVE));
return 0;
}
CFormation* CFormationCollection::GetTemplate( const CStrW& name )
{
// 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
CFormation* newTemplate = new CFormation();
if( !newTemplate->LoadXml( path ) )
{
LOG(ERROR, LOG_CATEGORY, "CFormationCollection::LoadTemplates(): Couldn't load template \"%s\"", path.c_str());
delete newTemplate;
return( NULL );
}
LOG(NORMAL, LOG_CATEGORY, "CFormationCollection::LoadTemplates(): Loaded template \"%s\"", path.c_str());
m_templates[name] = newTemplate;
return newTemplate;
}
void CFormationCollection::GetFormationNames( std::vector<CStrW>& names )
{
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 );
}
CFormationCollection::~CFormationCollection()
{
for( templateMap::iterator it = m_templates.begin(); it != m_templates.end(); ++it )
delete( it->second );
}