-
Notifications
You must be signed in to change notification settings - Fork 1
/
wcx_mpq.cpp
1001 lines (844 loc) · 33 KB
/
wcx_mpq.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************/
/* wcx_mpq.cpp Copyright (c) Ladislav Zezula 2003 */
/*---------------------------------------------------------------------------*/
/* Main file for Total Commander MPQ plugin */
/*---------------------------------------------------------------------------*/
/* Date Ver Who Comment */
/* -------- ---- --- ------- */
/* 28.07.03 1.00 Lad The first version of wcx_mpq.cpp */
/*****************************************************************************/
#include "wcx_mpq.h" // Our functions
#include "resource.h" // Resource symbols
#include "StormLibT.inl" // UTF-8 and UNICODE support for StormLib
//-----------------------------------------------------------------------------
// Local variables
PFN_PROCESS_DATAA PfnProcessDataA; // Process data procedure (ANSI)
PFN_PROCESS_DATAW PfnProcessDataW; // Process data procedure (UNICODE)
PFN_CHANGE_VOLUMEA PfnChangeVolA; // Change volume procedure (ANSI)
PFN_CHANGE_VOLUMEW PfnChangeVolW; // Change volume procedure (UNICODE)
//-----------------------------------------------------------------------------
// CanYouHandleThisFile(W) allows the plugin to handle files with different
// extensions than the one defined in Total Commander
// https://www.ghisler.ch/wiki/index.php?title=CanYouHandleThisFile
static DWORD GetStreamFlagsFromFileName(LPCTSTR szFileName, bool bCheckReadOnly = false)
{
DWORD dwBaseFlags = 0;
// If the file has read-only attribute, then all read only
if(bCheckReadOnly && (GetFileAttributes(szFileName) & FILE_ATTRIBUTE_READONLY))
dwBaseFlags |= STREAM_FLAG_READ_ONLY;
// Add flags based on file name
while(szFileName[0] != 0)
{
// Did we find a possible extension?
if(szFileName[0] == '.')
{
if(!_tcsicmp(szFileName, _T(".mpq.part")))
return dwBaseFlags | STREAM_PROVIDER_PARTIAL | BASE_PROVIDER_FILE;
if(!_tcsicmp(szFileName, _T(".MPQE")))
return dwBaseFlags | STREAM_PROVIDER_MPQE | BASE_PROVIDER_FILE;
if(!_tcsicmp(szFileName, _T(".MPQ.0")))
return dwBaseFlags | STREAM_PROVIDER_BLOCK4 | BASE_PROVIDER_FILE;
}
// Move to the next character
szFileName++;
}
return dwBaseFlags | STREAM_PROVIDER_FLAT | BASE_PROVIDER_FILE;
}
// This function is called when Total Commander tries to process an archive with the plugin
BOOL WINAPI CanYouHandleThisFileW(LPCWSTR szFileName)
{
HANDLE hMpq = NULL;
DWORD dwFlags = GetStreamFlagsFromFileName(szFileName) | MPQ_OPEN_NO_ATTRIBUTES | MPQ_OPEN_NO_LISTFILE | STREAM_FLAG_READ_ONLY;
// Try to open the archive
if(SFileOpenArchive(szFileName, 0, dwFlags, &hMpq))
{
SFileCloseArchive(hMpq);
return TRUE;
}
return FALSE;
}
BOOL WINAPI CanYouHandleThisFile(LPCSTR szFileName)
{
return CanYouHandleThisFileW(TAnsiToWide(szFileName));
}
//-----------------------------------------------------------------------------
// OpenArchive(W) should perform all necessary operations when an archive is
// to be opened
// https://www.ghisler.ch/wiki/index.php?title=OpenArchive
static void FileTimeToDosFTime(DOS_FTIME & DosTime, DWORD dwFileTimeHi, DWORD dwFileTimeLo)
{
SYSTEMTIME stUTC; // Local file time
SYSTEMTIME st; // Local file time
FILETIME ft; // System file time
ft.dwHighDateTime = dwFileTimeHi;
ft.dwLowDateTime = dwFileTimeLo;
FileTimeToSystemTime(&ft, &stUTC);
SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &st);
DosTime.ft_year = st.wYear - 60;
DosTime.ft_month = st.wMonth;
DosTime.ft_day = st.wDay;
DosTime.ft_hour = st.wHour;
DosTime.ft_min = st.wMinute;
DosTime.ft_tsec = (st.wSecond / 2);
}
static void GetArchiveTime(HANDLE hMPQ, DOS_FTIME & Time)
{
TMPQArchive * ha = (TMPQArchive *)hMPQ;
FILETIME ft;
FileStream_GetTime(ha->pStream, (ULONGLONG *)&ft);
FileTimeToDosFTime(Time, ft.dwHighDateTime, ft.dwLowDateTime);
}
static HANDLE OpenArchiveAW(TOpenArchiveData * pArchiveData, LPCWSTR szArchiveName)
{
TOpenMpqInfo * pInfo = NULL;
HANDLE hMPQ = NULL;
size_t nSize;
DWORD dwFileCount = 0;
// Check the valid parameters
if(pArchiveData && szArchiveName && szArchiveName[0])
{
// Check the valid archive access
if(pArchiveData->OpenMode == PK_OM_LIST || pArchiveData->OpenMode == PK_OM_EXTRACT)
{
// Try to open the archive
if(SFileOpenArchive(szArchiveName, 0, GetStreamFlagsFromFileName(szArchiveName, true), &hMPQ))
{
// Add the external listfile for the archive
if(g_cfg.szListFile[0] != 0)
SFileAddListFile(hMPQ, g_cfg.szListFile);
// Create the handle for Total Commander
SFileGetFileInfo(hMPQ, SFileMpqBlockTableSize, &dwFileCount, sizeof(DWORD), NULL);
nSize = sizeof(TOpenMpqInfo) + (dwFileCount * sizeof(BYTE));
// Allocate the buffer
pInfo = (TOpenMpqInfo *)HeapAlloc(g_hHeap, HEAP_ZERO_MEMORY, nSize);
if(pInfo != NULL)
{
// Fill the archive info
pInfo->dwMaxFileIndex = dwFileCount;
pInfo->dwSignature = ID_MPQ;
pInfo->nOpenMode = pArchiveData->OpenMode;
GetArchiveTime(hMPQ, pInfo->ArchiveTime);
pInfo->hMPQ = hMPQ;
// Return the open result
pArchiveData->OpenResult = ERROR_SUCCESS;
return (HANDLE)(pInfo);
}
else
{
pArchiveData->OpenResult = E_NO_MEMORY;
}
}
else
{
pArchiveData->OpenResult = E_UNKNOWN_FORMAT;
}
}
else
{
pArchiveData->OpenResult = E_NOT_SUPPORTED;
}
}
else
{
pArchiveData->OpenResult = E_NOT_SUPPORTED;
}
return NULL;
}
HANDLE WINAPI OpenArchiveW(TOpenArchiveData * pArchiveData)
{
return OpenArchiveAW(pArchiveData, pArchiveData->szArchiveNameW);
}
HANDLE WINAPI OpenArchive(TOpenArchiveData * pArchiveData)
{
return OpenArchiveAW(pArchiveData, TAnsiToWide(pArchiveData->szArchiveNameA));
}
//-----------------------------------------------------------------------------
// CloseArchive should perform all necessary operations when an archive
// is about to be closed.
// https://www.ghisler.ch/wiki/index.php?title=CloseArchive
static TOpenMpqInfo * IsValidArchiveHandle(HANDLE hArchive)
{
if(hArchive != NULL && hArchive != INVALID_HANDLE_VALUE)
{
if((static_cast<TOpenMpqInfo *>(hArchive))->dwSignature == ID_MPQ)
{
return static_cast<TOpenMpqInfo *>(hArchive);
}
}
return NULL;
}
int WINAPI CloseArchive(HANDLE hArchive)
{
TOpenMpqInfo * pInfo;
// Check the right parameters
if((pInfo = IsValidArchiveHandle(hArchive)) != NULL)
{
// Close the archive
if(pInfo->hMPQ != NULL)
SFileCloseArchive(pInfo->hMPQ);
pInfo->hMPQ = NULL;
// Free the structure itself
HeapFree(g_hHeap, 0, pInfo);
return ERROR_SUCCESS;
}
return E_NOT_SUPPORTED;
}
//-----------------------------------------------------------------------------
// GetPackerCaps tells Totalcmd what features your packer plugin supports.
// https://www.ghisler.ch/wiki/index.php?title=GetPackerCaps
// GetPackerCaps tells Total Commander what features we support
int WINAPI GetPackerCaps()
{
return(PK_CAPS_NEW | // Can create new archives
PK_CAPS_MODIFY | // Can modify existing archives
PK_CAPS_MULTIPLE | // Archive can contain multiple files
PK_CAPS_DELETE | // Can delete files
PK_CAPS_OPTIONS | // Has options dialog
// PK_CAPS_MEMPACK | // Supports packing in memory
PK_CAPS_BY_CONTENT | // Detect archive type by content
PK_CAPS_SEARCHTEXT // Allow searching for text in archives created with this plugin
// PK_CAPS_HIDE // Show as normal files (hide packer icon, open with Ctrl+PgDn, not Enter)
);
}
//-----------------------------------------------------------------------------
// ProcessFile should unpack the specified file or test the integrity of the archive.
// https://www.ghisler.ch/wiki/index.php?title=ProcessFile
static int CallProcessDataProc(LPCWSTR szFullPath, int nSize)
{
// Prioritize UNICODE version of the callback, if exists.
// This leads to nicer progress dialog shown by Total Commander
if(PfnProcessDataW != NULL)
return PfnProcessDataW(szFullPath, nSize);
// Call ANSI version of callback, if needed
if(PfnProcessDataA != NULL)
return PfnProcessDataA(TWideToAnsi(szFullPath), nSize);
// Continue the operation
return TRUE;
}
static void MergePath(LPWSTR szFullPath, size_t ccFullPath, LPCWSTR szPath, LPCWSTR szName)
{
// Always the buffer with zero
if(ccFullPath != 0)
{
szFullPath[0] = 0;
// Append destination path, if exists
if(szPath && szPath[0])
{
StringCchCopy(szFullPath, ccFullPath, szPath);
}
// Append the name, if any
if(szName && szName[0])
{
// Append backslash
AddBackslash(szFullPath, ccFullPath);
StringCchCat(szFullPath, ccFullPath, szName);
}
}
}
int WINAPI ProcessFileW(HANDLE hArchive, int nOperation, LPCWSTR szDestPath, LPCWSTR szDestName)
{
TOpenMpqInfo * pInfo;
HANDLE hLocFile = INVALID_HANDLE_VALUE;
HANDLE hMpqFile = NULL; // File handle (Archived file)
WCHAR szFullPath[MAX_PATH];
int nResult = E_NOT_SUPPORTED; // Result reported to Total Commander
// Check whether it's the valid archive handle
if((pInfo = IsValidArchiveHandle(hArchive)) != NULL)
{
// If verify or skip the file, do nothing
if(nOperation == PK_TEST || nOperation == PK_SKIP)
return 0;
// Do we have to extract the file? If yes, the file must be saved in TOpenMpqInfo
if(nOperation != PK_EXTRACT || pInfo->sf.cFileName[0] == 0)
return E_NOT_SUPPORTED;
// Set the locale that is intended to be open
SFileSetLocale(pInfo->sf.lcLocale);
// Open the source file
if(SFileOpenFileEx(pInfo->hMPQ, pInfo->sf.cFileName, 0, &hMpqFile))
{
// Construct the full path name
MergePath(szFullPath, _countof(szFullPath), szDestPath, szDestName);
// Open the local file
hLocFile = CreateFile(szFullPath, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);
if(hLocFile != INVALID_HANDLE_VALUE)
{
DWORD dwTotalBytes = 0;
bool bEndOfFile = false;
// Copy the file's content
while(bEndOfFile == false)
{
DWORD dwBytesWritten = 0;
DWORD dwBytesRead = 0;
BYTE Buffer[0x1000];
// Tell Total Commader what we are doing
if(!CallProcessDataProc(szFullPath, dwTotalBytes))
break;
// Read the source file
if(!SFileReadFile(hMpqFile, Buffer, sizeof(Buffer), &dwBytesRead, NULL))
{
if(GetLastError() != ERROR_HANDLE_EOF)
{
nResult = E_EREAD;
break;
}
nResult = ERROR_SUCCESS;
bEndOfFile = true;
}
// Write the target file
if(!WriteFile(hLocFile, Buffer, dwBytesRead, &dwBytesWritten, NULL))
{
nResult = E_EWRITE;
break;
}
// Increment the total bytes
dwTotalBytes += dwBytesRead;
}
// Close the local file
CloseHandle(hLocFile);
}
else
{
nResult = E_ECREATE;
}
// Close the MPQ file
SFileCloseFile(hMpqFile);
}
else
{
nResult = E_EREAD;
}
}
return nResult;
}
int WINAPI ProcessFile(HANDLE hArchive, int nOperation, LPCSTR szDestPath, LPCSTR szDestName)
{
return ProcessFileW(hArchive, nOperation, TAnsiToWide(szDestPath), TUTF8ToWide(szDestName));
}
//-----------------------------------------------------------------------------
// Totalcmd calls ReadHeader to find out what files are in the archive.
// https://www.ghisler.ch/wiki/index.php?title=ReadHeader
static void StoreFileName(TOpenMpqInfo * pInfo, void * pvBuffer, size_t ccBuffer, bool bWideName)
{
if(bWideName == false)
{
LPSTR szBuffer = (LPSTR)(pvBuffer);
StringCchCopyExA(szBuffer, ccBuffer, pInfo->sf.cFileName, &szBuffer, &ccBuffer, 0);
if(pInfo->sf.lcLocale && g_cfg.bAddLocaleToName)
{
StringCchPrintfA(szBuffer, ccBuffer, " (%04lX)", pInfo->sf.lcLocale);
}
}
else
{
LPWSTR szBuffer = (LPWSTR)(pvBuffer);
StringCchCopyExW(szBuffer, ccBuffer, TUTF8ToWide(pInfo->sf.cFileName), &szBuffer, &ccBuffer, 0);
if(pInfo->sf.lcLocale && g_cfg.bAddLocaleToName)
{
StringCchPrintfW(szBuffer, ccBuffer, L" (%04lX)", pInfo->sf.lcLocale);
}
}
}
template <typename HDR>
static void StoreFoundFile(TOpenMpqInfo * pInfo, HDR * pHeaderData, bool bWideName)
{
// Store the file name
StoreFileName(pInfo, pHeaderData->FileName, _countof(pHeaderData->FileName), bWideName);
// Store the file time
pHeaderData->FileTime = pInfo->ArchiveTime;
if(pInfo->sf.dwFileTimeHi != 0 || pInfo->sf.dwFileTimeLo != 0)
FileTimeToDosFTime(pHeaderData->FileTime, pInfo->sf.dwFileTimeHi, pInfo->sf.dwFileTimeLo);
// Store the file sizes
pHeaderData->UnpSize = pInfo->sf.dwFileSize;
pHeaderData->PackSize = pInfo->sf.dwCompSize;
// Store file attributes
pHeaderData->FileAttr = FILE_ATTRIBUTE_ARCHIVE;
if(pInfo->sf.dwFileFlags & MPQ_FILE_COMPRESS_MASK)
pHeaderData->FileAttr |= FILE_ATTRIBUTE_COMPRESSED;
if(pInfo->sf.dwFileFlags & MPQ_FILE_ENCRYPTED)
pHeaderData->FileAttr |= FILE_ATTRIBUTE_ENCRYPTED;
pInfo->FoundFile[pInfo->sf.dwBlockIndex] = TRUE;
}
template <typename HDR>
int ReadHeaderTemplate(HANDLE hArchive, HDR * pHeaderData, bool bWideName = false)
{
TOpenMpqInfo * pInfo;
// Check the right parameters
if((pInfo = IsValidArchiveHandle(hArchive)) != NULL)
{
// Split the action
for(;;)
{
switch(pInfo->LoadState)
{
case LOADSTATE_FIND_FIRST:
pInfo->hFind = SFileFindFirstFile(pInfo->hMPQ, "*", &pInfo->sf, NULL);
pInfo->bResult = TRUE;
// If mothing has been found, go to the "FindNext" phase
if(pInfo->hFind == NULL)
{
pInfo->LoadState = LOADSTATE_COMPLETE;
break;
}
StoreFoundFile(pInfo, pHeaderData, bWideName);
pInfo->LoadState = LOADSTATE_FIND_NEXT;
return 0;
case LOADSTATE_FIND_NEXT:
// Search the next file
if(pInfo->hFind != NULL)
{
pInfo->bResult = SFileFindNextFile(pInfo->hFind, &pInfo->sf);
if(pInfo->bResult == FALSE)
{
pInfo->LoadState = LOADSTATE_COMPLETE;
break;
}
StoreFoundFile(pInfo, pHeaderData, bWideName);
return 0;
}
// Nothing has been found.
pInfo->LoadState = LOADSTATE_COMPLETE;
break;
case LOADSTATE_COMPLETE: // No more files in the archive.
default:
return E_END_ARCHIVE;
}
}
}
return E_NOT_SUPPORTED;
}
int WINAPI ReadHeader(HANDLE hArchive, THeaderData * pHeaderData)
{
// Use the common template function
return ReadHeaderTemplate(hArchive, pHeaderData);
}
int WINAPI ReadHeaderEx(HANDLE hArchive, THeaderDataEx * pHeaderData)
{
// Use the common template function
return ReadHeaderTemplate(hArchive, pHeaderData);
}
int WINAPI ReadHeaderExW(HANDLE hArchive, THeaderDataExW * pHeaderData)
{
// Use the common template function
return ReadHeaderTemplate(hArchive, pHeaderData, true);
}
//-----------------------------------------------------------------------------
// SetChangeVolProc(W) allows you to notify user about changing a volume when packing files
// https://www.ghisler.ch/wiki/index.php?title=SetChangeVolProc
// This function allows you to notify user
// about changing a volume when packing files
void WINAPI SetChangeVolProc(HANDLE /* hArchive */, PFN_CHANGE_VOLUMEA PfnChangeVol)
{
PfnChangeVolA = PfnChangeVol;
}
void WINAPI SetChangeVolProcW(HANDLE /* hArchive */, PFN_CHANGE_VOLUMEW PfnChangeVol)
{
PfnChangeVolW = PfnChangeVol;
}
//-----------------------------------------------------------------------------
// SetProcessDataProc(W) allows you to notify user about the progress when you un/pack files
// Note that Total Commander may use INVALID_HANDLE_VALUE for the hArchive parameter
// https://www.ghisler.ch/wiki/index.php?title=SetProcessDataProc
void WINAPI SetProcessDataProc(HANDLE /* hArchive */, PFN_PROCESS_DATAA PfnProcessData)
{
PfnProcessDataA = PfnProcessData;
}
void WINAPI SetProcessDataProcW(HANDLE /* hArchive */, PFN_PROCESS_DATAW PfnProcessData)
{
PfnProcessDataW = PfnProcessData;
}
//-----------------------------------------------------------------------------
// PackFiles(W) specifies what should happen when a user creates, or adds files to the archive
// https://www.ghisler.ch/wiki/index.php?title=PackFiles
typedef struct _SFILE_CALLBACK_DATA
{
LPCWSTR szFileName;
DWORD dwWritten;
} SFILE_CALLBACK_DATA, * PSFILE_CALLBACK_DATA;
static void WINAPI AddFileCallback(void * pvUserData, DWORD dwBytesWritten, DWORD dwTotalBytes, bool bFinalCall)
{
PSFILE_CALLBACK_DATA pCBData = (PSFILE_CALLBACK_DATA)pvUserData;
UNREFERENCED_PARAMETER(dwTotalBytes);
UNREFERENCED_PARAMETER(bFinalCall);
// Inform Total Commander about what are we doing
CallProcessDataProc(pCBData->szFileName, dwBytesWritten - pCBData->dwWritten);
pCBData->dwWritten = dwBytesWritten;
}
static DWORD FixupMaxFileCount(LPCWSTR szAddList, DWORD dwMaxFileCount)
{
LPCWSTR szAddFile;
DWORD dwFilesToAdd = 0;
for(szAddFile = szAddList; *szAddFile != 0; szAddFile += wcslen(szAddFile) + 1)
{
if(++dwFilesToAdd > dwMaxFileCount)
{
dwMaxFileCount *= 2;
}
}
return dwMaxFileCount;
}
static LPWSTR NewMulStr(LPCSTR szMulStr)
{
LPCSTR szSaveMulStr = szMulStr;
LPWSTR szMulStrW = NULL;
size_t nLength = 0;
if(szMulStr != NULL)
{
// Count the length of the multi string
while(szMulStr[0] != 0)
{
szMulStr = szMulStr + strlen(szMulStr) + 1;
}
// Allocate space
nLength = (szMulStr - szSaveMulStr + 1);
if((szMulStrW = new WCHAR[nLength]) != NULL)
{
MultiByteToWideChar(CP_ACP, 0, szSaveMulStr, (int)nLength, szMulStrW, (int)nLength);
}
}
return szMulStrW;
}
static bool IsFileExcludedFromCompressionEncryption(LPCTSTR szFileName)
{
LPCTSTR szExcludedTypes = g_cfg.szExcludedTypes;
LPCTSTR szExt;
size_t nLength;
// Get the file extension
szExt = GetFileExtension(szFileName);
if(szExt++ != NULL)
{
nLength = _tcslen(szExt);
while(*szExcludedTypes != 0)
{
// Skip spaces
while(0 < *szExcludedTypes && *szExcludedTypes <= 0x20)
szExcludedTypes++;
// Compare the extension
if(!_tcsnicmp(szExcludedTypes, szExt, nLength))
{
if(szExcludedTypes[nLength] == ';' || szExcludedTypes[nLength] == 0)
return true;
}
// Skip the current extension
while(*szExcludedTypes != 0 && *szExcludedTypes != ';')
szExcludedTypes++;
while(*szExcludedTypes == ';')
szExcludedTypes++;
}
}
// Not in any of the excluded types
return false;
}
static bool OpenOrCreateArchive(LPCWSTR szPackedFile, DWORD dwCreateFlags, DWORD dwMaxFileCount, PHANDLE PtrMPQ)
{
// Try to open an existing archive
if(SFileOpenArchive(szPackedFile, 0, GetStreamFlagsFromFileName(szPackedFile, false), PtrMPQ))
return true;
// ERROR_FILE_NOT_FOUND: Create new archive
// ERROR_BAD_FORMAT: Convert existing file to a MPQ archive
if(SFileCreateArchive(szPackedFile, dwCreateFlags, dwMaxFileCount, PtrMPQ))
return true;
return false;
}
static int PackFileToArchiveW(
HANDLE hMPQ,
LPCWSTR szTrgPath,
LPCWSTR szSrcPath,
DWORD dwMpqFlags,
DWORD dwDataCompression,
int nFlags)
{
SFILE_CALLBACK_DATA CBData = {0};
// Prepare the add callback
CBData.szFileName = szSrcPath;
CBData.dwWritten = 0;
SFileSetAddFileCallback(hMPQ, AddFileCallback, &CBData);
// Check if the file is one of the files excluded from compression and encryption
if(IsFileExcludedFromCompressionEncryption(szSrcPath))
dwMpqFlags &= ~(MPQ_FILE_COMPRESS_MASK | MPQ_FILE_ENCRYPTED);
// Set the locale that the file should have
SFileSetLocale(g_cfg.lcFileLocale);
// Add the file to the archive
if(SFileAddFileExW(hMPQ, szSrcPath, szTrgPath, dwMpqFlags, dwDataCompression, dwDataCompression))
{
if(nFlags & PK_PACK_MOVE_FILES)
DeleteFile(szSrcPath);
return ERROR_SUCCESS;
}
return E_EWRITE;
}
static int PackFilesToArchiveW(
HANDLE hMPQ,
LPCWSTR szSubPath,
LPCWSTR szSrcPath,
LPCWSTR szAddList,
DWORD dwDefMpqFlags,
DWORD dwDefDataCompression,
int nFlags)
{
TCHAR szSourcePath[MAX_PATH];
TCHAR szTargetPath[MAX_PATH];
int nResult = 0;
// Now pass the add list
for(LPCWSTR szAddFile = szAddList; szAddFile[0] != 0 && nResult == 0; szAddFile += wcslen(szAddFile) + 1)
{
// Create full path of the source file
MergePath(szSourcePath, _countof(szSourcePath), szSrcPath, szAddFile);
MergePath(szTargetPath, _countof(szTargetPath), szSubPath, szAddFile);
// Check the attributes. When there's a folder, we ignore it, because
// Total Commander includes all files in the folder after the folder name
if(GetFileAttributes(szSourcePath) & FILE_ATTRIBUTE_DIRECTORY)
continue;
// Tell Total Commander what file we are adding
if(!CallProcessDataProc(szAddFile, 0))
break;
// Add the file to the archive
nResult = PackFileToArchiveW(hMPQ,
szTargetPath,
szSourcePath,
dwDefMpqFlags,
dwDefDataCompression,
nFlags);
}
return nResult;
}
int WINAPI PackFilesW(LPCWSTR szPackedFile, LPCWSTR szSubPath, LPCWSTR szSrcPath, LPCWSTR szAddList, int nFlags)
{
HANDLE hMPQ = NULL;
DWORD dwDefDataCompression = 0;
DWORD dwMaxFileCount = g_cfg.dwMaxFileCount;
DWORD dwCreateFlags = MPQ_CREATE_ATTRIBUTES;
DWORD dwDefMpqFlags = MPQ_FILE_REPLACEEXISTING;
int nResult = E_NOT_SUPPORTED;
// Test the valid parameters
if(szPackedFile && szPackedFile[0] && szAddList && szAddList[0])
{
// Count the files. If count is bigger than hash table size,
// increment the hash table size.
dwMaxFileCount = FixupMaxFileCount(szAddList, g_cfg.dwMaxFileCount);
// Choose the data compression method
switch(g_cfg.dwCompression)
{
case FILE_COMPRESSION_INFLATE:
dwDefDataCompression = MPQ_COMPRESSION_PKWARE;
dwDefMpqFlags |= MPQ_FILE_COMPRESS;
break;
case FILE_COMPRESSION_ZLIB:
dwDefDataCompression = MPQ_COMPRESSION_ZLIB;
dwDefMpqFlags |= MPQ_FILE_COMPRESS;
break;
case FILE_COMPRESSION_BZIP2:
dwDefDataCompression = MPQ_COMPRESSION_BZIP2;
dwDefMpqFlags |= MPQ_FILE_COMPRESS;
break;
case FILE_COMPRESSION_LZMA:
dwDefDataCompression = MPQ_COMPRESSION_LZMA;
dwDefMpqFlags |= MPQ_FILE_COMPRESS;
break;
}
// Choose the encryption
if(g_cfg.bEncryptAddedFiles)
dwDefMpqFlags |= MPQ_FILE_ENCRYPTED;
// Choose archive format
if(g_cfg.bCreateMpqV2)
dwCreateFlags |= MPQ_CREATE_ARCHIVE_V2;
// Check the file name and get the appropriate flags
if(OpenOrCreateArchive(szPackedFile, dwCreateFlags, dwMaxFileCount, &hMPQ))
{
// (Recursively) add all files to that archive
nResult = PackFilesToArchiveW(hMPQ,
szSubPath,
szSrcPath,
szAddList,
dwDefMpqFlags,
dwDefDataCompression,
nFlags);
SFileCloseArchive(hMPQ);
}
else
{
nResult = E_BAD_ARCHIVE;
}
}
return nResult;
}
// PackFiles adds file(s) to an archive
int WINAPI PackFiles(LPCSTR szPackedFile, LPCSTR szSubPath, LPCSTR szSrcPath, LPCSTR szAddList, int nFlags)
{
LPWSTR szAddListW;
int nResult = E_OUTOFMEMORY;
if((szAddListW = NewMulStr(szAddList)) != NULL)
{
nResult = PackFilesW(TAnsiToWide(szPackedFile),
TAnsiToWide(szSubPath),
TAnsiToWide(szSrcPath),
szAddListW,
nFlags);
delete [] szAddListW;
}
return nResult;
}
//-----------------------------------------------------------------------------
// DeleteFiles(W) should delete the specified files from the archive
// https://www.ghisler.ch/wiki/index.php?title=DeleteFiles
static LPWSTR ExtractLocaleFromFileName(LPCWSTR szFileNameWithLocale, LCID * PtrLocale)
{
LCID lcLocale = 0;
LPWSTR szLocalePtr;
LPWSTR szLocaleEnd;
LPWSTR szFileName;
LPWSTR szTemp;
size_t nLength;
// Create the copy of the file name
if((szFileName = NewStr(szFileNameWithLocale)) != NULL)
{
// We expect the format "%s (%u)"
if((nLength = wcslen(szFileName)) > 0)
{
// Does the name end with closing parenthesis?
if(szFileName[nLength - 1] == ')')
{
// Walk back as long as we found a digit
szLocaleEnd = szFileName + nLength - 1;
szLocalePtr = szLocaleEnd - 1;
while(szLocalePtr > szFileName && isxdigit(szLocalePtr[0]))
szLocalePtr--;
// Did we encounter an opening parenthesis?
if(szLocalePtr > (szFileName + 1) && szLocalePtr[0] == '(' && szLocalePtr[-1] == ' ' && (szLocaleEnd - szLocalePtr) == 5)
{
// Convert the locale to integer
lcLocale = wcstol(szLocalePtr + 1, &szTemp, 16);
// Cut the locale from the file name
szLocalePtr[-1] = 0;
}
}
}
// Give the locale to the caller
if(PtrLocale != NULL)
{
PtrLocale[0] = lcLocale;
}
}
return szFileName;
}
static int RemoveFiles(HANDLE hMpq, LPCWSTR szMask, LPCWSTR szListFile, LCID lcLocale)
{
SFILE_FIND_DATA sf;
HANDLE hFind;
BOOL bFileFound = TRUE;
int nResult = ERROR_SUCCESS;
// We always need to search the archive for the file mask
hFind = SFileFindFirstFileW(hMpq, szMask, &sf, szListFile);
if(hFind != NULL)
{
// Keep deleting as long as there is something to delete
while(bFileFound)
{
// Only delete files with matching locale
if(lcLocale == 0 || sf.lcLocale == lcLocale)
{
// Set the proper file locale
SFileSetLocale(lcLocale);
// Attempt to remove the file
if(!SFileRemoveFile(hMpq, sf.cFileName, SFILE_OPEN_FROM_MPQ))
{
nResult = E_BAD_DATA;
break;
}
}
// Attempt to find the next file
bFileFound = SFileFindNextFile(hFind, &sf);
}
// Close the search handle
SFileFindClose(hFind);
}
return nResult;
}
// Delete files from the archive
int WINAPI DeleteFilesW(LPCWSTR szPackedFile, LPCWSTR szDeleteList)
{
HANDLE hMpq = NULL;
LPCTSTR szListFile = (g_cfg.szListFile[0] != 0) ? g_cfg.szListFile : NULL;
LPCWSTR szFileToDelete = NULL;
LPCWSTR szDeleteEntry = NULL;
int nDeleteCount = 0;
int nResult = ERROR_SUCCESS;
// Test the valid parameters
if(szPackedFile && szPackedFile[0] && szDeleteList && szDeleteList[0])
{
// Open the archive
if(SFileOpenArchive(szPackedFile, 0, 0, &hMpq))
{
// Delete all files from the archive.
for(szDeleteEntry = szDeleteList; szDeleteEntry[0] != 0; szDeleteEntry = szDeleteEntry + wcslen(szDeleteEntry) + 1)
{
LCID lcLocale = 0;
// Tell Total Commander what we are doing
if(!CallProcessDataProc(szDeleteEntry, nDeleteCount++))
break;
// If we allow the locale to be part of file name, we have to extract it from there
szFileToDelete = szDeleteEntry;
if(g_cfg.bAddLocaleToName)
szFileToDelete = ExtractLocaleFromFileName(szDeleteEntry, &lcLocale);
// First try internal listfile
RemoveFiles(hMpq, szFileToDelete, NULL, lcLocale);
// Then external listfile, if any
if(g_cfg.szListFile[0] != 0)
RemoveFiles(hMpq, szFileToDelete, g_cfg.szListFile, lcLocale);
// Free allocated memory
if(szFileToDelete != szDeleteEntry)
delete[] szFileToDelete;
}
// Compact the archive
if(nResult == ERROR_SUCCESS && g_cfg.bCompactAfterDelete)
SFileCompactArchive(hMpq, szListFile, false);
// Close the archive and exit
SFileCloseArchive(hMpq);
}
else
{
nResult = E_BAD_ARCHIVE;
}
}
else
{
nResult = E_NOT_SUPPORTED;
}
return nResult;
}
int WINAPI DeleteFiles(LPCSTR szPackedFile, LPCSTR szDeleteList)
{
LPWSTR szDeleteListW;
int nResult = E_OUTOFMEMORY;
if((szDeleteListW = NewMulStr(szDeleteList)) != NULL)
{
nResult = DeleteFilesW(TAnsiToWide(szPackedFile), szDeleteListW);
delete [] szDeleteListW;
}
return nResult;
}
//-----------------------------------------------------------------------------
// ConfigurePacker gets called when the user clicks the Configure button
// from within "Pack files..." dialog box in Totalcmd.
// https://www.ghisler.ch/wiki/index.php?title=ConfigurePacker
void WINAPI ConfigurePacker(HWND hParent, HINSTANCE hDllInstance)
{
UNREFERENCED_PARAMETER(hDllInstance);
SettingsDialog(hParent);
}
//-----------------------------------------------------------------------------
// PackSetDefaultParams is called immediately after loading the DLL, before
// any other function. This function is new in version 2.1. It requires Total
// Commander >=5.51, but is ignored by older versions.
// https://www.ghisler.ch/wiki/index.php?title=PackSetDefaultParams
void WINAPI PackSetDefaultParams(TPackDefaultParamStruct * dps)
{
// Set default configuration.
SetDefaultConfiguration();
g_szIniFile[0] = 0;
// If INI file, load it from it too.
if(dps != NULL && dps->DefaultIniName[0])
{
StringCchCopyX(g_szIniFile, _countof(g_szIniFile), dps->DefaultIniName);
LoadConfiguration();
}