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