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