00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "MeshProcessor.h"
00018 #include "../core/GameManager.h"
00019 #include <algorithm>
00020
00021
00022
00023 MeshProcessor::MeshProcessor(AssetGroup *parent)
00024 : AssetProcessor(parent, "meshes"), mIsReloading(false)
00025 {
00026 }
00027
00028
00029 MeshProcessor::~MeshProcessor()
00030 {
00031 removeAssets();
00032 }
00033
00034
00035 IAnimatedMesh* MeshProcessor::getMesh(const std::string &fileName)
00036 {
00037 #ifdef WIN32
00038 std::string tmp = fileName;
00039 std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
00040 std::map<std::string, IAnimatedMesh*>::iterator it = mMeshes.find(tmp);
00041 #else
00042 std::map<std::string, IAnimatedMesh*>::iterator it = mMeshes.find(fileName);
00043 #endif
00044
00045 if(it != mMeshes.end())
00046 return it->second;
00047
00048 return 0;
00049 }
00050
00051
00052 bool MeshProcessor::loadAsset(const std::string &fileName)
00053 {
00054
00055 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00056 ISceneManager *pSceneMgr = GameManager::Instance()->getSceneManager();
00057
00058
00059 std::string workingDir = (fileSystem->getWorkingDirectory()).c_str();
00060
00061 if(!fileSystem->changeWorkingDirectoryTo( (pParent->getBaseDir()).c_str() ) )
00062 return false;
00063
00064 if(!fileSystem->changeWorkingDirectoryTo("./meshes/"))
00065 return false;
00066
00067
00068 IAnimatedMesh *mesh = pSceneMgr->getMesh(fileName.c_str());
00069
00070 if(mesh)
00071 {
00072
00073 mMeshes[fileName] = mesh;
00074 mesh->grab();
00075
00076
00077 GameManager::Instance()->getSceneManager()->getMeshCache()->removeMesh(mesh);
00078
00079
00080 if(mIsReloading)
00081 pParent->emitEvent(std::string("meshes/") + fileName, mesh);
00082
00083 else
00084 pParent->createEventSlot(std::string("meshes/") + fileName);
00085 }
00086
00087
00088 fileSystem->changeWorkingDirectoryTo(workingDir.c_str());
00089
00090 return (mesh) ? true : false;
00091 }
00092
00093
00094 void MeshProcessor::loadAssets()
00095 {
00096
00097 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00098 ISceneManager *pSceneMgr = GameManager::Instance()->getSceneManager();
00099
00100
00101 std::string workingDir = (fileSystem->getWorkingDirectory()).c_str();
00102
00103 if(!fileSystem->changeWorkingDirectoryTo( (pParent->getBaseDir()).c_str() ) )
00104 return;
00105
00106 if(!fileSystem->changeWorkingDirectoryTo("./meshes/"))
00107 return;
00108
00109
00110 IFileList *fileList = fileSystem->createFileList();
00111
00112 for(u32 i = 0; i < fileList->getFileCount(); i++)
00113 {
00114
00115 std::string fileName = (fileList->getFileName(i)).c_str();
00116
00117 if(fileName == "..")
00118 continue;
00119
00120
00121 IAnimatedMesh *mesh = pSceneMgr->getMesh(fileName.c_str());
00122
00123 if(mesh)
00124 {
00125
00126 mMeshes[fileName] = mesh;
00127 mesh->grab();
00128
00129
00130 GameManager::Instance()->getSceneManager()->getMeshCache()->removeMesh(mesh);
00131
00132
00133 if(mIsReloading)
00134 pParent->emitEvent(std::string("meshes/") + fileName, mesh);
00135
00136 else
00137 pParent->createEventSlot(std::string("meshes/") + fileName);
00138 }
00139 }
00140
00141
00142 fileList->drop();
00143
00144 fileSystem->changeWorkingDirectoryTo(workingDir.c_str());
00145 }
00146
00147
00148 bool MeshProcessor::reloadAsset(const std::string &fileName)
00149 {
00150 mIsReloading = true;
00151
00152 removeAsset(fileName);
00153 bool succeeded = loadAsset(fileName);
00154
00155 mIsReloading = false;
00156
00157 return succeeded;
00158 }
00159
00160
00161 void MeshProcessor::reloadAssets()
00162 {
00163 mIsReloading = true;
00164
00165 removeAssets();
00166 loadAssets();
00167
00168 mIsReloading = false;
00169 }
00170
00171
00172 bool MeshProcessor::removeAsset(const std::string &fileName)
00173 {
00174
00175 #ifdef WIN32
00176 std::string tmp = fileName;
00177 std::transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
00178 std::map<std::string, IAnimatedMesh*>::iterator it = mMeshes.find(tmp);
00179 #else
00180 std::map<std::string, IAnimatedMesh*>::iterator it = mMeshes.find(fileName);
00181 #endif
00182
00183
00184 if(it != mMeshes.end())
00185 {
00186
00187 if(!mIsReloading)
00188 {
00189 std::string slotName = std::string("meshes/") + it->first;
00190
00191 pParent->emitEvent(slotName, NULL);
00192 pParent->removeEventSlot(slotName);
00193 }
00194
00195
00196 IAnimatedMesh *mesh = it->second;
00197 mesh->drop();
00198 mMeshes.erase(it);
00199
00200 return true;
00201 }
00202
00203
00204 else return false;
00205 }
00206
00207
00208 void MeshProcessor::removeAssets()
00209 {
00210
00211 std::map<std::string, IAnimatedMesh*>::iterator it;
00212
00213 for(it = mMeshes.begin(); it != mMeshes.end(); it++)
00214 {
00215
00216 if(!mIsReloading)
00217 {
00218 std::string slotName = std::string("meshes/") + it->first;
00219
00220 pParent->emitEvent(slotName, NULL);
00221 pParent->removeEventSlot(slotName);
00222 }
00223
00224
00225 IAnimatedMesh *mesh = it->second;
00226 mesh->drop();
00227 }
00228
00229
00230 mMeshes.clear();
00231 }
00232
00233
00234 AssetProcessor* MeshProcessor::createProcessor(AssetGroup *parent)
00235 {
00236 return new MeshProcessor(parent);
00237 }
00238
00239