From ba5b6ca75187e18942c463f7300b49061ebdc183 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Sun, 25 Mar 2012 14:24:51 +0000 Subject: A globally-accessible OS-independent GetDirectoryContents() function for listing all objects in a folder as an AStringList git-svn-id: http://mc-server.googlecode.com/svn/trunk@433 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/Bindings.cpp | 4 ++- source/Bindings.h | 2 +- source/StringUtils.cpp | 44 ++++++++++++++++++++++++++++++++ source/StringUtils.h | 5 ++++ source/cFileFormatUpdater.cpp | 58 ++++--------------------------------------- source/cPlugin_NewLua.cpp | 53 +++++++++++++++++---------------------- 6 files changed, 81 insertions(+), 85 deletions(-) diff --git a/source/Bindings.cpp b/source/Bindings.cpp index 8e3c4bd96..26af3e663 100644 --- a/source/Bindings.cpp +++ b/source/Bindings.cpp @@ -1,6 +1,6 @@ /* ** Lua binding: AllToLua -** Generated automatically by tolua++-1.0.92 on 03/11/12 17:55:41. +** Generated automatically by tolua++-1.0.92 on 03/25/12 16:23:48. */ #ifndef __cplusplus @@ -17574,6 +17574,7 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S) tolua_constant(tolua_S,"E_ENT_LOOK",E_ENT_LOOK); tolua_constant(tolua_S,"E_REL_ENT_MOVE_LOOK",E_REL_ENT_MOVE_LOOK); tolua_constant(tolua_S,"E_ENT_TELEPORT",E_ENT_TELEPORT); + tolua_constant(tolua_S,"E_ENT_HEAD_LOOK",E_ENT_HEAD_LOOK); tolua_constant(tolua_S,"E_ENT_STATUS",E_ENT_STATUS); tolua_constant(tolua_S,"E_METADATA",E_METADATA); tolua_constant(tolua_S,"E_PRE_CHUNK",E_PRE_CHUNK); @@ -17594,6 +17595,7 @@ TOLUA_API int tolua_AllToLua_open (lua_State* tolua_S) tolua_constant(tolua_S,"E_CREATIVE_INVENTORY_ACTION",E_CREATIVE_INVENTORY_ACTION); tolua_constant(tolua_S,"E_UPDATE_SIGN",E_UPDATE_SIGN); tolua_constant(tolua_S,"E_PLAYER_LIST_ITEM",E_PLAYER_LIST_ITEM); + tolua_constant(tolua_S,"E_PLAYER_ABILITIES",E_PLAYER_ABILITIES); tolua_constant(tolua_S,"E_PING",E_PING); tolua_constant(tolua_S,"E_DISCONNECT",E_DISCONNECT); tolua_array(tolua_S,"g_BlockLightValue",tolua_get_AllToLua_g_BlockLightValue,tolua_set_AllToLua_g_BlockLightValue); diff --git a/source/Bindings.h b/source/Bindings.h index c100812cf..fc265fb0b 100644 --- a/source/Bindings.h +++ b/source/Bindings.h @@ -1,6 +1,6 @@ /* ** Lua binding: AllToLua -** Generated automatically by tolua++-1.0.92 on 03/11/12 17:55:42. +** Generated automatically by tolua++-1.0.92 on 03/25/12 16:23:48. */ /* Exported function */ diff --git a/source/StringUtils.cpp b/source/StringUtils.cpp index c9dbb1113..1e629c25f 100644 --- a/source/StringUtils.cpp +++ b/source/StringUtils.cpp @@ -159,3 +159,47 @@ void ReplaceString(AString & iHayStack, const AString & iNeedle, const AString & +AStringList GetDirectoryContents(const char * a_Directory) +{ + AStringList AllFiles; + + #ifdef _WIN32 + + AString FileFilter = AString(a_Directory) + "*.*"; + HANDLE hFind; + WIN32_FIND_DATA FindFileData; + + if ((hFind = FindFirstFile(FileFilter.c_str(), &FindFileData)) != INVALID_HANDLE_VALUE) + { + do + { + AllFiles.push_back(FindFileData.cFileName); + } while (FindNextFile(hFind, &FindFileData)); + FindClose(hFind); + } + + #else // _WIN32 + + DIR * dp; + struct dirent *dirp; + if ((dp = opendir(a_Directory)) == NULL) + { + LOGERROR("Error (%i) opening %s\n", errno, a_Directory ); + } + else + { + while ((dirp = readdir(dp)) != NULL) + { + AllFiles.push_back(dirp->d_name); + } + closedir(dp); + } + + #endif // else _WIN32 + + return AllFiles; +} + + + + diff --git a/source/StringUtils.h b/source/StringUtils.h index 8805c1d99..262c01e49 100644 --- a/source/StringUtils.h +++ b/source/StringUtils.h @@ -15,6 +15,7 @@ typedef std::string AString; typedef std::vector AStringVector; +typedef std::list AStringList; @@ -41,6 +42,10 @@ extern int NoCaseCompare(const AString & s1, const AString & s2); /// Replaces *each* occurence of iNeedle in iHayStack with iReplaceWith extern void ReplaceString(AString & iHayStack, const AString & iNeedle, const AString & iReplaceWith); +/// Returns the list of all items in the specified directory (files, folders, nix pipes, whatever's there) +extern AStringList GetDirectoryContents(const char * a_Directory); + + // If you have any other string helper functions, declare them here diff --git a/source/cFileFormatUpdater.cpp b/source/cFileFormatUpdater.cpp index d17a35bb0..b58239eb2 100644 --- a/source/cFileFormatUpdater.cpp +++ b/source/cFileFormatUpdater.cpp @@ -12,13 +12,6 @@ -typedef std::list< std::string > StringList; -StringList GetDirectoryContents( const char* a_Directory ); - - - - - void cFileFormatUpdater::UpdateFileFormat() { UpdatePlayersOfWorld("world"); @@ -31,13 +24,13 @@ void cFileFormatUpdater::UpdateFileFormat() // Convert player .bin files to JSON void cFileFormatUpdater::UpdatePlayersOfWorld( const char* a_WorldName ) { - std::string PlayerDir = std::string( a_WorldName ) + "/player/"; + AString PlayerDir = AString( a_WorldName ) + "/player/"; - StringList AllFiles = GetDirectoryContents( PlayerDir.c_str() ); - for( StringList::iterator itr = AllFiles.begin(); itr != AllFiles.end(); ++itr ) + AStringList AllFiles = GetDirectoryContents( PlayerDir.c_str() ); + for (AStringList::iterator itr = AllFiles.begin(); itr != AllFiles.end(); ++itr ) { - std::string & FileName = *itr; - if( FileName.rfind(".bin") != std::string::npos ) // Get only the files ending in .bin + AString & FileName = *itr; + if (FileName.rfind(".bin") != AString::npos) // Get only the files ending in .bin { PlayerBINtoJSON( (PlayerDir + FileName).c_str() ); } @@ -148,44 +141,3 @@ void cFileFormatUpdater::PlayerBINtoJSON( const char* a_FileName ) - -// Helper function -StringList GetDirectoryContents( const char* a_Directory ) -{ - StringList AllFiles; -#ifdef _WIN32 - std::string FileFilter = std::string( a_Directory ) + "*.*"; - HANDLE hFind; - WIN32_FIND_DATA FindFileData; - - if( ( hFind = FindFirstFile(FileFilter.c_str(), &FindFileData) ) != INVALID_HANDLE_VALUE) - { - do - { - AllFiles.push_back( FindFileData.cFileName ); - } while( FindNextFile(hFind, &FindFileData) ); - FindClose(hFind); - } -#else - DIR *dp; - struct dirent *dirp; - if( (dp = opendir( a_Directory ) ) == NULL) - { - LOGERROR("Error (%i) opening %s\n", errno, a_Directory ); - } - else - { - while ((dirp = readdir(dp)) != NULL) - { - AllFiles.push_back( dirp->d_name ); - } - closedir(dp); - } -#endif - - return AllFiles; -} - - - - diff --git a/source/cPlugin_NewLua.cpp b/source/cPlugin_NewLua.cpp index a09005a6e..bae24d0ff 100644 --- a/source/cPlugin_NewLua.cpp +++ b/source/cPlugin_NewLua.cpp @@ -16,7 +16,7 @@ extern "C" #include "ManualBindings.h" #ifdef _WIN32 -#include "wdirent.h" +// #include "wdirent.h" #else #include #endif @@ -59,38 +59,32 @@ bool cPlugin_NewLua::Initialize() std::string PluginPath = std::string("Plugins/") + m_Directory + "/"; // Load all files for this plugin, and execute them - DIR* dp; - struct dirent *entry; - if(dp = opendir( PluginPath.c_str() )) + AStringList Files = GetDirectoryContents(PluginPath.c_str()); + for (AStringList::const_iterator itr = Files.begin(); itr != Files.end(); ++itr) { - while(entry = readdir(dp)) + if (itr->rfind(".lua") == AString::npos) { - std::string FileName = entry->d_name; - if( FileName.find(".lua") != std::string::npos ) - { - std::string Path = PluginPath + FileName; - int s = luaL_loadfile(m_LuaState, Path.c_str() ); - if( report_errors( m_LuaState, s ) ) - { - LOGERROR("Can't load plugin %s because of an error in file %s", m_Directory.c_str(), Path.c_str() ); - lua_close( m_LuaState ); - m_LuaState = 0; - return false; - } - - s = lua_pcall(m_LuaState, 0, LUA_MULTRET, 0); - if( report_errors( m_LuaState, s ) ) - { - LOGERROR("Error in plugin %s in file %s", m_Directory.c_str(), Path.c_str() ); - lua_close( m_LuaState ); - m_LuaState = 0; - return false; - } - } + continue; + } + AString Path = PluginPath + *itr; + int s = luaL_loadfile(m_LuaState, Path.c_str() ); + if( report_errors( m_LuaState, s ) ) + { + LOGERROR("Can't load plugin %s because of an error in file %s", m_Directory.c_str(), Path.c_str() ); + lua_close( m_LuaState ); + m_LuaState = 0; + return false; } - closedir( dp ); - } + s = lua_pcall(m_LuaState, 0, LUA_MULTRET, 0); + if( report_errors( m_LuaState, s ) ) + { + LOGERROR("Error in plugin %s in file %s", m_Directory.c_str(), Path.c_str() ); + lua_close( m_LuaState ); + m_LuaState = 0; + return false; + } + } // for itr - Files[] // Call intialize function if( !PushFunction("Initialize") ) @@ -101,7 +95,6 @@ bool cPlugin_NewLua::Initialize() } tolua_pushusertype(m_LuaState, this, "cPlugin_NewLua"); - if( !CallFunction(1, 1, "Initialize") ) { -- cgit v1.2.3