1 /*******************************************************************************
\r
2 TPI - flexible but useless plug-in framework.
\r
3 Copyright (C) 2002-2009 Silky
\r
5 This library is free software; you can redistribute it and/or modify it under
\r
6 the terms of the GNU Lesser General Public License as published by the Free
\r
7 Software Foundation; either version 2.1 of the License, or (at your option)
\r
10 This library is distributed in the hope that it will be useful, but WITHOUT
\r
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
\r
15 You should have received a copy of the GNU Lesser General Public License along
\r
16 with this library; if not, write to the Free Software Foundation, Inc.,
\r
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
\r
20 *******************************************************************************/
\r
22 //******************************************************************************
\r
24 //******************************************************************************
\r
26 #include "../../common/header/plugin.h"
\r
27 #include "../../common/header/plugin-extra.h"
\r
28 #include "../../common/library/library.h"
\r
29 #include <wx/file.h>
\r
30 #include <wx/stdpaths.h>
\r
32 #include <windows.h>
\r
33 #include "xpiLibrary.h"
\r
35 //******************************************************************************
\r
37 //******************************************************************************
\r
42 //******************************************************************************
\r
44 //******************************************************************************
\r
46 int __stdcall CallbackProc(int _nNow, int _nMax, long _lData)
\r
49 TPI_PROCESSINFO * piInfo = (TPI_PROCESSINFO *) _lData;
\r
50 piInfo->nProcessedSize = _nNow;
\r
51 piInfo->fiInfo.nUnpackedSize = _nMax;
\r
54 return g_prProc != NULL && g_prProc(TPI_NOTIFY_COMMON, piInfo) == TPI_CALLBACK_CANCEL;
\r
57 //******************************************************************************
\r
59 //******************************************************************************
\r
61 int ErrorCodeConvert(int nErrorCode)
\r
65 case -1:case XPI_NO_FUNCTION: return TPI_ERROR_U_USE_LIBRARY;
\r
66 case 0: return TPI_ERROR_SUCCESS;
\r
67 case 1: case XPI_ABORT: return TPI_ERROR_D_SKIPPED;
\r
68 case 2: case XPI_NOT_SUPPRORT: return TPI_ERROR_ARC_UNSUPPORTED;
\r
69 case 3: case XPI_OUT_OF_ORDER: return TPI_ERROR_ARC_BROKEN_MISC;
\r
70 case 4: case XPI_NO_MEMORY: return TPI_ERROR_D_OUTOFMEMORY;
\r
71 case 5: case XPI_MEMORY_ERROR: return TPI_ERROR_D_USEMEMORY;
\r
72 case 6: case XPI_FILE_READ_ERROR: return TPI_ERROR_IO_ARC_READ;
\r
73 case XPI_OTHER_ERROR: return TPI_ERROR_UNDEFINED;
\r
74 case XPI_WINDOW_ERROR: return TPI_ERROR_UNDEFINED;
\r
75 case XPI_FILE_WRITE_ERROR: return TPI_ERROR_IO_FILE_WRITE;
\r
76 case XPI_END_OF_FILE: return TPI_ERROR_ARC_BROKEN_MISC;
\r
77 default: return TPI_ERROR_UNDEFINED;
\r
81 //******************************************************************************
\r
83 //******************************************************************************
\r
90 int __stdcall GetPluginInformation
\r
92 unsigned int _uInfoId,
\r
99 return TPI_ERROR_D_PARAMETER;
\r
103 case TPI_INFO_VERSION_MAJOR:
\r
104 case TPI_INFO_VERSION_MINOR:
\r
105 * (int *) _pPtr = 0;
\r
107 case TPI_INFO_VERSION_API:
\r
108 * (int *) _pPtr = 2;
\r
111 return TPI_ERROR_D_PARAMETER;
\r
113 return TPI_ERROR_SUCCESS;
\r
116 int __stdcall GetFormatInformation(TPI_FORMATINFO *, bool)
\r
118 return TPI_ERROR_D_UNSUPPORTED;
\r
121 int __stdcall LoadPlugin
\r
123 const wxString & _szArcName,
\r
129 if (! hFile.Exists(_szArcName) || ! hFile.Open(_szArcName, wxFile::read))
\r
131 return TPI_ERROR_IO_ARC_OPEN;
\r
136 ::ZeroMemory(buffer, sizeof(buffer));
\r
137 if (hFile.Read(buffer, sizeof(buffer)) == wxInvalidOffset)
\r
140 return TPI_ERROR_IO_ARC_READ;
\r
145 wxString szSPIPath = wxPathOnly(p.GetExecutablePath()) + wxT("/lib/"), szSPIName;
\r
146 wxDir fs(szSPIPath);
\r
147 if (fs.GetFirst(& szSPIName, wxT("*.spi")))
\r
152 g_hLib = ::LoadLibrary((szSPIPath + szSPIName).wchar_str());
\r
153 if (g_hLib == NULL)
\r
158 // GetPluginInfoを実行。
\r
159 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetPluginInfo");
\r
160 char szPluginType[5]; // 種類4bytes + NULL
\r
162 || ((int (PASCAL *)(int, char *, int)) fpProc)(0, szPluginType, sizeof(szPluginType)) <= 0
\r
163 || szPluginType[2] != 'I' || szPluginType[3] != 'N')
\r
165 ::FreeLibrary(g_hLib);
\r
170 fpProc = ::GetProcAddress(g_hLib, "IsSupported");
\r
171 if (fpProc == NULL)
\r
173 ::FreeLibrary(g_hLib);
\r
177 if (((BOOL (PASCAL *)(const char *, unsigned long)) fpProc)(_szArcName.ToUTF8(), (unsigned long) buffer))
\r
180 return TPI_ERROR_SUCCESS;
\r
183 while (fs.GetNext(& szSPIName));
\r
185 return TPI_ERROR_U_LOAD_LIBRARY;
\r
188 int __stdcall FreePlugin
\r
190 void * // _pReserved
\r
193 ::FreeLibrary(g_hLib);
\r
194 return TPI_ERROR_SUCCESS;
\r
197 int __stdcall OpenArchive
\r
199 const wxString & _szArcName,
\r
200 void * * _hArchive,
\r
204 * _hArchive = new wxString(_szArcName);
\r
205 return TPI_ERROR_SUCCESS;
\r
208 int __stdcall CloseArchive
\r
213 delete (wxString *) _hArchive;
\r
214 return TPI_ERROR_SUCCESS;
\r
217 int __stdcall GetFileInformation
\r
220 TPI_FILEINFO * _fiInfo,
\r
224 static wxString szXPIPath, szXPIName, szArcName = * (wxString *) _hArchive;
\r
225 static int nColorDepth;
\r
230 szXPIPath = wxPathOnly(p.GetExecutablePath()) + wxT("/lib/");
\r
231 fs.Open(szXPIPath);
\r
234 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetPictureInfo");
\r
235 if (fpProc == NULL)
\r
237 return TPI_ERROR_U_USE_LIBRARY;
\r
239 PictureInfo picInfo;
\r
240 int nErrorCode = ErrorCodeConvert(((int (PASCAL *)(const char *, long, unsigned int, PictureInfo *)) fpProc)(szArcName.ToUTF8(), 0, 0, & picInfo));
\r
241 if (nErrorCode != TPI_ERROR_SUCCESS)
\r
245 nColorDepth = picInfo.colorDepth;
\r
246 ::LocalUnlock(picInfo.hInfo);
\r
247 ::LocalFree(picInfo.hInfo);
\r
250 if (_bFirst ? fs.GetFirst(& szXPIName, wxT("*.xpi")) : fs.GetNext(& szXPIName))
\r
255 HMODULE hXPI = ::LoadLibrary((szXPIPath + szXPIName).wchar_str());
\r
261 // GetPluginInfoを実行。
\r
262 FARPROC fpProc = ::GetProcAddress(hXPI, "GetPluginInfo");
\r
265 || ((int (PASCAL *)(int, char *, int)) fpProc)(0, szTemp, sizeof(szTemp)) <= 0
\r
266 || szTemp[2] != 'X' || szTemp[3] != 'N'
\r
267 || ((int (PASCAL *)(int, char *, int)) fpProc)(2, szTemp, sizeof(szTemp)) <= 0)
\r
269 ::FreeLibrary(hXPI);
\r
274 fpProc = ::GetProcAddress(hXPI, "IsSupported");
\r
275 if (fpProc == NULL || ! ((BOOL (PASCAL *)(int)) fpProc)(nColorDepth))
\r
277 ::FreeLibrary(hXPI);
\r
281 _fiInfo->fnFileName = wxFileName(szArcName);
\r
282 _fiInfo->fnFileName.SetVolume(wxEmptyString);
\r
283 _fiInfo->fnFileName.SetPath(szXPIName);
\r
284 _fiInfo->fnFileName.SetExt(MB2String(szTemp));
\r
285 _fiInfo->szStoredName = _fiInfo->fnFileName.GetFullPath();
\r
286 return TPI_ERROR_SUCCESS;
\r
288 while (fs.GetNext(& szXPIName));
\r
290 return TPI_ERROR_S_ENDOFDATA;
\r
293 int __stdcall GetArchiveInformation
\r
296 TPI_ARCHIVEINFO * _aiInfo
\r
300 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetPictureInfo");
\r
301 if (fpProc == NULL)
\r
303 return TPI_ERROR_U_USE_LIBRARY;
\r
305 PictureInfo piInfo;
\r
306 int nErrorCode = ErrorCodeConvert(((int (PASCAL *)(const char *, long, unsigned int, PictureInfo *)) fpProc)(((wxString *) _hArchive)->ToUTF8(), 0, 0, & piInfo));
\r
307 if (nErrorCode != TPI_ERROR_SUCCESS)
\r
311 _aiInfo->szComment = MB2String((char *) ::LocalLock(piInfo.hInfo));
\r
312 ::LocalUnlock(piInfo.hInfo);
\r
313 ::LocalFree(piInfo.hInfo);
\r
315 // GetPluginInfoを実行。
\r
316 fpProc = ::GetProcAddress(g_hLib, "GetPluginInfo");
\r
317 if (fpProc != NULL)
\r
320 memset(szTemp, 0, sizeof(szTemp));
\r
321 if (((int (PASCAL *)(int, char *, int)) fpProc)(2, szTemp, sizeof(szTemp) - 1) > 0)
\r
323 _aiInfo->fiInfo.szSuffix = MB2String(szTemp);
\r
325 if (((int (PASCAL *)(int, char *, int)) fpProc)(3, szTemp, sizeof(szTemp) - 1) > 0)
\r
327 _aiInfo->fiInfo.szTypeName = MB2String(szTemp);
\r
330 _aiInfo->fiInfo.fArchive = false;
\r
331 _aiInfo->fiInfo.szTPIName = wxT("xpiLibrary");
\r
332 _aiInfo->fiInfo.eSupportedCommand = TPI_COMMAND_EXTRACT;
\r
334 return TPI_ERROR_SUCCESS;
\r
337 int __stdcall Command
\r
339 wxULongLong_t _eCommand,
\r
340 TPI_SWITCHES * _swInfo,
\r
341 const wxString & _szArcName,
\r
342 const wxArrayString & _asFiles
\r
345 if (_eCommand != TPI_COMMAND_EXTRACT)
\r
347 return TPI_ERROR_D_UNSUPPORTED;
\r
351 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetPictureInfo");
\r
352 if (fpProc == NULL)
\r
354 return TPI_ERROR_U_USE_LIBRARY;
\r
356 PictureInfo picInfo;
\r
357 int nErrorCode = ErrorCodeConvert(((int (PASCAL *)(const char *, long, unsigned int, PictureInfo *)) fpProc)(_szArcName.ToUTF8(), 0, 0, & picInfo));
\r
358 if (nErrorCode != TPI_ERROR_SUCCESS)
\r
364 wxFileName _fnArcName(_szArcName);
\r
365 TPI_PROCESSINFO piInfo;
\r
366 piInfo.eMessage = TPI_MESSAGE_STATUS;
\r
367 piInfo.eStatus = TPI_STATUS_OPENARCHIVE;
\r
368 piInfo.fiInfo.fnFileName = _fnArcName;
\r
369 if (CallbackProc(0, 0, (long) & piInfo))
\r
371 return TPI_ERROR_D_SKIPPED;
\r
375 HANDLE hInfo, hMemory;
\r
376 fpProc = ::GetProcAddress(g_hLib, "GetPicture");
\r
377 if (fpProc == NULL)
\r
379 return TPI_ERROR_U_USE_LIBRARY;
\r
381 nErrorCode = ErrorCodeConvert(((int (PASCAL *)(const char *, long, unsigned int, HANDLE *, HANDLE *, FARPROC, long)) fpProc)(_szArcName.ToUTF8(), 0, 0, & hInfo, & hMemory, (FARPROC) CallbackProc, (long) & piInfo));
\r
382 if (nErrorCode == TPI_ERROR_SUCCESS && (hMemory == NULL || hInfo == NULL))
\r
384 return TPI_ERROR_UNDEFINED;
\r
386 if (nErrorCode != TPI_ERROR_SUCCESS)
\r
391 wxString szXPIPath;
\r
394 szXPIPath = wxPathOnly(p.GetExecutablePath()) + wxT("/lib/");
\r
397 for (size_t i = 0; i < _asFiles.GetCount(); i++)
\r
400 piInfo.eMessage = TPI_MESSAGE_ASK;
\r
401 piInfo.eStatus = TPI_PARAM_DEST;
\r
402 piInfo.fiInfo.fnFileName = wxFileName(_asFiles[i]);
\r
403 piInfo.fnDestination = piInfo.fiInfo.fnFileName;
\r
404 piInfo.fnDestination.SetPath(_swInfo->fnDestinationDirectory.GetPath());
\r
405 if (CallbackProc(0, 0, (long) & piInfo))
\r
407 nErrorCode = TPI_ERROR_D_SKIPPED;
\r
410 if (! piInfo.fnDestination.IsOk())
\r
416 HMODULE hXPI = ::LoadLibrary((szXPIPath + piInfo.fiInfo.fnFileName.GetPath()).wchar_str());
\r
419 nErrorCode = TPI_ERROR_U_USE_LIBRARY;
\r
423 // GetPluginInfoを実行。
\r
424 FARPROC fpProc = ::GetProcAddress(hXPI, "GetPluginInfo");
\r
427 || ((int (PASCAL *)(int, char *, int)) fpProc)(2, szTemp, sizeof(szTemp)) <= 0)
\r
429 ::FreeLibrary(hXPI);
\r
430 nErrorCode = TPI_ERROR_U_USE_LIBRARY;
\r
435 piInfo.eMessage = TPI_MESSAGE_STATUS;
\r
436 piInfo.eStatus = TPI_STATUS_BEGINPROCESS;
\r
437 if (CallbackProc(0, 0, (long) & piInfo))
\r
439 ::FreeLibrary(hXPI);
\r
440 nErrorCode = TPI_ERROR_D_SKIPPED;
\r
445 piInfo.eStatus = TPI_STATUS_INPROCESS;
\r
446 fpProc = ::GetProcAddress(hXPI, "CreatePicture");
\r
447 if (fpProc == NULL)
\r
449 ::FreeLibrary(hXPI);
\r
450 nErrorCode = TPI_ERROR_U_USE_LIBRARY;
\r
453 nErrorCode = ErrorCodeConvert(((int (PASCAL *)(const char *, long, HANDLE *, HANDLE *, PictureInfo *, FARPROC, long)) fpProc)(piInfo.fnDestination.GetFullPath().ToUTF8(), 0, & hInfo, & hMemory, & picInfo, (FARPROC) CallbackProc, (long) & piInfo));
\r
454 ::FreeLibrary(hXPI);
\r
457 piInfo.eStatus = TPI_STATUS_ENDPROCESS;
\r
458 if (CallbackProc(0, 0, (long) & piInfo))
\r
460 nErrorCode = TPI_ERROR_D_SKIPPED;
\r
464 ::LocalUnlock(hInfo);
\r
465 ::LocalUnlock(hMemory);
\r
466 ::LocalUnlock(picInfo.hInfo);
\r
467 ::LocalFree(hInfo);
\r
468 ::LocalFree(hMemory);
\r
469 ::LocalFree(picInfo.hInfo);
\r
470 if (nErrorCode != TPI_ERROR_SUCCESS)
\r
476 piInfo.eMessage = TPI_MESSAGE_STATUS;
\r
477 piInfo.eStatus = TPI_STATUS_CLOSEARCHIVE;
\r
478 piInfo.fiInfo.fnFileName = _fnArcName;
\r
479 if (CallbackProc(0, 0, (long) & piInfo))
\r
481 return TPI_ERROR_D_SKIPPED;
\r
484 return TPI_ERROR_SUCCESS;
\r
487 int __stdcall SetCallbackProc
\r
489 TPI_PROC _prArcProc
\r
493 if (_prArcProc == NULL)
\r
495 return TPI_ERROR_D_PARAMETER;
\r
497 g_prProc = * _prArcProc;
\r
499 return TPI_ERROR_SUCCESS;
\r