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 BOOL __stdcall DllMain(HMODULE, DWORD fdwReason, void *)
\r
50 case DLL_PROCESS_DETACH:
\r
51 ::FreeLibrary(g_hLib);
\r
57 //******************************************************************************
\r
59 //******************************************************************************
\r
61 int __stdcall CallbackProc(int _nNow, int, long _lData)
\r
64 TPI_PROCESSINFO * piInfo = (TPI_PROCESSINFO *) _lData;
\r
65 piInfo->nProcessedSize = _nNow;
\r
68 return g_prProc != NULL && g_prProc(TPI_NOTIFY_COMMON, piInfo) == TPI_CALLBACK_CANCEL;
\r
71 //******************************************************************************
\r
73 //******************************************************************************
\r
75 int ErrorCodeConvert(int nErrorCode)
\r
79 case -1: return TPI_ERROR_U_USE_LIBRARY;
\r
80 case 0: return TPI_ERROR_SUCCESS;
\r
81 case 1: return TPI_ERROR_D_SKIPPED;
\r
82 case 2: return TPI_ERROR_ARC_UNSUPPORTED;
\r
83 case 3: return TPI_ERROR_ARC_BROKEN_MISC;
\r
84 case 4: return TPI_ERROR_D_OUTOFMEMORY;
\r
85 case 5: return TPI_ERROR_D_USEMEMORY;
\r
86 case 6: return TPI_ERROR_IO_ARC_READ;
\r
87 default: return TPI_ERROR_UNDEFINED;
\r
91 //******************************************************************************
\r
93 //******************************************************************************
\r
100 int __stdcall GetPluginInformation
\r
102 unsigned int _uInfoId,
\r
109 return TPI_ERROR_D_PARAMETER;
\r
113 case TPI_INFO_VERSION_MAJOR:
\r
114 case TPI_INFO_VERSION_MINOR:
\r
115 * (int *) _pPtr = 0;
\r
117 case TPI_INFO_VERSION_API:
\r
118 * (int *) _pPtr = 2;
\r
121 return TPI_ERROR_D_PARAMETER;
\r
123 return TPI_ERROR_SUCCESS;
\r
126 int __stdcall GetFormatInformation(TPI_FORMATINFO *, bool)
\r
128 return TPI_ERROR_D_UNSUPPORTED;
\r
131 int __stdcall LoadPlugin
\r
133 const wxString & _szArcName,
\r
138 wxString szSPIPath = wxPathOnly(p.GetExecutablePath()) + wxT("/lib/"), szSPIName;
\r
139 wxDir fs(szSPIPath);
\r
140 if (fs.GetFirst(& szSPIName, wxT("*.spi")))
\r
145 g_hLib = ::LoadLibrary((szSPIPath + szSPIName).wchar_str());
\r
146 if (g_hLib == NULL)
\r
151 // GetPluginInfoを実行。
\r
152 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetPluginInfo");
\r
153 char szPluginType[5]; // 種類4bytes + NULL
\r
155 || ((int (PASCAL *)(int, char *, int)) fpProc)(0, szPluginType, sizeof(szPluginType)) <= 0
\r
156 || szPluginType[2] != 'A' || szPluginType[3] != 'M')
\r
158 ::FreeLibrary(g_hLib);
\r
163 if (CheckArchive(_szArcName, NULL) == TPI_ERROR_SUCCESS)
\r
166 return TPI_ERROR_SUCCESS;
\r
169 while (fs.GetNext(& szSPIName));
\r
171 return TPI_ERROR_U_LOAD_LIBRARY;
\r
174 int __stdcall FreePlugin
\r
176 void * // _pReserved
\r
179 ::FreeLibrary(g_hLib);
\r
180 return TPI_ERROR_SUCCESS;
\r
183 int __stdcall CheckArchive
\r
185 const wxString & _szArcName,
\r
186 wxULongLong_t * _llFileCount
\r
189 FARPROC fpProc = ::GetProcAddress(g_hLib, "IsSupported");
\r
190 if (fpProc == NULL)
\r
192 return TPI_ERROR_U_USE_LIBRARY;
\r
195 wxFile hFile(_szArcName, wxFile::read);
\r
196 if (! hFile.IsOpened())
\r
198 return TPI_ERROR_IO_ARC_OPEN;
\r
202 ::ZeroMemory(buffer, sizeof(buffer));
\r
203 if (hFile.Read(buffer, sizeof(buffer)) == wxInvalidOffset)
\r
206 return TPI_ERROR_IO_ARC_READ;
\r
209 if (! ((BOOL (PASCAL *)(const char *, unsigned long)) fpProc)(_szArcName.ToUTF8(), (unsigned long) buffer))
\r
212 return TPI_ERROR_D_UNSUPPORTED;
\r
216 fpProc = ::GetProcAddress(g_hLib, "GetArchiveInfo");
\r
217 if (fpProc == NULL)
\r
219 return TPI_ERROR_U_USE_LIBRARY;
\r
223 int nReturnCode = ErrorCodeConvert(((int (PASCAL *)(const char *, long, unsigned int, HLOCAL *)) fpProc)(_szArcName.ToUTF8(), 0, 0, & hMemory));
\r
224 if (nReturnCode == TPI_ERROR_SUCCESS)
\r
226 if (_llFileCount != NULL)
\r
228 * _llFileCount = ::LocalSize(hMemory) / sizeof(fileInfo);
\r
230 ::LocalFree(hMemory);
\r
233 return nReturnCode;
\r
236 int __stdcall OpenArchive
\r
238 const wxString & _szArcName,
\r
242 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetArchiveInfo");
\r
243 if (fpProc == NULL)
\r
245 return TPI_ERROR_U_USE_LIBRARY;
\r
248 int nReturnCode = ErrorCodeConvert(((int (PASCAL *)(const char *, long, unsigned int, HLOCAL *)) fpProc)(_szArcName.ToUTF8(), 0, 0, _hArchive));
\r
249 if (nReturnCode != TPI_ERROR_SUCCESS)
\r
251 return nReturnCode;
\r
254 return * _hArchive == NULL ? TPI_ERROR_IO_ARC_OPEN : nReturnCode;
\r
257 int __stdcall CloseArchive
\r
262 return ::LocalFree(_hArchive) == NULL ? TPI_ERROR_SUCCESS : TPI_ERROR_IO_ARC_CLOSE;
\r
265 int __stdcall GetFileInformation
\r
268 TPI_FILEINFO * _fiInfo,
\r
272 static wxULongLong_t nFileCount, nFilePointer;
\r
277 nFileCount = ::LocalSize(_hArchive) / sizeof(fileInfo);
\r
280 if (nFilePointer > nFileCount)
\r
282 return TPI_ERROR_S_ENDOFDATA;
\r
285 fileInfo pfiInfo = ((fileInfo *) ::LocalLock(_hArchive))[nFilePointer];
\r
286 if (pfiInfo.method[0] == 0)
\r
288 // 丁寧なプラグインのための数合わせ (^^;
\r
289 nFileCount = nFilePointer;
\r
290 return TPI_ERROR_S_ENDOFDATA;
\r
293 _fiInfo->dwAttribute = 0;
\r
294 _fiInfo->dwCRC32 = pfiInfo.crc;
\r
295 _fiInfo->nPackedSize = pfiInfo.compsize;
\r
296 _fiInfo->nUnpackedSize = pfiInfo.filesize;
\r
297 _fiInfo->wCompressRatio = pfiInfo.compsize >= pfiInfo.filesize ? 1000 : (WORD) (1000 * pfiInfo.compsize / pfiInfo.filesize);
\r
298 _fiInfo->nFileId = nFilePointer++;
\r
299 _fiInfo->tmModified = pfiInfo.timestamp;
\r
300 _fiInfo->szStoredName = MB2String(pfiInfo.path) + MB2String(pfiInfo.filename);
\r
301 _fiInfo->fnFileName = wxFileName(_fiInfo->szStoredName);
\r
302 _fiInfo->szMethod = MB2String((char *) pfiInfo.method);
\r
303 _fiInfo->pCustomInfo = (void *) pfiInfo.position;
\r
304 ::LocalUnlock(_hArchive);
\r
306 return TPI_ERROR_SUCCESS;
\r
309 int __stdcall GetArchiveInformation
\r
312 TPI_ARCHIVEINFO * _aiInfo
\r
315 // GetPluginInfoを実行。
\r
316 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetPluginInfo");
\r
317 if (fpProc != NULL)
\r
320 if (((int (PASCAL *)(int, char *, int)) fpProc)(2, szTemp, sizeof(szTemp)) > 0)
\r
322 _aiInfo->fiInfo.szSuffix = MB2String(szTemp);
\r
324 if (((int (PASCAL *)(int, char *, int)) fpProc)(3, szTemp, sizeof(szTemp)) > 0)
\r
326 _aiInfo->fiInfo.szTypeName = MB2String(szTemp);
\r
329 _aiInfo->fiInfo.szTPIName = wxT("spiLibrary");
\r
330 _aiInfo->fiInfo.eSupportedCommand = TPI_COMMAND_EXTRACT;
\r
332 return TPI_ERROR_SUCCESS;
\r
335 int __stdcall Command
\r
337 wxULongLong_t _eCommand,
\r
338 TPI_SWITCHES * _swInfo,
\r
339 const wxString & _szArcName,
\r
340 const wxArrayString & _szFiles
\r
343 if (_eCommand != TPI_COMMAND_EXTRACT)
\r
345 return TPI_ERROR_D_UNSUPPORTED;
\r
349 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetFile");
\r
350 if (fpProc == NULL)
\r
352 return TPI_ERROR_U_USE_LIBRARY;
\r
357 int nErrorCode = OpenArchive(_szArcName, & hArchive);
\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 piInfo.eStatus = TPI_STATUS_INPROCESS;
\r
376 nErrorCode = GetFileInformation(hArchive, & piInfo.fiInfo, true);
\r
377 if (nErrorCode == TPI_ERROR_SUCCESS)
\r
382 piInfo.eStatus = TPI_STATUS_BEGINPROCESS;
\r
383 if (CallbackProc(0, 0, (long) & piInfo))
\r
385 nErrorCode = TPI_ERROR_D_SKIPPED;
\r
388 piInfo.eStatus = TPI_STATUS_INPROCESS;
\r
391 if ((! _szFiles.IsEmpty()) && _szFiles.Index(piInfo.fiInfo.szStoredName) == wxNOT_FOUND)
\r
397 wxString szTargetPath = _swInfo->fnDestinationDirectory.GetPathWithSep();
\r
398 if (_swInfo->fStoreDirectoryPathes)
\r
401 szTargetPath += piInfo.fiInfo.fnFileName.GetFullPath();
\r
402 wxFileName fnDest(szTargetPath);
\r
403 if (! fnDest.Mkdir(0777, wxPATH_MKDIR_FULL) || ::wxDirExists(fnDest.GetFullPath()))
\r
405 nErrorCode = TPI_ERROR_IO_DIR_WRITE;
\r
411 szTargetPath += piInfo.fiInfo.fnFileName.GetFullName();
\r
414 // ファイル出力には対応してないのでメモリ出力で代行。
\r
415 HLOCAL hMemory = NULL;
\r
416 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
417 if (nErrorCode == TPI_ERROR_SUCCESS && hMemory == NULL)
\r
419 nErrorCode = TPI_ERROR_UNDEFINED;
\r
421 if (nErrorCode != TPI_ERROR_SUCCESS)
\r
429 if (! hFile.Create(szTargetPath, true))
\r
431 nErrorCode = TPI_ERROR_IO_FILE_OPEN;
\r
435 bool bErrorOccured = hFile.Write(::LocalLock(hMemory), piInfo.fiInfo.nUnpackedSize) != piInfo.fiInfo.nUnpackedSize;
\r
436 ::LocalUnlock(hMemory);
\r
437 ::LocalFree(hMemory);
\r
441 nErrorCode = TPI_ERROR_IO_FILE_WRITE;
\r
444 nErrorCode = TPI_ERROR_SUCCESS;
\r
447 piInfo.eStatus = TPI_STATUS_ENDPROCESS;
\r
448 if (CallbackProc(0, 0, (long) & piInfo))
\r
450 nErrorCode = TPI_ERROR_D_SKIPPED;
\r
454 while ((nErrorCode = GetFileInformation(hArchive, & piInfo.fiInfo, false)) != TPI_ERROR_S_ENDOFDATA);
\r
455 if (nErrorCode == TPI_ERROR_S_ENDOFDATA)
\r
458 nErrorCode = TPI_ERROR_SUCCESS;
\r
461 CloseArchive(hArchive);
\r
466 int __stdcall SetCallbackProc
\r
468 TPI_PROC _prArcProc
\r
472 if (_prArcProc == NULL)
\r
474 return TPI_ERROR_D_PARAMETER;
\r
476 g_prProc = * _prArcProc;
\r
478 return TPI_ERROR_SUCCESS;
\r