00001 #ifndef _SCRIPTMGR_H_
00002 #define _SCRIPTMGR_H_
00003
00004 #include <stdio.h>
00005 #include <io.h>
00006 #include <set>
00007
00008 #include "ZakEngine/Messages.h"
00009 #include "ZakEngine/FileLog.h"
00010 #include "ZakEngine/defines.h"
00011 #include "ZakEngine/MathUtil.h"
00012 #include "ZakEngine/Input.h"
00013 #include "ZakEngine/list.h"
00014 #include "angelscript.h"
00015 #include "asvectorhelper.h"
00016 #include "Script.h"
00017
00018 #ifdef _DEBUG
00019 #pragma comment(lib, "angelscriptd.lib")
00020 #else
00021 #pragma comment(lib, "angelscript.lib")
00022 #endif
00023
00024 namespace zak {
00025
00026 class Script;
00027
00028 class BinaryOutput : public asIBinaryStream {
00029
00030 FILE *_file;
00031
00032 BinaryOutput(){}
00033
00034 public:
00035 void Read(void *ptr, asUINT size) {
00036 fread(ptr, 1, size, _file);
00037 }
00038 void Write(const void *ptr, asUINT size) {
00039 fwrite(ptr, 1, size, _file);
00040 }
00041
00042 BinaryOutput(const char* fname, const char* mode) {
00043 fopen_s(&_file, fname, mode);
00044 }
00045 ~BinaryOutput() { if (_file) fclose(_file); _file = NULL; }
00046 };
00047
00106 class ZAKENGINE_API ScriptMgr
00107 {
00108 public:
00109
00113 static bool StartUp();
00114
00118 static void Shutdown();
00119
00126 static int RegisterGlobalProperty(char *szDecl, void* var)
00127 {
00128 return _asEngine->RegisterGlobalProperty(szDecl, var);
00129 }
00130
00138 static int RegisterGlobalFunction(char *szDecl, asUPtr funcPointer, asDWORD callConv)
00139 {
00140 return _asEngine->RegisterGlobalFunction(szDecl, funcPointer, callConv);
00141 }
00142
00150 static int RegisterObjectType (const char *szClassName, int iSize, asDWORD flags)
00151 {
00152 return _asEngine->RegisterObjectType(szClassName, iSize, flags);
00153 }
00154
00162 static int RegisterObjectProperty (const char *szClassName, const char *szPropertyDecl, int byteOffset)
00163 {
00164 return _asEngine->RegisterObjectProperty(szClassName, szPropertyDecl, byteOffset);
00165 }
00166
00175 static int RegisterObjectMethod(const char *szClassName, const char *szDecl, asUPtr funcPointer, asDWORD callConv)
00176 {
00177 return _asEngine->RegisterObjectMethod(szClassName, szDecl, funcPointer, callConv);
00178 }
00179
00189 static int RegisterObjectBehaviour(const char *szClassName, asDWORD behaviour, const char *szDeclaration, asUPtr funcPointer, asDWORD callConv)
00190 {
00191 return _asEngine->RegisterObjectBehaviour(szClassName, behaviour, szDeclaration, funcPointer, callConv);
00192 }
00193
00200 static Script* AddScript(char *szModule, char *szSection);
00201
00207 static bool RemoveScript(Script *script);
00208
00213 static bool ClearScriptList();
00214
00221 static bool CompileModule(char *szModule);
00222
00229 static bool ExecuteString(char *szModule, char *script) { return ExecuteString(szModule, script, NULL); }
00230
00231
00232 static bool LoadByteCode (char *szModule, char *szFilename);
00233 static bool SaveByteCode (char *szModule, char *szFilename);
00234
00235 static asIScriptEngine *GetEngine() { return _asEngine; }
00236
00237 private:
00238 static bool RegisterClasses();
00239
00240 static void ConsoleTrace(string str) { Console::Trace(str); }
00241 static void ConsoleTrace(float var) { Console::Trace("%3.5f",var); }
00242 static void ConsoleTrace(double var) { Console::Trace("%3.5e",var); }
00243 static void ConsoleTrace(int var) { Console::Trace("%d",var); }
00244 static void ConsoleTrace(bool var) { (var?Console::Trace("true"):Console::Trace("false")); }
00245 static void ConsoleTrace(asUINT var) { Console::Trace("%u",var); }
00246 static void LogTrace(string str) { Log.Trace("%s\n", str.c_str()); }
00247 static void LogTrace(float var) { Log.Trace("%3.5f\n", var); }
00248 static void LogTrace(bool var) { (var?Log.Trace("true\n"):Log.Trace("false\n")); }
00249 static void LogTrace(double var) { Log.Trace("%3.5e\n", var); }
00250 static void LogTrace(int var) { Log.Trace("%d\n", var); }
00251 static void LogTrace(asUINT var) { Log.Trace("%u\n", var); }
00252
00253 static float scriptRadToDegree(float angle);
00254 static float scriptDegreeToRad(float angle);
00255
00256 static BYTE scriptKeyDown(BYTE key);
00257 static BYTE scriptKeyUp(BYTE key);
00258 static BYTE scriptKeyPressed(BYTE key);
00259
00260 static BYTE scriptMousePress(BYTE btn);
00261 static bool scriptMouseClick(BYTE btn);
00262 static long scriptMouseRelPos(BYTE btn);
00263 static long scriptMouseAbsPos(BYTE btn);
00264 static long scriptMouseWorldPos(BYTE btn);
00265 static float scriptMouseRelPosf(BYTE btn);
00266 static float scriptMouseAbsPosf(BYTE btn);
00267 static float scriptMouseWorldPosf(BYTE btn);
00268
00269 static long scriptJoystickPos(int devNum, int axis);
00270 static long scriptJoystickRot(int devNum, int axis);
00271 static float scriptJoystickPosf(int devNum, int axis);
00272 static float scriptJoystickRotf(int devNum, int axis);
00273 static BYTE scriptJoystickButtons(int devNum, int button);
00274 static int scriptJoystickCount();
00275
00276 static void ConsoleExecute(string arg);
00277 static void ConsoleExecuteM(string arg);
00278 static void ConsoleGet(string arg);
00279
00280 static void ConstructIntArray(vector<int> *self) {
00281 new(self) vector<int>();
00282 }
00283
00284 static void ConstructIntArray(int length, vector<int> *self) {
00285 new(self) vector<int>(length);
00286 }
00287
00288 static void DestructIntArray(vector<int> *self) {
00289 self->~vector();
00290 }
00291
00292
00293 static void ConstructBoolArray(vector<bool> *self) {
00294 new(self) vector<bool>();
00295 }
00296
00297 static void ConstructBoolArray(int length, vector<bool> *self) {
00298 new(self) vector<bool>(length);
00299 }
00300
00301 static void DestructBoolArray(vector<bool> *self) {
00302 self->~vector();
00303 }
00304
00305 static void ConstructDoubleArray(vector<double> *self) {
00306 new(self) vector<double>();
00307 }
00308
00309 static void ConstructDoubleArray(int length, vector<double> *self) {
00310 new(self) vector<double>(length);
00311 }
00312
00313 static void DestructDoubleArray(vector<double> *self) {
00314 self->~vector();
00315 }
00316
00317 static void ConstructULongArray(vector<unsigned long> *self) {
00318 new(self) vector<unsigned long>();
00319 }
00320
00321 static void ConstructULongArray(int length, vector<unsigned long> *self) {
00322 new(self) vector<unsigned long>(length);
00323 }
00324
00325 static void DestructULongArray(vector<unsigned long> *self) {
00326 self->~vector();
00327 }
00328
00329 static void ConstructUInt8Array(vector<unsigned char> *self) {
00330 new(self) vector<unsigned char>();
00331 }
00332
00333 static void ConstructUInt8Array(int length, vector<unsigned char> *self) {
00334 new(self) vector<unsigned char>(length);
00335 }
00336
00337 static void DestructUInt8Array(vector<unsigned char> *self) {
00338 self->~vector();
00339 }
00340
00341
00342 static void ConstructUInt16Array(vector<unsigned short> *self) {
00343 new(self) vector<unsigned short>();
00344 }
00345
00346 static void ConstructUInt16Array(int length, vector<unsigned short> *self) {
00347 new(self) vector<unsigned short>(length);
00348 }
00349
00350 static void DestructUInt16Array(vector<unsigned short> *self) {
00351 self->~vector();
00352 }
00353
00354 static void ConstructInt8Array(vector<char> *self) {
00355 new(self) vector<char>();
00356 }
00357
00358 static void ConstructInt8Array(int length, vector<char> *self) {
00359 new(self) vector<char>(length);
00360 }
00361
00362 static void DestructInt8Array(vector<char> *self) {
00363 self->~vector();
00364 }
00365
00366
00367 static void ConstructInt16Array(vector<short> *self) {
00368 new(self) vector<short>();
00369 }
00370
00371 static void ConstructInt16Array(int length, vector<short> *self) {
00372 new(self) vector<short>(length);
00373 }
00374
00375 static void DestructInt16Array(vector<short> *self) {
00376 self->~vector();
00377 }
00378
00379 static void ConstructLongArray(vector<long> *self);
00380 static void ConstructLongArray(int length, vector<long> *self);
00381 static void DestructLongArray(vector<long> *self);
00382
00383 static void ConstructFloatArray(vector<float> *self);
00384 static void ConstructFloatArray(int length, vector<float> *self);
00385 static void DestructFloatArray(vector<float> *self);
00386
00387 static bool LoadScript (char *szFilename, char *szModule, char *szSection);
00388 static bool LoadScript (char *szFilename, string &script, string &dir);
00389
00390
00391 static int ProcessInclude (string &script, string &dir);
00392
00393 static asIScriptContext* CreateContext() { return _asEngine->CreateContext(); }
00394
00395 static int ReleaseContext(asIScriptContext *gsContext) { return gsContext->Release(); }
00396 static int Prepare(asIScriptContext *asContext, int iFunctionId) { return asContext->Prepare(iFunctionId); }
00397 static int Execute (asIScriptContext *asContext) { return asContext->Execute(); }
00398 static bool ExecuteString (char *szModule, char *script, asIScriptContext **asContext, asDWORD flags=0);
00399
00400 static int GetFuncIDByDecl(char *szModule, char *szFuncDecl) { return _asEngine->GetFunctionIDByDecl(szModule, szFuncDecl); }
00401 static const char *GetFuncDeclaration(int funcId) { return _asEngine->GetFunctionDeclaration(funcId);}
00402
00403 static void MessageCallback(const asSMessageInfo *msg, void *param);
00404
00405 static asIScriptEngine *_asEngine;
00406 static List<Script*> _list;
00407 static set<string> _includedScripts;
00408
00409
00410 friend class Script;
00411 };
00412
00413 }
00414
00415 #endif // _SCRIPTMGR_H_