gen.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include "main.h"
  2. #include "gen.h"
  3. #include <vector>
  4. #include "../nu/AutoWide.h"
  5. #include "..\WAT\wa_logger.h"
  6. std::vector<winampGeneralPurposePlugin*> gen_plugins;
  7. extern LARGE_INTEGER freq;
  8. int got_ml = 0;
  9. typedef struct _PLUGINORDER
  10. {
  11. LPCWSTR name;
  12. bool found;
  13. } PLUGINORDER;
  14. static PLUGINORDER preload[] =
  15. {
  16. { L"gen_crasher.dll", false }, //
  17. { L"gen_ff.dll", false }, //
  18. { L"gen_hotkeys.dll", false }, //
  19. { L"gen_tray.dll", false }, //
  20. { L"gen_ml.dll", false }, //
  21. { L"gen_jumpex.dll", false },
  22. };
  23. void LoadGenPlugin(const wchar_t* filename)
  24. {
  25. wchar_t file[MAX_PATH] = { 0 };
  26. PathCombineW(file, PLUGINDIR, filename);
  27. if (!wa::files::file_exists(file))
  28. {
  29. wsprintfW( _log_message_w, L"The plugin '%s' is not found in the \"Plugins\" folder!", filename );
  30. LOG_ERROR( _log_message_w );
  31. return;
  32. }
  33. HMODULE hLib = LoadLibraryW(file);
  34. if (hLib == NULL)
  35. {
  36. DWORD l_error_code = ::GetLastError();
  37. wsprintfW( _log_message_w, L"Error when loading the plugin '%s'! Error code : %d!", filename, l_error_code );
  38. LOG_ERROR( _log_message_w );
  39. return;
  40. }
  41. winampGeneralPurposePlugin* (*pr)();
  42. pr = (winampGeneralPurposePlugin * (__cdecl*)(void)) GetProcAddress(hLib, "winampGetGeneralPurposePlugin");
  43. if ( pr == NULL )
  44. {
  45. wsprintfW( _log_message_w, L"No entry point found for the plugin '%s'!", filename );
  46. LOG_ERROR( _log_message_w );
  47. FreeModule(hLib);
  48. return;
  49. }
  50. winampGeneralPurposePlugin* plugin = pr();
  51. if ( plugin && (plugin->version == GPPHDR_VER || plugin->version == GPPHDR_VER_U) )
  52. {
  53. wsprintfW( _log_message_w, L"The plugin '%s' is correctly loaded!", filename );
  54. LOG_DEBUG( _log_message_w );
  55. if ( g_safeMode )
  56. {
  57. char desc[128] = { 0 };
  58. lstrcpynA(desc, plugin->description, sizeof(desc));
  59. if ( desc[0] && !memcmp(desc, "nullsoft(", 9) )
  60. {
  61. char* p = strrchr(desc, ')');
  62. if ( p )
  63. {
  64. *p = 0;
  65. if ( _wcsicmp(filename, AutoWide(desc + 9)) )
  66. {
  67. FreeModule(hLib);
  68. return;
  69. }
  70. }
  71. }
  72. else
  73. {
  74. // TODO need to look into making this into a controlled
  75. // list or something like that without the need for
  76. // a client update so it's possible for user to fix
  77. //
  78. // for some plug-ins, we sadly need to leave them loaded
  79. // otherwise they'll just cause Winamp to keep crashing!
  80. if ( _wcsicmp(PathFindFileNameW(filename), L"gen_Wake_up_call.dll") )
  81. {
  82. FreeModule(hLib);
  83. }
  84. return;
  85. }
  86. }
  87. plugin->hwndParent = hMainWindow;
  88. plugin->hDllInstance = hLib;
  89. if ( plugin->init() == GEN_INIT_SUCCESS )
  90. {
  91. wsprintfW( _log_message_w, L"The plugin '%s' is initialized!", filename );
  92. LOG_DEBUG( _log_message_w );
  93. if ( !_wcsicmp(filename, L"gen_ml.dll") )
  94. got_ml = 1;
  95. gen_plugins.push_back(plugin);
  96. }
  97. else
  98. {
  99. wsprintfW( _log_message_w, L"An error occurs when initializing the plugin '%s'!", filename );
  100. LOG_ERROR( _log_message_w );
  101. FreeModule( hLib );
  102. }
  103. }
  104. else
  105. {
  106. wsprintfW( _log_message_w, L"Either the plugin '%s' can't be loaded, either its version is incorrect!", filename );
  107. LOG_ERROR( _log_message_w );
  108. FreeModule( hLib );
  109. }
  110. }
  111. void load_genplugins()
  112. {
  113. int i = 0, count = sizeof(preload) / sizeof(PLUGINORDER);
  114. for ( ; i < count; i++ )
  115. LoadGenPlugin(preload[i].name);
  116. wchar_t dirstr[MAX_PATH] = { 0 };
  117. WIN32_FIND_DATAW d = { 0 };
  118. PathCombineW(dirstr, PLUGINDIR, L"GEN_*.DLL");
  119. HANDLE h = FindFirstFileW(dirstr, &d);
  120. if ( h != INVALID_HANDLE_VALUE )
  121. {
  122. do
  123. {
  124. for ( i = 0; i < count && (preload[i].found || lstrcmpiW(preload[i].name, d.cFileName)); i++ );
  125. if ( i == count )
  126. LoadGenPlugin(d.cFileName);
  127. else
  128. preload[i].found = true;
  129. } while ( FindNextFileW(h, &d) );
  130. FindClose(h);
  131. }
  132. }
  133. void unload_genplugins()
  134. {
  135. size_t x = gen_plugins.size();
  136. while ( x-- )
  137. {
  138. if ( gen_plugins[x] )
  139. {
  140. wchar_t filename[MAX_PATH] = { 0 };
  141. if ( !(config_no_visseh & 4) )
  142. {
  143. try
  144. {
  145. if ( gen_plugins[x]->quit )
  146. gen_plugins[x]->quit();
  147. }
  148. catch ( ... )
  149. {
  150. }
  151. }
  152. else
  153. {
  154. if ( gen_plugins[x]->quit )
  155. gen_plugins[x]->quit();
  156. }
  157. gen_plugins[x] = 0;
  158. }
  159. }
  160. try
  161. {
  162. gen_plugins.clear();
  163. }
  164. catch ( ... )
  165. {
  166. }
  167. }