-
Notifications
You must be signed in to change notification settings - Fork 1
/
UMainForm.cpp
1913 lines (1822 loc) · 60.4 KB
/
UMainForm.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
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "UMainForm.h"
#include "USettingsForm.h"
#include "StormLib.h"
#include "URecursiveSearch.hpp"
#include "IniFiles.hpp"
#include "UFileLock.h"
#include "UBinaryFile.h"
#define WriteLog MainForm->mmoLog->Lines->Add
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "JvExMask"
#pragma link "JvToolEdit"
#pragma link "ZipForge"
#pragma resource "*.dfm"
TMainForm *MainForm;
//#define FileNameIsValid(x) (__FileNameIsValid(x) || IsImportFile(x.LowerCase()))
//#define FileNameIsValid __FileNameIsValid
//---------------------------------------------------------------------------
unsigned int nLastUnpackedFilesCount = 0;
// TLockedFile* IniLock = NULL;
bool bDebugSaveNames = false;
bool bFullAnalyze = false;
const DWORD nDefaultMapNameSize = 200;
UnicodeString __SourceMap = "", __DestMap = "";
TMPQRepackerStatus __Status = mrsIdle;
//---------------------------------------------------------------------------
inline DWORD CompIndexToCompressionFlag(TMPQCompression c)
{
switch(c)
{
case mcHUFFMAN:
return MPQ_COMPRESSION_HUFFMANN;
case mcZLIB:
return MPQ_COMPRESSION_ZLIB;
case mcPKWARE:
return MPQ_COMPRESSION_PKWARE;
case mcBZIP2:
return MPQ_COMPRESSION_BZIP2;
case mcSPARSE:
return MPQ_COMPRESSION_SPARSE;
case mcLZMA:
return MPQ_COMPRESSION_LZMA;
default:
return NULL;
}
}
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
//IniLock = new TLockedFile(IniPath);
//IniLock->Lock();
frmSettings = new TfrmSettings(this);
mmoLog->Clear();
zpfrgPacker->BaseDir = AppPath;
edtTempDir->Text = GetTempDir() + "MPQRepackerTemp\\";
FindCheatPacks();
LoadSettings();
}
bool StreamToStrings(TStream* From, TStrings* To, bool Clear = false)
{
try
{
char* szBuffer = (char*)malloc(From->Size);
From->Position = 0;
From->Read(szBuffer, From->Size);
if(Clear) To->Clear();
To->Text = To->Text + UnicodeString(szBuffer);
free(szBuffer);
return true;
}
catch(...){return false;}
}
void RemoveDoubles(TStrings* S)
{
return; // Íå âûïîëíÿòü ôóíêöèþ
TStrings* __S = new TStringList();
for(int i = 0; i < S->Count-1; i++)
if(!StrInList(__S, S->Strings[i])) __S->Add(S->Strings[i]);
S->Clear();
S->Text = __S->Text;
VCL_FREE(__S);
}
DWORD ReadFileHeader(const UnicodeString File)
{
DWORD dw = 0;
Classes::TFileStream* fs = new Classes::TFileStream(File, fmOpenRead);
fs->Read(&dw, 4);
VCL_FREE(fs);
return dw;
}
void AnalyzeAllUnknown(TStrings* List, const UnicodeString Archive)
{
const UnicodeString TempDir = GetTempDir() + "MPQRepacker\\";
__MkDir(TempDir);
TStrings* __s = new TStringList(), *s = new TStringList();
ExtractUnknownFiles(Archive, __s, TempDir, false);
for(int i = 0; i < __s->Count; i++)
{
Application->ProcessMessages();
if(FileExists(TempDir + __s->Strings[i]))
{
// WriteLog("Àíàëèç " + __s->Strings[i] + "...");
bool bResult = false;
const UnicodeString File = TempDir + __s->Strings[i];
const UnicodeString Ext = ExtractFileExt(File);
if(Ext == ".slk" || Ext == ".blp") continue;
if(Ext == ".mdx" || Ext == ".xxx" || __s->Strings[i].Pos("war3map")) bResult = AnalyzeBinaryFile(s, Archive, File, true);
if(Ext == ".txt") bResult = AnalyzeTextFile(s, Archive, File);
if(bResult)
{
WriteLog("Àíàëèç " + __s->Strings[i] + "...");
AutoSearch(s, Archive);
MergeLists(s, List);
if(FileExists(File)) DeleteFile(File);
}
s->Clear();
}
}
//AutoSearch(s, Archive);
s->Free();
__s->Free();
}
void AnalyzeAllModels(TStrings* FileList, const UnicodeString Archive) // Àíàëèçèðóåò âñå ôàéëû MDX è MDL (â íèõ ìîæíî íàéòè èìåíà ôàéëîâ òåêñòóð)
{
TStrings* s = new TStringList(), *__s = new TStringList();
for(int i = 0; i < FileList->Count; i++)
{
Application->ProcessMessages();
const UnicodeString Ext = ExtractFileExt(FileList->Strings[i]);
if(!StrInList(__s, FileList->Strings[i]) && (Ext == ".mdx" || Ext == ".mdl") && VerifyFile(Archive, FileList->Strings[i]))
{
WriteLog("Àíàëèç " + FileList->Strings[i] + "...");
AnalyzeBinaryFile(s, Archive, FileList->Strings[i]);
__s->Add(FileList->Strings[i]); // Ñïèñîê ïðîàíàëèçèðîâàííûõ ôàéëîâ
}
}
MergeLists(s, FileList);
__s->Free();
s->Free();
}
void AnalyzeAllTextFiles(TStrings* FileList, const UnicodeString Archive) // Àíàëèçèðóåò âñå ôàéëû TXT
{
TStrings* s = new TStringList(), *__s = new TStringList();
for(int i = 0; i < FileList->Count; i++)
{
Application->ProcessMessages();
const UnicodeString Ext = ExtractFileExt(FileList->Strings[i]);
if(!StrInList(__s, FileList->Strings[i]) && (Ext == ".txt"))
{
WriteLog("Àíàëèç " + FileList->Strings[i] + "...");
AnalyzeTextFile(s, Archive, FileList->Strings[i]);
__s->Add(FileList->Strings[i]); // Ñïèñîê ïðîàíàëèçèðîâàííûõ ôàéëîâ
}
}
MergeLists(s, FileList);
__s->Free();
s->Free();
}
bool AnalyzeAllSLK(TStrings* List, const UnicodeString Archive)
{
try
{
const UnicodeString Path = GetTempDir();
TStrings* s = new TStringList(), *analyzed = new TStringList();
const UnicodeString ScriptFile = Path + "analyze_tmp.slk";
LOOP(List->Count)
{
if(ExtractFileExt(List->Strings[i].LowerCase()) != ".slk") continue;
if(StrInList(analyzed, List->Strings[i])) continue;
if(ExtractFile(Archive, List->Strings[i], ScriptFile) != ERROR_SUCCESS) continue;
WriteLog("Àíàëèç " + ExtractFileName(List->Strings[i]) + "...");
if(!FileExists(ScriptFile))
{
s->Free();
return false;
}
else s->LoadFromFile(ScriptFile);
UnicodeString ScriptText = s->Text;
s->Clear();
// Èùåì âñå ñòðîêîâûå êîíñòàíòû (õóéíþ â äâîéíûõ êàâû÷êàõ):
int Pos1 = PosEx("\"", ScriptText, 1), Pos2 = PosEx("\"", ScriptText, Pos1 + 1);
while((Pos1 && Pos2) && (Pos1 < ScriptText.Length() && Pos2 < ScriptText.Length()))
{
try
{
if(ScriptText[Pos1 + 1] != '"')
{
const int nStrSize = (Pos2 - Pos1 - 1);
const UnicodeString Str = ReplaceStr(ScriptText.SubString(Pos1 + 1, nStrSize), "\\\\", "\\");
if(FileNameIsValid(Str)) s->Add(Str);
}
Pos1 = PosEx("\"", ScriptText, Pos2 + 1);
Pos2 = PosEx("\"", ScriptText, Pos1 + 1);
}
catch(...){break;}
}
analyzed->Add(List->Strings[i]);
if(bDebugSaveNames) s->SaveToFile(GetTempDir() + "MPQRepacker\\" + ExtractFileName(List->Strings[i]) + ".txt"); // Îòëàäêà
AutoSearch(s, Archive);
MergeLists(s, List);
DeleteFile(ScriptFile);
}
VCL_FREE(s);
VCL_FREE(analyzed);
return true;
}
catch(...){return false;}
}
void AutoSearch(TStrings* List, const UnicodeString Archive)
{
TStrings* s = new TStringList();
for(int i = 0; i < List->Count; i++)
{
// Óãîëîê êîïèïàñòà
Application->ProcessMessages();
if(List->Strings[i].Pos("BTN")) // Èêîíêè êíîïîê
{
UnicodeString ReplacedStr = " ";
//----------------------------------------------------
ReplacedStr = ReplaceStr(List->Strings[i], "BTN", "DISBTN");
if(!StrInList(s, ReplacedStr) && FileNameIsValid(ReplacedStr)) s->Add(ReplacedStr);
//----------------------------------------------------
ReplacedStr = ReplaceStr(List->Strings[i], "CommandButtons\\BTN", "CommandButtonsDisabled\\DISBTN");
if(!StrInList(s, ReplacedStr) && FileNameIsValid(ReplacedStr)) s->Add(ReplacedStr);
//----------------------------------------------------
ReplacedStr = ReplaceStr(List->Strings[i], "CommandButtons\\BTN", "PassiveButtons\\PASBTN");
if(!StrInList(s, ReplacedStr) && FileNameIsValid(ReplacedStr)) s->Add(ReplacedStr);
//----------------------------------------------------
ReplacedStr = ReplaceStr(List->Strings[i], "CommandButtonsDisabled\\DISBTN", "CommandButtons\\BTN");
if(!StrInList(s, ReplacedStr) && FileNameIsValid(ReplacedStr)) s->Add(ReplacedStr);
//----------------------------------------------------
ReplacedStr = ReplaceStr(List->Strings[i], "PassiveButtons\\PASBTN", "CommandButtons\\BTN");
if(!StrInList(s, ReplacedStr) && FileNameIsValid(ReplacedStr)) s->Add(ReplacedStr);
//----------------------------------------------------
ReplacedStr = ReplaceStr(List->Strings[i], "CommandButtonsDisabled\\DISBTN", "PassiveButtons\\PASBTN");
if(!StrInList(s, ReplacedStr) && FileNameIsValid(ReplacedStr)) s->Add(ReplacedStr);
//----------------------------------------------------
ReplacedStr = ReplaceStr(List->Strings[i], "PassiveButtons\\PASBTN", "CommandButtonsDisabled\\DISBTN");
if(!StrInList(s, ReplacedStr) && FileNameIsValid(ReplacedStr)) s->Add(ReplacedStr);
//----------------------------------------------------
}
if(ExtractFileExt(List->Strings[i]) == ".tga" || ExtractFileExt(List->Strings[i]) == ".blp" || ExtractFileExt(List->Strings[i]) == ".mdl" || ExtractFileExt(List->Strings[i]) == ".mdx")
{
UnicodeString ReplacedStr = " ";
//----------------------------------------------------
ReplacedStr = ChangeFileExt(List->Strings[i], ".mdx");
if(!StrInList(s, ReplacedStr) && FileNameIsValid(ReplacedStr)) s->Add(ReplacedStr);
//----------------------------------------------------
ReplacedStr = ChangeFileExt(List->Strings[i], ".mdl");
if(!StrInList(s, ReplacedStr) && FileNameIsValid(ReplacedStr)) s->Add(ReplacedStr);
//----------------------------------------------------
ReplacedStr = ChangeFileExt(List->Strings[i], ".blp");
if(!StrInList(s, ReplacedStr) && FileNameIsValid(ReplacedStr)) s->Add(ReplacedStr);
//----------------------------------------------------
ReplacedStr = ChangeFileExt(List->Strings[i], ".tga");
if(!StrInList(s, ReplacedStr) && FileNameIsValid(ReplacedStr)) s->Add(ReplacedStr);
//----------------------------------------------------
}
if(!StrInList(s, List->Strings[i]) && FileNameIsValid(List->Strings[i])) s->Add(List->Strings[i]);
}
RetrieveKnownFiles(s, Archive);
List->Text = s->Text;
s->Free();
}
void InsertStrToBuffer(const UnicodeString Str, char* szBuffer, const unsigned int Offset)
{
for(int i = 0; i < Str.Length(); i++)
{
szBuffer[i + Offset] = Str[i+1];
}
}
inline bool IsImportFile(const UnicodeString File)
{
const UnicodeString Ext = ExtractFileExt(File);
if(Ext == ".mp3" || Ext == ".mdx" || Ext == ".mdl" || Ext == ".tga" || Ext == ".blp" || Ext == ".avi" || Ext == ".ogg") return true;
else return false;
}
void GenerateWar3mapIMP(TStrings* FileList, const UnicodeString Archive, const UnicodeString Dir)
{
__MkDir(Dir);
const UnicodeString ImpPath = Dir + "war3map.imp";
if(FileExists(ImpPath)) DeleteFile(ImpPath);
TStrings* __s = new TStringList();
Classes::TFileStream* fs = new Classes::TFileStream(ImpPath, fmCreate);
BYTE szFirstLine[8] = {0x01, 0x00, 0x00, 0x00, 0x86, 0x00, 0x00, 0x00};
fs->Write(szFirstLine, 8);
for(int i = 0; i < FileList->Count; i++)
{
Application->ProcessMessages();
if(FileList->Strings[i].Length()
&& IsImportFile(FileList->Strings[i])
&& !StrInList(__s, FileList->Strings[i])
&& VerifyFile(Archive, FileList->Strings[i]))
{
const unsigned int nBufferSize = FileList->Strings[i].Length() + 2;
BYTE* szBuffer = (BYTE*)malloc(nBufferSize);
szBuffer[0] = 0x0D;
InsertStrToBuffer(FileList->Strings[i], (char*)szBuffer, 1);
szBuffer[nBufferSize - 1] = 0x00;
fs->Write(szBuffer, nBufferSize);
free(szBuffer);
__s->Add(FileList->Strings[i]);
}
}
fs->Position = 0;
const int nVersion = 0x01;
const int nCount = __s->Count;
fs->Write(&nVersion, 4);
fs->Write(&nCount, 4);
fs->Free();
__s->Free();
}
unsigned int GetFileCount(UnicodeString Archive)
{
char * szArchiveName = Archive.t_str();
HANDLE hMpq = NULL; // Open archive handle
int nError = ERROR_SUCCESS; // Error value
unsigned int nFileCount = 0; // Result
// Open an archive, e.g. "d2music.mpq"
if(nError == ERROR_SUCCESS)
{
if(!SFileOpenArchive(szArchiveName, 0, 0, &hMpq))
nError = GetLastError();
}
if(nError == ERROR_SUCCESS)
{
DWORD pcbLengthNeeded = sizeof(unsigned int);
if(!SFileGetFileInfo(hMpq, SFILE_INFO_NUM_FILES, &nFileCount, sizeof(unsigned int), &pcbLengthNeeded))
nError = GetLastError();
}
if(hMpq != NULL)
SFileCloseArchive(hMpq);
return nFileCount;
}
unsigned int GetFileCount(HANDLE hMPQ)
{
unsigned int nFileCount = 0; // Result
DWORD pcbLengthNeeded = sizeof(unsigned int);
if(!SFileGetFileInfo(hMPQ, SFILE_INFO_NUM_FILES, &nFileCount, sizeof(unsigned int), &pcbLengthNeeded))
return 0;
else return nFileCount;
}
inline double Ratio(double _100Proc, double _xProc)
{
if (_100Proc == 0) return 0;
try{return (_xProc / _100Proc) * 100;}
catch(Exception &e)
{
WriteLog(e.Message);
return 0;
}
}
UnicodeString GetUnknownFileExt(HANDLE hFile)
{
//-----------------------------------------------------------
const BYTE szBLPSignature[] = "BLP1\0";
const BYTE szMDXSignature[] = "MDLXV";
const BYTE szMP3Signature[] = {0xFF, 0xFB, 0x38, 0xC4, 0x00};
//-----------------------------------------------------------
BYTE szFirstBytes[5];
DWORD nReaded = 0;
SFileReadFile(hFile, szFirstBytes, 5, &nReaded, NULL);
// Msg(IntToStr((int)nReaded));
UnicodeString Result = ".xxx";
if(strncmp(szFirstBytes, szBLPSignature, 5) == 0) Result = ".blp";
if(strncmp(szFirstBytes, szMDXSignature, 5) == 0) Result = ".mdx";
if(strncmp(szFirstBytes, szMP3Signature, 5) == 0) Result = ".mp3";
return Result;
}
UnicodeString GetFileName(HANDLE hFile)
{
UnicodeString Result = "";
char __szBuffer[MAX_PATH];
char* szBuffer = __szBuffer;
try
{
SFileGetFileName(hFile, szBuffer);
Result = __SubString(szBuffer, 0, strlen(szBuffer));
}
catch(...){}
return Result;
}
DWORD GetFileSize(HANDLE hFile)
{
try
{
DWORD nSize = SFileGetFileSize(hFile, NULL);
return nSize;
}
catch(...){return 0;}
}
bool AddFilelistToMPQ(TStrings* List, HANDLE hMPQ)
{
try
{
if(hMPQ == NULL || List == NULL) return false;
const UnicodeString TempList = GetTempDir() + "~FLIST.tmp";
List->SaveToFile(TempList);
SFileAddListFile(hMPQ, UnicodeString(TempList).t_str());
if(FileExists(TempList)) DeleteFile(TempList);
return true;
}
catch(...) {return false;}
}
void InsertListToList(TStrings* Source, TStrings* Dest, unsigned int nInsertIndex)
{
for(int i = 0; i < Source->Count; i++)
{
Dest->Insert(nInsertIndex + i, Source->Strings[i]);
}
}
inline UnicodeString AddQuotes(const UnicodeString Source)
{
return "\"" + Source + "\"";
}
#ifndef ADVANCED_VCL_H
#define DeleteTransfers RemoveTransfers
inline UnicodeString RemoveTransfers(const UnicodeString Source)
{
UnicodeString Result = Source;
Result = ReplaceStr(Result, "\r", "");
Result = ReplaceStr(Result, "\n", "");
Result = ReplaceStr(Result, "\r\n", "");
return Result.Trim();
}
#endif
bool InsertCheats(const UnicodeString War3mapJ, const UnicodeString CP, UnicodeString Activator)
{
try
{
const UnicodeString CPPath = AppDataPath + "CheatPacks\\" + CP + "\\";
if(!DirectoryExists(CPPath)) return false;
if(!FileExists(War3mapJ)) return false;
TStrings* s = new TStringList(), *__s = new TStringList();
const UnicodeString DefaultActivator = RemoveTransfers(ReadFile(CPPath + "activator.txt"));
Activator = AddQuotes(Activator);
//s->LoadFromFile(War3mapJ);
s->Text = UTF8ReadFile(War3mapJ);
int nInsertIndex = FindStr(s, "globals") + 1;
__s->LoadFromFile(CPPath + "globals.txt");
InsertListToList(__s, s, nInsertIndex);
nInsertIndex = FindStr(s, "function main takes nothing returns nothing") + 1;
while(s->Strings[nInsertIndex].SubString(1,5) == "local") nInsertIndex++; // Ïîñëå ëîêàëîâ
__s->LoadFromFile(CPPath + "function main.txt");
InsertListToList(__s, s, nInsertIndex);
nInsertIndex = FindStr(s, "endglobals") + 1;
__s->LoadFromFile(CPPath + "endglobals.txt");
InsertListToList(__s, s, nInsertIndex);
s->Text = ReplaceStr(s->Text, DefaultActivator, Activator); \
char* szBuffer = UTF8String(s->Text).c_str();
UTF8WriteFile(War3mapJ, s->Text);
// s->SaveToFile(War3mapJ);
if(FileExists(CPPath + "strings.txt")) // GUI cheat pack
{
const UnicodeString War3mapWTS = ExtractFilePath(War3mapJ) + "war3map.wts";
s->Clear();
__s->LoadFromFile(CPPath + "strings.txt");
if(FileExists(War3mapWTS)) s->Text = UTF8ReadFile(War3mapWTS);
MergeLists(__s, s);
UTF8WriteFile(War3mapWTS, s->Text);
// s->SaveToFile(War3mapWTS);
}
s->Free();
__s->Free();
return true;
}
catch(Exception &e)
{
Msg("Îøèáêà â ôóíêöèè InsertCheats: " + e.Message, true);
return false;
}
}
BOOL AddFileToMPQWithSFMPQ(const UnicodeString Archive, const UnicodeString File, const UnicodeString ArcFile, const unsigned nMaxFiles)
{
try
{
const UnicodeString sfmpq_dll = GetTempDir() + "sfmpq_api.dll";
if(!ResUnpack("SFMPQ_dll_internal", sfmpq_dll, 0)) return FALSE;
LoadDLL(hDll, sfmpq_dll);
P_MpqOpenArchiveForUpdate __open = (P_MpqOpenArchiveForUpdate)GetProcAddress(hDll, "MpqOpenArchiveForUpdate");
P_MpqCloseUpdatedArchive __close = (P_MpqCloseUpdatedArchive)GetProcAddress(hDll, "MpqCloseUpdatedArchive");
P_MpqAddFileToArchiveEx __add = (P_MpqAddFileToArchiveEx)GetProcAddress(hDll, "MpqAddFileToArchiveEx");
MPQHANDLE hMpq = (*__open)(BUF(Archive), MOAU_OPEN_EXISTING, nMaxFiles);
BOOL bResult = (*__add)(hMpq, BUF(File), BUF(ArcFile), MAFA_ENCRYPT + MAFA_COMPRESS + MAFA_REPLACE_EXISTING, MAFA_COMPRESS_DEFLATE, Z_BEST_COMPRESSION);
if(HANDLE_VALID(hMpq)) (*__close)(hMpq, 0);
// Debug:
if(bResult) WriteLog("Ôàéë " + ArcFile + " óïàêîâàí â àðõèâ ñ ïîìîùüþ SFMPQ!");
else WriteLog("Îøèáêà óïàêîâêè ôàéëà ñ ïîìîùüþ SFMPQ.");
//--------
FreeDLL(hDll);
return bResult;
}
catch(...)
{
return FALSE;
}
}
void TMainForm::FindCheatPacks()
{
const UnicodeString Path = AppDataPath + "CheatPacks\\";
if(!DirectoryExists(Path))
{
__MkDir(Path);
const UnicodeString Archive = GetTempDir() + "CheatPacks.zip";
if(!ResUnpack("CheatPacks", Archive))
{
RemoveDir(Path);
return;
}
zpfrgPacker->BaseDir = Path;
zpfrgPacker->FileName = Archive;
zpfrgPacker->OpenArchive(fmOpenRead);
zpfrgPacker->ExtractFiles("*.txt", faAnyFile, "");
zpfrgPacker->CloseArchive();
}
cbbCheatPack->Clear();
TSearchRec s_rec;
int r = FindFirst(Path + "*", faDirectory, s_rec);
while (r == 0)
{
if(s_rec.Name != "." && s_rec.Name != "..") cbbCheatPack->Items->Add(s_rec.Name);
r = FindNext(s_rec);
}
if(cbbCheatPack->Items->Count < 1)
{
grpCheats->Enabled = false;
cbbCheatPack->Text = "×èòû íå íàéäåíû";
chkInsertCheats->Checked = false;
chkInsertCheats->Enabled = false;
}
else
{
grpCheats->Enabled = true;
}
cbbCheatPack->ItemIndex = 0;
}
bool ExtractUnknownFiles(UnicodeString Archive, TStrings* List, const UnicodeString Dir, bool bWriteLog)
{
char * szArchiveName = Archive.t_str();
HANDLE hMpq = NULL; // Open archive handle
int nError = ERROR_SUCCESS; // Result value
bool bResult = true;
// Open an archive, e.g. "d2music.mpq"
if(nError == ERROR_SUCCESS)
{
bResult = SFileOpenArchive(szArchiveName, 0, 0, &hMpq);
if(!bResult)
nError = GetLastError();
}
if(nError == ERROR_SUCCESS)
{
AddFilelistToMPQ(List, hMpq);
TFileEntry* FEntry = ((TMPQArchive*)hMpq)->pFileTable;
unsigned int nFileCount = GetFileCount(hMpq);
for(int i = 0; i < nFileCount; i++)
{
if(FEntry[i].szFileName == NULL) // Èìÿ ôàéëà íå îïðåäåëåíî
{
HANDLE hFile = NULL;
SFileOpenFileEx(hMpq, (const char*)FEntry[i].dwHashIndex, SFILE_OPEN_FROM_MPQ | SFILE_OPEN_BY_INDEX, &hFile);
if(hFile == INVALID_HANDLE_VALUE) continue;
const UnicodeString NewFilename = GetFileName(hFile);
if(FileNameIsValid(NewFilename) && !StrInList(List, NewFilename) && GetFileSize(hFile))
{
const char* szFileName = (Dir + NewFilename).t_str();
HANDLE hLocalFile = CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
char szBuffer[0x10000];
DWORD dwBytes = 1;
while(dwBytes > 0)
{
SFileReadFile(hFile, szBuffer, sizeof(szBuffer), &dwBytes, NULL);
if(dwBytes > 0)
WriteFile(hLocalFile, szBuffer, dwBytes, &dwBytes, NULL);
}
if(hLocalFile != NULL)
CloseHandle(hLocalFile);
if(FileExists(szFileName) && GetFileSize(szFileName) == 0) DeleteFile(szFileName);
else
{
if(bWriteLog) WriteLog("Ðàñïàêîâêà " + NewFilename + "...");
nLastUnpackedFilesCount++;
}
List->Add(NewFilename);
}
if(hFile != NULL)
SFileCloseFile(hFile);
}
}
bResult = true;
}
if(hMpq != NULL)
SFileCloseArchive(hMpq);
return bResult;
}
void RetrieveKnownFiles(TStrings* List, UnicodeString Archive)
{
char * szArchiveName = Archive.t_str();
HANDLE hMpq = NULL; // Open archive handle
int nError = ERROR_SUCCESS; // Result value
// Open an archive, e.g. "d2music.mpq"
if(nError == ERROR_SUCCESS)
{
if(!SFileOpenArchive(szArchiveName, 0, 0, &hMpq))
nError = GetLastError();
}
if(nError == ERROR_SUCCESS)
{
AddFilelistToMPQ(List, hMpq);
List->Clear();
TFileEntry* FEntry = ((TMPQArchive*)hMpq)->pFileTable;
unsigned int nFileCount = GetFileCount(hMpq);
for(int i = 0; i < nFileCount; i++)
{
if(FEntry[i].szFileName != NULL) List->Add(FEntry[i].szFileName);
}
}
if(hMpq != NULL)
SFileCloseArchive(hMpq);
}
inline void WriteChars(Classes::TFileStream* fs, BYTE __BYTE, const unsigned int nCount)
{
BYTE szBuffer[] = {__BYTE};
for(int i = 0; i < nCount; i++) fs->Write(szBuffer, 1);
}
inline void __Insert(BYTE* szSource, BYTE* szDest, unsigned int nCount, unsigned int nOffset)
{
for(int i = 0; i < nCount; i++)
{
szDest[i + nOffset] = szSource[i];
}
}
void WriteWar3Header(const UnicodeString File)
{
Classes::TFileStream* fs = new Classes::TFileStream(File, fmOpenReadWrite);
fs->Position = 0;
BYTE szStartBuffer[] = {0x48, 0x4D, 0x33, 0x57, 0x00, 0x00, 0x00, 0x00};
BYTE* szMediumBuffer = "MPQ RePacker";
BYTE szEndBuffer[] = {0x00, 0x10, 0xDC};
const unsigned int nMediumBufferSize = strlen(szMediumBuffer);
const unsigned int nHeaderSize = 8 + 3 + nMediumBufferSize;
const unsigned int nOldFileSize = fs->Size;
BYTE* szOldFile = (BYTE*)malloc(nOldFileSize);
BYTE* szHeader = (BYTE*)malloc(nHeaderSize);
fs->Read(szOldFile, nOldFileSize);
__Insert(szStartBuffer, szHeader, 8, 0);
__Insert(szMediumBuffer, szHeader, nMediumBufferSize, 8);
__Insert(szEndBuffer, szHeader, 3, nMediumBufferSize + 8);
fs->Position = 0;
fs->Size = 0;
fs->Write(szHeader, nHeaderSize);
WriteChars(fs, 0x00, 2);
WriteChars(fs, 0x01, 1);
WriteChars(fs, 0x00, 472);
fs->Write(szOldFile, nOldFileSize);
fs->Free();
free(szHeader);
free(szOldFile);
}
bool CreateMPQ(UnicodeString MPQPath, HANDLE* hMPQ, DWORD nMaxFileCount = 500)
{
try
{
char* szMPQPath = MPQPath.t_str();
bool bResult = false;
ResUnpack("DummyMap", MPQPath);
bResult = SFileOpenArchive(szMPQPath, 0, 0, hMPQ);
// bResult = SFileCreateArchive(szMPQPath, MPQ_CREATE_ARCHIVE_V1 & MPQ_CREATE_ATTRIBUTES, nMaxFileCount, hMPQ);
return bResult;
}
catch(...){return false;}
}
void ChooseRule(const UnicodeString File, DWORD* Flags, DWORD* Compression)
{
const UnicodeString Ext = ExtractFileExt(File);
if(Ext == ".wav")
{
*Flags = MPQ_FILE_COMPRESS + MPQ_FILE_ENCRYPTED;
*Compression = MPQ_COMPRESSION_HUFFMANN + MPQ_COMPRESSION_ADPCM_STEREO;
return;
}
if(Ext == ".blp" || Ext == ".mpq" || Ext == ".w3m")
{
*Flags = 0;
*Compression = 0;
return;
}
if(Ext == ".mdx" || Ext == ".slk" || Ext == ".tga")
{
*Flags = MPQ_FILE_COMPRESS;
*Compression = CompIndexToCompressionFlag(nCompression);
return;
}
// Êàêîé-òî äðóãîé ôàéë
*Flags = MPQ_FILE_COMPRESS + MPQ_FILE_ENCRYPTED;
*Compression = CompIndexToCompressionFlag(nCompression);
}
bool AddFileToMPQ(HANDLE hMPQ, UnicodeString File, UnicodeString ArcFile)
{
try
{
DWORD dwFlags = 0, dwCompression = 0;
ChooseRule(File, &dwFlags, &dwCompression);
char *szFile = File.t_str(), *szArcFile = ArcFile.t_str();
bool bResult = SFileAddFileEx(hMPQ, szFile, szArcFile, MPQ_FILE_REPLACEEXISTING + dwFlags, dwCompression);
return bResult;
}
catch(Exception &e){WriteLog(e.Message); return false;}
}
void WriteWar3mapW3I(const UnicodeString Dir)
{
const UnicodeString FullPath = Dir + "war3map.w3i";
const BYTE War3mapW3I[] =
{
#include "war3map.w3i.inc"
};
if(FileExists(FullPath)) DeleteFile(FullPath);
Classes::TFileStream* fs = new Classes::TFileStream(FullPath, fmCreate);
fs->Write(War3mapW3I, sizeof(War3mapW3I));
fs->Free();
fs = NULL;
}
void WriteWar3mapUnitsDOO(const UnicodeString Dir)
{
const UnicodeString FullPath = Dir + "war3mapUnits.doo";
const BYTE War3mapUnitsDOO[] =
{
#include "war3mapUnits.doo.inc"
};
if(FileExists(FullPath)) return; // Íå çàìåíÿòü
else
{
Classes::TFileStream* fs = new Classes::TFileStream(FullPath, fmCreate);
fs->Write(War3mapUnitsDOO, sizeof(War3mapUnitsDOO));
fs->Free();
fs = NULL;
}
}
bool DeobfuscateJass(TStrings* JFile)
{
for(unsigned int i = 0; i < JFile->Count; i++)
{
// UnicodeString First7 = JFile->Strings[i].SubString(1,7);
// if(First7 != "functio" && First7 != "endfunc"
// && First7 != "globals" && First7 != "endglob")
// {
// if(JFile->Strings[i].Length() && JFile->Strings[i][1] != '\t') JFile->Strings[i] = "\t" + JFile->Strings[i];
// }
//elseJFile->Insert(i, ""); // Âñòàâëÿåì ïóñòóþ ñòðîêó
if(JFile->Strings[i] == "function main takes nothing returns nothing") JFile->Strings[i] == "function main2 takes nothing returns nothing";
}
return true;
}
inline void MoveFile(UnicodeString From, UnicodeString To)
{
if(FileExists(To)) DeleteFile(To);
MoveFile((const char*)From.t_str(), (const char*)To.t_str());
}
bool CreateMapWithXDep(const UnicodeString CreatedMap, const UnicodeString DirWithFiles, bool bWriteLog)
{
try
{
const UnicodeString Dir = GetTempDir() + "XDEP\\";
const UnicodeString Exe = Dir + "xdep.exe";
const UnicodeString Ini = Dir + "xdep.ini";
const UnicodeString SfmpqExe = Dir + "sfmpq.exe";
const UnicodeString SfmpqDll = Dir + "sfmpq.dll";
const UnicodeString DummyMap = Dir + "TempMap.w3x";
const UnicodeString ProcessedMap = Dir + "ProcessedMap.w3x";
if(!DirectoryExists(Dir)) __MkDir(Dir);
ResUnpack("xdepEXE", Exe);
ResUnpack("xdepINI", Ini);
ResUnpack("DummyMap", DummyMap);
ResUnpack("sfmpqEXE", SfmpqExe);
ResUnpack("sfmpqDLL", SfmpqDll);
const UnicodeString TempDir = Dir + "TempMap.w3x.temp\\files\\";
__MkDir(TempDir);
TStrings* s = new TStringList();
// BuildFileList(DirWithFiles, s);
if(FileExists(DirWithFiles + "(listfile)")) s->LoadFromFile(DirWithFiles + "(listfile)");
else BuildFileList(DirWithFiles, s);
for(int i = 0; i < s->Count; i++)
{
UnicodeString From = DirWithFiles + s->Strings[i];
UnicodeString To = TempDir + s->Strings[i];
if(!DirectoryExists(ExtractFilePath(To))) __MkDir(ExtractFilePath(To));
CopyFile(From.t_str(), To.t_str(), false);
}
const UnicodeString OldWorkingDir = GetCurrentDir();
ChDir(Dir);
if(bWriteLog) WriteLog("Óïàêîâêà ôàéëîâ...");
ExecuteProcess(Exe, "", INFINITE);
Sleep(100);
FORM_TO_FRONT(MainForm);
// CaptureConsoleOutput(Exe, "", MainForm->mmoLog);
ChDir(OldWorkingDir);
DeleteFile(Exe);
DeleteFile(Ini);
DeleteFile(DummyMap);
DeleteFile(SfmpqExe);
DeleteFile(SfmpqDll);
if(FileExists(ProcessedMap))
{
if(bWriteLog) WriteLog("Êàðòà óñïåøíî ñîáðàíà!");
if(FileExists(CreatedMap)) DeleteFile(CreatedMap);
MoveFile(ProcessedMap, CreatedMap);
DeleteNonEmptyDir(Dir, MainForm->Handle);
return true;
}
else
{
if(bWriteLog) WriteLog("Îøèáêà ñáîðêè êàðòû.");
DeleteNonEmptyDir(Dir, MainForm->Handle);
return false;
}
}
catch(...) {return false;}
}
void LocalizeFileList(TStrings* List, const UnicodeString Dir)
{
for(int i = 0; i < List->Count; i++)
{
List->Strings[i] = ReplaceStr(List->Strings[i], Dir, "");
}
}
bool BuildFileList(const UnicodeString Dir, TStrings* List)
{
try
{
if(List == NULL) return false;
List->Clear();
FileFinder(Dir, "*", true, (TStringList*)List);
LocalizeFileList(List, Dir);
return true;
}
catch(...) {return false;}
}
bool PackFilesToMPQ(UnicodeString Archive, const UnicodeString Dir = AppPath + "Temp\\")
{
if(ExtractFilePath(Archive).Length() < 3) Archive = AppPath + ExtractFileName(Archive);
const char* szArchive = Archive.t_str();
TStrings* s = new TStringList();
HANDLE hMpq = NULL;
int nError = ERROR_SUCCESS;
if(FileExists(Dir + "(listfile)")) s->LoadFromFile(Dir + "(listfile)");
else BuildFileList(Dir, s);
const unsigned int nFileCount = s->Count;
if(FileExists(Archive)) DeleteFile(Archive);
bool bReturn = CreateMPQ(Archive, &hMpq, nFileCount);
if(!bReturn) nError = GetLastError();
if(hMpq == NULL) return false;
WriteLog("Óïàêîâêà ôàéëîâ â " + ExtractFileName(Archive));
Sleep(100);
HANDLE hTempFile = 0;
try
{
SFileCreateFile(hMpq, "temp.txt", 0, 0, 0, MPQ_FILE_REPLACEEXISTING + MPQ_FILE_DELETE_MARKER, &hTempFile);
}
catch(...) {}
if(HANDLE_VALID(hTempFile)) SFileFinishFile(hTempFile);
for(int i = 0; i < s->Count; i++)
{
Application->ProcessMessages();
if(s->Strings[i] == "(listfile)" || s->Strings[i] == "(attributes)" || s->Strings[i] == "(signature)")
continue;
if(s->Strings[i].LowerCase() == "scripts\\war3map.j")
s->Strings[i] = "war3map.j";
if(AddFileToMPQ(hMpq, Dir + s->Strings[i], s->Strings[i]))
WriteLog("Óïàêîâêà " + s->Strings[i] + "...");
else
{
nError = GetLastError();
WriteLog("Îøèáêà óïàêîâêè " + s->Strings[i] + " (Êîä îøèáêè: " + IntToStr(nError) + ")");
if(hMpq != NULL)
{
SFileCloseArchive(hMpq);
}
AddFileToMPQWithSFMPQ(Archive, Dir + s->Strings[i], s->Strings[i], nFileCount);
SFileOpenArchive(BUF(Archive), 0, 0, &hMpq);
SFileUpdateFileAttributes(hMpq, BUF(s->Strings[i]));
// 111111111111111111111111
}
}
WriteLog("Óïàêîâêà çàâåðøåíà");
AddFilelistToMPQ(s, hMpq);
SFileCompactArchive(hMpq);
SFileSetAttributes(hMpq, MPQ_ATTRIBUTE_ALL);
if(hMpq != NULL) SFileCloseArchive(hMpq);
VCL_FREE(s);
return true;
}
void ReadInternalListfile(TStrings* List, const UnicodeString Archive)
{
const UnicodeString TempFile = GetTempDir() + "(listfile)";
if(!ExtractFile(Archive, LISTFILE_NAME, TempFile)) return;
if(!FileExists(TempFile)) return;
TStrings* s = new TStringList();
s->LoadFromFile(TempFile);
MergeLists(s, List);
s->Free();
if(FileExists(TempFile)) DeleteFile(TempFile);
}
bool MPQIsValid(UnicodeString Archive, bool bWriteLog)
{
HANDLE hMpq = NULL;
bool bResult = SFileOpenArchive(Archive.t_str(), 0, 0, &hMpq);
if(bWriteLog && !bResult)
{
int nError = GetLastError();
UnicodeString s = "";
switch(nError)
{
case ERROR_FILE_CORRUPT:
s = "Ôàéë ïîâðåæäåí!";
break;
case ERROR_BAD_FORMAT:
s = "Íåâîçìîæíî îòêðûòü ôàéë êàê MPQ àðõèâ!";
break;
case ERROR_NOT_ENOUGH_MEMORY:
s = "Íå õâàòàåò ïàìÿòè!";
break;
case ERROR_AVI_FILE:
s = "AVI ôàéë ñ ðàñøèðåíèåì MPQ!";
break;
case ERROR_ACCESS_DENIED:
s = "Îòêàçàíî â äîñòóïå.";
break;
case ERROR_SHARING_VIOLATION:
case ERROR_LOCK_VIOLATION:
s = "Ôàéë óæå îòêðûò â äðóãîé ïðîãðàììå.";
break;
default:
s = "Íåèçâåñòíàÿ îøèáêà! (Êîä îøèáêè: " + IntToStr(nError) + ")";
}
s = "Îøèáêà: " + s;
WriteLog(s);
}
if(hMpq != NULL) SFileCloseArchive(hMpq);
return bResult;
}
void LoadExternalListfile(TStrings* S, const UnicodeString File)
{
if(!FileExists(File)) return;
TStrings* _s = new TStringList();
_s->LoadFromFile(File);
MergeLists(_s, S);
VCL_FREE(_s);
}
bool ExtractAllFiles(const UnicodeString Archive, bool bRewriteW3I, bool bRewriteDOO, bool bUnpackUnknown, const UnicodeString Dir, const UnicodeString Listfile, bool bWriteLog)
{
try
{
if(!FileExists(Archive))
{
if(bWriteLog) WriteLog("Ôàéë " + ExtractFileName(Archive) + " íå íàéäåí!");
return false;
}
TStrings* s = new TStringList(), *s1 = new TStringList();
PluginManager->SendCodeToAll(CODE_FILE_SEARCH_START, pfcFileFindHooked);
if(!PluginManager->Overrides.Search.Enabled)
{
const UnicodeString BinaryFiles[] = {/*"war3map.imp",*/ "war3map.w3a", "war3map.w3c", "war3map.w3d", "war3map.w3h", "war3map.w3g", "war3map.w3i", "war3map.w3p", "war3map.w3r", "war3map.w3s", "war3map.w3t", "war3map.w3u", "war3map.w3q"};