Spinning Topp Logo BlackTopp Studios
inc
resourcemanager.h
Go to the documentation of this file.
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _resourcemanager_h
41 #define _resourcemanager_h
42 
43 #include "datatypes.h"
44 #include "entresolmanager.h"
45 #include "entresolmanagerfactory.h"
46 #include "singleton.h"
47 #include "Resource/resourceenumerations.h"
48 #include "Resource/datastream.h"
49 
50 /// @file
51 /// @brief The defintion of the Resource Manager.
52 
53 namespace Ogre
54 {
55  class ResourceGroupManager;
56 }
57 
58 namespace Mezzanine
59 {
60  namespace Resource
61  {
62  class AssetGroup;
63 
64  // Used by the scripting language binder to help create bindgings for this class. SWIG does know to creation template instances
65  #ifdef SWIG
66  %template(SingletonResourceManager) Singleton<ResourceManager>;
67  #endif
68 
69  ///////////////////////////////////////////////////////////////////////////////
70  /// @class ResourceManager
71  /// @headerfile resourcemanager.h
72  /// @brief This is the manager responsible for the loading and unloading of files.
73  /// @details This class is responsible for the reading and writing of files of all kinds, be
74  /// it graphical meshes, physics data, or XMl files.
75  ///////////////////////////////////////
77  {
78  public:
79  /// @brief Basic container type for AssetGroup storage in this class.
80  typedef std::map<String,AssetGroup*> AssetGroupContainer;
81  /// @brief Iterator type for AssetGroup instances stored in this class.
82  typedef AssetGroupContainer::iterator AssetGroupIterator;
83  /// @brief Const Iterator type for AssetGroup instances stored in this class.
84  typedef AssetGroupContainer::const_iterator ConstAssetGroupIterator;
85 
86  /// @brief Basic container type for @ref DataStream storage by this class.
87  typedef std::vector<Resource::DataStreamPtr> DataStreamContainer;
88  /// @brief Iterator type for @ref DataStream instances stored by this class.
89  typedef DataStreamContainer::iterator DataStreamIterator;
90  /// @brief Const Iterator type for @ref DataStream instances stored by this class.
91  typedef DataStreamContainer::const_iterator ConstDataStreamIterator;
92  /// @brief Basic container type for named @ref DataStream storage by this class.
93  typedef std::map<String,Resource::DataStreamPtr> NamedDataStreamContainer;
94  /// @brief Iterator type for named @ref DataStream instances stored by this class.
95  typedef NamedDataStreamContainer::iterator NamedDataStreamIterator;
96  /// @brief Const Iterator type for named @ref DataStream instances stored by this class.
97  typedef NamedDataStreamContainer::const_iterator ConstNamedDataStreamIterator;
98 
99  /// @brief A String containing the name of this manager implementation.
101  /// @brief A ManagerType enum value used to describe the type of interface/functionality this manager provides.
103  protected:
104  /// @internal
105  /// @brief Container storing all of the asset groups created and managed by this manager.
106  AssetGroupContainer AssetGroups;
107  /// @internal
108  /// @brief The location of engine data.
110  /// @internal
111  /// @brief Encapsulates the functionality of the ogre resource group manager.
112  Ogre::ResourceGroupManager* OgreResource;
113  public:
114  /// @brief Class constructor.
115  /// @details Standard manager constructor.
116  /// @param EngineDataPath The directory for engine specific data.
117  /// @param ArchiveType The name of the type of archive at this path.
118  ResourceManager(const String& EngineDataPath = ".", const ArchiveType ArchType = AT_FileSystem);
119  /// @brief XML constructor.
120  /// @param XMLNode The node of the xml document to construct from.
121  ResourceManager(const XML::Node& XMLNode);
122  /// @details Class Destructor.
123  virtual ~ResourceManager();
124 
125  ///////////////////////////////////////////////////////////////////////////////
126  // Stream Management
127 
128  /// @brief Opens a stream to an asset in an AssetGroup.
129  /// @param AssetName The identity of the asset to be opened (commonly a file name).
130  /// @param GroupName The name of the AssetGroup where the Asset can be found.
131  Resource::DataStreamPtr OpenAssetStream(const String& AssetName, const String& GroupName);
132 
133  /// @brief Creates a stream from a memory buffer.
134  /// @note The created stream will take ownership of the buffer you provide. If you want it to have a separate buffer then create a copy and pass that in.
135  /// @param Buffer A pointer to the memory to stream from.
136  /// @param BufferSize The size of the provided buffer in bytes.
137  /// @return Returns a @ref CountedPtr to the stream to the provided buffer.
138  Resource::DataStreamPtr CreateDataStream(void* Buffer, const UInt32 BufferSize);
139  /// @brief Creates a named stream from a memory buffer.
140  /// @note The created stream will take ownership of the buffer you provide. If you want it to have a separate buffer then create a copy and pass that in.
141  /// @param AssetName The name to be given to the created stream.
142  /// @param Buffer A pointer to the memory to stream from.
143  /// @param BufferSize The size of the provided buffer in bytes.
144  /// @return Returns a @ref CountedPtr to the stream to the provided buffer.
145  Resource::DataStreamPtr CreateDataStream(const String& AssetName, void* Buffer, const UInt32 BufferSize);
146  /// @brief Creates a named stream from a memory buffer and adds it to the named AssetGroup.
147  /// @note The created stream will take ownership of the buffer you provide. If you want it to have a separate buffer then create a copy and pass that in.
148  /// @param AssetName The name to be given to the created stream.
149  /// @param GroupName The name of the AssetGroup this stream will be added to.
150  /// @param Buffer A pointer to the memory to stream from.
151  /// @param BufferSize The size of the provided buffer in bytes.
152  /// @return Returns a @ref CountedPtr to the stream to the provided buffer.
153  Resource::DataStreamPtr CreateDataStream(const String& AssetName, const String& GroupName, void* Buffer, const UInt32 BufferSize);
154 
155  ///////////////////////////////////////////////////////////////////////////////
156  // AssetGroup Management
157 
158  /// @brief Adds a location for graphical resources.
159  /// @details This function will add a location on the disk to find files needed to create and
160  /// manipulate graphical objects. Once an asset is added it must be initalized using
161  /// ResourceManager::InitResourceGroup(String Group).
162  /// @param Location The location on the file system the asset can be found.
163  /// @param Type The kind of file system the location can be found in. @n
164  /// Options are: filesystem, zip.
165  /// @param Group The name of the group the resources at this location belong to. If the group does not exist it will be created.
166  /// @param Recursive Whether or not to search sub-directories.
167  void AddAssetLocation(const String& Location, const ArchiveType Type, const String& Group, const Boole Recursive = false);
168 
169  /// @brief Creates a new asset group.
170  /// @param GroupName The name to be given to the created asset group.
171  /// @return Returns a pointer to the created AssetGroup.
172  AssetGroup* CreateAssetGroup(const String& GroupName);
173  /// @brief Gets an AssetGroup by name. If it does not exist it will be created.
174  /// @param GroupName The name of the group to create or retrieve.
175  /// @return Returns a pointer to the requested AssetGroup.
176  AssetGroup* GetOrCreateAssetGroup(const String& GroupName);
177  /// @brief Gets an AssetGroup by name.
178  /// @param GroupName The name of the AssetGroup to retrieve.
179  /// @return Returns a pointer to the specified group, or NULL if it does not exist.
180  AssetGroup* GetAssetGroup(const String& GroupName);
181  /// @brief Gets an AssetGroup by name.
182  /// @exception If the requested AssetGroup is not found this will throw an exception.
183  /// @param GroupName The name of the AssetGroup to retrieve.
184  /// @return Returns a pointer to the specified group.
185  AssetGroup* GetAssetGroupExcept(const String& GroupName);
186  /// @brief Destroys an asset group, unloading all of it's resources.
187  /// @param GroupName The name of the asset group to destroy.
188  void DestroyAssetGroup(const String& GroupName);
189  /// @brief Destroys an asset group, unloading all of it's resources.
190  /// @param ToBeDestroyed A pointer to the asset group to be destroyed.
191  void DestroyAssetGroup(AssetGroup* ToBeDestroyed);
192  /// @brief Destroys all asset groups being stored by this manager.
193  void DestroyAllAssetGroups();
194 
195  /// @brief Makes a asset group ready to use.
196  /// @details After adding all of your assets and declaring them as nessessary, this function
197  /// is the final step. After calling this function any and all assets within the defined group
198  /// will be ready to use. Do not initialize any more groups then you need to however, as that will
199  /// take up memory and drop performance.
200  /// @param GroupName Name of the asset group.
201  void InitAssetGroup(const String& GroupName);
202 
203  ///////////////////////////////////////////////////////////////////////////////
204  // Asset Query
205 
206  /// @brief Gets the actual path to an asset.
207  /// @note This function currently only returns the first match, and doesn't check for multiple matches.
208  /// @param FileName The name of the file to search for.
209  /// @param Group The asset group to search in for the file.
210  /// @return Returns a string containing the path to the file.
211  String GetAssetPath(const String& FileName, const String& Group);
212 
213  ///////////////////////////////////////////////////////////////////////////////
214  // Utility
215 
216  /// @brief Get the pathname where engine data is stored.
217  /// @return A String that contains the path to where the engine data is stored.
218  String GetEngineDataDirectory() const;
219 
220  /// @brief Gets the dot-and-extention of this platforms plugins.
221  /// @return Returns the platform appropriate extention for plugin files.
222  String GetPluginExtension() const;
223 
224  /// @copydoc ManagerBase::Initialize()
225  virtual void Initialize();
226  /// @copydoc ManagerBase::Deinitialize()
227  virtual void Deinitialize();
228 
229  /// @brief Gets a string that describes an @ref ArchiveType.
230  /// @param ArchType A @ref ArchiveType That you want to log or pass to Ogre, or just need a @ref String that represents it.
231  /// @return A String that represents the @ref ArchiveType passed.
232  static String GetStringFromArchiveType(const Resource::ArchiveType ArchType);
233  /// @brief Gets an @ref ArchiveType from a string.
234  /// @param FromString The string to be converted to an archive type.
235  /// @return Returns a @ref ArchiveType corresponding to the string provided, or AT_Invalid if it is invalid.
236  static Resource::ArchiveType GetArchiveTypeFromString(const String& FromString);
237 
238  ///////////////////////////////////////////////////////////////////////////////
239  // Type Identifier Methods
240 
241  /// @copydoc ManagerBase::GetInterfaceType()
242  virtual ManagerType GetInterfaceType() const;
243  /// @copydoc ManagerBase::GetImplementationTypeName()
244  virtual String GetImplementationTypeName() const;
245  };//ResourceManager
246 
247  ///////////////////////////////////////////////////////////////////////////////
248  /// @class DefaultResourceManagerFactory
249  /// @headerfile resourcemanager.h
250  /// @brief A factory responsible for the creation and destruction of the default resourcemanager.
251  ///////////////////////////////////////
253  {
254  public:
255  /// @brief Class constructor.
257  /// @brief Class destructor.
259 
260  /// @copydoc ManagerFactory::GetManagerImplName()
261  String GetManagerImplName() const;
262  /// @copydoc ManagerFactory::GetManagerType() const
263  ManagerBase::ManagerType GetManagerType() const;
264 
265  /// @copydoc EntresolManagerFactory::CreateManager(const NameValuePairList&)
266  EntresolManager* CreateManager(const NameValuePairList& Params);
267  /// @copydoc EntresolManagerFactory::CreateManager(const XML::Node&)
268  EntresolManager* CreateManager(const XML::Node& XMLNode);
269  /// @copydoc EntresolManagerFactory::DestroyManager(EntresolManager*)
270  void DestroyManager(EntresolManager* ToBeDestroyed);
271  };//DefaultResourceManagerFactory
272  }//Resource
273 }//Mezzanine
274 
275 #endif
A factory responsible for the creation and destruction of the default resourcemanager.
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
Ogre::ResourceGroupManager * OgreResource
Encapsulates the functionality of the ogre resource group manager.
String EngineDataDir
The location of engine data.
ManagerType
A listing of Manager Types.
Definition: managerbase.h:65
This is a base class for factories that construct managers used by the Entresol class.
All the definitions for datatypes as well as some basic conversion functions are defined here...
A simple reference counting pointer.
Definition: countedptr.h:70
ArchiveType
Used to indicate what kind of resources the Entrosol should look for.
DataStreamContainer::iterator DataStreamIterator
Iterator type for DataStream instances stored by this class.
NamedDataStreamContainer::iterator NamedDataStreamIterator
Iterator type for named DataStream instances stored by this class.
DataStreamContainer::const_iterator ConstDataStreamIterator
Const Iterator type for DataStream instances stored by this class.
AssetGroupContainer::const_iterator ConstAssetGroupIterator
Const Iterator type for AssetGroup instances stored in this class.
This is the base class for all managers that do no describe properties of a single world...
AssetGroupContainer::iterator AssetGroupIterator
Iterator type for AssetGroup instances stored in this class.
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
NamedDataStreamContainer::const_iterator ConstNamedDataStreamIterator
Const Iterator type for named DataStream instances stored by this class.
static const String ImplementationName
A String containing the name of this manager implementation.
This is the manager responsible for the loading and unloading of files.
This is a class that stores a specific grouping of Assets, usually based on thier location...
Definition: assetgroup.h:53
std::map< String, AssetGroup * > AssetGroupContainer
Basic container type for AssetGroup storage in this class.
std::list< NameValuePair > NameValuePairList
This is a datatype mostly used for describing settings or parameters that can't be declared in advanc...
Definition: datatypes.h:206
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
AssetGroupContainer AssetGroups
Container storing all of the asset groups created and managed by this manager.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
static const ManagerBase::ManagerType InterfaceType
A ManagerType enum value used to describe the type of interface/functionality this manager provides...
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
std::vector< Resource::DataStreamPtr > DataStreamContainer
Basic container type for DataStream storage by this class.
std::map< String, Resource::DataStreamPtr > NamedDataStreamContainer
Basic container type for named DataStream storage by this class.
Declaration of DataStream.