OgreResourceGroupManager.h

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004 (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2009 Torus Knot Software Ltd
00008 
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015 
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018 
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 -----------------------------------------------------------------------------
00027 */
00028 #ifndef _ResourceGroupManager_H__
00029 #define _ResourceGroupManager_H__
00030 
00031 #include "OgrePrerequisites.h"
00032 #include "OgreSingleton.h"
00033 #include "OgreCommon.h"
00034 #include "OgreDataStream.h"
00035 #include "OgreResource.h"
00036 #include "OgreArchive.h"
00037 #include "OgreIteratorWrappers.h"
00038 #include <ctime>
00039 
00040 // If X11/Xlib.h gets included before this header (for example it happens when
00041 // including wxWidgets and FLTK), Status is defined as an int which we don't
00042 // want as we have an enum named Status.
00043 #ifdef Status
00044 #undef Status
00045 #endif
00046 
00047 namespace Ogre {
00048 
00085     class _OgreExport ResourceGroupListener
00086     {
00087     public:
00088         virtual ~ResourceGroupListener() {}
00089 
00099         virtual void resourceGroupScriptingStarted(const String& groupName, size_t scriptCount) = 0;
00107         virtual void scriptParseStarted(const String& scriptName, bool& skipThisScript) = 0;
00108 
00111         virtual void scriptParseEnded(const String& scriptName, bool skipped) = 0;
00113         virtual void resourceGroupScriptingEnded(const String& groupName) = 0;
00114 
00120         virtual void resourceGroupPrepareStarted(const String& groupName, size_t resourceCount)
00121                 { (void)groupName; (void)resourceCount; }
00122 
00126         virtual void resourcePrepareStarted(const ResourcePtr& resource)
00127                 { (void)resource; }
00128 
00131         virtual void resourcePrepareEnded(void) {}
00137         virtual void worldGeometryPrepareStageStarted(const String& description)
00138         { (void)description; }
00139 
00145         virtual void worldGeometryPrepareStageEnded(void) {}
00147         virtual void resourceGroupPrepareEnded(const String& groupName)
00148         { (void)groupName; }
00149 
00155         virtual void resourceGroupLoadStarted(const String& groupName, size_t resourceCount) = 0;
00159         virtual void resourceLoadStarted(const ResourcePtr& resource) = 0;
00162         virtual void resourceLoadEnded(void) = 0;
00168         virtual void worldGeometryStageStarted(const String& description) = 0;
00174         virtual void worldGeometryStageEnded(void) = 0;
00176         virtual void resourceGroupLoadEnded(const String& groupName) = 0;
00177     };
00178 
00184     class ResourceLoadingListener
00185     {
00186     public:
00187         virtual ~ResourceLoadingListener() {}
00188 
00190         virtual DataStreamPtr resourceLoading(const String &name, const String &group, Resource *resource) = 0;
00191 
00197         virtual void resourceStreamOpened(const String &name, const String &group, Resource *resource, DataStreamPtr& dataStream) = 0;
00198 
00201         virtual bool resourceCollision(Resource *resource, ResourceManager *resourceManager) = 0;
00202     };
00203 
00252     class _OgreExport ResourceGroupManager : public Singleton<ResourceGroupManager>, public ResourceAlloc
00253     {
00254     public:
00255         OGRE_AUTO_MUTEX // public to allow external locking
00257         static String DEFAULT_RESOURCE_GROUP_NAME;
00259         static String INTERNAL_RESOURCE_GROUP_NAME;
00261         static String BOOTSTRAP_RESOURCE_GROUP_NAME;
00263         static String AUTODETECT_RESOURCE_GROUP_NAME;
00265         static size_t RESOURCE_SYSTEM_NUM_REFERENCE_COUNTS;
00267         struct ResourceDeclaration
00268         {
00269             String resourceName;
00270             String resourceType;
00271             ManualResourceLoader* loader;
00272             NameValuePairList parameters;
00273         };
00275         typedef list<ResourceDeclaration>::type ResourceDeclarationList;
00276         typedef map<String, ResourceManager*>::type ResourceManagerMap;
00277         typedef MapIterator<ResourceManagerMap> ResourceManagerIterator;
00279         struct ResourceLocation
00280         {
00282             Archive* archive;
00284             bool recursive;
00285         };
00287         typedef list<ResourceLocation*>::type LocationList;
00288 
00289     protected:
00291         ResourceManagerMap mResourceManagerMap;
00292 
00294         typedef multimap<Real, ScriptLoader*>::type ScriptLoaderOrderMap;
00295         ScriptLoaderOrderMap mScriptLoaderOrderMap;
00296 
00297         typedef vector<ResourceGroupListener*>::type ResourceGroupListenerList;
00298         ResourceGroupListenerList mResourceGroupListenerList;
00299 
00300         ResourceLoadingListener *mLoadingListener;
00301 
00303         typedef map<String, Archive*>::type ResourceLocationIndex;
00304 
00306         typedef list<ResourcePtr>::type LoadUnloadResourceList;
00308         struct ResourceGroup
00309         {
00310             enum Status
00311             {
00312                 UNINITIALSED = 0,
00313                 INITIALISING = 1,
00314                 INITIALISED = 2,
00315                 LOADING = 3,
00316                 LOADED = 4
00317             };
00319             OGRE_AUTO_MUTEX
00321             OGRE_MUTEX(statusMutex)
00323             String name;
00325             Status groupStatus;
00327             LocationList locationList;
00329             ResourceLocationIndex resourceIndexCaseSensitive;
00331             ResourceLocationIndex resourceIndexCaseInsensitive;
00333             ResourceDeclarationList resourceDeclarations;
00335             // Group by loading order of the type (defined by ResourceManager)
00336             // (e.g. skeletons and materials before meshes)
00337             typedef map<Real, LoadUnloadResourceList*>::type LoadResourceOrderMap;
00338             LoadResourceOrderMap loadResourceOrderMap;
00340             String worldGeometry;
00342             SceneManager* worldGeometrySceneManager;
00343             // in global pool flag - if true the resource will be loaded even a different   group was requested in the load method as a parameter.
00344             bool inGlobalPool;
00345 
00346             void addToIndex(const String& filename, Archive* arch);
00347             void removeFromIndex(const String& filename, Archive* arch);
00348             void removeFromIndex(Archive* arch);
00349 
00350         };
00352         typedef map<String, ResourceGroup*>::type ResourceGroupMap;
00353         ResourceGroupMap mResourceGroupMap;
00354 
00356         String mWorldGroupName;
00357 
00363         void parseResourceGroupScripts(ResourceGroup* grp);
00368         void createDeclaredResources(ResourceGroup* grp);
00370         void addCreatedResource(ResourcePtr& res, ResourceGroup& group);
00372         ResourceGroup* getResourceGroup(const String& name);
00374         void dropGroupContents(ResourceGroup* grp);
00376         void deleteGroup(ResourceGroup* grp);
00378         ResourceGroup* findGroupContainingResourceImpl(const String& filename);
00380         void fireResourceGroupScriptingStarted(const String& groupName, size_t scriptCount);
00382         void fireScriptStarted(const String& scriptName, bool &skipScript);
00384         void fireScriptEnded(const String& scriptName, bool skipped);
00386         void fireResourceGroupScriptingEnded(const String& groupName);
00388         void fireResourceGroupLoadStarted(const String& groupName, size_t resourceCount);
00390         void fireResourceLoadStarted(const ResourcePtr& resource);
00392         void fireResourceLoadEnded(void);
00394         void fireResourceGroupLoadEnded(const String& groupName);
00396         void fireResourceGroupPrepareStarted(const String& groupName, size_t resourceCount);
00398         void fireResourcePrepareStarted(const ResourcePtr& resource);
00400         void fireResourcePrepareEnded(void);
00402         void fireResourceGroupPrepareEnded(const String& groupName);
00403 
00405         ResourceGroup* mCurrentGroup;
00406     public:
00407         ResourceGroupManager();
00408         virtual ~ResourceGroupManager();
00409 
00447         void createResourceGroup(const String& name, const bool inGlobalPool = true);
00448 
00449 
00489         void initialiseResourceGroup(const String& name);
00490 
00494         void initialiseAllResourceGroups(void);
00495 
00513         void prepareResourceGroup(const String& name, bool prepareMainResources = true, 
00514             bool prepareWorldGeom = true);
00515 
00533         void loadResourceGroup(const String& name, bool loadMainResources = true, 
00534             bool loadWorldGeom = true);
00535 
00551         void unloadResourceGroup(const String& name, bool reloadableOnly = true);
00552 
00564         void unloadUnreferencedResourcesInGroup(const String& name, 
00565             bool reloadableOnly = true);
00566 
00576         void clearResourceGroup(const String& name);
00577         
00583         void destroyResourceGroup(const String& name);
00584 
00592         bool isResourceGroupInitialised(const String& name);
00593 
00601         bool isResourceGroupLoaded(const String& name);
00602 
00603         /*** Verify if a resource group exists
00604         @param name The name of the resource group to look for
00605         */
00606         bool resourceGroupExists(const String& name);
00607 
00629         void addResourceLocation(const String& name, const String& locType, 
00630             const String& resGroup = DEFAULT_RESOURCE_GROUP_NAME, bool recursive = false);
00632         void removeResourceLocation(const String& name, 
00633             const String& resGroup = DEFAULT_RESOURCE_GROUP_NAME);
00635         bool resourceLocationExists(const String& name, 
00636             const String& resGroup = DEFAULT_RESOURCE_GROUP_NAME);
00637 
00672         void declareResource(const String& name, const String& resourceType,
00673             const String& groupName = DEFAULT_RESOURCE_GROUP_NAME,
00674             const NameValuePairList& loadParameters = NameValuePairList());
00714         void declareResource(const String& name, const String& resourceType,
00715             const String& groupName, ManualResourceLoader* loader,
00716             const NameValuePairList& loadParameters = NameValuePairList());
00727         void undeclareResource(const String& name, const String& groupName);
00728 
00748         DataStreamPtr openResource(const String& resourceName, 
00749             const String& groupName = DEFAULT_RESOURCE_GROUP_NAME,
00750             bool searchGroupsIfNotFound = true, Resource* resourceBeingLoaded = 0);
00751 
00763         DataStreamListPtr openResources(const String& pattern, 
00764             const String& groupName = DEFAULT_RESOURCE_GROUP_NAME);
00765         
00774         StringVectorPtr listResourceNames(const String& groupName, bool dirs = false);
00775 
00782         FileInfoListPtr listResourceFileInfo(const String& groupName, bool dirs = false);
00783 
00795         StringVectorPtr findResourceNames(const String& groupName, const String& pattern,
00796             bool dirs = false);
00797 
00802         bool resourceExists(const String& group, const String& filename);
00803 
00808         bool resourceExists(ResourceGroup* group, const String& filename);
00809         
00813         bool resourceExistsInAnyGroup(const String& filename);
00814 
00821         const String& findGroupContainingResource(const String& filename);
00822 
00832         FileInfoListPtr findResourceFileInfo(const String& group, const String& pattern,
00833             bool dirs = false);
00834 
00836         time_t resourceModifiedTime(const String& group, const String& filename); 
00841         StringVectorPtr listResourceLocations(const String& groupName);
00842 
00849         StringVectorPtr findResourceLocation(const String& groupName, const String& pattern);
00850 
00852         time_t resourceModifiedTime(ResourceGroup* group, const String& filename); 
00853 
00868         DataStreamPtr createResource(const String& filename, const String& groupName = DEFAULT_RESOURCE_GROUP_NAME, 
00869             bool overwrite = false, const String& locationPattern = StringUtil::BLANK);
00870 
00880         void deleteResource(const String& filename, const String& groupName = DEFAULT_RESOURCE_GROUP_NAME, 
00881             const String& locationPattern = StringUtil::BLANK);
00882 
00892         void deleteMatchingResources(const String& filePattern, const String& groupName = DEFAULT_RESOURCE_GROUP_NAME, 
00893             const String& locationPattern = StringUtil::BLANK);
00894 
00898         void addResourceGroupListener(ResourceGroupListener* l);
00900         void removeResourceGroupListener(ResourceGroupListener* l);
00901 
00908         void setWorldResourceGroupName(const String& groupName) {mWorldGroupName = groupName;}
00909 
00911         const String& getWorldResourceGroupName(void) const { return mWorldGroupName; }
00912 
00926         void linkWorldGeometryToResourceGroup(const String& group, 
00927             const String& worldGeometry, SceneManager* sceneManager);
00928 
00933         void unlinkWorldGeometryFromResourceGroup(const String& group);
00934 
00942         bool isResourceGroupInGlobalPool(const String& name);
00943 
00945         void shutdownAll(void);
00946 
00947 
00957         void _registerResourceManager(const String& resourceType, ResourceManager* rm);
00958 
00965         void _unregisterResourceManager(const String& resourceType);
00966 
00969         ResourceManagerIterator getResourceManagerIterator()
00970         { return ResourceManagerIterator(
00971             mResourceManagerMap.begin(), mResourceManagerMap.end()); }
00972 
00977         void _registerScriptLoader(ScriptLoader* su);
00978 
00982         void _unregisterScriptLoader(ScriptLoader* su);
00983 
00987         ScriptLoader *_findScriptLoader(const String &pattern);
00988 
00992         ResourceManager* _getResourceManager(const String& resourceType);
00993 
00997         void _notifyResourceCreated(ResourcePtr& res);
00998 
01002         void _notifyResourceRemoved(ResourcePtr& res);
01003 
01006         void _notifyResourceGroupChanged(const String& oldGroup, Resource* res);
01007 
01012         void _notifyAllResourcesRemoved(ResourceManager* manager);
01013 
01021         void _notifyWorldGeometryStageStarted(const String& description);
01029         void _notifyWorldGeometryStageEnded(void);
01030 
01036         StringVector getResourceGroups(void);
01043         ResourceDeclarationList getResourceDeclarationList(const String& groupName);
01044 
01049         const LocationList& getResourceLocationList(const String& groupName);
01050 
01052         void setLoadingListener(ResourceLoadingListener *listener);
01054         ResourceLoadingListener *getLoadingListener();
01055 
01071         static ResourceGroupManager& getSingleton(void);
01087         static ResourceGroupManager* getSingletonPtr(void);
01088 
01089     };
01092 }
01093 
01094 #endif

Copyright © 2008 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Thu Dec 31 16:27:13 2009