pluginloader.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "pluginloader.h"
  2. #include "../winamp/wa_ipc.h"
  3. #include "nu/AutoWide.h"
  4. #include <shlwapi.h>
  5. #include "../nu/MediaLibraryInterface.h"
  6. extern winampMediaLibraryPlugin plugin;
  7. C_ItemList m_plugins;
  8. extern HWND mainMessageWindow;
  9. int wmDeviceChange(WPARAM wParam, LPARAM lParam) {
  10. int ret=0;
  11. for(int i=0; i < m_plugins.GetSize(); i++) {
  12. PMPDevicePlugin * plugin = (PMPDevicePlugin *)m_plugins.Get(i);
  13. /*
  14. if(plugin->wmDeviceChange)
  15. {
  16. if(plugin->wmDeviceChange(wParam, lParam) == BROADCAST_QUERY_DENY)
  17. ret = BROADCAST_QUERY_DENY;
  18. }
  19. */
  20. if(plugin->MessageProc)
  21. {
  22. if(plugin->MessageProc(PMP_DEVICECHANGE,wParam,lParam,0) == BROADCAST_QUERY_DENY)
  23. ret = BROADCAST_QUERY_DENY;
  24. }
  25. }
  26. return ret;
  27. }
  28. PMPDevicePlugin * loadPlugin(wchar_t * file)
  29. {
  30. HINSTANCE m=LoadLibrary(file);
  31. if(m)
  32. {
  33. PMPDevicePlugin *(*gp)();
  34. gp=(PMPDevicePlugin *(__cdecl *)(void))GetProcAddress(m,"winampGetPMPDevicePlugin");
  35. if(!gp)
  36. {
  37. FreeLibrary(m);
  38. return NULL;
  39. }
  40. PMPDevicePlugin *devplugin=gp();
  41. if(!devplugin || devplugin->version != PMPHDR_VER)
  42. {
  43. FreeLibrary(m);
  44. return NULL;
  45. }
  46. devplugin->hDllInstance=m;
  47. devplugin->hwndLibraryParent=plugin.hwndLibraryParent;
  48. devplugin->hwndWinampParent=plugin.hwndWinampParent;
  49. devplugin->hwndPortablesParent=mainMessageWindow;
  50. devplugin->service = plugin.service;
  51. if(devplugin->init())
  52. {
  53. FreeLibrary(m);
  54. }
  55. else
  56. {
  57. m_plugins.Add((void *)devplugin);
  58. return devplugin;
  59. }
  60. }
  61. return NULL;
  62. }
  63. BOOL loadDevPlugins(int *count)
  64. {
  65. BOOL loaded = FALSE;
  66. wchar_t tofind[MAX_PATH] = {0};
  67. LPCWSTR dir = (LPCWSTR)SendMessage(plugin.hwndWinampParent,WM_WA_IPC,0,IPC_GETPLUGINDIRECTORYW);
  68. PathCombine(tofind, dir, L"pmp_*.dll");
  69. WIN32_FIND_DATA d = {0};
  70. HANDLE h = FindFirstFile(tofind,&d);
  71. if (h != INVALID_HANDLE_VALUE)
  72. {
  73. do
  74. {
  75. wchar_t file[MAX_PATH] = {0};
  76. PathCombine(file, dir, d.cFileName);
  77. loaded += (!!loadPlugin(file));
  78. }
  79. while(FindNextFile(h,&d));
  80. FindClose(h);
  81. }
  82. if (count) *count = m_plugins.GetSize();
  83. return loaded;
  84. }
  85. BOOL testForDevPlugins()
  86. {
  87. BOOL found = FALSE;
  88. wchar_t tofind[MAX_PATH] = {0};
  89. LPCWSTR dir = (LPCWSTR)SendMessage(plugin.hwndWinampParent,WM_WA_IPC,0,IPC_GETPLUGINDIRECTORYW);
  90. PathCombine(tofind, dir, L"pmp_*.dll");
  91. WIN32_FIND_DATA d = {0};
  92. HANDLE h = FindFirstFile(tofind,&d);
  93. if (h != INVALID_HANDLE_VALUE)
  94. {
  95. found = TRUE;
  96. FindClose(h);
  97. }
  98. return found;
  99. }
  100. extern int profile;
  101. static HANDLE hProfile = INVALID_HANDLE_VALUE;
  102. HANDLE GetProfileFileHandle()
  103. {
  104. if (profile)
  105. {
  106. if (hProfile == INVALID_HANDLE_VALUE)
  107. {
  108. wchar_t profileFile[MAX_PATH] = {0};
  109. PathCombineW(profileFile, mediaLibrary.GetIniDirectoryW(), L"profile.txt");
  110. hProfile = CreateFileW(profileFile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  111. if (hProfile != INVALID_HANDLE_VALUE)
  112. {
  113. // just to make sure we don't over-write things
  114. SetFilePointer(hProfile, NULL, NULL, FILE_END);
  115. }
  116. }
  117. return hProfile;
  118. }
  119. return INVALID_HANDLE_VALUE;
  120. }
  121. void unloadPlugin(PMPDevicePlugin *devplugin, int n=-1)
  122. {
  123. if(n == -1) for(int i=0; i<m_plugins.GetSize(); i++) if(m_plugins.Get(i) == (void*)devplugin) n=i;
  124. devplugin->quit();
  125. //if (devplugin->hDllInstance) FreeLibrary(devplugin->hDllInstance);
  126. m_plugins.Del(n);
  127. }
  128. void unloadDevPlugins()
  129. {
  130. int i=m_plugins.GetSize();
  131. HANDLE hProfile = GetProfileFileHandle();
  132. LARGE_INTEGER freq;
  133. QueryPerformanceFrequency(&freq);
  134. if (hProfile != INVALID_HANDLE_VALUE)
  135. {
  136. DWORD written = 0;
  137. WriteFile(hProfile, L"\r\n", 2, &written, NULL);
  138. }
  139. while (i-->0) // reverse order to aid in not fucking up subclassing shit
  140. {
  141. PMPDevicePlugin *devplugin=(PMPDevicePlugin *)m_plugins.Get(i);
  142. wchar_t profile[MAX_PATH*2] = {0}, filename[MAX_PATH] = {0};
  143. LARGE_INTEGER starttime, endtime;
  144. if (hProfile != INVALID_HANDLE_VALUE)
  145. {
  146. GetModuleFileNameW(devplugin->hDllInstance, filename, MAX_PATH);
  147. QueryPerformanceCounter(&starttime);
  148. }
  149. unloadPlugin(devplugin,i);
  150. if (hProfile != INVALID_HANDLE_VALUE)
  151. {
  152. QueryPerformanceCounter(&endtime);
  153. DWORD written = 0;
  154. unsigned int ms = (UINT)((endtime.QuadPart - starttime.QuadPart) * 1000 / freq.QuadPart);
  155. int len = swprintf(profile, L"Portable\t%s\t[%s]\t%dms\r\n", filename, devplugin->description, ms);
  156. WriteFile(hProfile, profile, len*sizeof(wchar_t), &written, NULL);
  157. }
  158. }
  159. if (hProfile != INVALID_HANDLE_VALUE)
  160. {
  161. DWORD written = 0;
  162. WriteFile(hProfile, L"\r\n", 2, &written, NULL);
  163. CloseHandle(hProfile);
  164. }
  165. }