From 093d22ce8b99654f76e57783a822760e4a7fc2d2 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Mon, 14 May 2012 03:36:43 +0000 Subject: [PATCH] - oh how I despise windows, another evil hack to get proper detection of vlc plugins --- source/glest_game/main/intro.cpp | 3 +- source/glest_game/main/main.cpp | 70 +++++++++ .../include/graphics/video_player.h | 3 +- .../include/platform/sdl/platform_util.h | 1 + .../sources/graphics/video_player.cpp | 142 ++++++++++-------- .../sources/platform/win32/platform_util.cpp | 26 ++++ 6 files changed, 181 insertions(+), 64 deletions(-) diff --git a/source/glest_game/main/intro.cpp b/source/glest_game/main/intro.cpp index 8aaaa255..26f92a77 100644 --- a/source/glest_game/main/intro.cpp +++ b/source/glest_game/main/intro.cpp @@ -506,7 +506,8 @@ Intro::Intro(Program *program): screen->w, screen->h, screen->format->BitsPerPixel, - vlcPluginsPath); + vlcPluginsPath, + SystemFlags::VERBOSE_MODE_ENABLED); player.PlayVideo(); return; } diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index 4ccd737c..29efb7f7 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -11,6 +11,7 @@ #ifdef WIN32 #include #include + #include #endif #include "math_wrapper.h" @@ -2725,6 +2726,75 @@ int glestMain(int argc, char** argv) { // printf("END ALLOC char 200\n"); // return -1; + +#if defined(WIN32) + //printf("*** VLC START ****\n"); + //if(verboseEnabled) printf("Trying [%s]\n",getenv("VLC_PLUGIN_PATH")); + if(getenv("VLC_PLUGIN_PATH") == NULL) { + //printf("*** VLC C ****\n"); + + // For windows check registry for install path + std::string strValue = getRegKey("Software\\VideoLAN\\VLC", "InstallDir"); + if(strValue != "") { + if(strValue.length() >= 2) { + if(strValue[0] == '"') { + strValue = strValue.erase(0); + } + if(strValue[strValue.length()-1] == '"') { + strValue = strValue.erase(strValue.length()-1); + } + if(strValue[strValue.length()-1] != '\\') { + strValue += "\\"; + } + } + strValue += "plugins"; + string newstrValue = "VLC_PLUGIN_PATH=" + strValue; + _putenv(newstrValue.c_str()); + //bool result = SetEnvironmentVariableA("VLC_PLUGIN_PATH",strValue.c_str()); + + //Open the registry key. + wstring subKey = L"Environment"; + HKEY keyHandle; + DWORD dwDisposition; + RegCreateKeyEx(HKEY_CURRENT_USER,subKey.c_str(),0, NULL, 0, KEY_ALL_ACCESS, NULL, &keyHandle, &dwDisposition); + //Set the value. + std::auto_ptr wstr(Ansi2WideString(strValue.c_str())); + + wstring vlcPluginPath = wstring(wstr.get()); + DWORD len = (DWORD) sizeof(wchar_t) * vlcPluginPath.length() + 1; + RegSetValueEx(keyHandle, L"VLC_PLUGIN_PATH", 0, REG_SZ, (PBYTE)vlcPluginPath.c_str(), len); + RegCloseKey(keyHandle); + + subKey = L"System\\CurrentControlSet\\Control\\Session Manager\\Environment"; + RegCreateKeyEx(HKEY_LOCAL_MACHINE,subKey.c_str(),0, NULL, 0, KEY_ALL_ACCESS, NULL, &keyHandle, &dwDisposition); + //Set the value. + wstr.reset(Ansi2WideString(strValue.c_str())); + vlcPluginPath = wstring(wstr.get()); + len = (DWORD) sizeof(wchar_t) * vlcPluginPath.length() + 1; + RegSetValueEx(keyHandle, L"VLC_PLUGIN_PATH", 0, REG_SZ, (PBYTE)vlcPluginPath.c_str(), len); + RegCloseKey(keyHandle); + + RegFlushKey(keyHandle); + + string test = "SET " + newstrValue; + system(test.c_str()); + DWORD dwReturnValue=0; + LRESULT error1 = SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM) + L"Environment", SMTO_ABORTIFHUNG, 5000, &dwReturnValue); + //SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM) "Environment", SMTO_ABORTIFHUNG, 10000, NULL ); + //SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM) L"Environment", SMTO_ABORTIFHUNG, 10000, NULL ); + //SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM) TEXT("Environment"), SMTO_ABORTIFHUNG,10000, &dwReturnValue); + + //printf("*** VLC D [%s] error1 = %d dwReturnValue = %d ****\n",getenv("VLC_PLUGIN_PATH"),error1,dwReturnValue); + //system(argv[0]); + const char *const *newargv = &argv[1]; + _execv( argv[0], newargv ); + return 0; + } + } +#endif + + PlatformExceptionHandler::application_binary= executable_path(argv[0],true); mg_app_name = GameConstants::application_name; mailStringSupport = mailString; diff --git a/source/shared_lib/include/graphics/video_player.h b/source/shared_lib/include/graphics/video_player.h index eba91e73..86f52152 100644 --- a/source/shared_lib/include/graphics/video_player.h +++ b/source/shared_lib/include/graphics/video_player.h @@ -26,12 +26,13 @@ protected: int height; int colorBits; string pluginsPath; + bool verboseEnabled; bool stop; public: VideoPlayer(string filename, SDL_Surface *surface, int width, int height, - int colorBits, string pluginsPath); + int colorBits, string pluginsPath,bool verboseEnabled=false); virtual ~VideoPlayer(); void PlayVideo(); diff --git a/source/shared_lib/include/platform/sdl/platform_util.h b/source/shared_lib/include/platform/sdl/platform_util.h index 8ecd9837..2e773e68 100644 --- a/source/shared_lib/include/platform/sdl/platform_util.h +++ b/source/shared_lib/include/platform/sdl/platform_util.h @@ -90,6 +90,7 @@ LONG WINAPI UnhandledExceptionFilter2(struct _EXCEPTION_POINTERS *ExceptionInfo) LPWSTR Ansi2WideString(LPCSTR lpaszString); std::string utf8_encode(const std::wstring wstr); std::wstring utf8_decode(const std::string &str); +std::string getRegKey(const std::string& location, const std::string& name); void message(string message); bool ask(string message); diff --git a/source/shared_lib/sources/graphics/video_player.cpp b/source/shared_lib/sources/graphics/video_player.cpp index 6ee5e489..e3fb4168 100644 --- a/source/shared_lib/sources/graphics/video_player.cpp +++ b/source/shared_lib/sources/graphics/video_player.cpp @@ -14,6 +14,7 @@ #include #include #include + #if defined(WIN32) #include #endif @@ -22,6 +23,35 @@ #include #endif +#if defined(WIN32) +/** +* @param location The location of the registry key. For example "Software\\Bethesda Softworks\\Morrowind" +* @param name the name of the registry key, for example "Installed Path" +* @return the value of the key or an empty string if an error occured. +*/ +std::string getRegKey(const std::string& location, const std::string& name){ + HKEY key; + CHAR value[1024]; + DWORD bufLen = 1024*sizeof(CHAR); + long ret; + ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, location.c_str(), 0, KEY_QUERY_VALUE, &key); + if( ret != ERROR_SUCCESS ){ + return std::string(); + } + ret = RegQueryValueExA(key, name.c_str(), 0, 0, (LPBYTE) value, &bufLen); + RegCloseKey(key); + if ( (ret != ERROR_SUCCESS) || (bufLen > 1024*sizeof(TCHAR)) ){ + return std::string(); + } + string stringValue = value; + size_t i = stringValue.length(); + while( i > 0 && stringValue[i-1] == '\0' ){ + --i; + } + return stringValue.substr(0,i); +} +#endif + struct ctx { GLuint textureId; // Texture ID SDL_Surface *surf; @@ -77,13 +107,16 @@ static void display(void *data, void *id) { } VideoPlayer::VideoPlayer(string filename, SDL_Surface *surface, - int width, int height,int colorBits,string pluginsPath) { + int width, int height,int colorBits,string pluginsPath, + bool verboseEnabled) { this->filename = filename; this->surface = surface; this->width = width; this->height = height; this->colorBits = colorBits; this->pluginsPath = pluginsPath; + this->verboseEnabled = verboseEnabled; + this->stop = false; } @@ -112,7 +145,10 @@ void VideoPlayer::PlayVideo() { std::vector vlc_argv; vlc_argv.push_back("--no-xlib" /* tell VLC to not use Xlib */); vlc_argv.push_back("--no-video-title-show"); -// vlc_argv.push_back(pluginParam.c_str()); + if(verboseEnabled) vlc_argv.push_back("--verbose=2"); +#if defined(WIN32) + if(verboseEnabled) _putenv("VLC_VERBOSE=2"); +#endif int vlc_argc = vlc_argv.size(); // char const *vlc_argv[] = @@ -155,73 +191,55 @@ void VideoPlayer::PlayVideo() { /* * Initialise libVLC */ + if(verboseEnabled) printf("Trying [%s]\n",getenv("VLC_PLUGIN_PATH")); libvlc = libvlc_new(vlc_argc, &vlc_argv[0]); +#if defined(WIN32) if(libvlc == NULL) { // For windows check registry for install path -#if defined(WIN32) && !defined(__MINGW32__) - //Open the registry key. - wstring subKey = L"Software\\VideoLAN\\VLC"; - HKEY keyHandle; - //DWORD dwDisposition; - LONG lres = RegOpenKeyEx(HKEY_LOCAL_MACHINE,subKey.c_str(),0, KEY_READ, &keyHandle); - if(lres == ERROR_SUCCESS) { - std::string strValue; - //GetStringRegKey(keyHandle, L"", strValue, L""); - char szBuffer[4096]; - DWORD dwBufferSize = sizeof(szBuffer); - ULONG nError = RegQueryValueEx(keyHandle, L"", 0, NULL, (LPBYTE)szBuffer, &dwBufferSize); - if (ERROR_SUCCESS == nError) { - strValue = szBuffer; - } - - RegCloseKey(keyHandle); - - if(strValue != "") { -#if defined(WIN32) - strValue = "VLC_PLUGIN_PATH=" + strValue; - _putenv(strValue.c_str()); -#else - setenv("VLC_PLUGIN_PATH",strValue.c_str(),1); -#endif - libvlc = libvlc_new(vlc_argc, &vlc_argv[0]); - } - } -#endif - - if(libvlc == NULL) { -#if defined(WIN32) - _putenv("VLC_PLUGIN_PATH=c:\\program files\\videolan\\vlc\\plugins"); -#else - setenv("VLC_PLUGIN_PATH","c:\\program files\\videolan\\vlc\\plugins",1); -#endif - libvlc = libvlc_new(vlc_argc, &vlc_argv[0]); - } - if(libvlc == NULL) { -#if defined(WIN32) - _putenv("VLC_PLUGIN_PATH=\\program files\\videolan\\vlc\\plugins"); -#else - setenv("VLC_PLUGIN_PATH","\\program files\\videolan\\vlc\\plugins",1); -#endif - libvlc = libvlc_new(vlc_argc, &vlc_argv[0]); - if(libvlc == NULL) { -#if defined(WIN32) - _putenv("VLC_PLUGIN_PATH=c:\\program files (x86)\\videolan\\vlc\\plugins"); -#else - setenv("VLC_PLUGIN_PATH","c:\\program files (x86)\\videolan\\vlc\\plugins",1); -#endif - libvlc = libvlc_new(vlc_argc, &vlc_argv[0]); - - if(libvlc == NULL) { -#if defined(WIN32) - _putenv("VLC_PLUGIN_PATH=\\program files (x86)\\videolan\\vlc\\plugins"); -#else - setenv("VLC_PLUGIN_PATH","\\program files (x86)\\videolan\\vlc\\plugins",1); -#endif - libvlc = libvlc_new(vlc_argc, &vlc_argv[0]); + std::string strValue = getRegKey("Software\\VideoLAN\\VLC", "InstallDir"); + if(strValue != "") { + if(strValue.length() >= 2) { + if(strValue[0] == '"') { + strValue = strValue.erase(0); + } + if(strValue[strValue.length()-1] == '"') { + strValue = strValue.erase(strValue.length()-1); } } + strValue = "VLC_PLUGIN_PATH=" + strValue; + _putenv(strValue.c_str()); + + if(verboseEnabled) printf("Trying [%s]\n",getenv("VLC_PLUGIN_PATH")); + libvlc = libvlc_new(vlc_argc, &vlc_argv[0]); + } + + if(libvlc == NULL) { + _putenv("VLC_PLUGIN_PATH=c:\\program files\\videolan\\vlc\\plugins"); + + if(verboseEnabled) printf("Trying [%s]\n",getenv("VLC_PLUGIN_PATH")); + libvlc = libvlc_new(vlc_argc, &vlc_argv[0]); + } + if(libvlc == NULL) { + _putenv("VLC_PLUGIN_PATH=\\program files\\videolan\\vlc\\plugins"); + + if(verboseEnabled) printf("Trying [%s]\n",getenv("VLC_PLUGIN_PATH")); + libvlc = libvlc_new(vlc_argc, &vlc_argv[0]); + } + if(libvlc == NULL) { + _putenv("VLC_PLUGIN_PATH=c:\\program files (x86)\\videolan\\vlc\\plugins"); + + if(verboseEnabled) printf("Trying [%s]\n",getenv("VLC_PLUGIN_PATH")); + libvlc = libvlc_new(vlc_argc, &vlc_argv[0]); + } + if(libvlc == NULL) { + _putenv("VLC_PLUGIN_PATH=\\program files (x86)\\videolan\\vlc\\plugins"); + + if(verboseEnabled) printf("Trying [%s]\n",getenv("VLC_PLUGIN_PATH")); + libvlc = libvlc_new(vlc_argc, &vlc_argv[0]); } } +#endif + if(libvlc != NULL) { m = libvlc_media_new_path(libvlc, filename.c_str()); mp = libvlc_media_player_new_from_media(m); diff --git a/source/shared_lib/sources/platform/win32/platform_util.cpp b/source/shared_lib/sources/platform/win32/platform_util.cpp index 62ca4eeb..86d589af 100644 --- a/source/shared_lib/sources/platform/win32/platform_util.cpp +++ b/source/shared_lib/sources/platform/win32/platform_util.cpp @@ -75,6 +75,32 @@ std::wstring utf8_decode(const std::string &str) { return wstrTo; } +/** +* @param location The location of the registry key. For example "Software\\Bethesda Softworks\\Morrowind" +* @param name the name of the registry key, for example "Installed Path" +* @return the value of the key or an empty string if an error occured. +*/ +std::string getRegKey(const std::string& location, const std::string& name){ + HKEY key; + CHAR value[1024]; + DWORD bufLen = 1024*sizeof(CHAR); + long ret; + ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, location.c_str(), 0, KEY_QUERY_VALUE, &key); + if( ret != ERROR_SUCCESS ){ + return std::string(); + } + ret = RegQueryValueExA(key, name.c_str(), 0, 0, (LPBYTE) value, &bufLen); + RegCloseKey(key); + if ( (ret != ERROR_SUCCESS) || (bufLen > 1024*sizeof(TCHAR)) ){ + return std::string(); + } + string stringValue = value; + size_t i = stringValue.length(); + while( i > 0 && stringValue[i-1] == '\0' ){ + --i; + } + return stringValue.substr(0,i); +} LONG WINAPI PlatformExceptionHandler::handler(LPEXCEPTION_POINTERS pointers){