void PerlembParser::ReloadQuests() { try { if(perl == nullptr) { perl = new Embperl; } else { perl->Reinit(); } MapFunctions(); } catch(std::exception &e) { if(perl != nullptr) { delete perl; perl = nullptr; } Log.Out(Logs::General, Logs::Status, "Error re-initializing perlembed: %s", e.what()); throw e.what(); } errors_.clear(); npc_quest_status_.clear(); global_npc_quest_status_ = questUnloaded; player_quest_status_ = questUnloaded; global_player_quest_status_ = questUnloaded; item_quest_status_.clear(); spell_quest_status_.clear(); }
void LuaParser::ReloadQuests() { loaded_.clear(); errors_.clear(); lua_encounter_events_registered.clear(); if(L) { lua_close(L); } L = luaL_newstate(); luaL_openlibs(L); if(luaopen_bit(L) != 1) { std::string error = lua_tostring(L, -1); AddError(error); } if(luaL_dostring(L, "math.randomseed(os.time())")) { std::string error = lua_tostring(L, -1); AddError(error); } #ifdef SANITIZE_LUA_LIBS //io lua_pushnil(L); lua_setglobal(L, "io"); //some os/debug are okay some are not lua_getglobal(L, "os"); lua_pushnil(L); lua_setfield(L, -2, "exit"); lua_pushnil(L); lua_setfield(L, -2, "execute"); lua_pushnil(L); lua_setfield(L, -2, "getenv"); lua_pushnil(L); lua_setfield(L, -2, "remove"); lua_pushnil(L); lua_setfield(L, -2, "rename"); lua_pushnil(L); lua_setfield(L, -2, "setlocale"); lua_pushnil(L); lua_setfield(L, -2, "tmpname"); lua_pop(L, 1); lua_pushnil(L); lua_setglobal(L, "collectgarbage"); lua_pushnil(L); lua_setglobal(L, "loadfile"); #endif lua_getglobal(L, "package"); lua_getfield(L, -1, "path"); std::string module_path = lua_tostring(L,-1); module_path += ";./lua_modules/?.lua"; lua_pop(L, 1); lua_pushstring(L, module_path.c_str()); lua_setfield(L, -2, "path"); lua_pop(L, 1); MapFunctions(L); //load init std::string path = "quests/"; path += QUEST_GLOBAL_DIRECTORY; path += "/script_init.lua"; FILE *f = fopen(path.c_str(), "r"); if(f) { fclose(f); if(luaL_dofile(L, "quests/global/script_init.lua")) { std::string error = lua_tostring(L, -1); AddError(error); } } //zone init - always loads after global if(zone) { std::string zone_script = "quests/"; zone_script += zone->GetShortName(); zone_script += "/script_init.lua"; f = fopen(zone_script.c_str(), "r"); if(f) { fclose(f); if(luaL_dofile(L, zone_script.c_str())) { std::string error = lua_tostring(L, -1); AddError(error); } } } }
int main(int argc, char** argv) { char *envstring = NULL; int iResult; string tempStr(REC_UNIT_INFO), tempStr1(REC_MODE); string path; string strShapeId; string strWordId; char infilelist[MAX_PATH]; string outfile("wordrectst.out"); vector<LTKTraceGroup> fieldInk; int charIndex; wstring eolstr(L"\r\n"); int i; // first argument is the logical project name // second argument is the ink file to recognize // third argument is the output file if(argc < 4) { cout << "\nUsage:"; cout << "\nwordrectst <logical projectname> <list file to recognize> <outputfile>"; cout << "\nlist of valid <logicalname>s is available in $LIPI_ROOT/projects/lipiengine.cfg file"; cout << endl; delete utilPtr; return -1; } // Get the LIPI_ROOT environment variable envstring = getenv(LIPIROOT_ENV_STRING); if(envstring == NULL) { cout << "\nError, Environment variable is not set LIPI_ROOT\n"; delete utilPtr; return -1; } // Load the LipiEngine.DLL hLipiEngine = NULL; iResult = utilPtr->loadSharedLib(envstring, LIPIENGINE_MODULE_STR, &hLipiEngine); if(iResult != SUCCESS) { cout << "Error loading LipiEngine module" << endl; delete utilPtr; return -1; } if(MapFunctions() != 0) { cout << "Error fetching exported functions of the module" << endl; delete utilPtr; return -1; } // create an instance of LipiEngine Module ptrObj = createLTKLipiEngine(); // set the LIPI_ROOT path in Lipiengine module instance ptrObj->setLipiRootPath(envstring); // Initialize the LipiEngine module iResult = ptrObj->initializeLipiEngine(); if(iResult != SUCCESS) { cout << iResult << ": Error initializing LipiEngine.\n"; utilPtr->unloadSharedLib(hLipiEngine); delete utilPtr; return -1; } // Assign the logical name of the project to this string, i.e. TAMIL_WORD string strLogicalName = string(argv[1]); strcpy(infilelist, argv[2]); outfile = argv[3]; LTKWordRecognizer *pWordReco = NULL; ptrObj->createWordRecognizer(strLogicalName,&pWordReco); if(pWordReco == NULL) { cout << "\nError creating Word Recognizer\n"; utilPtr->unloadSharedLib(hLipiEngine); delete utilPtr; return -1; } // You can also use project and profile name to create LipiEngine instance as follows... // string strProjectName = "tamil_boxed_field"; // string strProfileName = "default"; // LTKWordRecognizer *pWordReco = ptrObj->createWordRecognizer(&strProjectName, &strProfileName); int iErrorCode = 0; LTKRecognitionContext *recoContext = new LTKRecognitionContext(); if(iErrorCode != 0) { cout << "\nError creating recognition context.\n"; ptrObj->deleteWordRecognizer(pWordReco); utilPtr->unloadSharedLib(hLipiEngine); delete utilPtr; return -1; } LTKCaptureDevice deviceContext; LTKScreenContext screenContext; int numChoices = 2; // Setting the device attributes deviceContext.setSamplingRate(120); deviceContext.setXDPI(2500); deviceContext.setYDPI(2500); deviceContext.setUniformSampling(true); // Set the engine to recognizer recoContext->setWordRecoEngine(pWordReco); // set the device context recoContext->setDeviceContext(deviceContext); // set the screen context recoContext->setScreenContext(screenContext); recoContext->setFlag(tempStr,REC_UNIT_CHAR); recoContext->setFlag(tempStr1,REC_MODE_STREAMING); // set the number of choices required recoContext->setNumResults(numChoices); ifstream in(infilelist); if(in == NULL) { cout << "Test list file open error : " << infilelist << endl; delete utilPtr; return -1; } ofstream resultfile(outfile.c_str(),ios::out|ios::binary); //a Header of 0xFEFF is required to identify this is a //16 bit unicode file const unsigned short fHeader = 0xfeff; resultfile.write((char*)&fHeader,sizeof(unsigned short)); while(in) { //Get the file name if(!getline(in,path,' ')) { break; } //Get the word ID getline(in,strWordId); //iWordID = atoi(strShapeId.c_str()); cout << path << endl; try { fieldInk.clear(); //read the word file readWordFile(path, fieldInk, deviceContext, screenContext); for(charIndex = 0; charIndex < fieldInk.size(); ++charIndex) { recoContext->beginRecoUnit(); recoContext->addTraceGroups(LTKTraceGroupVector(1,fieldInk.at(charIndex))); recoContext->endRecoUnit(); } } catch(LTKException e) { LOG(LTKLogger::LTK_LOGLEVEL_ERR) << e.getExceptionMessage(); return FAILURE; } //Calling recognize and retrieving the top result { LTKWordRecoResult result; vector<LTKWordRecoResult> r2; recoContext->recognize(); recoContext->getTopResult(result); recoContext->getNextBestResults(numChoices-1, r2); vector<unsigned short> resultVec = result.getResultWord(); if(!resultVec.empty()) { resultfile.write((char *)&(resultVec.at(0)), resultVec.size()*sizeof(unsigned short)); resultfile.write((char*)eolstr.c_str(),eolstr.length()*sizeof(unsigned short)); for(i =0; i<r2.size(); ++i) { resultVec = r2.at(i).getResultWord(); resultfile.write((char *)&(resultVec.at(0)), resultVec.size()*sizeof(unsigned short)); resultfile.write((char*)eolstr.c_str(),eolstr.length()*sizeof(unsigned short)); } } recoContext->clearRecognitionResult(); } } resultfile.close(); //delete word recognition instance if(pWordReco) { ptrObj->deleteWordRecognizer(pWordReco); } //delete recognition context object if(recoContext) { //ptrObj->deleteRecognitionContext(recoContext); delete recoContext; } //unload the LipiEngine module from memory... utilPtr->unloadSharedLib(hLipiEngine); delete utilPtr; return 0; }
void LuaParser::ReloadQuests() { loaded_.clear(); errors_.clear(); lua_encounter_events_registered.clear(); lua_encounters_loaded.clear(); for (auto encounter : lua_encounters) { encounter.second->Depop(); } lua_encounters.clear(); if(L) { lua_close(L); } L = luaL_newstate(); luaL_openlibs(L); if(luaopen_bit(L) != 1) { std::string error = lua_tostring(L, -1); AddError(error); } if(luaL_dostring(L, "math.randomseed(os.time())")) { std::string error = lua_tostring(L, -1); AddError(error); } #ifdef SANITIZE_LUA_LIBS //io lua_pushnil(L); lua_setglobal(L, "io"); //some os/debug are okay some are not lua_getglobal(L, "os"); lua_pushnil(L); lua_setfield(L, -2, "exit"); lua_pushnil(L); lua_setfield(L, -2, "execute"); lua_pushnil(L); lua_setfield(L, -2, "getenv"); lua_pushnil(L); lua_setfield(L, -2, "remove"); lua_pushnil(L); lua_setfield(L, -2, "rename"); lua_pushnil(L); lua_setfield(L, -2, "setlocale"); lua_pushnil(L); lua_setfield(L, -2, "tmpname"); lua_pop(L, 1); lua_pushnil(L); lua_setglobal(L, "collectgarbage"); lua_pushnil(L); lua_setglobal(L, "loadfile"); #endif // lua 5.2+ defines these #if defined(LUA_VERSION_MAJOR) && defined(LUA_VERSION_MINOR) const char lua_version[] = LUA_VERSION_MAJOR "." LUA_VERSION_MINOR; #elif LUA_VERSION_NUM == 501 const char lua_version[] = "5.1"; #else #error Incompatible lua version #endif #ifdef WINDOWS const char libext[] = ".dll"; #else // lua doesn't care OSX doesn't use sonames const char libext[] = ".so"; #endif lua_getglobal(L, "package"); lua_getfield(L, -1, "path"); std::string module_path = lua_tostring(L,-1); module_path += ";./" + Config->LuaModuleDir + "?.lua;./" + Config->LuaModuleDir + "?/init.lua"; // luarock paths using lua_modules as tree // to path it adds foo/share/lua/5.1/?.lua and foo/share/lua/5.1/?/init.lua module_path += ";./" + Config->LuaModuleDir + "share/lua/" + lua_version + "/?.lua"; module_path += ";./" + Config->LuaModuleDir + "share/lua/" + lua_version + "/?/init.lua"; lua_pop(L, 1); lua_pushstring(L, module_path.c_str()); lua_setfield(L, -2, "path"); lua_pop(L, 1); lua_getglobal(L, "package"); lua_getfield(L, -1, "cpath"); module_path = lua_tostring(L, -1); module_path += ";./" + Config->LuaModuleDir + "?" + libext; // luarock paths using lua_modules as tree // luarocks adds foo/lib/lua/5.1/?.so for cpath module_path += ";./" + Config->LuaModuleDir + "lib/lua/" + lua_version + "/?" + libext; lua_pop(L, 1); lua_pushstring(L, module_path.c_str()); lua_setfield(L, -2, "cpath"); lua_pop(L, 1); MapFunctions(L); //load init std::string path = Config->QuestDir; path += "/"; path += QUEST_GLOBAL_DIRECTORY; path += "/script_init.lua"; FILE *f = fopen(path.c_str(), "r"); if(f) { fclose(f); if(luaL_dofile(L, path.c_str())) { std::string error = lua_tostring(L, -1); AddError(error); } } //zone init - always loads after global if(zone) { std::string zone_script = Config->QuestDir; zone_script += "/"; zone_script += zone->GetShortName(); zone_script += "/script_init_v"; zone_script += std::to_string(zone->GetInstanceVersion()); zone_script += ".lua"; f = fopen(zone_script.c_str(), "r"); if(f) { fclose(f); if(luaL_dofile(L, zone_script.c_str())) { std::string error = lua_tostring(L, -1); AddError(error); } return; } zone_script = Config->QuestDir; zone_script += "/"; zone_script += zone->GetShortName(); zone_script += "/script_init.lua"; f = fopen(zone_script.c_str(), "r"); if(f) { fclose(f); if(luaL_dofile(L, zone_script.c_str())) { std::string error = lua_tostring(L, -1); AddError(error); } } } }