00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "Script.h"
00019
00020 #ifdef __COMPILE_WITH_ANGELSCRIPT__
00021
00022 #include "../core/GameManager.h"
00023
00024
00025
00026
00027 u32 Script::mIDCount = 0;
00028
00029
00030 Script::Script(const std::string &name)
00031 : mName(name), mModule(NULL)
00032 {
00033 mID = mIDCount++;
00034
00035
00036 ScriptManager *scriptMgr = GameManager::Instance()->getScriptManager();
00037
00038
00039 mModule = scriptMgr->getEngine()->GetModule(mName.c_str(), asGM_ALWAYS_CREATE);
00040 mContext = scriptMgr->getEngine()->CreateContext();
00041 }
00042
00043
00044 Script::~Script()
00045 {
00046
00047 ScriptManager *scriptMgr = GameManager::Instance()->getScriptManager();
00048
00049
00050 if(mModule != NULL)
00051 scriptMgr->getEngine()->DiscardModule(mName.c_str());
00052
00053 mContext->Release();
00054 }
00055
00056
00057 u32 Script::getID() const
00058 {
00059 return mID;
00060 }
00061
00062
00063 const std::string& Script::getName() const
00064 {
00065 return mName;
00066 }
00067
00068
00069 asIScriptContext* Script::getContext() const
00070 {
00071 return mContext;
00072 }
00073
00074
00075 asIScriptModule* Script::getModule() const
00076 {
00077 return mModule;
00078 }
00079
00080
00081 bool Script::loadScript(const std::string &fileName)
00082 {
00083
00084 IFileSystem *fileSystem = GameManager::Instance()->getDevice()->getFileSystem();
00085 ScriptManager *scriptMgr = GameManager::Instance()->getScriptManager();
00086
00087
00088 if(mModule != NULL)
00089 {
00090 scriptMgr->getEngine()->DiscardModule(mName.c_str());
00091 mModule = NULL;
00092 }
00093
00094
00095 if(!fileSystem->existFile(fileName.c_str()))
00096 return false;
00097
00098
00099 mModule = scriptMgr->getEngine()->GetModule(mName.c_str(), asGM_ALWAYS_CREATE);
00100
00101
00102 IReadFile *file = fileSystem->createAndOpenFile(fileName.c_str());
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 file->drop();
00110
00111
00112 mModule->AddScriptSection("main", fBuffer);
00113 scriptMgr->addScriptSectionsToScript(this);
00114
00115
00116 int r = mModule->Build();
00117 if(r < 0)
00118 return false;
00119
00120
00121 delete[] fBuffer;
00122
00123 return true;
00124 }
00125
00126
00127 bool Script::loadScript(IReadFile *file)
00128 {
00129
00130 ScriptManager *scriptMgr = GameManager::Instance()->getScriptManager();
00131
00132
00133 if(mModule != NULL)
00134 {
00135 scriptMgr->getEngine()->DiscardModule(mName.c_str());
00136 mModule = NULL;
00137 }
00138
00139
00140 if(!file)
00141 return false;
00142
00143
00144 mModule = scriptMgr->getEngine()->GetModule(mName.c_str(), asGM_ALWAYS_CREATE);
00145
00146
00147 long fSize = file->getSize();
00148 c8 * fBuffer = new c8[fSize+1];
00149 memset(fBuffer, 0, fSize+1);
00150
00151 file->read(fBuffer, fSize);
00152
00153
00154 mModule->AddScriptSection("main", fBuffer);
00155 scriptMgr->addScriptSectionsToScript(this);
00156
00157
00158 int r = mModule->Build();
00159 if(r < 0)
00160 return false;
00161
00162
00163 delete[] fBuffer;
00164
00165 return true;
00166 }
00167
00168
00169 bool Script::unloadScript()
00170 {
00171 ScriptManager *scriptMgr = GameManager::Instance()->getScriptManager();
00172
00173 if(mModule != NULL)
00174 {
00175 scriptMgr->getEngine()->DiscardModule(mName.c_str());
00176 mModule = NULL;
00177 return true;
00178 }
00179
00180 return false;
00181 }
00182
00183
00184 bool Script::executeFunctionByName(const std::string &name, const std::string &args)
00185 {
00186
00187 if(mModule == NULL)
00188 return false;
00189
00190
00191 int functionID = mModule->GetFunctionIdByName(name.c_str());
00192
00193 if(functionID == asERROR || functionID == asMULTIPLE_FUNCTIONS || functionID == asNO_FUNCTION)
00194 return false;
00195
00196
00197 std::stringstream ss;
00198 ss << name << "(" << args << ");";
00199
00200 mModule->GetEngine()->ExecuteString(mName.c_str(), ss.str().c_str(), &mContext,
00201 asEXECSTRING_ONLY_PREPARE | asEXECSTRING_USE_MY_CONTEXT);
00202
00203
00204 int r = mContext->Execute();
00205 if(r == asEXECUTION_FINISHED)
00206 {
00207 return true;
00208 }
00209
00210 else return false;
00211 }
00212
00213 #endif // __COMPILE_WITH_ANGELSCRIPT__
00214
00215