1
0

plugin.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. #include "main.h"
  2. #include "ml.h"
  3. #include "itemlist.h"
  4. #include "../winamp/gen.h"
  5. #include "config.h"
  6. #include "../winamp/wa_ipc.h"
  7. #include "../winamp/ipc_pe.h"
  8. #include "resource.h"
  9. #include "comboskin.h"
  10. #include "../winamp/wa_dlg.h"
  11. #include "childwnd.h"
  12. #include "sendto.h"
  13. #include "api__gen_ml.h"
  14. #include "../nu/autowide.h"
  15. #include "../nu/autochar.h"
  16. #if defined(_WIN64)
  17. #include "../Elevator/IFileTypeRegistrar_64.h"
  18. #else
  19. #include "../Elevator/IFileTypeRegistrar_32.h"
  20. #endif
  21. #include <time.h>
  22. #include <shlwapi.h>
  23. #include <strsafe.h>
  24. #include "..\WAT\wa_logger.h"
  25. #define ListView_InsertColumnW(hwnd, iCol, pcol) \
  26. (int)SNDMSG((hwnd), LVM_INSERTCOLUMNW, (WPARAM)(int)(iCol), (LPARAM)(const LV_COLUMNW *)(pcol))
  27. #define ListView_InsertItemW(hwnd, pitem) \
  28. (int)SNDMSG((hwnd), LVM_INSERTITEMW, 0, (LPARAM)(const LV_ITEMW *)(pitem))
  29. #define ListView_SetItemTextW(hwndLV, i, iSubItem_, pszText_) \
  30. { LV_ITEMW _macro_lvi;\
  31. _macro_lvi.iSubItem = (iSubItem_);\
  32. _macro_lvi.pszText = (pszText_);\
  33. SNDMSG((hwndLV), LVM_SETITEMTEXTW, (WPARAM)(i), (LPARAM)(LV_ITEMW *)&_macro_lvi);\
  34. }
  35. #define ListView_SetItemW(hwnd, pitem) \
  36. (BOOL)SNDMSG((hwnd), LVM_SETITEMW, 0, (LPARAM)(LV_ITEMW *)(pitem))
  37. #define ListView_GetItemW(hwnd, pitem) \
  38. (BOOL)SNDMSG((hwnd), LVM_GETITEMW, 0, (LPARAM)(LV_ITEMW *)(pitem))
  39. extern "C" winampGeneralPurposePlugin plugin;
  40. C_ItemList m_plugins;
  41. static HCURSOR link_hand_cursor;
  42. LRESULT link_handlecursor(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  43. {
  44. LRESULT ret = CallWindowProcW((WNDPROC)GetPropW(hwndDlg, L"link_proc"), hwndDlg, uMsg, wParam, lParam);
  45. // override the normal cursor behaviour so we have a hand to show it is a link
  46. if ( uMsg == WM_SETCURSOR )
  47. {
  48. if ( (HWND)wParam == hwndDlg )
  49. {
  50. if ( !link_hand_cursor )
  51. {
  52. link_hand_cursor = LoadCursor(NULL, IDC_HAND);
  53. }
  54. SetCursor(link_hand_cursor);
  55. return TRUE;
  56. }
  57. }
  58. return ret;
  59. }
  60. void link_startsubclass(HWND hwndDlg, UINT id)
  61. {
  62. HWND ctrl = GetDlgItem(hwndDlg, id);
  63. SetPropW(ctrl, L"link_proc",
  64. (HANDLE)(LONG_PTR)SetWindowLongPtrW(ctrl, GWLP_WNDPROC, (LONGX86)(LONG_PTR)link_handlecursor));
  65. }
  66. void link_handledraw(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  67. {
  68. if ( uMsg == WM_DRAWITEM )
  69. {
  70. DRAWITEMSTRUCT* di = (DRAWITEMSTRUCT*)lParam;
  71. if ( di->CtlType == ODT_BUTTON )
  72. {
  73. wchar_t wt[123] = { 0 };
  74. int y;
  75. RECT r;
  76. HPEN hPen, hOldPen;
  77. GetDlgItemTextW(hwndDlg, (INT)wParam, wt, 123);
  78. // draw text
  79. SetTextColor(di->hDC, (di->itemState & ODS_SELECTED) ? RGB(220, 0, 0) : RGB(0, 0, 220));
  80. r = di->rcItem;
  81. r.left += 2;
  82. DrawTextW(di->hDC, wt, -1, &r, DT_VCENTER | DT_SINGLELINE);
  83. memset(&r, 0, sizeof(r));
  84. DrawTextW(di->hDC, wt, -1, &r, DT_SINGLELINE | DT_CALCRECT);
  85. // draw underline
  86. y = di->rcItem.bottom - ((di->rcItem.bottom - di->rcItem.top) - (r.bottom - r.top)) / 2 - 1;
  87. hPen = CreatePen(PS_SOLID, 0, (di->itemState & ODS_SELECTED) ? RGB(220, 0, 0) : RGB(0, 0, 220));
  88. hOldPen = (HPEN)SelectObject(di->hDC, hPen);
  89. MoveToEx(di->hDC, di->rcItem.left + 2, y, NULL);
  90. LineTo(di->hDC, di->rcItem.right + 2 - ((di->rcItem.right - di->rcItem.left) - (r.right - r.left)), y);
  91. SelectObject(di->hDC, hOldPen);
  92. DeleteObject(hPen);
  93. }
  94. }
  95. }
  96. /* In Winamp's preferences, Plugins->Media Library */
  97. static bool pluginsLoaded;
  98. INT_PTR CALLBACK PluginsProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  99. {
  100. if ( uMsg == WM_INITDIALOG )
  101. {
  102. pluginsLoaded = false;
  103. extern BOOL init2();
  104. if ( !g_hwnd ) init2();
  105. WIN32_FIND_DATAW d = { 0 };
  106. link_startsubclass(hwndDlg, IDC_PLUGINVERS);
  107. HWND listWindow = GetDlgItem(hwndDlg, IDC_GENLIB);
  108. if ( NULL != listWindow )
  109. {
  110. RECT r = { 0 }, rc = { 0 };
  111. GetWindowRect(listWindow, &r);
  112. GetClientRect(listWindow, &r);
  113. MapWindowPoints(listWindow, hwndDlg, (LPPOINT)&r, 2);
  114. InflateRect(&r, 2, 2);
  115. DestroyWindow(listWindow);
  116. listWindow = CreateWindowExW(WS_EX_NOPARENTNOTIFY | WS_EX_CLIENTEDGE, WC_LISTVIEWW, L"",
  117. WS_CHILD | WS_VISIBLE | WS_TABSTOP | LVS_REPORT | LVS_SINGLESEL |
  118. LVS_SHOWSELALWAYS | LVS_SORTASCENDING | LVS_NOCOLUMNHEADER | LVS_NOSORTHEADER,
  119. r.left, r.top, r.right - r.left, r.bottom - r.top,
  120. hwndDlg, (HMENU)IDC_GENLIB, NULL, NULL);
  121. SetWindowPos(listWindow, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOSENDCHANGING);
  122. ListView_SetExtendedListViewStyleEx(listWindow, LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP, LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP);
  123. SendMessage(listWindow, WM_SETFONT, SendMessage(hwndDlg, WM_GETFONT, 0, 0), FALSE);
  124. LVCOLUMNW lvc = { 0 };
  125. ListView_InsertColumnW(listWindow, 0, &lvc);
  126. ListView_InsertColumnW(listWindow, 1, &lvc);
  127. wchar_t filename[MAX_PATH] = { 0 }, description[512] = { 0 };
  128. for ( int x = 0; x < m_plugins.GetSize(); x++ )
  129. {
  130. winampMediaLibraryPlugin* mlplugin = (winampMediaLibraryPlugin*)m_plugins.Get(x);
  131. if ( mlplugin && mlplugin->hDllInstance )
  132. {
  133. wchar_t buf[512] = { 0 };
  134. LVITEMW lvi = { LVIF_TEXT | LVIF_PARAM, x, 0 };
  135. lstrcpynW(buf, (mlplugin->version >= MLHDR_VER_U ? (wchar_t*)mlplugin->description : AutoWide(mlplugin->description)), 512);
  136. lvi.pszText = buf;
  137. lvi.lParam = x;
  138. lvi.iItem = ListView_InsertItemW(listWindow, &lvi);
  139. GetModuleFileNameW(mlplugin->hDllInstance, filename, ARRAYSIZE(filename));
  140. PathStripPathW(filename);
  141. lvi.mask = LVIF_TEXT;
  142. lvi.iSubItem = 1;
  143. lvi.pszText = filename;
  144. ListView_SetItemW(listWindow, &lvi);
  145. }
  146. }
  147. wchar_t dirstr[MAX_PATH] = { 0 };
  148. PathCombineW(dirstr, pluginPath, L"ML_*.DLL");
  149. HANDLE h = FindFirstFileW(dirstr, &d);
  150. if ( h != INVALID_HANDLE_VALUE )
  151. {
  152. do
  153. {
  154. PathCombineW(dirstr, pluginPath, d.cFileName);
  155. HMODULE b = LoadLibraryExW(dirstr, NULL, LOAD_LIBRARY_AS_DATAFILE);
  156. int x = 0;
  157. for ( x = 0; b && (x != m_plugins.GetSize()); x++ )
  158. {
  159. winampMediaLibraryPlugin* mlplugin = (winampMediaLibraryPlugin*)m_plugins.Get(x);
  160. if ( mlplugin->hDllInstance == b )
  161. {
  162. break;
  163. }
  164. }
  165. if ( x == m_plugins.GetSize() || !b )
  166. {
  167. LVITEMW lvi = { LVIF_TEXT | LVIF_PARAM, x, 0 };
  168. lvi.pszText = d.cFileName;
  169. lvi.lParam = -2;
  170. lvi.iItem = ListView_InsertItemW(listWindow, &lvi);
  171. lvi.mask = LVIF_TEXT;
  172. lvi.iSubItem = 1;
  173. lvi.pszText = WASABI_API_LNGSTRINGW(IDS_NOT_LOADED);
  174. ListView_SetItemW(listWindow, &lvi);
  175. }
  176. FreeLibrary(b);
  177. } while ( FindNextFileW(h, &d) );
  178. FindClose(h);
  179. }
  180. GetClientRect(listWindow, &r);
  181. ListView_SetColumnWidth(listWindow, 1, LVSCW_AUTOSIZE);
  182. ListView_SetColumnWidth(listWindow, 0, (r.right - r.left) - ListView_GetColumnWidth(listWindow, 1));
  183. if ( NULL != WASABI_API_APP )
  184. WASABI_API_APP->DirectMouseWheel_EnableConvertToMouseWheel(listWindow, TRUE);
  185. pluginsLoaded = true;
  186. }
  187. }
  188. else if ( uMsg == WM_NOTIFY )
  189. {
  190. LPNMHDR p = (LPNMHDR)lParam;
  191. if ( p->idFrom == IDC_GENLIB )
  192. {
  193. if ( p->code == LVN_ITEMCHANGED )
  194. {
  195. LPNMLISTVIEW pnmv = (LPNMLISTVIEW)lParam;
  196. LVITEM lvi = { LVIF_PARAM, pnmv->iItem };
  197. if ( ListView_GetItem(p->hwndFrom, &lvi) && (pnmv->uNewState & LVIS_SELECTED) )
  198. {
  199. int loaded = (lvi.lParam != -2);
  200. if ( loaded )
  201. {
  202. winampMediaLibraryPlugin* mlplugin;
  203. if ( lvi.lParam >= 0 && lvi.lParam < m_plugins.GetSize() && (mlplugin = (winampMediaLibraryPlugin*)m_plugins.Get(lvi.lParam)) )
  204. {
  205. EnableWindow(GetDlgItem(hwndDlg, IDC_UNINST), !!mlplugin->hDllInstance);
  206. // enables / disables the config button as applicable instead of the
  207. // "This plug-in has no configuration implemented" message (opt-in)
  208. EnableWindow(GetDlgItem(hwndDlg, IDC_GENCONF), (!mlplugin->MessageProc(ML_MSG_NO_CONFIG, 0, 0, 0)));
  209. }
  210. }
  211. else
  212. {
  213. EnableWindow(GetDlgItem(hwndDlg, IDC_GENCONF), 0);
  214. }
  215. EnableWindow(GetDlgItem(hwndDlg, IDC_UNINST), 1);
  216. }
  217. else
  218. {
  219. EnableWindow(GetDlgItem(hwndDlg, IDC_GENCONF), 0);
  220. EnableWindow(GetDlgItem(hwndDlg, IDC_UNINST), 0);
  221. }
  222. }
  223. else if ( p->code == NM_DBLCLK )
  224. {
  225. PostMessage(hwndDlg, WM_COMMAND, MAKEWPARAM(IDC_GENCONF, 0), (LPARAM)GetDlgItem(hwndDlg, IDC_GENCONF));
  226. }
  227. }
  228. else if ( p->code == HDN_ITEMCHANGINGW )
  229. {
  230. if ( pluginsLoaded )
  231. {
  232. #if defined(_WIN64)
  233. SetWindowLong(hwndDlg, DWLP_MSGRESULT, TRUE);
  234. #else
  235. SetWindowLong(hwndDlg, DWL_MSGRESULT, TRUE);
  236. #endif
  237. return TRUE;
  238. }
  239. }
  240. }
  241. else if ( uMsg == WM_DESTROY )
  242. {
  243. HWND listWindow = GetDlgItem(hwndDlg, IDC_GENLIB);
  244. if ( IsWindow(listWindow) && (NULL != WASABI_API_APP) )
  245. WASABI_API_APP->DirectMouseWheel_EnableConvertToMouseWheel(listWindow, FALSE);
  246. }
  247. else if ( uMsg == WM_COMMAND )
  248. {
  249. switch ( LOWORD(wParam) )
  250. {
  251. case IDC_GENCONF:
  252. {
  253. if ( IsWindowEnabled(GetDlgItem(hwndDlg, IDC_GENCONF)) )
  254. {
  255. HWND listWindow = GetDlgItem(hwndDlg, IDC_GENLIB);
  256. LVITEM lvi = { LVIF_PARAM, ListView_GetSelectionMark(listWindow) };
  257. if ( ListView_GetItem(listWindow, &lvi) )
  258. {
  259. winampMediaLibraryPlugin* mlplugin;
  260. if ( lvi.lParam >= 0 && lvi.lParam < m_plugins.GetSize() && (mlplugin = (winampMediaLibraryPlugin*)m_plugins.Get(lvi.lParam)) )
  261. {
  262. if ( mlplugin->MessageProc && mlplugin->MessageProc(ML_MSG_CONFIG, (INT_PTR)hwndDlg, 0, 0) )
  263. {
  264. }
  265. else
  266. {
  267. wchar_t title[128] = { 0 };
  268. MessageBoxW(hwndDlg, WASABI_API_LNGSTRINGW(IDS_NO_CONFIG_PRESENT),
  269. WASABI_API_LNGSTRINGW_BUF(IDS_ML_PLUGIN_INFO, title, 128), MB_OK);
  270. }
  271. }
  272. }
  273. }
  274. }
  275. return FALSE;
  276. case IDC_UNINST:
  277. {
  278. if ( IsWindowEnabled(GetDlgItem(hwndDlg, IDC_UNINST)) )
  279. {
  280. HWND listWindow = GetDlgItem(hwndDlg, IDC_GENLIB);
  281. int which_sel = ListView_GetSelectionMark(listWindow);
  282. LVITEM lvi = { LVIF_PARAM, which_sel };
  283. if ( ListView_GetItem(listWindow, &lvi) )
  284. {
  285. winampMediaLibraryPlugin* mlplugin;
  286. wchar_t title[32] = { 0 };
  287. int msgBox = MessageBoxW(hwndDlg, WASABI_API_LNGSTRINGW(IDS_UNINSTALL_PROMPT),
  288. WASABI_API_LNGSTRINGW_BUF(IDS_UINSTALL_CONFIRMATION, title, 32),
  289. MB_YESNO | MB_ICONEXCLAMATION);
  290. if ( lvi.lParam >= 0 && lvi.lParam < m_plugins.GetSize() && (mlplugin = (winampMediaLibraryPlugin*)m_plugins.Get(lvi.lParam)) && msgBox == IDYES )
  291. {
  292. int ret = 0;
  293. int (*pr)(HINSTANCE hDllInst, HWND hwndDlg, int param);
  294. *(void**)&pr = (void*)GetProcAddress(mlplugin->hDllInstance, "winampUninstallPlugin");
  295. if ( pr )ret = pr(mlplugin->hDllInstance, hwndDlg, 0);
  296. // ok to uninstall but do with a full restart (default/needed in subclassing cases)
  297. if ( ret == ML_PLUGIN_UNINSTALL_REBOOT )
  298. {
  299. wchar_t buf[MAX_PATH] = { 0 };
  300. GetModuleFileNameW(mlplugin->hDllInstance, buf, MAX_PATH);
  301. WritePrivateProfileStringW(L"winamp", L"remove_genplug", buf, WINAMP_INI);
  302. WritePrivateProfileStringW(L"winamp", L"show_prefs", L"-1", WINAMP_INI);
  303. PostMessage(plugin.hwndParent, WM_WA_IPC, 0, IPC_RESTARTWINAMP);
  304. }
  305. // added from 5.37+ so we can do true on-the-fly removals (will fall back to default if fails)
  306. else if ( ret == ML_PLUGIN_UNINSTALL_NOW )
  307. {
  308. // get the filename before we free the dll otherwise things may go boom
  309. wchar_t buf[MAX_PATH] = { 0 };
  310. GetModuleFileNameW(mlplugin->hDllInstance, buf, MAX_PATH);
  311. mlplugin->quit();
  312. //if (mlplugin->hDllInstance) FreeLibrary(mlplugin->hDllInstance);
  313. m_plugins.Del(lvi.lParam);
  314. // try to use the elevator to do this
  315. IFileTypeRegistrar* registrar = (IFileTypeRegistrar*)SendMessage(plugin.hwndParent, WM_WA_IPC, 0, IPC_GET_FILEREGISTRAR_OBJECT);
  316. if ( registrar && (registrar != (IFileTypeRegistrar*)1) )
  317. {
  318. if ( registrar->DeleteItem(buf) != S_OK )
  319. {
  320. WritePrivateProfileStringW(L"winamp", L"remove_genplug", buf, WINAMP_INI);
  321. WritePrivateProfileStringW(L"winamp", L"show_prefs", L"-1", WINAMP_INI);
  322. PostMessage(plugin.hwndParent, WM_WA_IPC, 0, IPC_RESTARTWINAMP);
  323. }
  324. else
  325. ListView_DeleteItem(listWindow, which_sel);
  326. registrar->Release();
  327. }
  328. }
  329. }
  330. // will cope with not loaded plug-ins so we can still remove them, etc
  331. else if ( lvi.lParam == -2 && msgBox == IDYES )
  332. {
  333. wchar_t buf[1024] = { 0 }, base[1024] = { 0 };
  334. GetModuleFileNameW(plugin.hDllInstance, base, sizeof(base) / sizeof(wchar_t));
  335. LVITEMW lvi = { LVIF_TEXT, which_sel };
  336. lvi.pszText = buf;
  337. lvi.cchTextMax = ARRAYSIZE(buf);
  338. ListView_GetItemW(listWindow, &lvi);
  339. wchar_t* p = wcschr(buf, L'.');
  340. if ( p && *p == L'.' )
  341. {
  342. p += 4;
  343. *p = 0;
  344. PathRemoveFileSpecW(base);
  345. PathAppendW(base, buf);
  346. }
  347. // try to use the elevator to do this
  348. IFileTypeRegistrar* registrar = (IFileTypeRegistrar*)SendMessage(plugin.hwndParent, WM_WA_IPC, 0, IPC_GET_FILEREGISTRAR_OBJECT);
  349. if ( registrar && (registrar != (IFileTypeRegistrar*)1) )
  350. {
  351. if ( registrar->DeleteItem(base) != S_OK )
  352. {
  353. WritePrivateProfileStringW(L"winamp", L"remove_genplug", base, WINAMP_INI);
  354. WritePrivateProfileStringW(L"winamp", L"show_prefs", L"-1", WINAMP_INI);
  355. PostMessage(plugin.hwndParent, WM_WA_IPC, 0, IPC_RESTARTWINAMP);
  356. }
  357. else
  358. ListView_DeleteItem(listWindow, which_sel);
  359. registrar->Release();
  360. }
  361. // otherwise revert to a standard method
  362. else
  363. {
  364. WritePrivateProfileStringW(L"winamp", L"remove_genplug", base, WINAMP_INI);
  365. WritePrivateProfileStringW(L"winamp", L"show_prefs", L"-1", WINAMP_INI);
  366. PostMessage(plugin.hwndParent, WM_WA_IPC, 0, IPC_RESTARTWINAMP);
  367. }
  368. }
  369. // resets the focus to the listbox so it'll keep ui response working
  370. SetFocus(GetDlgItem(hwndDlg, IDC_GENLIB));
  371. }
  372. }
  373. }
  374. return FALSE;
  375. case IDC_PLUGINVERS:
  376. myOpenURLWithFallback(hwndDlg, L"http://www.google.com/search?q=Winamp+Library+Plugins", L"http://www.google.com/search?q=Winamp+Library+Plugins");
  377. return TRUE;
  378. }
  379. }
  380. link_handledraw(hwndDlg, uMsg, wParam, lParam);
  381. return 0;
  382. }
  383. typedef struct _PLUGINORDER
  384. {
  385. LPCWSTR name;
  386. bool found;
  387. } PLUGINORDER;
  388. static PLUGINORDER preload[] =
  389. {
  390. { L"ml_local.dll", false },
  391. { L"ml_downloads.dll", false },
  392. { L"ml_playlists.dll", false },
  393. { L"ml_wire.dll", false },
  394. { L"ml_online.dll", false },
  395. { L"ml_history.dll", false },
  396. { L"ml_rg.dll", false },
  397. { L"ml_bookmarks.dll", false },
  398. { L"ml_disc.dll", false },
  399. { L"ml_nowplaying2.dll", false },
  400. { L"ml_devices.dll", false },
  401. { L"ml_pmp.dll", false },
  402. };
  403. static HANDLE hProfile = INVALID_HANDLE_VALUE;
  404. HANDLE GetProfileFileHandle(int mode)
  405. {
  406. if ( profile & mode )
  407. {
  408. if ( hProfile == INVALID_HANDLE_VALUE )
  409. {
  410. wchar_t profileFile[MAX_PATH] = { 0 };
  411. PathCombineW(profileFile, WINAMP_INI_DIR, ((mode == 2) ? L"profile_load.txt" : L"profile.txt"));
  412. hProfile = CreateFileW(profileFile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  413. if ( hProfile != INVALID_HANDLE_VALUE )
  414. {
  415. // just to make sure we don't over-write things
  416. SetFilePointer(hProfile, NULL, NULL, FILE_END);
  417. }
  418. }
  419. return hProfile;
  420. }
  421. return INVALID_HANDLE_VALUE;
  422. }
  423. void LoadPlugin(const wchar_t* filename)
  424. {
  425. wsprintfW( _log_message_w, L"filename( %s )", filename );
  426. LOG_DEBUG( _log_message_w );
  427. wchar_t profile[MAX_PATH * 2] = { 0 };
  428. LARGE_INTEGER starttime, endtime;
  429. if ( hProfile != INVALID_HANDLE_VALUE )
  430. {
  431. QueryPerformanceCounter(&starttime);
  432. }
  433. wchar_t file[MAX_PATH] = { 0 };
  434. PathCombineW(file, pluginPath, filename);
  435. if ( !wa::files::file_exists( file ) )
  436. {
  437. wsprintfW( _log_message_w, L"The plugin '%s' is not found in the \"Plugins\" folder!", filename );
  438. LOG_ERROR( _log_message_w );
  439. return;
  440. }
  441. HMODULE hLib = LoadLibraryW(file);
  442. if (hLib == NULL)
  443. {
  444. DWORD l_error_code = ::GetLastError();
  445. wsprintfW( _log_message_w, L"Error when loading the plugin '%s'! Error code : %d!", filename, l_error_code );
  446. LOG_ERROR( _log_message_w );
  447. return;
  448. }
  449. winampMediaLibraryPlugin* (*pr)();
  450. pr = (winampMediaLibraryPlugin * (__cdecl*)(void)) GetProcAddress(hLib, "winampGetMediaLibraryPlugin");
  451. if (pr == NULL)
  452. {
  453. wsprintfW( _log_message_w, L"No entry point found for the plugin '%s'!", filename );
  454. LOG_ERROR( _log_message_w );
  455. FreeLibrary(hLib);
  456. return;
  457. }
  458. winampMediaLibraryPlugin* mlplugin = pr();
  459. if ( !mlplugin || (mlplugin->version > MLHDR_VER && mlplugin->version < MLHDR_VER_OLD) )
  460. {
  461. wsprintfW( _log_message_w, L"Either the plugin '%s' can't be loaded, or its version is incorrect!", filename );
  462. LOG_ERROR( _log_message_w );
  463. FreeLibrary(hLib);
  464. return;
  465. }
  466. wsprintfW( _log_message_w, L"The plugin '%s' is correctly loaded!", filename );
  467. LOG_DEBUG( _log_message_w );
  468. if ( g_safeMode != 1 )
  469. {
  470. if ( g_safeMode == 2 )
  471. {
  472. FreeModule(hLib);
  473. return;
  474. }
  475. char desc[128] = { 0 };
  476. lstrcpynA(desc, mlplugin->description, sizeof(desc));
  477. if ( desc[0] && !memcmp(desc, "nullsoft(", 9) )
  478. {
  479. char* p = strrchr(desc, ')');
  480. if ( p )
  481. {
  482. *p = 0;
  483. if ( stricmp(AutoChar(filename), (desc + 9)) )
  484. {
  485. FreeModule(hLib);
  486. return;
  487. }
  488. }
  489. }
  490. else
  491. {
  492. FreeModule(hLib);
  493. return;
  494. }
  495. }
  496. mlplugin->hwndLibraryParent = g_hwnd;
  497. mlplugin->hwndWinampParent = plugin.hwndParent;
  498. mlplugin->hDllInstance = hLib;
  499. int index = m_plugins.GetSize();
  500. if ( mlplugin->version >= MLHDR_VER )
  501. {
  502. mlplugin->service = WASABI_API_SVC;
  503. }
  504. if ( mlplugin->init() == ML_INIT_SUCCESS )
  505. {
  506. wsprintfW( _log_message_w, L"The plugin '%s' is initialized!", filename );
  507. LOG_DEBUG( _log_message_w );
  508. m_plugins.Add( (void *)mlplugin );
  509. }
  510. else
  511. {
  512. wsprintfW( _log_message_w, L"An error occurs when initializing the plugin '%s'!", filename );
  513. LOG_ERROR( _log_message_w );
  514. FreeLibrary( hLib );
  515. return;
  516. }
  517. if ( hProfile != INVALID_HANDLE_VALUE && mlplugin->hDllInstance )
  518. {
  519. QueryPerformanceCounter(&endtime);
  520. DWORD written = 0;
  521. unsigned int ms = (UINT)((endtime.QuadPart - starttime.QuadPart) * 1000 / freq.QuadPart);
  522. int len = swprintf(profile, L"Library\t%s\t[%s]\t%dms\r\n", file,
  523. (mlplugin->version >= MLHDR_VER_U ? (wchar_t*)mlplugin->description : AutoWide(mlplugin->description)), ms);
  524. SetFilePointer(hProfile, NULL, NULL, FILE_END);
  525. WriteFile(hProfile, profile, len * sizeof(wchar_t), &written, NULL);
  526. }
  527. }
  528. void loadMlPlugins()
  529. {
  530. HANDLE hProfile = GetProfileFileHandle(2);
  531. WIN32_FIND_DATAW d = { 0 };
  532. wchar_t tofind[MAX_PATH] = { 0 };
  533. int i, count = ARRAYSIZE(preload);
  534. for ( i = 0; i < count; i++ )
  535. LoadPlugin(preload[i].name);
  536. PathCombineW(tofind, pluginPath, L"ML_*.DLL");
  537. HANDLE h = FindFirstFileW(tofind, &d);
  538. if ( h != INVALID_HANDLE_VALUE )
  539. {
  540. do
  541. {
  542. for ( i = 0; i < count && (preload[i].found || lstrcmpiW(preload[i].name, d.cFileName)); i++ );
  543. if ( i == count )
  544. LoadPlugin(d.cFileName);
  545. else
  546. preload[i].found = true;
  547. } while ( FindNextFileW(h, &d) );
  548. FindClose(h);
  549. }
  550. if ( hProfile != INVALID_HANDLE_VALUE )
  551. {
  552. DWORD written = 0;
  553. WriteFile(hProfile, L"\r\n", 2, &written, NULL);
  554. CloseHandle(hProfile);
  555. hProfile = INVALID_HANDLE_VALUE;
  556. }
  557. if ( !m_plugins.GetSize() )
  558. ShowWindow(GetDlgItem(g_hwnd, IDC_NO_VIEW), SW_SHOW);
  559. }
  560. void unloadMlPlugins()
  561. {
  562. HANDLE hProfile = GetProfileFileHandle(1);
  563. if ( hProfile != INVALID_HANDLE_VALUE )
  564. {
  565. DWORD written = 0;
  566. WriteFile(hProfile, L"\r\n", 2, &written, NULL);
  567. }
  568. int i = m_plugins.GetSize();
  569. while ( i-- > 0 ) // reverse order to aid in not fucking up subclassing shit
  570. {
  571. winampMediaLibraryPlugin* mlplugin = (winampMediaLibraryPlugin*)m_plugins.Get(i);
  572. wchar_t profile[MAX_PATH * 2] = { 0 }, filename[MAX_PATH] = { 0 };
  573. LARGE_INTEGER starttime, endtime;
  574. if ( hProfile != INVALID_HANDLE_VALUE )
  575. {
  576. GetModuleFileNameW(mlplugin->hDllInstance, filename, MAX_PATH);
  577. QueryPerformanceCounter(&starttime);
  578. }
  579. if ( mlplugin->quit )
  580. mlplugin->quit(); // deals with 'virtual' ml items on restart
  581. if ( hProfile != INVALID_HANDLE_VALUE && mlplugin->hDllInstance )
  582. {
  583. QueryPerformanceCounter(&endtime);
  584. DWORD written = 0;
  585. unsigned int ms = (UINT)((endtime.QuadPart - starttime.QuadPart) * 1000 / freq.QuadPart);
  586. int len = swprintf(profile, L"Library\t%s\t[%s]\t%dms\r\n", filename,
  587. (mlplugin->version >= MLHDR_VER_U ? (wchar_t*)mlplugin->description : AutoWide(mlplugin->description)), ms);
  588. SetFilePointer(hProfile, NULL, NULL, FILE_END);
  589. WriteFile(hProfile, profile, len * sizeof(wchar_t), &written, NULL);
  590. }
  591. m_plugins.Del(i);
  592. }
  593. if ( hProfile != INVALID_HANDLE_VALUE )
  594. {
  595. DWORD written = 0;
  596. WriteFile(hProfile, L"\r\n", 2, &written, NULL);
  597. CloseHandle(hProfile);
  598. hProfile = INVALID_HANDLE_VALUE;
  599. }
  600. }
  601. INT_PTR plugin_SendMessage(int message_type, INT_PTR param1, INT_PTR param2, INT_PTR param3)
  602. {
  603. for ( int i = 0; i < m_plugins.GetSize(); i++ )
  604. {
  605. winampMediaLibraryPlugin* mlplugin = (winampMediaLibraryPlugin*)m_plugins.Get(i);
  606. if ( mlplugin && mlplugin->MessageProc )
  607. {
  608. INT_PTR h = mlplugin->MessageProc(message_type, param1, param2, param3);
  609. if ( h )
  610. return h;
  611. }
  612. }
  613. return 0;
  614. }