00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "ScriptManager.h"
00020
00021 #ifdef __COMPILE_WITH_ANGELSCRIPT__
00022
00023 #include "../core/GameManager.h"
00024
00025
00026 #include "vendor/angelscript/scriptstdstring.h"
00027 #include "vendor/irrlicht/asIrrlicht.h"
00028
00029 #include "assets/asProcessors.h"
00030 #include "components/asComponents.h"
00031
00032 #include "core/asAssetGroup.h"
00033 #include "core/asAssetManager.h"
00034 #include "core/asAssetProcessor.h"
00035 #include "core/asDataStack.h"
00036 #include "core/asDataStore.h"
00037 #include "core/asEntity.h"
00038 #include "core/asEntityComponent.h"
00039 #include "core/asEntityManager.h"
00040 #include "core/asEventManager.h"
00041 #include "core/asGameManager.h"
00042 #include "core/asGameState.h"
00043
00044 #include "scripting/asScript.h"
00045 #include "scripting/asScriptManager.h"
00046
00047 #include "sound/asSoundManager.h"
00048
00049
00050
00051
00052 ScriptManager::ScriptManager()
00053 {
00054 }
00055
00056
00057 ScriptManager::~ScriptManager()
00058 {
00059 }
00060
00061
00062 void ScriptManager::init()
00063 {
00064
00065 pEngine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
00066
00067 RegisterStdString(pEngine);
00068 pEngine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL);
00069
00070
00071 pEngine->RegisterTypedef("c8", "int8");
00072 pEngine->RegisterTypedef("f32", "float");
00073 pEngine->RegisterTypedef("f64", "double");
00074 pEngine->RegisterTypedef("s16", "int16");
00075 pEngine->RegisterTypedef("s32", "int");
00076 pEngine->RegisterTypedef("u16", "uint16");
00077 pEngine->RegisterTypedef("u32", "uint");
00078 pEngine->RegisterTypedef("u8", "uint8");
00079
00080
00081 bindIrrlicht(pEngine);
00082
00083
00084 bindAssetGroup(pEngine);
00085 bindAssetManager(pEngine);
00086 bindAssetProcessor(pEngine);
00087
00088 bindDataStack(pEngine);
00089 bindDataStore(pEngine);
00090
00091 bindEntity(pEngine);
00092 bindEntityComponent(pEngine);
00093 bindEntityManager(pEngine);
00094
00095 bindEventManager(pEngine);
00096
00097 #ifdef __COMPILE_WITH_SFML_AUDIO__
00098 bindSoundManager(pEngine);
00099 #endif // __COMPILE_WITH_SFML_AUDIO__
00100
00101 bindScript(pEngine);
00102 bindScriptManager(pEngine);
00103
00104 bindAssetProcessors(pEngine);
00105 bindComponents(pEngine);
00106
00107 bindGameState(pEngine);
00108 bindGameManager(pEngine);
00109 }
00110
00111
00112 void ScriptManager::clear()
00113 {
00114
00115 removeScripts();
00116
00117
00118 mScriptSections.clear();
00119
00120
00121 pEngine->Release();
00122 }
00123
00124
00125 void ScriptManager::grab()
00126 {
00127
00128 }
00129
00130
00131 void ScriptManager::drop()
00132 {
00133
00134 }
00135
00136
00137 asIScriptEngine* ScriptManager::getEngine() const
00138 {
00139 return pEngine;
00140 }
00141
00142
00143 bool ScriptManager::addScript(Script *script)
00144 {
00145
00146 if(script == NULL)
00147 return false;
00148
00149
00150 if(getScript(script->getName()))
00151 return false;
00152
00153
00154 mScripts.push_back(script);
00155 script->grab();
00156
00157 return true;
00158 }
00159
00160
00161 bool ScriptManager::addScriptSection(const std::string &name, const std::string §ion)
00162 {
00163
00164 if(mScriptSections.find(name) != mScriptSections.end())
00165 return false;
00166
00167
00168 mScriptSections[name] = section;
00169
00170 return true;
00171 }
00172
00173
00174 void ScriptManager::addScriptSectionsToScript(Script *script)
00175 {
00176
00177 if(script == NULL)
00178 return;
00179
00180
00181 asIScriptModule *module = script->getModule();
00182 std::map<std::string, std::string>::iterator it;
00183
00184 for(it = mScriptSections.begin(); it != mScriptSections.end(); it++)
00185 module->AddScriptSection((it->first).c_str(), (it->second).c_str());
00186 }
00187
00188
00189 Script* ScriptManager::createScript(const std::string &name, bool grab)
00190 {
00191
00192 if(grab && getScript(name))
00193 return NULL;
00194
00195
00196 Script *script = new Script(name);
00197
00198 if(grab)
00199 {
00200 mScripts.push_back(script);
00201 script->grab();
00202 }
00203
00204 return script;
00205 }
00206
00207
00208 Script* ScriptManager::createScriptFromFile(const std::string &fileName, bool grab)
00209 {
00210
00211 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00212
00213
00214 core::stringw wFilename = core::stringw(fileName.c_str());
00215 std::string fileBase = (fileSystem->getFileBasename(wFilename)).c_str();
00216
00217
00218 if(grab && getScript(fileBase))
00219 return NULL;
00220
00221
00222 Script *script = new Script(fileBase);
00223 script->loadScript(fileName);
00224
00225
00226 if(grab)
00227 {
00228 mScripts.push_back(script);
00229 script->grab();
00230 }
00231
00232 return script;
00233 }
00234
00235
00236 Script* ScriptManager::createScriptFromFile(IReadFile *file, bool grab)
00237 {
00238
00239 if(grab && getScript( (file->getFileName()).c_str() ))
00240 return NULL;
00241
00242
00243 Script *script = new Script( (file->getFileName()).c_str() );
00244 script->loadScript(file);
00245
00246 if(grab)
00247 {
00248 mScripts.push_back(script);
00249 script->grab();
00250 }
00251
00252 return script;
00253 }
00254
00255
00256 Script* ScriptManager::getScript(const u32 id)
00257 {
00258 for(u32 i = 0; i < mScripts.size(); i++)
00259 {
00260 if(mScripts[i]->getID() == id)
00261 return mScripts[i];
00262 }
00263
00264 return NULL;
00265 }
00266
00267
00268 Script* ScriptManager::getScript(const std::string &name)
00269 {
00270 for(u32 i = 0; i < mScripts.size(); i++)
00271 {
00272 if(mScripts[i]->getName() == name)
00273 return mScripts[i];
00274 }
00275
00276 return NULL;
00277 }
00278
00279
00280 void ScriptManager::removeScripts()
00281 {
00282 for(u32 i = 0; i < mScripts.size(); i++)
00283 mScripts[i]->drop();
00284
00285 mScripts.clear();
00286 }
00287
00288
00289 void ScriptManager::removeScriptSections()
00290 {
00291 mScriptSections.clear();
00292 }
00293
00294
00295 bool ScriptManager::removeScript(Script *script)
00296 {
00297
00298 if(script == NULL)
00299 return false;
00300
00301
00302 vector<Script*>::iterator it;
00303
00304 for(it = mScripts.begin(); it < mScripts.end(); it++)
00305 {
00306 Script *scr = *it;
00307
00308 if(scr == script)
00309 {
00310 scr->drop();
00311 mScripts.erase(it);
00312 return true;
00313 }
00314 }
00315
00316
00317 return false;
00318 }
00319
00320
00321 bool ScriptManager::removeScript(const u32 id)
00322 {
00323
00324 vector<Script*>::iterator it;
00325
00326 for(it = mScripts.begin(); it < mScripts.end(); it++)
00327 {
00328 Script *scr = *it;
00329
00330 if(scr->getID() == id)
00331 {
00332 scr->drop();
00333 mScripts.erase(it);
00334 return true;
00335 }
00336 }
00337
00338
00339 return false;
00340 }
00341
00342
00343 bool ScriptManager::removeScript(const std::string &name)
00344 {
00345
00346 vector<Script*>::iterator it;
00347
00348 for(it = mScripts.begin(); it < mScripts.end(); it++)
00349 {
00350 Script *scr = *it;
00351
00352 if(scr->getName() == name)
00353 {
00354 scr->drop();
00355 mScripts.erase(it);
00356 return true;
00357 }
00358 }
00359
00360
00361 return false;
00362 }
00363
00364
00365 bool ScriptManager::removeScriptSection(const std::string &name)
00366 {
00367
00368 std::map<std::string, std::string>::iterator it;
00369 it = mScriptSections.find(name);
00370
00371 if(it != mScriptSections.end())
00372 {
00373 mScriptSections.erase(it);
00374 return true;
00375 }
00376
00377
00378 return false;
00379 }
00380
00381
00382
00383
00384 void MessageCallback(const asSMessageInfo *msg, void *param)
00385 {
00386 const char *type = "ERR ";
00387 if(msg->type == asMSGTYPE_WARNING)
00388 type = "WARN";
00389 else if(msg->type == asMSGTYPE_INFORMATION)
00390 type = "INFO";
00391 printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message);
00392 }
00393
00394 #endif // __COMPILE_WITH_ANGELSCRIPT__
00395
00396