#include #include #include #include #include #include #include "scriptstdstring.h" #include "scriptbuilder.h" class ByteCodeCompilator : public asIBinaryStream { public: ByteCodeCompilator(FILE *fp) : f(fp) {} void Write(const void *ptr, asUINT size) { if( size == 0 ) return; fwrite(ptr, size, 1, f); } void Read(void *ptr, asUINT size) { if( size == 0 ) return; fread(ptr, size, 1, f); } protected: FILE *f; }; bool TextCodeExist() { std::ifstream file("main.as"); if(file) return true; else return false; } bool ByteCodeExist() { std::ifstream file("bin.asc"); if(file) return true; else return false; } // Implement a simple message callback function void MessageCallback(const asSMessageInfo *msg, void *param) { const char *type = "ERR "; if( msg->type == asMSGTYPE_WARNING ) type = "WARN"; else if( msg->type == asMSGTYPE_INFORMATION ) type = "INFO"; std::cout<section<row<col<message<RegisterGlobalFunction("void print(int value)", asFUNCTION(print), asCALL_CDECL); CScriptBuilder builder; r = builder.StartNewModule(pScriptEngine,"MyModule"); asIScriptModule *mod; if(TextCodeExist()) //si le code text existe { FILE* FileLoaded; FileLoaded=fopen("bin.asc","w+"); ByteCodeCompilator compilator(FileLoaded); builder.AddSectionFromFile("main.as"); builder.BuildModule(); mod = pScriptEngine->GetModule("MyModule"); //and where loading the file to put the byte code in mod->SaveByteCode(&compilator); } else if(ByteCodeExist()) { FILE* FileLoaded; FileLoaded=fopen("bin.asc","r+"); ByteCodeCompilator* compilator=new ByteCodeCompilator(FileLoaded); builder.BuildModule(); mod=pScriptEngine->GetModule("MyModule"); std::cout<LoadByteCode(compilator)<GetFunctionByDecl("void main()"); asIScriptContext *ctx = pScriptEngine->CreateContext(); ctx->Prepare(func); r = ctx->Execute(); }