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
174 FARPROC fpProc = ::GetProcAddress(g_hLib, "IsSupported");
\r
175 if (fpProc == NULL)
\r
177 return TPI_ERROR_U_USE_LIBRARY;
\r
181 if (! hFile.Exists(_szArcName) || ! hFile.Open(_szArcName, wxFile::read))
\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 return ((BOOL (PASCAL *)(const char *, unsigned long)) fpProc)(_szArcName.ToUTF8(), (unsigned long) buffer) ? TPI_ERROR_SUCCESS : TPI_ERROR_D_UNSUPPORTED;
\r
197 int __stdcall OpenArchive
\r
199 const wxString & _szArcName,
\r
203 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetArchiveInfo");
\r
204 if (fpProc == NULL)
\r
206 return TPI_ERROR_U_USE_LIBRARY;
\r
209 int nReturnCode = ErrorCodeConvert(((int (PASCAL *)(const char *, long, unsigned int, HLOCAL *)) fpProc)(_szArcName.ToUTF8(), 0, 0, _hArchive));
\r
210 if (nReturnCode != TPI_ERROR_SUCCESS)
\r
212 return nReturnCode;
\r
215 return * _hArchive == NULL ? TPI_ERROR_IO_ARC_OPEN : nReturnCode;
\r
218 int __stdcall CloseArchive
\r
223 return ::LocalFree(_hArchive) == NULL ? TPI_ERROR_SUCCESS : TPI_ERROR_IO_ARC_CLOSE;
\r
226 int __stdcall GetFileInformation
\r
229 TPI_FILEINFO * _fiInfo,
\r
233 static wxULongLong_t nFileCount, nFilePointer;
\r
238 nFileCount = ::LocalSize(_hArchive) / sizeof(fileInfo);
\r
241 if (nFilePointer > nFileCount)
\r
243 return TPI_ERROR_S_ENDOFDATA;
\r
246 fileInfo pfiInfo = ((fileInfo *) ::LocalLock(_hArchive))[nFilePointer];
\r
247 if (pfiInfo.method[0] == 0)
\r
249 // 丁寧なプラグインのための数合わせ (^^;
\r
250 nFileCount = nFilePointer;
\r
251 return TPI_ERROR_S_ENDOFDATA;
\r
254 _fiInfo->dwAttribute = 0;
\r
255 _fiInfo->dwCRC32 = pfiInfo.crc;
\r
256 _fiInfo->nPackedSize = pfiInfo.compsize;
\r
257 _fiInfo->nUnpackedSize = pfiInfo.filesize;
\r
258 _fiInfo->wCompressRatio = pfiInfo.compsize >= pfiInfo.filesize ? 1000 : (WORD) (1000 * pfiInfo.compsize / pfiInfo.filesize);
\r
259 _fiInfo->nFileId = nFilePointer++;
\r
260 _fiInfo->tmModified = pfiInfo.timestamp;
\r
261 _fiInfo->szStoredName = MB2String(pfiInfo.path) + MB2String(pfiInfo.filename);
\r
262 _fiInfo->fnFileName = wxFileName(_fiInfo->szStoredName);
\r
263 _fiInfo->szMethod = MB2String((char *) pfiInfo.method);
\r
264 _fiInfo->pCustomInfo = (void *) pfiInfo.position;
\r
265 ::LocalUnlock(_hArchive);
\r
267 return TPI_ERROR_SUCCESS;
\r
270 int __stdcall GetArchiveInformation
\r
273 TPI_ARCHIVEINFO * _aiInfo
\r
276 // GetPluginInfoを実行。
\r
277 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetPluginInfo");
\r
278 if (fpProc != NULL)
\r
281 if (((int (PASCAL *)(int, char *, int)) fpProc)(2, szTemp, sizeof(szTemp)) > 0)
\r
283 _aiInfo->fiInfo.szSuffix = MB2String(szTemp);
\r
285 if (((int (PASCAL *)(int, char *, int)) fpProc)(3, szTemp, sizeof(szTemp)) > 0)
\r
287 _aiInfo->fiInfo.szTypeName = MB2String(szTemp);
\r
290 _aiInfo->fiInfo.szTPIName = wxT("spiLibrary");
\r
291 _aiInfo->fiInfo.eSupportedCommand = TPI_COMMAND_EXTRACT;
\r
293 return TPI_ERROR_SUCCESS;
\r
296 int __stdcall Command
\r
298 wxULongLong_t _eCommand,
\r
299 TPI_SWITCHES * _swInfo,
\r
300 const wxString & _szArcName,
\r
301 const wxArrayString & _szFiles
\r
304 if (_eCommand != TPI_COMMAND_EXTRACT)
\r
306 return TPI_ERROR_D_UNSUPPORTED;
\r
310 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetFile");
\r
311 if (fpProc == NULL)
\r
313 return TPI_ERROR_U_USE_LIBRARY;
\r
318 int nErrorCode = OpenArchive(_szArcName, & hArchive);
\r
319 if (nErrorCode != TPI_ERROR_SUCCESS)
\r
325 wxFileName _fnArcName(_szArcName);
\r
326 TPI_PROCESSINFO piInfo;
\r
327 piInfo.eMessage = TPI_MESSAGE_STATUS;
\r
328 piInfo.eStatus = TPI_STATUS_OPENARCHIVE;
\r
329 piInfo.fiInfo.fnFileName = _fnArcName;
\r
330 if (CallbackProc(0, 0, (long) & piInfo))
\r
332 return TPI_ERROR_D_SKIPPED;
\r
336 piInfo.eStatus = TPI_STATUS_INPROCESS;
\r
337 nErrorCode = GetFileInformation(hArchive, & piInfo.fiInfo, true);
\r
338 if (nErrorCode == TPI_ERROR_SUCCESS)
\r
343 piInfo.eStatus = TPI_STATUS_BEGINPROCESS;
\r
344 if (CallbackProc(0, 0, (long) & piInfo))
\r
346 nErrorCode = TPI_ERROR_D_SKIPPED;
\r
349 piInfo.eStatus = TPI_STATUS_INPROCESS;
\r
352 if ((! _szFiles.IsEmpty()) && _szFiles.Index(piInfo.fiInfo.szStoredName) == wxNOT_FOUND)
\r
358 wxString szTargetPath = _swInfo->fnDestinationDirectory.GetPathWithSep();
\r
359 if (_swInfo->fStoreDirectoryPathes)
\r
362 szTargetPath += piInfo.fiInfo.fnFileName.GetFullPath();
\r
363 wxFileName fnDest(szTargetPath);
\r
364 if (! fnDest.Mkdir(0777, wxPATH_MKDIR_FULL) || ::wxDirExists(fnDest.GetFullPath()))
\r
366 nErrorCode = TPI_ERROR_IO_DIR_WRITE;
\r
372 szTargetPath += piInfo.fiInfo.fnFileName.GetFullName();
\r
375 // ファイル出力には対応してないのでメモリ出力で代行。
\r
376 HLOCAL hMemory = NULL;
\r
377 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
378 if (nErrorCode == TPI_ERROR_SUCCESS && hMemory == NULL)
\r
380 nErrorCode = TPI_ERROR_UNDEFINED;
\r
382 if (nErrorCode != TPI_ERROR_SUCCESS)
\r
390 if (! hFile.Create(szTargetPath, true))
\r
392 nErrorCode = TPI_ERROR_IO_FILE_OPEN;
\r
396 bool bErrorOccured = hFile.Write(::LocalLock(hMemory), piInfo.fiInfo.nUnpackedSize) != piInfo.fiInfo.nUnpackedSize;
\r
397 ::LocalUnlock(hMemory);
\r
398 ::LocalFree(hMemory);
\r
402 nErrorCode = TPI_ERROR_IO_FILE_WRITE;
\r
405 nErrorCode = TPI_ERROR_SUCCESS;
\r
408 piInfo.eStatus = TPI_STATUS_ENDPROCESS;
\r
409 if (CallbackProc(0, 0, (long) & piInfo))
\r
411 nErrorCode = TPI_ERROR_D_SKIPPED;
\r
415 while ((nErrorCode = GetFileInformation(hArchive, & piInfo.fiInfo, false)) != TPI_ERROR_S_ENDOFDATA);
\r
416 if (nErrorCode == TPI_ERROR_S_ENDOFDATA)
\r
419 nErrorCode = TPI_ERROR_SUCCESS;
\r
422 CloseArchive(hArchive);
\r
427 int __stdcall SetCallbackProc
\r
429 TPI_PROC _prArcProc
\r
433 if (_prArcProc == NULL)
\r
435 return TPI_ERROR_D_PARAMETER;
\r
437 g_prProc = * _prArcProc;
\r
439 return TPI_ERROR_SUCCESS;
\r