Spinning Topp Logo BlackTopp Studios
inc
resourcemanager.cpp
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_cpp
41 #define _resourcemanager_cpp
42 
44 #include "Resource/resourceenumerations.h"
45 #include "Resource/filestream.h"
46 #include "Resource/memorystream.h"
47 #include "Resource/assetgroup.h"
48 
49 #include "Graphics/meshmanager.h"
50 
51 #include "stringtool.h"
52 
53 #include <iostream>
54 #include <fstream>
55 #include <cstdlib>
56 
57 #include <Ogre.h>
58 //#include <btBulletWorldImporter.h>
59 //#include <btBulletDynamicsCommon.h>
60 
61 namespace Mezzanine
62 {
63  template<> Resource::ResourceManager* Singleton<Resource::ResourceManager>::SingletonPtr = NULL;
64 
65  namespace Resource
66  {
67  const String ResourceManager::ImplementationName = "DefaultResourceManager";
68  const ManagerBase::ManagerType ResourceManager::InterfaceType = ManagerBase::MT_ResourceManager;
69 
70  ResourceManager::ResourceManager(const String& EngineDataPath, const Resource::ArchiveType ArchType)
71  {
72  this->OgreResource = Ogre::ResourceGroupManager::getSingletonPtr();
73  this->EngineDataDir = EngineDataPath;
74  this->AddAssetLocation(EngineDataPath, ArchType, "EngineData", false);
75  this->CreateAssetGroup("");
76  }
77 
79  {
80  this->OgreResource = Ogre::ResourceGroupManager::getSingletonPtr();
81  this->CreateAssetGroup("");
82  /// @todo This class currently doesn't initialize anything from XML, if that changes this constructor needs to be expanded.
83  }
84 
86  {
87  this->Deinitialize();
88  this->DestroyAllAssetGroups();
89  }
90 
91  ///////////////////////////////////////////////////////////////////////////////
92  // Stream Management
93 
95  { return this->GetAssetGroupExcept(GroupName)->OpenAssetStream(AssetName); }
96 
98  { return this->GetAssetGroupExcept("")->CreateDataStream(Buffer,BufferSize); }
99 
100  Resource::DataStreamPtr ResourceManager::CreateDataStream(const String& AssetName, void* Buffer, const UInt32 BufferSize)
101  { return this->GetAssetGroupExcept("")->CreateDataStream(AssetName,Buffer,BufferSize); }
102 
103  Resource::DataStreamPtr ResourceManager::CreateDataStream(const String& AssetName, const String& GroupName, void* Buffer, const UInt32 BufferSize)
104  { return this->GetAssetGroupExcept(GroupName)->CreateDataStream(AssetName,Buffer,BufferSize); }
105 
106  ///////////////////////////////////////////////////////////////////////////////
107  // AssetGroup Management
108 
109  void ResourceManager::AddAssetLocation(const String& Location, const ArchiveType Type, const String& Group, Boole Recursive)
110  {
111  this->GetOrCreateAssetGroup(Group);
112  this->OgreResource->addResourceLocation(Location, this->GetStringFromArchiveType(Type), Group, Recursive);
113  }
114 
116  {
117  AssetGroupIterator GroupIt = this->AssetGroups.find(GroupName);
118  if( GroupIt != this->AssetGroups.end() ) {
119  MEZZ_EXCEPTION(ExceptionBase::II_DUPLICATE_IDENTITY_EXCEPTION,"An AssetGroup with the name \"" + GroupName + "\" already exists.");
120  }
121 
122  AssetGroup* NewGroup = new AssetGroup(GroupName);
123  this->AssetGroups[GroupName] = NewGroup;
124  return NewGroup;
125  }
126 
128  {
129  AssetGroup* Ret = this->GetAssetGroup(GroupName);
130  if( Ret == NULL ) {
131  Ret = this->CreateAssetGroup(GroupName);
132  }
133  return Ret;
134  }
135 
137  {
138  AssetGroupIterator GroupIt = this->AssetGroups.find(GroupName);
139  if( GroupIt != this->AssetGroups.end() ) {
140  return (*GroupIt).second;
141  }
142  return NULL;
143  }
144 
146  {
147  AssetGroupIterator GroupIt = this->AssetGroups.find(GroupName);
148  if( GroupIt == this->AssetGroups.end() ) {
149  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_NOT_FOUND_EXCEPTION,"AssetGroup named \"" + GroupName + "\" not found.");
150  }
151  return (*GroupIt).second;
152  }
153 
155  {
156  AssetGroupIterator GroupIt = this->AssetGroups.find(GroupName);
157  if( GroupIt != this->AssetGroups.end() ) {
158  delete (*GroupIt).second;
159  this->AssetGroups.erase(GroupIt);
160  }
161  }
162 
164  {
165  this->DestroyAssetGroup( ToBeDestroyed->GetName() );
166  }
167 
169  {
170  for( AssetGroupIterator GroupIt = this->AssetGroups.begin() ; GroupIt != this->AssetGroups.end() ; ++GroupIt )
171  { delete (*GroupIt).second; }
172  this->AssetGroups.clear();
173  }
174 
176  {
177  AssetGroupIterator GroupIt = this->AssetGroups.find(GroupName);
178  if( GroupIt != this->AssetGroups.end() ) {
179  (*GroupIt).second->InitializeAssets();
180  }
181  }
182 
183  ///////////////////////////////////////////////////////////////////////////////
184  // Resource/Asset Query
185 
186  String ResourceManager::GetAssetPath(const String& FileName, const String& Group)
187  {
188  Ogre::FileInfoListPtr FileList = this->OgreResource->listResourceFileInfo(Group);
189  for( Whole X = 0 ; X < FileList->size() ; ++X )
190  {
191  if( FileName == FileList->at(X).filename ) {
192  //return FileList->at(X).path;
193  return FileList->at(X).archive->getName() + "/" + FileList->at(X).path;
194  }
195  }
196  return "";
197  }
198 
199  ///////////////////////////////////////////////////////////////////////////////
200  // Utility
201 
203  {
204  return EngineDataDir;
205  }
206 
208  {
209  #ifdef MEZZ_WINDOWS
210  return ".dll";
211  #elif MEZZ_LINUX
212  return ".so";
213  #elif MEZZ_MACOSX
214  return ".dylib";
215  #endif
216  }
217 
219  {
220  Initialized = true;
221  }
222 
224  {
225  Initialized = false;
226  }
227 
229  {
230  switch(ArchType)
231  {
233  return String("FileSystem");
234  case Resource::AT_Zip:
235  return String("Zip");
236  default:
237  MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION, "Invalid archive type passed to ResourceManager::GetStringFromArchiveType.");
238  return String("");
239  }
240  }
241 
243  {
244  if(String("FileSystem")==FromString)
245  { return Resource::AT_FileSystem; }
246  if(String("Zip")==FromString)
247  { return Resource::AT_Zip; }
248  MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION, "Invalid archive type passed to ResourceManager::GetArchiveTypeFromString.");
249  return AT_Invalid;
250  }
251 
252  ///////////////////////////////////////////////////////////////////////////////
253  // Type Identifier Methods
254 
257 
260 
261  ///////////////////////////////////////////////////////////////////////////////
262  // DefaultResourceManagerFactory Methods
263 
265  { }
266 
268  { }
269 
272 
275 
277  {
279  /// @todo Add something to log a warning that the manager exists and was requested to be constructed when we have a logging manager set up.
281  }
282  if( Params.empty() ) {
283  return new ResourceManager();
284  }
285 
286  String EngineDataPath = ".";
288  for( NameValuePairList::const_iterator ParIt = Params.begin() ; ParIt != Params.end() ; ++ParIt )
289  {
290  String Lower = (*ParIt).first;
292  if( "enginedatapath" == Lower ) {
293  EngineDataPath = (*ParIt).second;
294  }else if( "archivetype" == Lower ) {
295  ArchType = ResourceManager::GetArchiveTypeFromString((*ParIt).second);
296  }
297  }
298  return new ResourceManager(EngineDataPath,ArchType);
299  }
300 
302  {
304  /// @todo Add something to log a warning that the manager exists and was requested to be constructed when we have a logging manager set up.
306  }
307  return new ResourceManager(XMLNode);
308  }
309 
311  { delete ToBeDestroyed; }
312  }//Resource
313 }//Mezzanine
314 
315 #endif
virtual String GetImplementationTypeName() const
This Allows any manager name to be sent to a stream. Primarily used for logging.
virtual ~DefaultResourceManagerFactory()
Class destructor.
Thrown when duplicates of teh same identity string exist.
Definition: exception.h:95
AssetGroup * GetOrCreateAssetGroup(const String &GroupName)
Gets an AssetGroup by name. If it does not exist it will be created.
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
Resource::DataStreamPtr OpenAssetStream(const String &AssetName, const String &GroupName)
Opens a stream to an asset in an AssetGroup.
Ogre::ResourceGroupManager * OgreResource
Encapsulates the functionality of the ogre resource group manager.
AssetGroup * CreateAssetGroup(const String &GroupName)
Creates a new asset group.
String EngineDataDir
The location of engine data.
ManagerType
A listing of Manager Types.
Definition: managerbase.h:65
ManagerBase::ManagerType GetManagerType() const
Gets the type of manager that is created by this factory.
Thrown when the requested identity could not be found.
Definition: exception.h:94
void InitAssetGroup(const String &GroupName)
Makes a asset group ready to use.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
String GetManagerImplName() const
Gets the name of the manager that is created by this factory.
A simple reference counting pointer.
Definition: countedptr.h:70
Declaration of FileStream.
virtual void Deinitialize()
Removes this manager from any necessary configuration so it can be safely disposed of...
String GetAssetPath(const String &FileName, const String &Group)
Gets the actual path to an asset.
ArchiveType
Used to indicate what kind of resources the Entrosol should look for.
const String & GetName() const
Gets the name of this asset group.
Definition: assetgroup.cpp:67
static Resource::ArchiveType GetArchiveTypeFromString(const String &FromString)
Gets an ArchiveType from a string.
void ToLowerCase(String &Source)
Converts all upper case characters in a string to their respective lower case.
Definition: stringtool.cpp:193
static Boole SingletonValid()
Checks to see if the singleton pointer is valid.
Definition: singleton.h:110
static ResourceManager * GetSingletonPtr()
Fetches a pointer to the singleton.
Definition: singleton.h:97
This is the base class for all managers that do no describe properties of a single world...
Resource::DataStreamPtr CreateDataStream(void *Buffer, const UInt32 BufferSize)
Creates a stream from a memory buffer.
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
String GetEngineDataDirectory() const
Get the pathname where engine data is stored.
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
The defintion of the Resource Manager.
Resource::DataStreamPtr CreateDataStream(void *Buffer, const UInt32 BufferSize)
Creates a stream from a memory buffer.
Definition: assetgroup.cpp:97
void AddAssetLocation(const String &Location, const ArchiveType Type, const String &Group, const Boole Recursive=false)
Adds a location for graphical resources.
void DestroyManager(EntresolManager *ToBeDestroyed)
Destroys a Manager created by this factory.
Declaration of MemoryStream.
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
static String GetStringFromArchiveType(const Resource::ArchiveType ArchType)
Gets a string that describes an ArchiveType.
Look for stuff in zip files even if the extension is not '.zip'.
Thrown when parameters are checked at runtime and found invalid.
Definition: exception.h:108
virtual void Initialize()
Configures this manager for use prior to entering the main loop.
EntresolManager * CreateManager(const NameValuePairList &Params)
Creates a manager of the type represented by this factory.
String GetPluginExtension() const
Gets the dot-and-extention of this platforms plugins.
AssetGroupContainer AssetGroups
Container storing all of the asset groups created and managed by this manager.
void DestroyAllAssetGroups()
Destroys all asset groups being stored by this manager.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
ResourceManager(const String &EngineDataPath=".", const ArchiveType ArchType=AT_FileSystem)
Class constructor.
AssetGroup * GetAssetGroup(const String &GroupName)
Gets an AssetGroup by name.
AssetGroup * GetAssetGroupExcept(const String &GroupName)
Gets an AssetGroup by name. the requested AssetGroup is not found this will throw an exception...
static const ManagerBase::ManagerType InterfaceType
A ManagerType enum value used to describe the type of interface/functionality this manager provides...
void DestroyAssetGroup(const String &GroupName)
Destroys an asset group, unloading all of it's resources.
Indicates this valid was messed up unrecoverably, most likely by a bug.
Resource::DataStreamPtr OpenAssetStream(const String &AssetName)
Opens a stream to an asset in an AssetGroup.
Definition: assetgroup.cpp:80
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
virtual ManagerType GetInterfaceType() const
This returns the type of this manager.
Boole Initialized
Simple Boole indicating whether or not this manager has been initialized.
Definition: managerbase.h:111