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