// 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 Eluna::Initialize() { uint32 oldMSTime = GetCurrTime(); lua_scripts.clear(); lua_extensions.clear(); lua_folderpath = eConfigMgr->GetStringDefault("Eluna.ScriptPath", "lua_scripts"); #if PLATFORM == PLATFORM_UNIX || PLATFORM == PLATFORM_APPLE if (lua_folderpath[0] == '~') if (const char* home = getenv("HOME")) lua_folderpath.replace(0, 1, home); #endif ELUNA_LOG_INFO("[Eluna]: Searching scripts from `%s`", lua_folderpath.c_str()); GetScripts(lua_folderpath + "/extensions", lua_extensions); GetScripts(lua_folderpath, lua_scripts); ELUNA_LOG_DEBUG("[Eluna]: Loaded %u scripts in %u ms", uint32(lua_scripts.size()), GetTimeDiff(oldMSTime)); // Create global eluna new Eluna(); }
// Finds lua script files from given path (including subdirectories) and pushes them to scripts void Eluna::GetScripts(std::string path, ScriptPaths& 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()); std::string ext = fullpath.substr(fullpath.length() - 4, 4); if (ext != ".lua" && ext != ".dll") continue; // was correct, add path to scripts to load ELUNA_LOG_DEBUG("[Eluna]: GetScripts add path `%s`", fullpath.c_str()); scripts.erase(fullpath); scripts.insert(fullpath); } }
void Eluna::LoadScriptPaths() { uint32 oldMSTime = ElunaUtil::GetCurrTime(); lua_scripts.clear(); lua_extensions.clear(); lua_folderpath = eConfigMgr->GetStringDefault("Eluna.ScriptPath", "lua_scripts"); #if PLATFORM == PLATFORM_UNIX || PLATFORM == PLATFORM_APPLE if (lua_folderpath[0] == '~') if (const char* home = getenv("HOME")) lua_folderpath.replace(0, 1, home); #endif ELUNA_LOG_INFO("[Eluna]: Searching scripts from `%s`", lua_folderpath.c_str()); lua_requirepath.clear(); GetScripts(lua_folderpath); // Erase last ; if (!lua_requirepath.empty()) lua_requirepath.erase(lua_requirepath.end() - 1); ELUNA_LOG_DEBUG("[Eluna]: Loaded %u scripts in %u ms", uint32(lua_scripts.size() + lua_extensions.size()), ElunaUtil::GetTimeDiff(oldMSTime)); }
// Finds lua script files from given path (including subdirectories) and pushes them to scripts void Eluna::GetScripts(std::string path) { ELUNA_LOG_DEBUG("[Eluna]: GetScripts from path `%s`", path.c_str()); #ifdef USING_BOOST boost::filesystem::path someDir(path); boost::filesystem::directory_iterator end_iter; if (boost::filesystem::exists(someDir) && boost::filesystem::is_directory(someDir)) { lua_requirepath += path + "/?;" + path + "/?.lua;" + path + "/?.ext;" + path + "/?.dll;" + path + "/?.so;"; for (boost::filesystem::directory_iterator dir_iter(someDir); dir_iter != end_iter; ++dir_iter) { std::string fullpath = dir_iter->path().generic_string(); // Check if file is hidden #ifdef WIN32 DWORD dwAttrib = GetFileAttributes(fullpath.c_str()); if (dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_HIDDEN)) continue; #endif #ifdef UNIX std::string name = dir_iter->path().filename().generic_string().c_str(); if (name != ".." || name != "." || name[0] == '.') continue; #endif // load subfolder if (boost::filesystem::is_directory(dir_iter->status())) { GetScripts(fullpath); continue; } if (boost::filesystem::is_regular_file(dir_iter->status())) { // was file, try add std::string filename = dir_iter->path().filename().generic_string(); AddScriptPath(filename, fullpath); } } } #else ACE_Dirent dir; if (dir.open(path.c_str()) == -1) // Error opening directory, return return; lua_requirepath += path + "/?;" + path + "/?.lua;" + path + "/?.ext;" + path + "/?.dll;" + path + "/?.so;"; 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; // Check if file is hidden #ifdef WIN32 DWORD dwAttrib = GetFileAttributes(fullpath.c_str()); if (dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_HIDDEN)) continue; #endif #ifdef UNIX std::string name = directory->d_name.c_str(); if (name != ".." || name != "." || name[0] == '.') continue; #endif 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); continue; } // was file, try add std::string filename = directory->d_name; AddScriptPath(filename, fullpath); } #endif }