[692] | 1 | /*
|
---|
| 2 | -----------------------------------------------------------------------------
|
---|
| 3 | This source file is part of OGRE
|
---|
| 4 | (Object-oriented Graphics Rendering Engine)
|
---|
| 5 | For the latest info, see http://www.ogre3d.org/
|
---|
| 6 |
|
---|
| 7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
| 8 | Also see acknowledgements in Readme.html
|
---|
| 9 |
|
---|
| 10 | This program is free software; you can redistribute it and/or modify it under
|
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
| 13 | version.
|
---|
| 14 |
|
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
| 18 |
|
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
| 23 | -----------------------------------------------------------------------------
|
---|
| 24 | */
|
---|
| 25 | #include "OgreStableHeaders.h"
|
---|
| 26 |
|
---|
| 27 | #include "OgreArchiveManager.h"
|
---|
| 28 |
|
---|
| 29 | #include "OgreArchiveFactory.h"
|
---|
| 30 | #include "OgreArchive.h"
|
---|
| 31 | #include "OgreException.h"
|
---|
| 32 | #include "OgreLogManager.h"
|
---|
| 33 |
|
---|
| 34 | namespace Ogre {
|
---|
| 35 | typedef void (*createFunc)( Archive**, const String& );
|
---|
| 36 |
|
---|
| 37 | //-----------------------------------------------------------------------
|
---|
| 38 | template<> ArchiveManager* Singleton<ArchiveManager>::ms_Singleton = 0;
|
---|
| 39 | ArchiveManager* ArchiveManager::getSingletonPtr(void)
|
---|
| 40 | {
|
---|
| 41 | return ms_Singleton;
|
---|
| 42 | }
|
---|
| 43 | ArchiveManager& ArchiveManager::getSingleton(void)
|
---|
| 44 | {
|
---|
| 45 | assert( ms_Singleton ); return ( *ms_Singleton );
|
---|
| 46 | }
|
---|
| 47 | //-----------------------------------------------------------------------
|
---|
| 48 |
|
---|
| 49 | //-----------------------------------------------------------------------
|
---|
| 50 | Archive* ArchiveManager::load( const String& filename, const String& archiveType)
|
---|
| 51 | {
|
---|
| 52 | ArchiveMap::iterator i = mArchives.find(filename);
|
---|
| 53 | Archive* pArch = 0;
|
---|
| 54 |
|
---|
| 55 | if (i == mArchives.end())
|
---|
| 56 | {
|
---|
| 57 | // Search factories
|
---|
| 58 | ArchiveFactoryMap::iterator it = mArchFactories.find(archiveType);
|
---|
| 59 | if (it == mArchFactories.end())
|
---|
| 60 | // Factory not found
|
---|
| 61 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Cannot find an archive factory "
|
---|
| 62 | "to deal with archive of type " + archiveType, "ArchiveManager::load");
|
---|
| 63 |
|
---|
| 64 | pArch = it->second->createInstance(filename);
|
---|
| 65 | pArch->load();
|
---|
| 66 | mArchives[filename] = pArch;
|
---|
| 67 |
|
---|
| 68 | }
|
---|
| 69 | else
|
---|
| 70 | {
|
---|
| 71 | pArch = i->second;
|
---|
| 72 | }
|
---|
| 73 | return pArch;
|
---|
| 74 | }
|
---|
| 75 | //-----------------------------------------------------------------------
|
---|
| 76 | void ArchiveManager::unload(Archive* arch)
|
---|
| 77 | {
|
---|
| 78 | unload(arch->getName());
|
---|
| 79 | }
|
---|
| 80 | //-----------------------------------------------------------------------
|
---|
| 81 | void ArchiveManager::unload(const String& filename)
|
---|
| 82 | {
|
---|
| 83 | ArchiveMap::iterator i = mArchives.find(filename);
|
---|
| 84 |
|
---|
| 85 | if (i != mArchives.end())
|
---|
| 86 | {
|
---|
| 87 | i->second->unload();
|
---|
| 88 | // Find factory to destroy
|
---|
| 89 | ArchiveFactoryMap::iterator fit = mArchFactories.find(i->second->getType());
|
---|
| 90 | if (fit == mArchFactories.end())
|
---|
| 91 | {
|
---|
| 92 | // Factory not found
|
---|
| 93 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Cannot find an archive factory "
|
---|
| 94 | "to deal with archive of type " + i->second->getType(), "ArchiveManager::~ArchiveManager");
|
---|
| 95 | }
|
---|
| 96 | fit->second->destroyInstance(i->second);
|
---|
| 97 | mArchives.erase(i);
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | //-----------------------------------------------------------------------
|
---|
| 101 | ArchiveManager::~ArchiveManager()
|
---|
| 102 | {
|
---|
| 103 | // Unload & delete resources in turn
|
---|
| 104 | for( ArchiveMap::iterator it = mArchives.begin(); it != mArchives.end(); ++it )
|
---|
| 105 | {
|
---|
| 106 | Archive* arch = it->second;
|
---|
| 107 | // Unload
|
---|
| 108 | arch->unload();
|
---|
| 109 | // Find factory to destroy
|
---|
| 110 | ArchiveFactoryMap::iterator fit = mArchFactories.find(arch->getType());
|
---|
| 111 | if (fit == mArchFactories.end())
|
---|
| 112 | {
|
---|
| 113 | // Factory not found
|
---|
| 114 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Cannot find an archive factory "
|
---|
| 115 | "to deal with archive of type " + arch->getType(), "ArchiveManager::~ArchiveManager");
|
---|
| 116 | }
|
---|
| 117 | fit->second->destroyInstance(arch);
|
---|
| 118 |
|
---|
| 119 | }
|
---|
| 120 | // Empty the list
|
---|
| 121 | mArchives.clear();
|
---|
| 122 | }
|
---|
| 123 | //-----------------------------------------------------------------------
|
---|
| 124 | void ArchiveManager::addArchiveFactory(ArchiveFactory* factory)
|
---|
| 125 | {
|
---|
| 126 | mArchFactories.insert( ArchiveFactoryMap::value_type( factory->getType(), factory ) );
|
---|
| 127 | LogManager::getSingleton().logMessage("ArchiveFactory for archive type " + factory->getType() + " registered.");
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | }
|
---|
| 131 |
|
---|