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 "spiLibrary.h"
\r
35 //******************************************************************************
\r
37 //******************************************************************************
\r
42 //******************************************************************************
\r
44 //******************************************************************************
\r
46 int __stdcall CallbackProc(int _nNow, int, long _lData)
\r
49 TPI_PROCESSINFO * piInfo = (TPI_PROCESSINFO *) _lData;
\r
50 piInfo->nProcessedSize = _nNow;
\r
53 return g_prProc != NULL && g_prProc(TPI_NOTIFY_COMMON, piInfo) == TPI_CALLBACK_CANCEL;
\r
56 //******************************************************************************
\r
58 //******************************************************************************
\r
60 int ErrorCodeConvert(int nErrorCode)
\r
64 case -1: return TPI_ERROR_U_USE_LIBRARY;
\r
65 case 0: return TPI_ERROR_SUCCESS;
\r
66 case 1: return TPI_ERROR_D_SKIPPED;
\r
67 case 2: return TPI_ERROR_ARC_UNSUPPORTED;
\r
68 case 3: return TPI_ERROR_ARC_BROKEN_MISC;
\r
69 case 4: return TPI_ERROR_D_OUTOFMEMORY;
\r
70 case 5: return TPI_ERROR_D_USEMEMORY;
\r
71 case 6: return TPI_ERROR_IO_ARC_READ;
\r
72 default: return TPI_ERROR_UNDEFINED;
\r
76 //******************************************************************************
\r
78 //******************************************************************************
\r
85 int __stdcall GetPluginInformation
\r
87 unsigned int _uInfoId,
\r
94 return TPI_ERROR_D_PARAMETER;
\r
98 case TPI_INFO_VERSION_MAJOR:
\r
99 case TPI_INFO_VERSION_MINOR:
\r
100 * (int *) _pPtr = 0;
\r
102 case TPI_INFO_VERSION_API:
\r
103 * (int *) _pPtr = 2;
\r
106 return TPI_ERROR_D_PARAMETER;
\r
108 return TPI_ERROR_SUCCESS;
\r
111 int __stdcall GetFormatInformation(TPI_FORMATINFO *, bool)
\r
113 return TPI_ERROR_D_UNSUPPORTED;
\r
116 int __stdcall LoadPlugin
\r
118 const wxString & _szArcName,
\r
123 wxString szSPIPath = wxPathOnly(p.GetExecutablePath()) + wxT("/lib/"), szSPIName;
\r
124 wxDir fs(szSPIPath);
\r
125 if (fs.GetFirst(& szSPIName, wxT("*.spi")))
\r
130 g_hLib = ::LoadLibrary((szSPIPath + szSPIName).wchar_str());
\r
131 if (g_hLib == NULL)
\r
136 // GetPluginInfoを実行。
\r
137 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetPluginInfo");
\r
138 char szPluginType[5]; // 種類4bytes + NULL
\r
140 || ((int (PASCAL *)(int, char *, int)) fpProc)(0, szPluginType, sizeof(szPluginType)) <= 0
\r
141 || szPluginType[2] != 'A' || szPluginType[3] != 'M')
\r
143 ::FreeLibrary(g_hLib);
\r
148 if (CheckArchive(_szArcName, NULL) == TPI_ERROR_SUCCESS)
\r
151 return TPI_ERROR_SUCCESS;
\r
154 while (fs.GetNext(& szSPIName));
\r
156 return TPI_ERROR_U_LOAD_LIBRARY;
\r
159 int __stdcall FreePlugin
\r
161 void * // _pReserved
\r
164 ::FreeLibrary(g_hLib);
\r
165 return TPI_ERROR_SUCCESS;
\r
168 int __stdcall CheckArchive
\r
170 const wxString & _szArcName,
\r
171 wxULongLong_t * _llFileCount
\r
174 FARPROC fpProc = ::GetProcAddress(g_hLib, "IsSupported");
\r
175 if (fpProc == NULL)
\r
177 return TPI_ERROR_U_USE_LIBRARY;
\r
180 wxFile hFile(_szArcName, wxFile::read);
\r
181 if (! hFile.IsOpened())
\r
183 return TPI_ERROR_IO_ARC_OPEN;
\r
187 ::ZeroMemory(buffer, sizeof(buffer));
\r
188 if (hFile.Read(buffer, sizeof(buffer)) == wxInvalidOffset)
\r
191 return TPI_ERROR_IO_ARC_READ;
\r
194 if (! ((BOOL (PASCAL *)(const char *, unsigned long)) fpProc)(_szArcName.ToUTF8(), (unsigned long) buffer))
\r
197 return TPI_ERROR_D_UNSUPPORTED;
\r
201 fpProc = ::GetProcAddress(g_hLib, "GetArchiveInfo");
\r
202 if (fpProc == NULL)
\r
204 return TPI_ERROR_U_USE_LIBRARY;
\r
208 int nReturnCode = ErrorCodeConvert(((int (PASCAL *)(const char *, long, unsigned int, HLOCAL *)) fpProc)(_szArcName.ToUTF8(), 0, 0, & hMemory));
\r
209 if (nReturnCode == TPI_ERROR_SUCCESS)
\r
211 if (_llFileCount != NULL)
\r
213 * _llFileCount = ::LocalSize(hMemory) / sizeof(fileInfo);
\r
215 ::LocalFree(hMemory);
\r
218 return nReturnCode;
\r
221 int __stdcall OpenArchive
\r
223 const wxString & _szArcName,
\r
227 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetArchiveInfo");
\r
228 if (fpProc == NULL)
\r
230 return TPI_ERROR_U_USE_LIBRARY;
\r
233 int nReturnCode = ErrorCodeConvert(((int (PASCAL *)(const char *, long, unsigned int, HLOCAL *)) fpProc)(_szArcName.ToUTF8(), 0, 0, _hArchive));
\r
234 if (nReturnCode != TPI_ERROR_SUCCESS)
\r
236 return nReturnCode;
\r
239 return * _hArchive == NULL ? TPI_ERROR_IO_ARC_OPEN : nReturnCode;
\r
242 int __stdcall CloseArchive
\r
247 return ::LocalFree(_hArchive) == NULL ? TPI_ERROR_SUCCESS : TPI_ERROR_IO_ARC_CLOSE;
\r
250 int __stdcall GetFileInformation
\r
253 TPI_FILEINFO * _fiInfo,
\r
257 static wxULongLong_t nFileCount, nFilePointer;
\r
262 nFileCount = ::LocalSize(_hArchive) / sizeof(fileInfo);
\r
265 if (nFilePointer > nFileCount)
\r
267 return TPI_ERROR_S_ENDOFDATA;
\r
270 fileInfo pfiInfo = ((fileInfo *) ::LocalLock(_hArchive))[nFilePointer];
\r
271 if (pfiInfo.method[0] == 0)
\r
273 // 丁寧なプラグインのための数合わせ (^^;
\r
274 nFileCount = nFilePointer;
\r
275 return TPI_ERROR_S_ENDOFDATA;
\r
278 _fiInfo->dwAttribute = 0;
\r
279 _fiInfo->dwCRC32 = pfiInfo.crc;
\r
280 _fiInfo->nPackedSize = pfiInfo.compsize;
\r
281 _fiInfo->nUnpackedSize = pfiInfo.filesize;
\r
282 _fiInfo->wCompressRatio = pfiInfo.compsize >= pfiInfo.filesize ? 1000 : (WORD) (1000 * pfiInfo.compsize / pfiInfo.filesize);
\r
283 _fiInfo->nFileId = nFilePointer++;
\r
284 _fiInfo->tmModified = pfiInfo.timestamp;
\r
285 _fiInfo->szStoredName = MB2String(pfiInfo.path) + MB2String(pfiInfo.filename);
\r
286 _fiInfo->fnFileName = wxFileName(_fiInfo->szStoredName);
\r
287 _fiInfo->szMethod = MB2String((char *) pfiInfo.method);
\r
288 _fiInfo->pCustomInfo = (void *) pfiInfo.position;
\r
289 ::LocalUnlock(_hArchive);
\r
291 return TPI_ERROR_SUCCESS;
\r
294 int __stdcall GetArchiveInformation
\r
297 TPI_ARCHIVEINFO * _aiInfo
\r
300 // GetPluginInfoを実行。
\r
301 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetPluginInfo");
\r
302 if (fpProc != NULL)
\r
305 if (((int (PASCAL *)(int, char *, int)) fpProc)(2, szTemp, sizeof(szTemp)) > 0)
\r
307 _aiInfo->fiInfo.szSuffix = MB2String(szTemp);
\r
309 if (((int (PASCAL *)(int, char *, int)) fpProc)(3, szTemp, sizeof(szTemp)) > 0)
\r
311 _aiInfo->fiInfo.szTypeName = MB2String(szTemp);
\r
314 _aiInfo->fiInfo.szTPIName = wxT("spiLibrary");
\r
315 _aiInfo->fiInfo.eSupportedCommand = TPI_COMMAND_EXTRACT;
\r
317 return TPI_ERROR_SUCCESS;
\r
320 int __stdcall Command
\r
322 wxULongLong_t _eCommand,
\r
323 TPI_SWITCHES * _swInfo,
\r
324 const wxString & _szArcName,
\r
325 const wxArrayString & _szFiles
\r
328 if (_eCommand != TPI_COMMAND_EXTRACT)
\r
330 return TPI_ERROR_D_UNSUPPORTED;
\r
334 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetFile");
\r
335 if (fpProc == NULL)
\r
337 return TPI_ERROR_U_USE_LIBRARY;
\r
342 int nErrorCode = OpenArchive(_szArcName, & hArchive);
\r
343 if (nErrorCode != TPI_ERROR_SUCCESS)
\r
349 wxFileName _fnArcName(_szArcName);
\r
350 TPI_PROCESSINFO piInfo;
\r
351 piInfo.eMessage = TPI_MESSAGE_STATUS;
\r
352 piInfo.eStatus = TPI_STATUS_OPENARCHIVE;
\r
353 piInfo.fiInfo.fnFileName = _fnArcName;
\r
354 if (CallbackProc(0, 0, (long) & piInfo))
\r
356 return TPI_ERROR_D_SKIPPED;
\r
360 piInfo.eStatus = TPI_STATUS_INPROCESS;
\r
361 nErrorCode = GetFileInformation(hArchive, & piInfo.fiInfo, true);
\r
362 if (nErrorCode == TPI_ERROR_SUCCESS)
\r
367 piInfo.eStatus = TPI_STATUS_BEGINPROCESS;
\r
368 if (CallbackProc(0, 0, (long) & piInfo))
\r
370 nErrorCode = TPI_ERROR_D_SKIPPED;
\r
373 piInfo.eStatus = TPI_STATUS_INPROCESS;
\r
376 if ((! _szFiles.IsEmpty()) && _szFiles.Index(piInfo.fiInfo.szStoredName) == wxNOT_FOUND)
\r
382 wxString szTargetPath = _swInfo->fnDestinationDirectory.GetPathWithSep();
\r
383 if (_swInfo->fStoreDirectoryPathes)
\r
386 szTargetPath += piInfo.fiInfo.fnFileName.GetFullPath();
\r
387 wxFileName fnDest(szTargetPath);
\r
388 if (! fnDest.Mkdir(0777, wxPATH_MKDIR_FULL) || ::wxDirExists(fnDest.GetFullPath()))
\r
390 nErrorCode = TPI_ERROR_IO_DIR_WRITE;
\r
396 szTargetPath += piInfo.fiInfo.fnFileName.GetFullName();
\r
399 // ファイル出力には対応してないのでメモリ出力で代行。
\r
400 HLOCAL hMemory = NULL;
\r
401 nErrorCode = ErrorCodeConvert(((int (PASCAL *)(const char *, long, char *, unsigned int, FARPROC, long)) fpProc)(_szArcName.ToUTF8(), (long) piInfo.fiInfo.pCustomInfo, (char *) & hMemory, 0x0100, (FARPROC) CallbackProc, (long) & piInfo));
\r
402 if (nErrorCode == TPI_ERROR_SUCCESS && hMemory == NULL)
\r
404 nErrorCode = TPI_ERROR_UNDEFINED;
\r
406 if (nErrorCode != TPI_ERROR_SUCCESS)
\r
414 if (! hFile.Create(szTargetPath, true))
\r
416 nErrorCode = TPI_ERROR_IO_FILE_OPEN;
\r
420 bool bErrorOccured = hFile.Write(::LocalLock(hMemory), piInfo.fiInfo.nUnpackedSize) != piInfo.fiInfo.nUnpackedSize;
\r
421 ::LocalUnlock(hMemory);
\r
422 ::LocalFree(hMemory);
\r
426 nErrorCode = TPI_ERROR_IO_FILE_WRITE;
\r
429 nErrorCode = TPI_ERROR_SUCCESS;
\r
432 piInfo.eStatus = TPI_STATUS_ENDPROCESS;
\r
433 if (CallbackProc(0, 0, (long) & piInfo))
\r
435 nErrorCode = TPI_ERROR_D_SKIPPED;
\r
439 while ((nErrorCode = GetFileInformation(hArchive, & piInfo.fiInfo, false)) != TPI_ERROR_S_ENDOFDATA);
\r
440 if (nErrorCode == TPI_ERROR_S_ENDOFDATA)
\r
443 nErrorCode = TPI_ERROR_SUCCESS;
\r
446 CloseArchive(hArchive);
\r
451 int __stdcall SetCallbackProc
\r
453 TPI_PROC _prArcProc
\r
457 if (_prArcProc == NULL)
\r
459 return TPI_ERROR_D_PARAMETER;
\r
461 g_prProc = * _prArcProc;
\r
463 return TPI_ERROR_SUCCESS;
\r