1 /*!
2 	@file
3 	@author		Albert Semenov
4 	@date		05/2008
5 */
6 
7 #include "MyGUI_OgreDataManager.h"
8 #include "MyGUI_OgreDiagnostic.h"
9 #include "MyGUI_OgreDataStream.h"
10 
11 #include <Ogre.h>
12 
13 #include "MyGUI_LastHeader.h"
14 
15 namespace MyGUI
16 {
17 
OgreDataManager()18 	OgreDataManager::OgreDataManager() :
19 		mAllGroups(false),
20 		mIsInitialise(false)
21 	{
22 	}
23 
initialise(const std::string & _group)24 	void OgreDataManager::initialise(const std::string& _group)
25 	{
26 		MYGUI_PLATFORM_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
27 		MYGUI_PLATFORM_LOG(Info, "* Initialise: " << getClassTypeName());
28 
29 		mGroup = _group;
30 		if (mGroup == Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME)
31 			mAllGroups = true;
32 		else
33 			mAllGroups = false;
34 
35 		MYGUI_PLATFORM_LOG(Info, getClassTypeName() << " successfully initialized");
36 		mIsInitialise = true;
37 	}
38 
shutdown()39 	void OgreDataManager::shutdown()
40 	{
41 		MYGUI_PLATFORM_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
42 		MYGUI_PLATFORM_LOG(Info, "* Shutdown: " << getClassTypeName());
43 
44 		MYGUI_PLATFORM_LOG(Info, getClassTypeName() << " successfully shutdown");
45 		mIsInitialise = false;
46 	}
47 
getData(const std::string & _name)48 	IDataStream* OgreDataManager::getData(const std::string& _name)
49 	{
50 		try
51 		{
52 			Ogre::DataStreamPtr stream = Ogre::ResourceGroupManager::getSingleton().openResource(_name, mGroup, true);
53 			OgreDataStream* data = new OgreDataStream(stream);
54 
55 			return data;
56 		}
57 		catch (const Ogre::FileNotFoundException& _e)
58 		{
59 			MYGUI_PLATFORM_LOG(Warning, _e.getDescription());
60 		}
61 
62 		return nullptr;
63 	}
64 
freeData(IDataStream * _data)65 	void OgreDataManager::freeData(IDataStream* _data)
66 	{
67 		delete _data;
68 	}
69 
isDataExist(const std::string & _name)70 	bool OgreDataManager::isDataExist(const std::string& _name)
71 	{
72 		if (mAllGroups)
73 			return Ogre::ResourceGroupManager::getSingleton().resourceExistsInAnyGroup(_name);
74 		else
75 			return Ogre::ResourceGroupManager::getSingleton().resourceExists(mGroup, _name);
76 	}
77 
getDataListNames(const std::string & _pattern)78 	const VectorString& OgreDataManager::getDataListNames(const std::string& _pattern)
79 	{
80 		return getDataListNames(_pattern, false);
81 	}
82 
getDataListNames(const std::string & _pattern,bool _fullpath)83 	const VectorString& OgreDataManager::getDataListNames(const std::string& _pattern, bool _fullpath)
84 	{
85 		static VectorString result;
86 		result.clear();
87 
88 		VectorString search;
89 		if (mAllGroups)
90 		{
91 			Ogre::StringVector sp = Ogre::ResourceGroupManager::getSingleton().getResourceGroups();
92 			search.reserve(sp.size());
93 			for (size_t i = 0; i < sp.size(); i++)
94 				search.push_back(sp[i]);
95 		}
96 		else
97 			search.push_back(mGroup);
98 
99 		std::vector<Ogre::FileInfoListPtr> pFileInfos;
100 
101 		int resultSize = 0;
102 		for (size_t i = 0; i < search.size(); i++)
103 		{
104 			Ogre::FileInfoListPtr pFileInfo = Ogre::ResourceGroupManager::getSingleton().findResourceFileInfo(search[i], _pattern);
105 			resultSize += pFileInfo->size();
106 			if (!pFileInfo->empty())
107 				pFileInfos.push_back(pFileInfo);
108 			else
109 				pFileInfo.setNull();
110 		}
111 
112 		result.reserve(resultSize);
113 
114 		for (size_t i = 0; i < pFileInfos.size(); i++)
115 		{
116 			Ogre::FileInfoListPtr pFileInfo = pFileInfos[i];
117 			for (Ogre::FileInfoList::iterator fi = pFileInfo->begin(); fi != pFileInfo->end(); ++fi )
118 			{
119 				if (fi->path.empty())
120 				{
121 					bool found = false;
122 					for (VectorString::iterator iter = result.begin(); iter != result.end(); ++iter)
123 					{
124 						if (*iter == fi->filename)
125 						{
126 							found = true;
127 							break;
128 						}
129 					}
130 					if (!found)
131 					{
132 						result.push_back(_fullpath ? fi->archive->getName() + "/" + fi->filename : fi->filename);
133 					}
134 				}
135 			}
136 
137 			pFileInfo.setNull();
138 		}
139 
140 		return result;
141 	}
142 
getDataPath(const std::string & _name)143 	const std::string& OgreDataManager::getDataPath(const std::string& _name)
144 	{
145 		static std::string result;
146 		result.clear();
147 
148 		const VectorString& files = getDataListNames(_name, true);
149 		if (!files.empty())
150 		{
151 			result = files[0];
152 			if (files.size() > 1)
153 			{
154 				MYGUI_PLATFORM_LOG(Warning, "There are several files with name '" << _name << "'. '" << result << "' was used.");
155 				MYGUI_PLATFORM_LOG(Warning, "Other candidates are:");
156 				for (size_t index = 1; index < files.size(); index ++)
157 					MYGUI_PLATFORM_LOG(Warning, " - '" << files[index] << "'");
158 			}
159 		}
160 
161 		return result;
162 	}
163 
164 } // namespace MyGUI
165