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 #define MYUSE_LIBPATH 1
\r
27 #include "../../common/header/plugin.h"
\r
28 #include "../../common/header/plugin-extra.h"
\r
29 #include "../../common/library/library.h"
\r
30 #include <wx/file.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
105 case TPI_INFO_HANDLE_ON_COMMAND:
\r
106 * (int *) _pPtr = 1;
\r
109 return TPI_ERROR_D_PARAMETER;
\r
111 return TPI_ERROR_SUCCESS;
\r
114 int __stdcall GetFormatInformation(TPI_FORMATINFO *, bool)
\r
116 return TPI_ERROR_D_UNSUPPORTED;
\r
119 int __stdcall LoadPlugin
\r
121 const wxString & _szArcName,
\r
130 if (! hFile.Exists(_szArcName) || ! hFile.Open(_szArcName, wxFile::read))
\r
132 return TPI_ERROR_IO_ARC_OPEN;
\r
136 ::ZeroMemory(buffer, sizeof(buffer));
\r
137 if (hFile.Read(buffer, sizeof(buffer)) == wxInvalidOffset)
\r
139 return TPI_ERROR_IO_ARC_READ;
\r
143 wxString szSPIName;
\r
144 wxDir fs(g_szLibPath);
\r
145 if (fs.GetFirst(& szSPIName, wxT("*.spi")))
\r
150 g_hLib = ::LoadLibrary((g_szLibPath + szSPIName).wchar_str());
\r
151 if (g_hLib == NULL)
\r
156 // GetPluginInfoを実行。
\r
157 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetPluginInfo");
\r
158 char szPluginType[5]; // 種類4bytes + NULL
\r
160 || ((int (PASCAL *)(int, char *, int)) fpProc)(0, szPluginType, sizeof(szPluginType)) <= 0
\r
161 || szPluginType[2] != 'A' || szPluginType[3] != 'M')
\r
163 ::FreeLibrary(g_hLib);
\r
168 fpProc = ::GetProcAddress(g_hLib, "IsSupported");
\r
169 if (fpProc == NULL)
\r
171 ::FreeLibrary(g_hLib);
\r
175 if (((BOOL (PASCAL *)(const char *, unsigned long)) fpProc)(_szArcName.ToUTF8(), (unsigned long) buffer))
\r
178 if (_prProc != NULL)
\r
180 g_prProc = * _prProc;
\r
182 return TPI_ERROR_SUCCESS;
\r
185 while (fs.GetNext(& szSPIName));
\r
187 return TPI_ERROR_U_LOAD_LIBRARY;
\r
190 int __stdcall FreePlugin
\r
192 void * // _pReserved
\r
195 ::FreeLibrary(g_hLib);
\r
196 return TPI_ERROR_SUCCESS;
\r
199 int __stdcall OpenArchive
\r
201 const wxString & _szArcName,
\r
202 void * * _hArchive,
\r
203 wxULongLong_t * _nFileCount
\r
206 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetArchiveInfo");
\r
207 if (fpProc == NULL)
\r
209 return TPI_ERROR_U_USE_LIBRARY;
\r
212 int nReturnCode = ErrorCodeConvert(((int (PASCAL *)(const char *, long, unsigned int, HLOCAL *)) fpProc)(_szArcName.ToUTF8(), 0, 0, _hArchive));
\r
213 if (nReturnCode != TPI_ERROR_SUCCESS)
\r
215 return nReturnCode;
\r
217 if (_nFileCount != NULL)
\r
219 * _nFileCount = ::LocalSize(* _hArchive) / sizeof(fileInfo);
\r
221 return * _hArchive == NULL ? TPI_ERROR_IO_ARC_OPEN : nReturnCode;
\r
224 int __stdcall CloseArchive
\r
229 return ::LocalFree(_hArchive) == NULL ? TPI_ERROR_SUCCESS : TPI_ERROR_IO_ARC_CLOSE;
\r
232 int __stdcall GetFileInformation
\r
235 TPI_FILEINFO * _fiInfo,
\r
239 static wxULongLong_t nFileCount, nFilePointer;
\r
244 nFileCount = ::LocalSize(_hArchive) / sizeof(fileInfo);
\r
247 if (nFilePointer > nFileCount)
\r
249 return TPI_ERROR_S_ENDOFDATA;
\r
252 fileInfo pfiInfo = ((fileInfo *) ::LocalLock(_hArchive))[nFilePointer];
\r
253 if (pfiInfo.method[0] == 0)
\r
255 // 丁寧なプラグインのための数合わせ (^^;
\r
256 nFileCount = nFilePointer;
\r
257 return TPI_ERROR_S_ENDOFDATA;
\r
260 _fiInfo->dwAttribute = 0;
\r
261 _fiInfo->dwCRC32 = pfiInfo.crc;
\r
262 _fiInfo->nPackedSize = pfiInfo.compsize;
\r
263 _fiInfo->nUnpackedSize = pfiInfo.filesize;
\r
264 _fiInfo->wCompressRatio = pfiInfo.compsize >= pfiInfo.filesize ? 1000 : (WORD) (1000 * pfiInfo.compsize / pfiInfo.filesize);
\r
265 _fiInfo->nFileId = nFilePointer++;
\r
266 _fiInfo->tmModify = pfiInfo.timestamp;
\r
267 _fiInfo->szStoredName = MB2String(pfiInfo.path) + MB2String(pfiInfo.filename);
\r
268 _fiInfo->fnFileName = wxFileName(_fiInfo->szStoredName);
\r
269 _fiInfo->szMethod = MB2String((char *) pfiInfo.method);
\r
270 _fiInfo->pCustomInfo = (void *) pfiInfo.position;
\r
271 ::LocalUnlock(_hArchive);
\r
273 return TPI_ERROR_SUCCESS;
\r
276 int __stdcall GetArchiveInformation
\r
279 TPI_ARCHIVEINFO * _aiInfo
\r
282 // GetPluginInfoを実行。
\r
283 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetPluginInfo");
\r
284 if (fpProc != NULL)
\r
287 memset(szTemp, 0, sizeof(szTemp));
\r
288 if (((int (PASCAL *)(int, char *, int)) fpProc)(1, szTemp, sizeof(szTemp) - 1) > 0)
\r
290 _aiInfo->fiInfo.szEngineName = MB2String(szTemp);
\r
292 if (((int (PASCAL *)(int, char *, int)) fpProc)(2, szTemp, sizeof(szTemp) - 1) > 0)
\r
294 _aiInfo->fiInfo.szSuffix = MB2String(szTemp);
\r
296 if (((int (PASCAL *)(int, char *, int)) fpProc)(3, szTemp, sizeof(szTemp) - 1) > 0)
\r
298 _aiInfo->fiInfo.szTypeName = MB2String(szTemp);
\r
301 _aiInfo->fiInfo.szTPIName = wxT("spiLibrary");
\r
302 _aiInfo->fiInfo.eSupportedCommand = TPI_COMMAND_EXTRACT;
\r
304 return TPI_ERROR_SUCCESS;
\r
307 int __stdcall Command
\r
309 wxULongLong_t _eCommand,
\r
310 TPI_SWITCHES * _swInfo,
\r
312 const wxArrayString & _szFiles
\r
315 if (_eCommand != TPI_COMMAND_EXTRACT)
\r
317 return TPI_ERROR_D_UNSUPPORTED;
\r
321 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetFile");
\r
322 if (fpProc == NULL)
\r
324 return TPI_ERROR_U_USE_LIBRARY;
\r
328 wxFileName _fnArcName(_swInfo->szArcName);
\r
329 TPI_PROCESSINFO piInfo;
\r
330 piInfo.eMessage = TPI_MESSAGE_STATUS;
\r
331 piInfo.eStatus = TPI_STATUS_OPENARCHIVE;
\r
332 piInfo.fiInfo.fnFileName = _fnArcName;
\r
333 if (CallbackProc(0, 0, (long) & piInfo))
\r
335 return TPI_ERROR_D_SKIPPED;
\r
339 piInfo.eStatus = TPI_STATUS_INPROCESS;
\r
340 int nErrorCode = GetFileInformation(_hArchive, & piInfo.fiInfo, true);
\r
341 if (nErrorCode == TPI_ERROR_SUCCESS)
\r
346 if ((! _szFiles.IsEmpty()) && _szFiles.Index(piInfo.fiInfo.szStoredName) == wxNOT_FOUND)
\r
352 piInfo.eMessage = TPI_MESSAGE_ASK;
\r
353 piInfo.eStatus = TPI_PARAM_DEST;
\r
354 piInfo.fnDestination = wxFileName(_swInfo->fnDestinationDirectory.GetPathWithSep() + (_swInfo->fStoreDirectoryPathes ? piInfo.fiInfo.fnFileName.GetFullPath() : piInfo.fiInfo.fnFileName.GetFullName()));
\r
355 if (CallbackProc(0, 0, (long) & piInfo))
\r
357 nErrorCode = TPI_ERROR_D_SKIPPED;
\r
360 if (! piInfo.fnDestination.IsOk())
\r
366 if (! piInfo.fnDestination.Mkdir(0777, wxPATH_MKDIR_FULL))
\r
368 nErrorCode = TPI_ERROR_IO_DIR_WRITE;
\r
373 piInfo.eMessage = TPI_MESSAGE_STATUS;
\r
374 piInfo.eStatus = TPI_STATUS_BEGINPROCESS;
\r
375 if (CallbackProc(0, 0, (long) & piInfo))
\r
377 nErrorCode = TPI_ERROR_D_SKIPPED;
\r
380 piInfo.eStatus = TPI_STATUS_INPROCESS;
\r
382 // ファイル出力には対応してないのでメモリ出力で代行。
\r
383 HLOCAL hMemory = NULL;
\r
384 nErrorCode = ErrorCodeConvert(((int (PASCAL *)(const char *, long, char *, unsigned int, FARPROC, long)) fpProc)(_swInfo->szArcName.ToUTF8(), (long) piInfo.fiInfo.pCustomInfo, (char *) & hMemory, 0x0100, (FARPROC) CallbackProc, (long) & piInfo));
\r
385 if (nErrorCode == TPI_ERROR_SUCCESS && hMemory == NULL)
\r
387 nErrorCode = TPI_ERROR_UNDEFINED;
\r
389 if (nErrorCode != TPI_ERROR_SUCCESS)
\r
395 bool bErrorOccured;
\r
398 if (! hFile.Create(piInfo.fnDestination.GetFullPath()))
\r
400 nErrorCode = TPI_ERROR_IO_FILE_OPEN;
\r
404 bErrorOccured = hFile.Write(::LocalLock(hMemory), piInfo.fiInfo.nUnpackedSize) != piInfo.fiInfo.nUnpackedSize;
\r
405 ::LocalUnlock(hMemory);
\r
406 ::LocalFree(hMemory);
\r
411 nErrorCode = TPI_ERROR_IO_FILE_WRITE;
\r
414 nErrorCode = TPI_ERROR_SUCCESS;
\r
417 piInfo.eStatus = TPI_STATUS_ENDPROCESS;
\r
418 if (CallbackProc(0, 0, (long) & piInfo))
\r
420 nErrorCode = TPI_ERROR_D_SKIPPED;
\r
424 while ((nErrorCode = GetFileInformation(_hArchive, & piInfo.fiInfo, false)) != TPI_ERROR_S_ENDOFDATA);
\r
425 if (nErrorCode == TPI_ERROR_S_ENDOFDATA)
\r
428 nErrorCode = TPI_ERROR_SUCCESS;
\r