//========================================================================= ScriptRef* DbProxy::script_op(const ScriptAuth& auth, const ScriptRef& ref, const ScriptOp& op, const ScriptRef* right) { if (op.type() == ScriptOp::Lookup) { const std::string name = right->object()->get_string(); // Methods if ("set" == name) { return new ScriptMethodRef(ref,name); } std::auto_ptr<DbQuery> query(m_db.object()->new_query( "SELECT "+name+" FROM "+m_table+" WHERE "+m_key_field+" = ?")); ScriptList::Ref args(new ScriptList()); args.object()->give(m_key.ref_copy()); query->exec(&args); ScriptRef* result = 0; if (query->next_result()) { ScriptRef* row_ref = query->result_list(); ScriptList* row = dynamic_cast<ScriptList*>(row_ref->object()); if (row) result = row->take(0); delete row_ref; } return result; } return ScriptObject::script_op(auth,ref,op,right); }
void ScriptList::KeepList(ScriptList *list) { if (list == this) return; this->modifications++; ScriptList tmp; tmp.AddList(this); tmp.RemoveList(list); this->RemoveList(&tmp); }
void ScriptEventListener::registerScript(Script* script,const int8* functionName) { ScriptList* scriptList = getScriptList(functionName); if(!scriptList) { LOG(warning) << "Function not mapped [" << functionName << "]"; return; } scriptList->push_back(script); }
//========================================================================= int Database::simple_query_num(const std::string& query) { std::auto_ptr<DbQuery> q(new_query(query)); if (!q->exec(0)) return -1; if (!q->next_result()) return -1; ScriptRef* row_ref = q->result_list(); ScriptList* row = dynamic_cast<ScriptList*>(row_ref->object()); ScriptRef* a_num = row->get(0); int result = a_num ? a_num->object()->get_int() : -1; delete row_ref; return result; }
void ScriptEventListener::registerScript(Script* script,const int8* functionName) { ScriptList* scriptList = getScriptList(functionName); if(!scriptList) { gLogger->log(LogManager::NOTICE,"Script Event Listener: No function mapped for %s",functionName); return; } scriptList->push_back(script); }
void ScriptList::KeepList(ScriptList *list) { this->modifications++; ScriptList tmp; for (ScriptListMap::iterator iter = this->items.begin(); iter != this->items.end(); iter++) { tmp.AddItem((*iter).first); tmp.SetValue((*iter).first, (*iter).second); } tmp.RemoveList(list); this->RemoveList(&tmp); }
/* static */ ScriptList *ScriptIndustryType::GetAcceptedCargo(IndustryType industry_type) { if (!IsValidIndustryType(industry_type)) return NULL; const IndustrySpec *ins = ::GetIndustrySpec(industry_type); ScriptList *list = new ScriptList(); for (size_t i = 0; i < lengthof(ins->accepts_cargo); i++) { if (ins->accepts_cargo[i] != CT_INVALID) list->AddItem(ins->accepts_cargo[i]); } return list; }
// Finds lua script files from given path (including subdirectories) and pushes them to scripts void Eluna::GetScripts(std::string path, ScriptList& scripts) { ELUNA_LOG_DEBUG("[Eluna]: GetScripts from path `%s`", path.c_str()); ACE_Dirent dir; if (dir.open(path.c_str()) == -1) { ELUNA_LOG_ERROR("[Eluna]: Error No `%s` directory found, creating it", path.c_str()); ACE_OS::mkdir(path.c_str()); return; } ACE_DIRENT *directory = 0; while ((directory = dir.read())) { // Skip the ".." and "." files. if (ACE::isdotdir(directory->d_name)) continue; std::string fullpath = path + "/" + directory->d_name; ACE_stat stat_buf; if (ACE_OS::lstat(fullpath.c_str(), &stat_buf) == -1) continue; // load subfolder if ((stat_buf.st_mode & S_IFMT) == (S_IFDIR)) { GetScripts(fullpath, scripts); continue; } // was file, check extension ELUNA_LOG_DEBUG("[Eluna]: GetScripts Checking file `%s`", fullpath.c_str()); // split file name std::string filename = directory->d_name; uint32 extDot = filename.find_last_of('.'); if (extDot == std::string::npos) continue; std::string ext = filename.substr(extDot); filename = filename.substr(0, extDot); // check extension and add path to scripts to load if (ext != ".lua" && ext != ".dll") continue; LuaScript script; script.fileext = ext; script.filename = filename; script.filepath = fullpath; script.modulepath = fullpath.substr(0, fullpath.length() - ext.length()); scripts.push_back(script); ELUNA_LOG_DEBUG("[Eluna]: GetScripts add path `%s`", fullpath.c_str()); } }
void ScriptEventListener::handleScriptEvent(const int8* functionName,BString params) { ScriptList* scriptList = getScriptList(functionName); if(!scriptList) { LOG(warning) << "Function not mapped [" << functionName << "]"; return; } ScriptList::iterator it = scriptList->begin(); while(it != scriptList->end()) { (*it)->callFunction(functionName,"s",params.getAnsi()); ++it; } }
void ScriptEventListener::handleScriptEvent(const int8* functionName,BString params) { ScriptList* scriptList = getScriptList(functionName); if(!scriptList) { gLogger->log(LogManager::NOTICE, "Script Event Listener: No function mapped for %s",functionName); return; } ScriptList::iterator it = scriptList->begin(); while(it != scriptList->end()) { (*it)->callFunction(functionName,"s",params.getAnsi()); ++it; } }
//============================================================================= ScriptRef* Logger::script_op(const ScriptAuth& auth, const ScriptRef& ref, const ScriptOp& op, const ScriptRef* right) { if (op.type() == ScriptOp::Lookup) { const std::string name = right->object()->get_string(); // Methods if ("add" == name || "remove" == name || "set_async" == name) { return new ScriptMethodRef(ref,name); } // Properties if ("channels" == name) { ScriptList* list = new ScriptList(); for (ChannelMap::const_iterator it = m_channels.begin(); it != m_channels.end(); ++it) { list->give(it->second->ref_copy(ref.reftype())); } return new ScriptRef(list); } if ("async" == name) { return ScriptInt::new_ref(m_thread != 0); } // Sub-objects LogChannel* channel = find_channel(name); if (channel) { return new ScriptRef(channel); } } return ScriptObject::script_op(auth,ref,op,right); }
//============================================================================= ScriptRef* CacheLogChannel::script_op(const ScriptAuth& auth, const ScriptRef& ref, const ScriptOp& op, const ScriptRef* right) { if (op.type() == ScriptOp::Lookup) { const std::string name = right->object()->get_string(); // Properties if ("entries" == name) { ScriptList* list = new ScriptList(); for (std::list<std::string>::const_iterator it = m_cache.begin(); it != m_cache.end(); ++it) { list->give(ScriptString::new_ref(*it)); } return new ScriptRef(list); } } return ScriptObject::script_op(auth,ref,op,right); }
void Eluna::RunScripts() { uint32 oldMSTime = GetCurrTime(); uint32 count = 0; ScriptList scripts; scripts.insert(scripts.end(), lua_extensions.begin(), lua_extensions.end()); scripts.insert(scripts.end(), lua_scripts.begin(), lua_scripts.end()); lua_getglobal(L, "package"); luaL_getsubtable(L, -1, "loaded"); int modules = lua_gettop(L); for (ScriptList::const_iterator it = scripts.begin(); it != scripts.end(); ++it) { lua_getfield(L, modules, it->modulepath.c_str()); if (!lua_isnoneornil(L, -1)) { lua_pop(L, 1); ELUNA_LOG_DEBUG("[Eluna]: Extension was already loaded or required `%s`", it->filepath.c_str()); continue; } lua_pop(L, 1); if (!luaL_loadfile(L, it->filepath.c_str()) && !lua_pcall(L, 0, 1, 0)) { if (!lua_toboolean(L, -1)) { lua_pop(L, 1); Push(L, true); } lua_setfield(L, modules, it->modulepath.c_str()); // successfully loaded and ran file ELUNA_LOG_DEBUG("[Eluna]: Successfully loaded `%s`", it->filepath.c_str()); ++count; continue; } ELUNA_LOG_ERROR("[Eluna]: Error loading extension `%s`", it->filepath.c_str()); report(L); } lua_pop(L, 2); ELUNA_LOG_INFO("[Eluna]: Executed %u Lua scripts in %u ms", count, GetTimeDiff(oldMSTime)); }
void Eluna::RunScripts() { LOCK_ELUNA; if (!IsEnabled()) return; uint32 oldMSTime = ElunaUtil::GetCurrTime(); uint32 count = 0; ScriptList scripts; lua_extensions.sort(ScriptPathComparator); lua_scripts.sort(ScriptPathComparator); scripts.insert(scripts.end(), lua_extensions.begin(), lua_extensions.end()); scripts.insert(scripts.end(), lua_scripts.begin(), lua_scripts.end()); std::unordered_map<std::string, std::string> loaded; // filename, path lua_getglobal(L, "package"); // Stack: package luaL_getsubtable(L, -1, "loaded"); // Stack: package, modules int modules = lua_gettop(L); for (ScriptList::const_iterator it = scripts.begin(); it != scripts.end(); ++it) { // Check that no duplicate names exist if (loaded.find(it->filename) != loaded.end()) { ELUNA_LOG_ERROR("[Eluna]: Error loading `%s`. File with same name already loaded from `%s`, rename either file", it->filepath.c_str(), loaded[it->filename].c_str()); continue; } loaded[it->filename] = it->filepath; lua_getfield(L, modules, it->filename.c_str()); // Stack: package, modules, module if (!lua_isnoneornil(L, -1)) { lua_pop(L, 1); ELUNA_LOG_DEBUG("[Eluna]: `%s` was already loaded or required", it->filepath.c_str()); continue; } lua_pop(L, 1); // Stack: package, modules if (luaL_loadfile(L, it->filepath.c_str())) { // Stack: package, modules, errmsg ELUNA_LOG_ERROR("[Eluna]: Error loading `%s`", it->filepath.c_str()); Report(L); // Stack: package, modules continue; } // Stack: package, modules, filefunc if (ExecuteCall(0, 1)) { // Stack: package, modules, result if (lua_isnoneornil(L, -1) || (lua_isboolean(L, -1) && !lua_toboolean(L, -1))) { // if result evaluates to false, change it to true lua_pop(L, 1); Push(L, true); } lua_setfield(L, modules, it->filename.c_str()); // Stack: package, modules // successfully loaded and ran file ELUNA_LOG_DEBUG("[Eluna]: Successfully loaded `%s`", it->filepath.c_str()); ++count; continue; } } // Stack: package, modules lua_pop(L, 2); ELUNA_LOG_INFO("[Eluna]: Executed %u Lua scripts in %u ms", count, ElunaUtil::GetTimeDiff(oldMSTime)); OnLuaStateOpen(); }