/src/ogre/OgreMain/src/OgreArchiveManager.cpp
Line | Count | Source |
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-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | #include "OgreStableHeaders.h" |
29 | | |
30 | | namespace Ogre { |
31 | | typedef void (*createFunc)( Archive**, const String& ); |
32 | | |
33 | | //----------------------------------------------------------------------- |
34 | | template<> ArchiveManager* Singleton<ArchiveManager>::msSingleton = 0; |
35 | | ArchiveManager* ArchiveManager::getSingletonPtr(void) |
36 | 0 | { |
37 | 0 | return msSingleton; |
38 | 0 | } |
39 | | ArchiveManager& ArchiveManager::getSingleton(void) |
40 | 3 | { |
41 | 3 | assert( msSingleton ); return ( *msSingleton ); |
42 | 3 | } |
43 | | //----------------------------------------------------------------------- |
44 | | ArchiveManager::ArchiveManager() |
45 | 1 | { |
46 | 1 | } |
47 | | //----------------------------------------------------------------------- |
48 | | Archive* ArchiveManager::load( const String& filename, const String& archiveType, bool readOnly) |
49 | 0 | { |
50 | 0 | ArchiveMap::iterator i = mArchives.find(filename); |
51 | 0 | Archive* pArch = 0; |
52 | |
|
53 | 0 | if (i == mArchives.end()) |
54 | 0 | { |
55 | | // Search factories |
56 | 0 | ArchiveFactoryMap::iterator it = mArchFactories.find(archiveType); |
57 | 0 | if (it == mArchFactories.end()) |
58 | 0 | { |
59 | | // Factory not found |
60 | 0 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, |
61 | 0 | "Cannot find an ArchiveFactory for type '" + archiveType + "'"); |
62 | 0 | } |
63 | | |
64 | 0 | pArch = it->second->createInstance(filename, readOnly); |
65 | 0 | pArch->load(); |
66 | 0 | mArchives[filename] = pArch; |
67 | |
|
68 | 0 | } |
69 | 0 | else |
70 | 0 | { |
71 | 0 | pArch = i->second; |
72 | 0 | OgreAssert(pArch->isReadOnly() == readOnly, "existing archive location has different readOnly status"); |
73 | 0 | } |
74 | | |
75 | 0 | return pArch; |
76 | 0 | } |
77 | | //----------------------------------------------------------------------- |
78 | | void ArchiveManager::unload(Archive* arch) |
79 | 0 | { |
80 | 0 | unload(arch->getName()); |
81 | 0 | } |
82 | | //----------------------------------------------------------------------- |
83 | | void ArchiveManager::unload(const String& filename) |
84 | 0 | { |
85 | 0 | ArchiveMap::iterator i = mArchives.find(filename); |
86 | |
|
87 | 0 | if (i != mArchives.end()) |
88 | 0 | { |
89 | 0 | i->second->unload(); |
90 | | // Find factory to destroy. An archive factory created this file, it should still be there! |
91 | 0 | ArchiveFactoryMap::iterator fit = mArchFactories.find(i->second->getType()); |
92 | 0 | assert( fit != mArchFactories.end() && "Cannot find an ArchiveFactory " |
93 | 0 | "to deal with archive this type" ); |
94 | 0 | fit->second->destroyInstance(i->second); |
95 | 0 | mArchives.erase(i); |
96 | 0 | } |
97 | 0 | } |
98 | | //----------------------------------------------------------------------- |
99 | | ArchiveManager::ArchiveMapIterator ArchiveManager::getArchiveIterator(void) |
100 | 0 | { |
101 | 0 | return ArchiveMapIterator(mArchives.begin(), mArchives.end()); |
102 | 0 | } |
103 | | //----------------------------------------------------------------------- |
104 | | ArchiveManager::~ArchiveManager() |
105 | 1 | { |
106 | | // Thanks to http://www.viva64.com/en/examples/V509/ for finding the error for us! |
107 | | // (originally, it detected we were throwing using OGRE_EXCEPT in the destructor) |
108 | | // We now raise an assert. |
109 | | |
110 | | // Unload & delete resources in turn |
111 | 1 | for (auto& a : mArchives) |
112 | 0 | { |
113 | | // Unload |
114 | 0 | a.second->unload(); |
115 | | // Find factory to destroy. An archive factory created this file, it should still be there! |
116 | 0 | ArchiveFactoryMap::iterator fit = mArchFactories.find(a.second->getType()); |
117 | 0 | assert( fit != mArchFactories.end() && "Cannot find an ArchiveFactory " |
118 | 0 | "to deal with archive this type" ); |
119 | 0 | fit->second->destroyInstance(a.second); |
120 | 0 | } |
121 | | // Empty the list |
122 | 1 | mArchives.clear(); |
123 | 1 | } |
124 | | //----------------------------------------------------------------------- |
125 | | void ArchiveManager::addArchiveFactory(ArchiveFactory* factory) |
126 | 3 | { |
127 | 3 | mArchFactories.emplace(factory->getType(), factory); |
128 | 3 | LogManager::getSingleton().logMessage("ArchiveFactory for type '" + factory->getType() + "' registered"); |
129 | 3 | } |
130 | | |
131 | | } |
132 | | |