1
0

skininfo.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #define APSTUDIO_READONLY_SYMBOLS
  2. #include "main.h"
  3. #include "./skininfo.h"
  4. #include "./loadimage.h"
  5. #include "./langutil.h"
  6. #include "./setup_resource.h"
  7. #include "./api.h"
  8. #include <../xml/obj_xml.h>
  9. #include <api/service/waServiceFactory.h>
  10. #include "../xml/ifc_xmlreadercallbacki.h"
  11. #include "minizip/unzip.h"
  12. #include "../nu/AutoChar.h"
  13. #define ZIP_BUFFER_SIZE 2048
  14. static wchar_t szClassic[64] = {0, };
  15. #define CLASSIC_NAME() ((!*szClassic) ? getStringW(IDS_CLASSIC_SKIN_NAME, szClassic, sizeof(szClassic)/sizeof(wchar_t)) : szClassic)
  16. static BOOL ExtractCurrentFile(unzFile f, LPCWSTR pszPath);
  17. static INT ZipProcessSkinInfo(unzFile f, LPCWSTR pszTemp, SKININFO *psi, LPSTR szSearch, INT cchSearch); // returns length of search
  18. class SkinXMLCallback : public ifc_xmlreadercallbackI
  19. {
  20. public:
  21. SkinXMLCallback(SKININFO *psi, LPCWSTR pszSkinXML, LPWSTR pszImage, INT cchImage) // if pszIamge != NULL iamge path will be returned instead of loading image
  22. {
  23. this->psi = psi;
  24. this->pszImage = pszImage;
  25. this->cchImage = cchImage;
  26. if (this->pszImage) *this->pszImage = 0x00;
  27. StringCchCopyW(szPath, sizeof(szPath)/sizeof(wchar_t), pszSkinXML);
  28. PathRemoveFileSpecW(szPath);
  29. };
  30. void xmlReaderOnCharacterDataCallback(const wchar_t *xmlpath, const wchar_t *xmltag, const wchar_t *s);
  31. void xmlReaderOnStartElementCallback(const wchar_t *xmlpath, const wchar_t *xmltag, ifc_xmlreaderparams *params);
  32. protected:
  33. SKININFO *psi;
  34. wchar_t szPath[MAX_PATH*2];
  35. LPWSTR pszImage;
  36. INT cchImage;
  37. };
  38. static BOOL LoadXMLFile(obj_xml *parser, LPCWSTR pszFileName)
  39. {
  40. HANDLE hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
  41. if (INVALID_HANDLE_VALUE == hFile) return FALSE;
  42. BOOL br (TRUE);
  43. DWORD bytesRead=0;
  44. do
  45. {
  46. BYTE buffer[1024] = {0};
  47. bytesRead=0;
  48. if (ReadFile(hFile, buffer, 1024, &bytesRead, NULL) && bytesRead)
  49. {
  50. if (parser->xmlreader_feed(buffer, bytesRead)!=API_XML_SUCCESS) { br = FALSE; break; }
  51. }
  52. else
  53. {
  54. if (parser->xmlreader_feed(0, 0) != API_XML_SUCCESS) { br = FALSE; break; }
  55. bytesRead=0;
  56. }
  57. } while (bytesRead);
  58. CloseHandle(hFile);
  59. return br;
  60. }
  61. static BOOL ReadSkinInfo(LPCWSTR pszSkinXML, SKININFO *psi, LPWSTR pszImage, INT cchImage)
  62. {
  63. waServiceFactory *parserFactory = WASABI_API_SVC->service_getServiceByGuid(obj_xmlGUID);
  64. if (parserFactory)
  65. {
  66. obj_xml *parser = (obj_xml *)parserFactory->getInterface();
  67. if (parser)
  68. {
  69. SkinXMLCallback cb(psi, pszSkinXML, pszImage, cchImage);
  70. parser->xmlreader_registerCallback(L"WinampAbstractionLayer", &cb);
  71. parser->xmlreader_registerCallback(L"WinampAbstractionLayer\fSkinInfo\f*", &cb);
  72. parser->xmlreader_registerCallback(L"WasabiXML", &cb);
  73. parser->xmlreader_registerCallback(L"WasabiXML\fSkinInfo\f*", &cb);
  74. parser->xmlreader_registerCallback(L"SkinInfo*", &cb);
  75. parser->xmlreader_open();
  76. LoadXMLFile(parser, pszSkinXML);
  77. parser->xmlreader_unregisterCallback(&cb);
  78. parser->xmlreader_close();
  79. parserFactory->releaseInterface(parser);
  80. }
  81. }
  82. return TRUE;
  83. }
  84. void SkinXMLCallback::xmlReaderOnStartElementCallback(const wchar_t *xmlpath, const wchar_t *xmltag, ifc_xmlreaderparams *params)
  85. {
  86. DWORD lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
  87. if (CSTR_EQUAL == CompareStringW(lcid, NORM_IGNORECASE, L"WinampAbstractionLayer", -1, xmltag, -1) ||
  88. CSTR_EQUAL == CompareStringW(lcid, NORM_IGNORECASE, L"WasabiXML", -1, xmltag, -1))
  89. {
  90. const wchar_t *version = params->getItemValue(L"version");
  91. if (version) StringCchCopyW(psi->szWasabiVer, SI_VERMAX, version);
  92. else psi->szWasabiVer[0] = 0x00;
  93. }
  94. }
  95. void SkinXMLCallback::xmlReaderOnCharacterDataCallback(const wchar_t *xmlpath, const wchar_t *xmltag, const wchar_t *s)
  96. {
  97. static const wchar_t *tags[] = { L"name", L"version", L"comment", L"author", L"email", L"homepage", L"screenshot" };
  98. int cch, i, count = sizeof(tags)/sizeof(wchar_t*);
  99. DWORD lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
  100. LPWSTR p;
  101. for (i = 0; i < count; i++)
  102. {
  103. if (CSTR_EQUAL == CompareStringW(lcid, NORM_IGNORECASE, tags[i], -1, xmltag, -1)) break;
  104. }
  105. switch(i)
  106. {
  107. case 0: p = psi->szName; cch = SI_NAMEMAX; break;
  108. case 1: p = psi->szVersion; cch = SI_VERMAX; break;
  109. case 2: p = (SIF_COMMENT & psi->fMask) ? psi->pszComment : NULL; cch = psi->cchComment; break;
  110. case 3: p = psi->szAuthor; cch = SI_AUTHORMAX; break;
  111. case 4: p = psi->szEmail; cch = SI_EMAILMAX; break;
  112. case 5: p = psi->szHomePage; cch = SI_HOMEPAGEMAX; break;
  113. case 6:
  114. if ((SIF_PREVIEW & psi->fMask) && *s)
  115. {
  116. if (pszImage) StringCchCopyW(pszImage, cchImage, s);
  117. else
  118. {
  119. wchar_t szPic[MAX_PATH*2] = {0};
  120. psi->hPreview = WALoadImage(NULL, NULL, (PathIsRootW(s)) ? s : PathCombineW(szPic, szPath, s), FALSE);
  121. }
  122. }
  123. return;
  124. default: return;
  125. }
  126. if (p) StringCchCopyW(p, cch, s);
  127. }
  128. BOOL GetSkinInfo(LPCWSTR pszSkinPath, SKININFO *psi)
  129. {
  130. wchar_t szBuffer[MAX_PATH*2] = {0};
  131. if (!psi || sizeof(SKININFO) != psi->cbSize) return FALSE;
  132. psi->type = SKIN_TYPE_UNKNOWN;
  133. if (SIF_COMMENT & psi->fMask)
  134. {
  135. if (!psi->pszComment || !psi->cchComment) return FALSE;
  136. psi->pszComment[0] = 0x00;
  137. }
  138. if (SIF_PREVIEW & psi->fMask) psi->hPreview = NULL;
  139. if (!pszSkinPath || !*pszSkinPath || CSTR_EQUAL == CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
  140. pszSkinPath, -1, CLASSIC_NAME(), -1))
  141. {
  142. // default classic
  143. psi->type = SKIN_TYPE_CLASSIC;
  144. StringCchCopyW(psi->szName, SI_NAMEMAX, CLASSIC_NAME());
  145. if (SIF_COMMENT & psi->fMask) StringCchCopyW(psi->pszComment, psi->cchComment, L"Winamp base skin v5.5");
  146. if (SIF_PREVIEW & psi->fMask)
  147. psi->hPreview = WALoadImage2(L"PNG", MAKEINTRESOURCEW(IDB_PREVIEW_CLASSIC), FALSE);
  148. StringCchCopyW(psi->szAuthor, SI_AUTHORMAX, L"Steve Gedikian");
  149. StringCchCopyW(psi->szVersion, SI_VERMAX, L"2.0");
  150. return TRUE;
  151. }
  152. if (PathIsDirectoryW(pszSkinPath))
  153. {
  154. if (PathFileExistsW(PathCombineW(szBuffer, pszSkinPath, L"skin.xml")))
  155. {
  156. psi->type = SKIN_TYPE_MODERN;
  157. ReadSkinInfo(szBuffer, psi, NULL, 0);
  158. }
  159. else if (PathFileExistsW(PathCombineW(szBuffer, pszSkinPath, L"main.bmp")))
  160. {
  161. psi->type = SKIN_TYPE_CLASSIC;
  162. ReadSkinInfo(PathCombineW(szBuffer, pszSkinPath, L"skininfo.xml"), psi, NULL, 0);
  163. }
  164. }
  165. else
  166. {
  167. int len, start, lenSearch;
  168. DWORD lcid;
  169. wchar_t szTemp[MAX_PATH] = {0};
  170. char filename[MAX_PATH] = {0}, search[MAX_PATH] = {0};
  171. unzFile f;
  172. if (!GetTempPathW(MAX_PATH, szBuffer) || !GetTempFileNameW(szBuffer, L"WAS", 0, szTemp)) return FALSE;
  173. f = unzOpen(AutoChar(pszSkinPath));
  174. if (!f) return FALSE;
  175. lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
  176. search[0] = 1;
  177. lenSearch = 0;
  178. start = 0;
  179. do
  180. {
  181. int pos = -1, nr = unzGoToFirstFile(f);
  182. while (UNZ_OK == nr && *search)
  183. {
  184. pos++;
  185. if (start && pos == start) { start = 0; break; }
  186. if (UNZ_OK == unzGetCurrentFileInfo(f, NULL, filename, sizeof(filename), NULL, 0, NULL, 0) && *filename)
  187. {
  188. len = lstrlenA(filename);
  189. if (!len || filename[len - 1] == '/') // empty or folder
  190. {
  191. nr = unzGoToNextFile(f);
  192. continue;
  193. }
  194. if (SKIN_TYPE_UNKNOWN == psi->type)
  195. {
  196. if ((len == 8 || (len > 8 && '/' == filename[len - 8 - 1])) &&
  197. CSTR_EQUAL == CompareStringA(lcid, NORM_IGNORECASE, "main.bmp", -1, (filename + (len - 8)), -1))
  198. {
  199. psi->type = SKIN_TYPE_CLASSIC;
  200. StringCchCopyA(search, MAX_PATH, "skininfo.xml");
  201. lenSearch = lstrlenA(search);
  202. start = pos;
  203. }
  204. else if ((len == 8 || (len > 8 && '/' == filename[len - 8 - 1])) &&
  205. CSTR_EQUAL == CompareStringA(lcid, NORM_IGNORECASE, "skin.xml", -1, (filename + (len - 8)), -1))
  206. {
  207. psi->type = SKIN_TYPE_MODERN;
  208. lenSearch = ZipProcessSkinInfo(f, szTemp, psi, search, MAX_PATH);
  209. if (lenSearch) start = pos;
  210. }
  211. }
  212. else if ((len == lenSearch || (len > lenSearch && '/' == filename[len - lenSearch - 1])) &&
  213. CSTR_EQUAL == CompareStringA(lcid, NORM_IGNORECASE, search, -1, (filename + (len - lenSearch)), -1))
  214. {
  215. if (CSTR_EQUAL == CompareStringA(lcid, NORM_IGNORECASE, search, -1, "skininfo.xml", -1))
  216. {
  217. lenSearch = ZipProcessSkinInfo(f, szTemp, psi, search, MAX_PATH);
  218. if (lenSearch) start = pos;
  219. }
  220. else // image
  221. {
  222. if (ExtractCurrentFile(f, szTemp)) psi->hPreview = WALoadImage(NULL, NULL, szTemp, FALSE);
  223. else psi->hPreview = NULL;
  224. *search = 0x00;
  225. }
  226. }
  227. }
  228. nr = unzGoToNextFile(f);
  229. }
  230. } while(search && *search && start);
  231. unzClose(f);
  232. // delete tmp file
  233. DeleteFileW(szTemp);
  234. }
  235. return TRUE;
  236. }
  237. static BOOL ExtractCurrentFile(unzFile f, LPCWSTR pszPath)
  238. {
  239. HANDLE hFile;
  240. OVERLAPPED asyncIO;
  241. static BOOL isNT = -1;
  242. BOOL bSuccess(TRUE);
  243. int l(1), pos(0), bufNum(0);
  244. char buf[ZIP_BUFFER_SIZE*2] = {0};
  245. if (UNZ_OK != unzOpenCurrentFile(f)) return FALSE;
  246. if (-1 == isNT) isNT = (GetVersion() < 0x80000000);
  247. ZeroMemory(&asyncIO, sizeof(OVERLAPPED));
  248. if (isNT) asyncIO.hEvent = CreateEvent(NULL, FALSE, TRUE, NULL);
  249. hFile = CreateFileW(pszPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
  250. FILE_ATTRIBUTE_NORMAL | ((isNT) ? FILE_FLAG_OVERLAPPED : 0), NULL);
  251. if (INVALID_HANDLE_VALUE == hFile) { bSuccess = FALSE; hFile = NULL; }
  252. while (bSuccess && l > 0)
  253. {
  254. DWORD written = 0;
  255. bufNum = !bufNum;
  256. l = unzReadCurrentFile(f, buf+ZIP_BUFFER_SIZE*bufNum, ZIP_BUFFER_SIZE);
  257. if (l < 0) bSuccess = FALSE;
  258. if (isNT)
  259. {
  260. WaitForSingleObject(asyncIO.hEvent, INFINITE);
  261. if (l > 0)
  262. {
  263. asyncIO.Offset = pos;
  264. if (!WriteFile(hFile, buf+ZIP_BUFFER_SIZE*bufNum, l, NULL, &asyncIO) && ERROR_IO_PENDING != GetLastError())
  265. {
  266. bSuccess = FALSE;
  267. }
  268. pos += l;
  269. }
  270. }
  271. else
  272. {
  273. if (l > 0)
  274. {
  275. if (!WriteFile(hFile, buf+ZIP_BUFFER_SIZE*bufNum, l, &written, NULL)) bSuccess = FALSE;
  276. }
  277. }
  278. }
  279. if (hFile) CloseHandle(hFile);
  280. if (asyncIO.hEvent) CloseHandle(asyncIO.hEvent);
  281. unzCloseCurrentFile(f);
  282. return bSuccess;
  283. }
  284. static INT ZipProcessSkinInfo(unzFile f, LPCWSTR pszTemp, SKININFO *psi, LPSTR szSearch, INT cchSearch) // returns length of search
  285. {
  286. *szSearch = 0x00;
  287. if (ExtractCurrentFile(f, pszTemp))
  288. {
  289. wchar_t szImage[MAX_PATH] = {0};
  290. if (ReadSkinInfo(pszTemp, psi, szImage, MAX_PATH) && *szImage)
  291. {
  292. if(!PathIsRelativeW(szImage)) psi->hPreview = WALoadImage(NULL, NULL, szImage, FALSE);
  293. else StringCchCopyA(szSearch, cchSearch, AutoChar(szImage));
  294. }
  295. }
  296. return lstrlenA(szSearch);
  297. }