1 /***************************************************************************
2                           datamanager.cpp  -  Managing loading/unloading data objects
3                              -------------------
4     begin                : wo dec 1 2004
5     copyright            : (C) 2004 by CJP
6     email                : cornware-cjp@users.sourceforge.net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #include <cstdio>
19 
20 #include "datamanager.h"
21 #include "dataobject.h"
22 
CDataManager()23 CDataManager::CDataManager()
24 {
25 	//initialise array
26 	for(unsigned int i=0; i < CDataObject::eEnumTop; i++)
27 		m_Objects.push_back(vector<CDataObject *>());
28 }
29 
~CDataManager()30 CDataManager::~CDataManager()
31 {
32 	unloadAll(CDataObject::eNone);
33 }
34 
getObject(CDataObject::eDataType type,unsigned int ID)35 CDataObject *CDataManager::getObject(CDataObject::eDataType type, unsigned int ID)
36 {
37 	if(ID >= (m_Objects[type]).size())
38 	{
39 		CString name;
40 		if(type == CDataObject::eNone)           name = "None";
41 		if(type == CDataObject::eTrack)          name = "Track";
42 		if(type == CDataObject::eCollisionModel) name = "CollisionModel";
43 		if(type == CDataObject::eBound)          name = "Bound";
44 		if(type == CDataObject::eTileModel)      name = "TileModel";
45 		if(type == CDataObject::eMaterial)       name = "Material";
46 		if(type == CDataObject::eMovingObject)   name = "MovingObject";
47 		if(type == CDataObject::eGraphObj)       name = "GraphObj";
48 		if(type == CDataObject::eTexture)        name = "Texture";
49 		if(type == CDataObject::eSample)         name = "Sample";
50 		printf("Errror in CDataManager::getObject (shared/datamanager.cpp):\n"
51 			"  Object requested with ID %d, but there are only %d"
52 			"  objects of type %d (%s)\n",
53 			ID, (m_Objects[type]).size(), type, name.c_str());
54 		return NULL;
55 	}
56 
57 	return m_Objects[type][ID];
58 }
getObject(CDataObject::eDataType type,unsigned int ID) const59 const CDataObject *CDataManager::getObject(CDataObject::eDataType type, unsigned int ID) const
60 {
61 	if(ID >= (m_Objects[type]).size())
62 	{
63 		CString name;
64 		if(type == CDataObject::eNone)           name = "None";
65 		if(type == CDataObject::eTrack)          name = "Track";
66 		if(type == CDataObject::eCollisionModel) name = "CollisionModel";
67 		if(type == CDataObject::eBound)          name = "Bound";
68 		if(type == CDataObject::eTileModel)      name = "TileModel";
69 		if(type == CDataObject::eMaterial)       name = "Material";
70 		if(type == CDataObject::eMovingObject)   name = "MovingObject";
71 		if(type == CDataObject::eGraphObj)       name = "GraphObj";
72 		if(type == CDataObject::eTexture)        name = "Texture";
73 		if(type == CDataObject::eSample)         name = "Sample";
74 		printf("Errror in CDataManager::getObject (shared/datamanager.cpp):\n"
75 			"  Object requested with ID %d, but there are only %d"
76 			"  objects of type %d (%s)\n",
77 			ID, (m_Objects[type]).size(), type, name.c_str());
78 		return NULL;
79 	}
80 
81 	return m_Objects[type][ID];
82 }
83 
loadObject(const CString & filename,const CParamList plist,CDataObject::eDataType type)84 int CDataManager::loadObject(const CString &filename, const CParamList plist, CDataObject::eDataType type)
85 {
86 	int ID = findObject(filename, plist, type);
87 	if(ID >= 0)
88 		return ID;
89 
90 	//else
91 
92 	CDataObject *obj = createObject(filename, plist, type);
93 	if(obj == NULL) return -1;
94 	if(!obj->load(filename, plist))
95 	{
96 		obj->unload();
97 		delete obj;
98 		return -1;
99 	}
100 
101 	m_Objects[type].push_back(obj);
102 
103 	return m_Objects[type].size() - 1;
104 }
105 
unloadAll(CDataObject::eDataType type)106 void CDataManager::unloadAll(CDataObject::eDataType type)
107 {
108 	if(type == CDataObject::eNone)
109 	{
110 		for(unsigned int t=0; t < CDataObject::eEnumTop; t++)
111 			while(m_Objects[t].size() > 0)
112 			{
113 				unloadObject(t, 0);
114 			}
115 
116 		return;
117 	}
118 
119 	//Type-specific unload
120 	while(m_Objects[type].size() > 0)
121 		unloadObject(type, 0);
122 }
123 
getObjectArray(CDataObject::eDataType type)124 vector<CDataObject *> CDataManager::getObjectArray(CDataObject::eDataType type)
125 {
126 	return m_Objects[type];
127 }
128 
getObjectArray(CDataObject::eDataType type) const129 vector<const CDataObject *> CDataManager::getObjectArray(CDataObject::eDataType type) const
130 {
131 	vector<const CDataObject *> ret;
132 	for(unsigned int i=0; i < m_Objects[type].size(); i++)
133 		ret.push_back(m_Objects[type][i]);
134 
135 	return ret;
136 }
137 
getSubset(CDataObject::eDataType type,const CString & subset)138 vector<CDataObject *> CDataManager::getSubset(CDataObject::eDataType type, const CString &subset)
139 {
140 	//printf("Indices: \"%s\"\n", subset.c_str());
141 	vector<CDataObject *> ret;
142 
143 	CString indices = subset;
144 	while(true)
145 	{
146 		int sp = indices.inStr(' ');
147 		if(sp > 0)
148 		{
149 			int n = indices.mid(0,sp).toInt();
150 			indices= indices.mid(sp+1, indices.length()-sp-1);
151 			//printf("    Adding %d\n", n);
152 			CDataObject *obj = getObject(type, n);
153 			ret.push_back(obj);
154 		}
155 		else
156 		{
157 			//printf("    Adding %d\n", indices.toInt());
158 			CDataObject *obj = getObject(type, indices.toInt()); //the last index
159 			ret.push_back(obj);
160 			break;
161 		}
162 	}
163 
164 	return ret;
165 }
166 
loadFilesFromString(CDataObject::eDataType type,const CString & files)167 CString CDataManager::loadFilesFromString(CDataObject::eDataType type, const CString &files)
168 {
169 	CString theString = files;
170 	theString.Trim();
171 
172 	//printf("theString = \"%s\"\n", theString.c_str());
173 
174 	CString ret;
175 
176 	while(theString != "")
177 	{
178 		CString thisFile;
179 		int semicol = theString.inStr(';');
180 		if(semicol < 0)
181 		{
182 			thisFile = theString;
183 			theString = "";
184 		}
185 		else
186 		{
187 			thisFile = theString.mid(0, semicol);
188 			theString   = theString.mid(semicol+1);
189 		}
190 
191 		thisFile.Trim();
192 
193 		//printf("thisFile = \"%s\"\n", thisFile.c_str());
194 
195 		CString filename;
196 		CParamList plist;
197 		int space = thisFile.inStr(' ');
198 		if(space < 0)
199 		{
200 			filename = thisFile;
201 		}
202 		else
203 		{
204 			filename = thisFile.mid(0, space);
205 			thisFile = thisFile.mid(space+1);
206 
207 			//Texture parameters
208 			plist = CParamList(thisFile);
209 		}
210 
211 		//printf("filename = \"%s\"\n", filename.c_str());
212 
213 		int ID = loadObject(filename, plist, type);
214 
215 		if(ret == "")
216 			{ret = CString(ID);}
217 		else
218 			{ret += CString(" ") + ID;}
219 	}
220 
221 	return ret;
222 }
223 
createObject(const CString & filename,const CParamList & plist,CDataObject::eDataType type)224 CDataObject *CDataManager::createObject(const CString &filename, const CParamList &plist, CDataObject::eDataType type)
225 {
226 	if(type == CDataObject::eNone)
227 		return new CDataObject(this, CDataObject::eNone);
228 
229 	return NULL; //to be overloaded
230 }
231 
findObject(const CString & filename,const CParamList plist,CDataObject::eDataType type)232 int CDataManager::findObject(const CString &filename, const CParamList plist, CDataObject::eDataType type)
233 {
234 	for(unsigned int i=0; i < m_Objects[type].size(); i++)
235 	{
236 		CDataObject *obj = m_Objects[type][i];
237 		if(obj->getFilename() == filename && obj->getParamList() == plist)
238 			return i;
239 	}
240 
241 	return -1;
242 }
243 
unloadObject(unsigned int type,unsigned int i)244 void CDataManager::unloadObject(unsigned int type, unsigned int i)
245 {
246 	CDataObject *obj = m_Objects[type][i];
247 	obj->unload();
248 	delete obj;
249 	m_Objects[type].erase(m_Objects[type].begin() + i);
250 }
251