00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "ScriptProcessor.h"
00018
00019 #ifdef __COMPILE_WITH_ANGELSCRIPT__
00020
00021 #include "../core/GameManager.h"
00022
00023
00024
00025
00026 ScriptProcessor::ScriptProcessor(AssetGroup *parent)
00027 : AssetProcessor(parent, "scripts"), mIsReloading(false)
00028 {
00029 }
00030
00031
00032 ScriptProcessor::~ScriptProcessor()
00033 {
00034 removeAssets();
00035 }
00036
00037
00038 Script* ScriptProcessor::getScript(const std::string &fileName)
00039 {
00040
00041 std::map<std::string, Script*>::iterator itScripts = mScripts.find(fileName);
00042
00043 if(itScripts != mScripts.end())
00044 {
00045 Script *script = itScripts->second;
00046
00047 script->grab();
00048 return script;
00049 }
00050
00051
00052 std::map<std::string, std::pair<c8*, long> >::iterator itFiles = mScriptFiles.find(fileName);
00053
00054 if(itFiles != mScriptFiles.end())
00055 {
00056
00057 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00058
00059
00060 long fSize = (itFiles->second).second;
00061 c8 *fBuffer = (itFiles->second).first;
00062
00063 IReadFile *file = fileSystem->createMemoryReadFile(fBuffer, fSize, fileName.c_str());
00064
00065
00066 Script *script = GameManager::Instance()->getScriptManager()
00067 ->createScriptFromFile(file, false);
00068
00069 mScripts[fileName] = script;
00070 script->grab();
00071
00072
00073 file->drop();
00074
00075
00076 return script;
00077 }
00078
00079
00080 return NULL;
00081 }
00082
00083
00084 bool ScriptProcessor::loadAsset(const std::string &fileName)
00085 {
00086
00087 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00088
00089
00090 std::string workingDir = (fileSystem->getWorkingDirectory()).c_str();
00091
00092 if(!fileSystem->changeWorkingDirectoryTo( (pParent->getBaseDir()).c_str() ) )
00093 return false;
00094
00095 if(!fileSystem->changeWorkingDirectoryTo("./scripts/"))
00096 return false;
00097
00098
00099 IReadFile *file = fileSystem->createAndOpenFile(fileName.c_str());
00100
00101 if(file)
00102 {
00103
00104 long fSize = file->getSize();
00105 c8 *fBuffer = new c8[fSize+1];
00106 memset(fBuffer, 0, fSize+1);
00107
00108 file->read(fBuffer, fSize);
00109
00110
00111 mScriptFiles[fileName] = std::pair<c8*, long>(fBuffer, fSize);
00112 cout << "Loaded script: " << fileName << "\n";
00113
00114
00115 if(mIsReloading)
00116 {
00117 std::map<std::string, Script*>::iterator it = mScripts.find(fileName);
00118
00119 if(it != mScripts.end())
00120 {
00121 file->seek(0);
00122
00123 Script *script = it->second;
00124 script->unloadScript();
00125 script->loadScript(file);
00126 }
00127 }
00128
00129
00130 file->drop();
00131 }
00132
00133
00134 fileSystem->changeWorkingDirectoryTo(workingDir.c_str());
00135
00136 return (file) ? true : false;
00137 }
00138
00139
00140 void ScriptProcessor::loadAssets()
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;
00150
00151 if(!fileSystem->changeWorkingDirectoryTo("./scripts/"))
00152 return;
00153
00154
00155 IFileList *fileList = fileSystem->createFileList();
00156
00157 for(u32 i = 0; i < fileList->getFileCount(); i++)
00158 {
00159
00160 std::string fileName = (fileList->getFileName(i)).c_str();
00161
00162 if(fileName == "..")
00163 continue;
00164
00165
00166 IReadFile *file = fileSystem->createAndOpenFile(fileName.c_str());
00167
00168 if(file)
00169 {
00170
00171 long fSize = file->getSize();
00172 c8 *fBuffer = new c8[fSize+1];
00173 memset(fBuffer, 0, fSize+1);
00174
00175 file->read(fBuffer, fSize);
00176
00177
00178 mScriptFiles[fileName] = std::pair<c8*, long>(fBuffer, fSize);
00179 cout << "Loaded script: " << fileName << "\n";
00180
00181
00182 if(mIsReloading)
00183 {
00184 std::map<std::string, Script*>::iterator it = mScripts.find(fileName);
00185
00186 if(it != mScripts.end())
00187 {
00188 file->seek(0);
00189
00190 Script *script = it->second;
00191 script->unloadScript();
00192 script->loadScript(file);
00193 }
00194 }
00195
00196
00197 file->drop();
00198 }
00199 }
00200
00201
00202 fileList->drop();
00203
00204 fileSystem->changeWorkingDirectoryTo(workingDir.c_str());
00205 }
00206
00207
00208 bool ScriptProcessor::reloadAsset(const std::string &fileName)
00209 {
00210 mIsReloading = true;
00211
00212 removeAsset(fileName);
00213 bool succeeded = loadAsset(fileName);
00214
00215 mIsReloading = false;
00216
00217 return succeeded;
00218 }
00219
00220
00221 void ScriptProcessor::reloadAssets()
00222 {
00223
00224 mIsReloading = true;
00225
00226 removeAssets();
00227 loadAssets();
00228
00229 mIsReloading = false;
00230
00231
00232
00233 std::map<std::string, Script*> newMap;
00234
00235 std::map<std::string, std::pair<c8*, long> >::iterator itFiles;
00236 for(itFiles = mScriptFiles.begin(); itFiles != mScriptFiles.end(); itFiles++)
00237 {
00238 std::map<std::string, Script*>::iterator itScripts = mScripts.find(itFiles->first);
00239
00240 if(itScripts != mScripts.end())
00241 {
00242 newMap[itScripts->first] = itScripts->second;
00243 mScripts.erase(itScripts);
00244 }
00245 }
00246
00247
00248 std::map<std::string, Script*>::iterator itScripts;
00249 for(itScripts = mScripts.begin(); itScripts != mScripts.end(); itScripts++)
00250 (itScripts->second)->drop();
00251
00252 mScripts.clear();
00253
00254
00255 mScripts = newMap;
00256 }
00257
00258
00259 bool ScriptProcessor::removeAsset(const std::string &fileName)
00260 {
00261
00262 bool succeeded = false;
00263
00264
00265 std::map<std::string, std::pair<c8*, long> >::iterator it = mScriptFiles.find(fileName);
00266
00267
00268 if(it != mScriptFiles.end())
00269 {
00270
00271 c8 *fBuffer = (it->second).first;
00272 delete [] fBuffer;
00273 mScriptFiles.erase(it);
00274
00275 succeeded = true;
00276 }
00277
00278
00279 if(succeeded && !mIsReloading)
00280 {
00281 std::map<std::string, Script*>::iterator it = mScripts.find(fileName);
00282
00283
00284 if(it != mScripts.end())
00285 {
00286 (it->second)->drop();
00287 mScripts.erase(it);
00288 }
00289 }
00290
00291
00292 return succeeded;
00293 }
00294
00295
00296 void ScriptProcessor::removeAssets()
00297 {
00298
00299 std::map<std::string, std::pair<c8*, long> >::iterator itFiles;
00300
00301 for(itFiles = mScriptFiles.begin(); itFiles != mScriptFiles.end(); itFiles++)
00302 {
00303
00304 c8 *fBuffer = (itFiles->second).first;
00305 delete [] fBuffer;
00306 }
00307
00308
00309 mScriptFiles.clear();
00310
00311
00312 if(!mIsReloading)
00313 {
00314 std::map<std::string, Script*>::iterator itScripts;
00315
00316 for(itScripts = mScripts.begin(); itScripts != mScripts.end(); itScripts++)
00317 (itScripts->second)->drop();
00318
00319
00320 mScripts.clear();
00321 }
00322 }
00323
00324
00325 AssetProcessor* ScriptProcessor::createProcessor(AssetGroup *parent)
00326 {
00327 return new ScriptProcessor(parent);
00328 }
00329
00330 #endif // __COMPILE_WITH_ANGELSCRIPT__
00331
00332