00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "EntityProcessor.h"
00018 #include "../core/GameManager.h"
00019 #include <algorithm>
00020
00021
00022
00023
00024 EntityProcessor::EntityProcessor(AssetGroup *parent)
00025 : AssetProcessor(parent, "entities"), mIsReloading(false)
00026 {
00027 }
00028
00029
00030 EntityProcessor::~EntityProcessor()
00031 {
00032 removeAssets();
00033 }
00034
00035
00036 Entity* EntityProcessor::getEntity(const std::string &fileName)
00037 {
00038
00039 #ifdef WIN32
00040 std::string tmp = fileName;
00041 std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
00042 std::map<std::string, Entity*>::iterator itEntities = mEntities.find(tmp);
00043 #else
00044 std::map<std::string, Entity*>::iterator itEntities = mEntities.find(fileName);
00045 #endif
00046
00047 if(itEntities != mEntities.end())
00048 return itEntities->second;
00049
00050
00051 #ifdef WIN32
00052 std::map<std::string, std::pair<c8*, long> >::iterator itFiles = mEntityFiles.find(tmp);
00053 #else
00054 std::map<std::string, std::pair<c8*, long> >::iterator itFiles = mEntityFiles.find(fileName);
00055 #endif
00056
00057 if(itFiles != mEntityFiles.end())
00058 {
00059
00060 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00061
00062
00063 long fSize = (itFiles->second).second;
00064 c8 *fBuffer = (itFiles->second).first;
00065
00066 IReadFile *file = fileSystem->createMemoryReadFile(fBuffer, fSize, fileName.c_str());
00067 IXMLReader *xml = fileSystem->createXMLReader(file);
00068
00069
00070 Entity *entity = GameManager::Instance()->getEntityManager()
00071 ->createEntityFromXML(xml, NULL, pParent, false);
00072
00073 mEntities[fileName] = entity;
00074 entity->grab();
00075
00076
00077 xml->drop();
00078 file->drop();
00079
00080
00081 return entity;
00082 }
00083
00084
00085 return NULL;
00086 }
00087
00088
00089 void EntityProcessor::getEntityTree(Entity *root)
00090 {
00091
00092 std::map<std::string, std::pair<c8*, long> >::iterator itFiles;
00093
00094 for(itFiles = mEntityFiles.begin(); itFiles != mEntityFiles.end(); itFiles++)
00095 {
00096
00097 Entity *entity = NULL;
00098
00099
00100 std::map<std::string, Entity*>::iterator itEntities = mEntities.find(itFiles->first);
00101
00102 if(itEntities != mEntities.end())
00103 entity = itEntities->second;
00104
00105
00106 else
00107 {
00108
00109 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00110
00111
00112 long fSize = (itFiles->second).second;
00113 c8 *fBuffer = (itFiles->second).first;
00114
00115 IReadFile *file = fileSystem->createMemoryReadFile(fBuffer, fSize, (itFiles->first).c_str());
00116 IXMLReader *xml = fileSystem->createXMLReader(file);
00117
00118
00119 Entity *entity = GameManager::Instance()->getEntityManager()
00120 ->createEntityFromXML(xml, NULL, pParent, false);
00121
00122 mEntities[itFiles->first] = entity;
00123 entity->grab();
00124
00125
00126 xml->drop();
00127 file->drop();
00128 }
00129
00130
00131 if(entity != NULL)
00132 {
00133 if(entity->getParent() == NULL)
00134 entity->setParent(root);
00135 }
00136 }
00137 }
00138
00139
00140 bool EntityProcessor::loadAsset(const std::string &fileName)
00141 {
00142
00143 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00144
00145
00146 std::string workingDir = (fileSystem->getWorkingDirectory()).c_str();
00147
00148 if(!fileSystem->changeWorkingDirectoryTo( (pParent->getBaseDir()).c_str() ) )
00149 return false;
00150
00151 if(!fileSystem->changeWorkingDirectoryTo("./entities/"))
00152 return false;
00153
00154
00155 IReadFile *file = fileSystem->createAndOpenFile(fileName.c_str());
00156
00157 if(file)
00158 {
00159
00160 long fSize = file->getSize();
00161 c8 *fBuffer = new c8[fSize+1];
00162 memset(fBuffer, 0, fSize+1);
00163
00164 file->read(fBuffer, fSize);
00165
00166
00167 mEntityFiles[fileName] = std::pair<c8*, long>(fBuffer, fSize);
00168 cout << "Loaded entity: " << fileName << "\n";
00169
00170
00171 if(mIsReloading)
00172 {
00173 #ifdef WIN32
00174 std::string tmp = fileName;
00175 std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
00176 std::map<std::string, Entity*>::iterator it = mEntities.find(tmp);
00177 #else
00178 std::map<std::string, Entity*>::iterator it = mEntities.find(fileName);
00179 #endif
00180
00181 if(it != mEntities.end())
00182 {
00183 file->seek(0);
00184 IXMLReader *xml = fileSystem->createXMLReader(file);
00185
00186 Entity *entity = it->second;
00187
00188 entity->removeComponents();
00189
00190 if(entity->getParent() != NULL)
00191 {
00192 entity->setAbsolutePosition(entity->getParent()->getAbsolutePosition());
00193 entity->setAbsoluteRotation(entity->getParent()->getAbsoluteRotation());
00194 }
00195
00196 entity->loadXML(xml);
00197
00198 xml->drop();
00199 }
00200 }
00201
00202
00203 file->drop();
00204 }
00205
00206
00207 fileSystem->changeWorkingDirectoryTo(workingDir.c_str());
00208
00209 return (file) ? true : false;
00210 }
00211
00212
00213 void EntityProcessor::loadAssets()
00214 {
00215
00216 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00217
00218
00219 std::string workingDir = (fileSystem->getWorkingDirectory()).c_str();
00220
00221 if(!fileSystem->changeWorkingDirectoryTo( (pParent->getBaseDir()).c_str() ) )
00222 return;
00223
00224 if(!fileSystem->changeWorkingDirectoryTo("./entities/"))
00225 return;
00226
00227
00228 IFileList *fileList = fileSystem->createFileList();
00229
00230 for(u32 i = 0; i < fileList->getFileCount(); i++)
00231 {
00232
00233 std::string fileName = (fileList->getFileName(i)).c_str();
00234
00235 if(fileName == "..")
00236 continue;
00237
00238
00239 IReadFile *file = fileSystem->createAndOpenFile(fileName.c_str());
00240
00241 if(file)
00242 {
00243
00244 long fSize = file->getSize();
00245 c8 *fBuffer = new c8[fSize+1];
00246 memset(fBuffer, 0, fSize+1);
00247
00248 file->read(fBuffer, fSize);
00249
00250
00251 mEntityFiles[fileName] = std::pair<c8*, long>(fBuffer, fSize);
00252 cout << "Loaded entity: " << fileName << "\n";
00253
00254
00255 if(mIsReloading)
00256 {
00257 #ifdef WIN32
00258 std::string tmp = fileName;
00259 std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
00260 std::map<std::string, Entity*>::iterator it = mEntities.find(tmp);
00261 #else
00262 std::map<std::string, Entity*>::iterator it = mEntities.find(fileName);
00263 #endif
00264
00265 if(it != mEntities.end())
00266 {
00267 file->seek(0);
00268 IXMLReader *xml = fileSystem->createXMLReader(file);
00269
00270 Entity *entity = it->second;
00271
00272 entity->removeComponents();
00273
00274 if(entity->getParent() != NULL)
00275 {
00276 entity->setAbsolutePosition(entity->getParent()->getAbsolutePosition());
00277 entity->setAbsoluteRotation(entity->getParent()->getAbsoluteRotation());
00278 }
00279
00280 entity->loadXML(xml);
00281
00282 xml->drop();
00283 }
00284 }
00285
00286
00287 file->drop();
00288 }
00289 }
00290
00291
00292 fileList->drop();
00293
00294 fileSystem->changeWorkingDirectoryTo(workingDir.c_str());
00295 }
00296
00297
00298 bool EntityProcessor::reloadAsset(const std::string &fileName)
00299 {
00300 mIsReloading = true;
00301
00302 removeAsset(fileName);
00303 bool succeeded = loadAsset(fileName);
00304
00305 mIsReloading = false;
00306
00307 return succeeded;
00308 }
00309
00310
00311 void EntityProcessor::reloadAssets()
00312 {
00313
00314 mIsReloading = true;
00315
00316 removeAssets();
00317 loadAssets();
00318
00319 mIsReloading = false;
00320
00321
00322
00323 std::map<std::string, Entity*> newMap;
00324
00325 std::map<std::string, std::pair<c8*, long> >::iterator itFiles;
00326 for(itFiles = mEntityFiles.begin(); itFiles != mEntityFiles.end(); itFiles++)
00327 {
00328 std::map<std::string, Entity*>::iterator itEntities = mEntities.find(itFiles->first);
00329
00330 if(itEntities != mEntities.end())
00331 {
00332 newMap[itEntities->first] = itEntities->second;
00333 mEntities.erase(itEntities);
00334 }
00335 }
00336
00337
00338 std::map<std::string, Entity*>::iterator itEntities;
00339 for(itEntities = mEntities.begin(); itEntities != mEntities.end(); itEntities++)
00340 (itEntities->second)->drop();
00341
00342 mEntities.clear();
00343
00344
00345 mEntities = newMap;
00346 }
00347
00348
00349 bool EntityProcessor::removeAsset(const std::string &fileName)
00350 {
00351
00352 bool succeeded = false;
00353
00354
00355 #ifdef WIN32
00356 std::string tmp = fileName;
00357 std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
00358 std::map<std::string, std::pair<c8*, long> >::iterator it = mEntityFiles.find(fileName);
00359 #else
00360 std::map<std::string, std::pair<c8*, long> >::iterator it = mEntityFiles.find(fileName);
00361 #endif
00362
00363
00364 if(it != mEntityFiles.end())
00365 {
00366
00367 c8 *fBuffer = (it->second).first;
00368 delete [] fBuffer;
00369 mEntityFiles.erase(it);
00370
00371 succeeded = true;
00372 }
00373
00374
00375 if(succeeded && !mIsReloading)
00376 {
00377 #ifdef WIN32
00378 std::map<std::string, Entity*>::iterator it = mEntities.find(tmp);
00379 #else
00380 std::map<std::string, Entity*>::iterator it = mEntities.find(fileName);
00381 #endif
00382
00383
00384 if(it != mEntities.end())
00385 {
00386 (it->second)->drop();
00387 mEntities.erase(it);
00388 }
00389 }
00390
00391
00392 return succeeded;
00393 }
00394
00395
00396 void EntityProcessor::removeAssets()
00397 {
00398
00399 std::map<std::string, std::pair<c8*, long> >::iterator itFiles;
00400
00401 for(itFiles = mEntityFiles.begin(); itFiles != mEntityFiles.end(); itFiles++)
00402 {
00403
00404 c8 *fBuffer = (itFiles->second).first;
00405 delete [] fBuffer;
00406 }
00407
00408
00409 mEntityFiles.clear();
00410
00411
00412 if(!mIsReloading)
00413 {
00414 std::map<std::string, Entity*>::iterator itEntities;
00415
00416 for(itEntities = mEntities.begin(); itEntities != mEntities.end(); itEntities++)
00417 (itEntities->second)->drop();
00418
00419
00420 mEntities.clear();
00421 }
00422 }
00423
00424
00425 AssetProcessor* EntityProcessor::createProcessor(AssetGroup *parent)
00426 {
00427 return new EntityProcessor(parent);
00428 }
00429
00430