-
Notifications
You must be signed in to change notification settings - Fork 0
/
uFrmMain.pas
2020 lines (1872 loc) · 63.5 KB
/
uFrmMain.pas
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
unit uFrmMain;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, sSkinProvider, sSkinManager, Vcl.StdCtrls, sButton, Vcl.ExtCtrls, sPanel,
System.Actions, Vcl.ActnList, Vcl.PlatformDefaultStyleActnCtrls, Vcl.ActnMan, Vcl.Menus, sGroupBox, sLabel, sEdit,
uDM, sSplitter, VirtualTrees, sMemo, sDialogs, System.Generics.Collections,
Vcl.ComCtrls, AbComCtrls, AbBase, AbBrowse, AbZBrows, AbZipper, AbZipKit, AbArcTyp, uFTPItem, AnsiStrings,
acProgressBar, acAlphaHints, OverbyteIcsIniFiles, OverbyteIcsWinSock,
OverByteIcsUtils, OverByteIcsFtpCli, OverbyteIcsFtpSrvT, OverByteIcsWSocket, OverbyteIcsWndControl,
uFrmDirectoryForm,
MagentaCopy, MagentaFtp3, MagentaHttp, MagSubs1, OverbyteIcsHttpProt, OverbyteIcsHttpCCodzlib, magclasses,
{$IFDEF USE_SSL} OverbyteIcsSSLEAY,{$ENDIF} OverbyteIcsLogger, AbUnzper, IOUtils, ShellApi;
type
TSyncCmd = function : Boolean of object;
TAsyncCmd = procedure of object;
TfrmMain = class(TForm)
sSkinManager1: TsSkinManager;
sSkinProvider1: TsSkinProvider;
MainMenu1: TMainMenu;
mnuFile: TMenuItem;
mnuExit: TMenuItem;
Settings1: TMenuItem;
N1: TMenuItem;
H1: TMenuItem;
A1: TMenuItem;
sPanel1: TsPanel;
sGroupBox1: TsGroupBox;
sPanel2: TsPanel;
btnUpload: TsButton;
btnAddMapFiles: TsButton;
lstMAPFiles: TVirtualStringTree;
sPanel3: TsPanel;
sSplitter2: TsSplitter;
memStatus: TsMemo;
dlgOpen: TsOpenDialog;
sPanel4: TsPanel;
btnClearList: TsButton;
btnRemove: TsButton;
zip: TAbZipKit;
sPanel6: TsPanel;
lblUploadProgress: TsLabel;
sProgressBar1: TsProgressBar;
sAlphaHints1: TsAlphaHints;
sSplitter4: TsSplitter;
ftp: TFtpClient;
InfoLabel: TsLabel;
UnZipper: TAbUnZipper;
btnUnzip: TsButton;
procedure btnAddMapFilesClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure lstMAPFilesGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex;
TextType: TVSTTextType; var CellText: string);
procedure btnClearListClick(Sender: TObject);
procedure btnGetDirectoryClick(Sender: TObject);
procedure lstFtpFilesGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex;
TextType: TVSTTextType; var CellText: string);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure lstFtpFilesGetImageIndex(Sender: TBaseVirtualTree; Node: PVirtualNode; Kind: TVTImageKind;
Column: TColumnIndex; var Ghosted: Boolean; var ImageIndex: TImageIndex);
procedure lstFtpFilesNodeDblClick(Sender: TBaseVirtualTree; const HitInfo: THitInfo);
procedure txtFolderKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure lstFtpFilesGetNodeDataSize(Sender: TBaseVirtualTree; var NodeDataSize: Integer);
procedure btnDisconnectClick(Sender: TObject);
procedure btnUploadClick(Sender: TObject);
procedure btnRemoveClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure btnBackClick(Sender: TObject);
procedure btnConnectClick(Sender: TObject);
procedure ftpRequestDone(Sender: TObject; RqType: TFtpRequest; ErrCode: Word);
procedure btnRootClick(Sender: TObject);
procedure UnZipperArchiveProgress(Sender: TObject; Progress: Byte; var Abort: Boolean);
procedure btnUploadAgainClick(Sender: TObject);
procedure btnUnzipClick(Sender: TObject);
private
FMapFiles: TList<TMapFile>;
FCurrentNode: TFTPItem;
FFoundNode: TFTPItem;
FRoot: TFTPItem;
FDOD: TFTPItem; // /dod/ folder
FLogFileName: string;
FLogFile: TextFile;
FLastProgress : Int64;
FProgressCount : TFtpBigInt;
FDirFiles: TFileRecs;
FDirFileList: TFindList;
FTempDirectory: string;
procedure SynchronizeMapFiles;
procedure GetFTPDirectories;
procedure UpdateUI;
function CorrectTargetFolder: boolean;
function DoesFTpFileExist(fn: string): boolean;
procedure WalkTree(ftpItem: TFTPItem);
function ChangeFolder(node: TFTPItem; folder: string): TFTPItem;
function GetRootNode(node: TFTPItem): TFTPItem;
function SearchTree(node: TFTPItem; fn: string): TFTPItem;
procedure UpdateNode(folder: string; item: TAbArchiveItem; cleanFileName: string; ToStream: TMemoryStream;
zipFileName: string; fullPath: string);
function CleanItem(zipFileName: string; itemFileName: string): string;
function GetDODFolder: TFTPItem;
procedure CreateFolders(fullpath: string; currentFolder: string);
// procedure ConnectoToFTPServer;
procedure Log(status: string);
procedure ExecuteCmd(SyncCmd : TSyncCmd; ASyncCmd : TAsyncCmd; sync: boolean = false);
procedure DisplayHandler(Sender : TObject; var Msg : String);
procedure MkDir(baseDir: string; dirName: string);
procedure Cwd(dir: string);
procedure Connect;
procedure DisplayFile(FileName : String);
procedure Dir;
procedure Directory(dir: string);
procedure ListFiles;
function SetFTPThreadGen: integer;
function SetFTPGen: boolean;
procedure OnThreadEvent(LogLevel: TLogLevel; const Id, Info: String; var Cancel: boolean);
procedure OnThreadTerminate(Sender: TObject);
procedure OnCopyEvent(LogLevel: TLogLevel; Info: String; var Cancel: boolean);
procedure onProgressEvent (Sender: TObject; CopyProgress: TCopyProgress; var Cancel: boolean);
function GetFTPNodes(folder: string): TFTPItem;
procedure Upload;
procedure MoveFiles(ASourcePath, AFileSpec, ATargetPath: string; currentFolderOnly: boolean = false);
procedure MoveDirectories(ASourcePath, AFileSpec, ATargetPath: string);
function MoveDir(const fromDir, toDir: string): Boolean;
function DeleteDir(const Dir: string): Boolean;
procedure UploadMaps;
public
{ Public declarations }
end;
const
TEMP_FILE_NAME = 'FTPDIR.TXT';
ROOT = '/dod/';
MaxThreads = 20 ;
var
frmMain: TfrmMain;
MagHTTPClient: TMagHTTP ;
MagFTPClient: TMagFTP ;
MagFileCopyClient: TMagFileCopy ;
IcsLog: TIcsLogger ;
AbortFlag: boolean ;
IniFileName: string ;
MagFtpThreads: array [0..MaxThreads] of TMagFtpThread ;
CurThreads, NextThread: integer ;
implementation
const
CRLF: string = #13#10;
{$R *.dfm}
procedure TfrmMain.btnAddMapFilesClick(Sender: TObject);
var
i: Integer;
mf: TMapFile;
begin
if dlgOpen.Execute() then
begin
for i := 0 to dlgOpen.Files.Count-1 do
begin
mf.FileName := dlgOpen.Files[i];
FMapFiles.Add(mf);
end;
SynchronizeMapFiles;
UpdateUI;
end;
end;
procedure TfrmMain.btnBackClick(Sender: TObject);
begin
if FCurrentNode.Parent <> nil then
FCurrentNode := FCurrentNode.Parent;
UpdateUI;
end;
procedure TfrmMain.btnClearListClick(Sender: TObject);
begin
FMapFiles.Clear;
SynchronizeMapFiles;
Log('Cleared list');
UpdateUI;
end;
procedure TfrmMain.btnConnectClick(Sender: TObject);
begin
// Connect require HostName and Port which are set in ExecuteCmd
// Connect is 3 commands in sequence: Open, User and Pass.
ftp.UserName := dm.UserName;
ftp.Password := dm.Password;
ExecuteCmd(ftp.Connect, ftp.ConnectAsync);
end;
procedure TfrmMain.btnGetDirectoryClick(Sender: TObject);
begin
// if MessageDlg('DOD Mapper will retrieve listings of all files and folders from the FTP server.'+CRLF+
// 'This will take a few moments. Once this is done, the upload and verifictions of maps will be fast.'+CRLF+CRLF+
// 'Continue?', mtCustom,[mbYes,mbCancel], 0) = mrYes then
GetFTPDirectories;
end;
procedure TfrmMain.btnDisconnectClick(Sender: TObject);
begin
// Quit doesn't require any parameter
ExecuteCmd(ftp.Quit, ftp.QuitAsync);
end;
procedure TfrmMain.btnRemoveClick(Sender: TObject);
var
node: PVirtualNode;
data: PMapFile;
newMapFiles: TList<TMapFile>;
removedMapFiles: TList<string>;
i: Integer;
gotMilk: boolean;
x: Integer;
begin
if lstMAPFiles.SelectedCount > 0 then
begin
newMapFiles := TList<TMapFile>.Create;
removedMapFiles := TList<string>.Create;
for node in lstMAPFiles.SelectedNodes do
begin
if lstMAPFiles.Selected[node] then
begin
data := lstMAPFiles.GetNodeData(node);
removedMapFiles.Add(data.FileName);
Log('Removed '+data.FileName);
end;
end;
for i := 0 to FMapFiles.Count-1 do
begin
gotMilk := false;
for x := 0 to removedMapFiles.Count-1 do
begin
if FMapFiles[i].FileName = removedMapFiles[x] then
begin
gotMilk := true;
break;
end;
end;
if not gotMilk then
newMapFiles.Add(FMapFiles[i])
end;
FMapFiles := newMapFiles;
SynchronizeMapFiles;
UpdateUI;
end;
end;
procedure TfrmMain.btnRootClick(Sender: TObject);
begin
FCurrentNode := GetFTPNodes('/');
UpdateUI;
end;
procedure TfrmMain.btnUploadAgainClick(Sender: TObject);
begin
UploadMaps;
end;
procedure TfrmMain.btnUploadClick(Sender: TObject);
var
i: Integer;
ToStream : TMemoryStream;
Item : TAbArchiveItem;
x,zipPos: Integer;
folder: string;
cleanFileName: string;
fileName,zipFileName,leftDir: string;
foundZipFileNameInFolder: boolean;
begin
UploadMaps;
exit;
if FCurrentNode = nil then exit;
FRoot := GetRootNode(FCurrentNode);
FDOD := GetDODFolder;
FCurrentNode := FDOD;
// Change the folder on ther server to the DOD folder
// ConnectoToFTPServer;
//dm.ftp.ChangeDir(FDOD.Folder);
FTP.HostDirName := FDOD.Folder;
if not FTP.Cwd then
raise Exception.Create('Failed to change dir: ' + FTP.ErrorMessage);
UpdateUI;
if CorrectTargetFolder then
begin
Screen.Cursor := crHourGlass;
folder := FCurrentNode.Folder;
if folder[length(folder)]<>'/' then
folder := folder + '/';
try
for i := 0 to FMapFiles.Count-1 do
begin
Log('***********'+StringOfChar('*',length(FMapFiles[i].Filename)));
Log('Processing '+FMapFiles[i].Filename);
zip.OpenArchive(FMapFiles[i].Filename);
zipFileName := uppercase(ChangeFileExt(ExtractFileName(FMapFiles[i].Filename),''));
for x := 0 to zip.Count-1 do
begin
Item := zip.Items[x];
cleanFileName := CleanItem(FMapFiles[i].Filename, Item.FileName);
if length(cleanFileName)>0 then
begin
// 'sound/dod_walmart/alliesscoreshort.wav'
leftDir := zipFileName+'/';
foundZipFileNameInFolder := false;
if leftDir = copy(uppercase(Item.FileName),1,length(leftDir)) then
begin
foundZipFileNameInFolder := true;
leftDir := copy(Item.FileName,length(leftDir)+1);
end;
//zipPos := pos(leftDir,uppercase(Item.FileName));
if Item.FileName[length(Item.FileName)] = '/' then
begin
// It's a folder, remove '/' at end
// Also, remove the zip filename from the item filename
if foundZipFileNameInFolder then
fileName := folder+copy(Item.FileName,length(leftDir)+1)
else
fileName := folder+copy(Item.FileName,1,length(Item.FileName)-1);
end else
begin
if foundZipFileNameInFolder then
fileName := folder+copy(Item.FileName,length(leftDir)+1)
else
fileName := folder+Item.FileName;
end;
if fileName[length(fileName)] = '/' then
fileName := copy(fileName,1,length(fileName)-1);
if DoesFTpFileExist(fileName) then
begin
Log('Skipping '+fileName);
end else
begin
try
ToStream := TMemoryStream.Create;
zip.ExtractToStream(Item.FileName, ToStream);
ToStream.Position := 0;
UpdateNode(folder, Item, cleanFileName, ToStream, zipFileName, fileName);
finally
ToStream.Free;
end;
end;
end;
end;
end;
Log('Done!');
UpdateUI;
finally
Screen.Cursor := crArrow;
end;
end;
end;
procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
try
// if dm.ftp.Connected then
// dm.ftp.Disconnect(false);
CloseFile(FLogFile);
except
end;
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
FMapFiles := TList<TMapFile>.Create;
FTempDirectory := ExtractFilePath(Application.ExeName) + 'temp\dod\';
ForceDirectories(FTempDirectory);
end;
procedure TfrmMain.FormShow(Sender: TObject);
var
Data : TWSAData;
begin
lstMAPFiles.Colors.UnfocusedSelectionColor := frmMain.sSkinManager1.GetGlobalColor;
//lstFtpFiles.Colors.UnfocusedSelectionColor := frmMain.sSkinManager1.GetGlobalColor;
//lstFtpFiles.Colors.UnfocusedSelectionColor := frmMain.sSkinManager1.GetHighLightColor(true);
//lblUploadProgress.Font.Color := frmMain.sSkinManager1.GetGlobalColor;
FLogFileName := ExtractFilePath(Application.ExeName) + 'DODMapperLog.txt';
AssignFile(FLogFile, FLogFileName);
if FileExists(FLogFileName) then
Append(FLogFile)
else
Rewrite(FLogFile);
{ Display winsock info }
Data := WinsockInfo;
Log('Winsock version ' +
IntToStr(LOBYTE(Data.wHighVersion)) + '.' +
IntToStr(HIBYTE(Data.wHighVersion)));
Log(String(System.SysUtils.StrPas(Data.szDescription)));
Log(String(System.SysUtils.StrPas(Data.szSystemStatus)));
end;
procedure TfrmMain.ftpRequestDone(Sender: TObject; RqType: TFtpRequest; ErrCode: Word);
var
Cli : TFtpClient;
begin
Cli := TFtpClient(Sender);
Log('Request ' + LookupFTPReq (RqType) + ' Done.');
Log('StatusCode = ' + IntToStr(Cli.StatusCode));
Log('LastResponse was : ''' + Cli.LastResponse + '''');
if ErrCode = 0 then
Log('No error')
else
Log('Error = ' + IntToStr(ErrCode) +
' (' + Cli.ErrorMessage + ')');
{ Display last progress value }
InfoLabel.Caption := IntToStr(FProgressCount);
if ErrCode = 0 then begin
case RqType of
ftpFeatAsync,
ftpConnectFeatAsync,
ftpConnectFeatHostAsync :
{ If the server supports rfc 2640 we turn UTF-8 ON. }
{ see also http://wiki.filezilla-project.org/Character_Set }
{ For backward compatibility with servers that implement }
{ the long expired IETF draft }
{ http://tools.ietf.org/html/draft-ietf-ftpext-utf-8-option-00 }
{ it is also required to send the OPTS UTF8 ON command and to }
{ ignore a possible error response. }
if ftpFeatUtf8 in Cli.SupportedExtensions then begin
{ Sets property CodePage as well in Edit's OnChange }
// CodePageEdit.Text := IntToStr(CP_UTF8);
Log('Server seems to support RFC 2640 - UTF-8 turned on');
end;
ftpOptsAsync :
{ Opts UTF8 ON is only required for backward compatibility }
{ with servers not implementing rfc 2640 properly. }
if Cli.NewOpts = 'UTF8 ON' then
//CodePageEdit.Text := IntToStr(CP_UTF8)
else if Cli.NewOpts = 'UTF8 OFF' then
//CodePageEdit.Text := IntToStr(CP_ACP);
;
ftpDirAsync,
ftpDirectoryAsync,
ftpLsAsync,
ftpListAsync,
ftpMlsdAsync,
ftpSiteCmlsdAsync,
ftpXCmlsdAsync,
ftpXDmlsdAsync,
ftpSiteDmlsdAsync,
ftpSiteIndexAsync :
DisplayFile(TEMP_FILE_NAME);
ftpSizeAsync :
Log('File size is ' + IntToStr(Cli.SizeResult) +
' bytes' );
ftpPwdAsync,
ftpMkdAsync,
ftpCDupAsync,
ftpCwdAsync :
Log('Directory is "' + Cli.DirResult + '"');
ftpGetAsync :
InfoLabel.Caption := InfoLabel.Caption + ' [' +
IntToStr(IcsGetFileSize(Cli.LocalFileName)) + ']';
end;
end;
end;
procedure TfrmMain.lstFtpFilesGetImageIndex(Sender: TBaseVirtualTree; Node: PVirtualNode; Kind: TVTImageKind;
Column: TColumnIndex; var Ghosted: Boolean; var ImageIndex: TImageIndex);
var
Data: PFTPData;
begin
Data := Sender.GetNodeData(Node);
if (Kind in [ikNormal, ikSelected]) and (Column = 0) then
begin
if Data.Dir then
ImageIndex := 13;
end;
end;
procedure TfrmMain.lstFtpFilesGetNodeDataSize(Sender: TBaseVirtualTree; var NodeDataSize: Integer);
begin
NodeDataSize := SizeOf(TFTPData);
end;
procedure TfrmMain.lstFtpFilesGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex;
TextType: TVSTTextType; var CellText: string);
var
Data: PFTPData;
begin
Data := Sender.GetNodeData(Node);
if Assigned(Data) and (Data<>nil) then
begin
case Column of
0: CellText := Data.Name;
1:
begin
if Data.Size > 0 then
CellText := dm.FormatByteSize(Data.Size)
else
CellText := '';
end;
2: CellText := Data.ModifiedDate;
end;
end;
end;
procedure TfrmMain.lstFtpFilesNodeDblClick(Sender: TBaseVirtualTree; const HitInfo: THitInfo);
var
Data: PFTPData;
folder: string;
begin
data := Sender.GetNodeData(HitInfo.HitNode);
case HitInfo.HitColumn of
0:
begin
if Data.Dir then
begin
if FCurrentNode = nil then exit;
// folder := FCurrentNode.Folder;
// if folder[length(folder)]<>'/' then
// folder := folder + '/' + Data.Name
// else
// folder := folder + Data.Name;
//FCurrentNode := ChangeFolder(FCurrentNode,folder);
FCurrentNode := GetFTPNodes(Data.FullPath+Data.Name+'/');
if FCurrentNode <> nil then
UpdateUI;
end;
end;
end;
end;
procedure TfrmMain.lstMAPFilesGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex;
TextType: TVSTTextType; var CellText: string);
var
Data: PMapFile;
begin
Data := Sender.GetNodeData(Node);
case Column of
0: CellText := Data.FileName;
// 1:
// begin
// if Data.Size > 0 then
// CellText := dm.FormatByteSize(Data.Size)
// else
// CellText := '';
// end;
// 2: CellText := Data.ModifiedDate;
end;
end;
procedure TfrmMain.SynchronizeMapFiles;
var
node: PVirtualNode;
data: PMapFile;
i: integer;
begin
lstMAPFiles.BeginUpdate;
lstMAPFiles.Clear;
for i := 0 to FMapFiles.Count-1 do
begin
node := lstMAPFiles.AddChild(nil);
data := lstMAPFiles.GetNodeData(node);
data^.FileName := FMapFiles[i].FileName;
//data^.Size := FMapFiles[i].Size;
end;
lstMAPFiles.Refresh;
lstMAPFiles.EndUpdate;
end;
procedure TfrmMain.txtFolderKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
//FFolder := '';
end;
//procedure TfrmMain.SynchronizeFtpFiles(ftpNode: TFTPItem);
//var
// node: PVirtualNode;
// data: PFTPData;
// i: integer;
//begin
// lstFtpFiles.BeginUpdate;
// lstFtpFiles.Clear;
//
// for i := 0 to ftpNode.Children.Count-1 do
// begin
// if ftpNode.Children[i].Name <>'' then
// begin
// node := lstFtpFiles.AddChild(nil);
// data := lstFtpFiles.GetNodeData(node);
// data^.Name := ftpNode.Children[i].Name;
// data^.Size := ftpNode.Children[i].Size;
// data^.Dir := ftpNode.Children[i].Dir;
// data^.Level := 0;
// data^.ModifiedDate := ftpNode.Children[i].ModifiedDate;
// data^.FullPath := ftpNode.Children[i].Folder;
// end;
// end;
//
// lstFtpFiles.Refresh;
// lstFtpFiles.EndUpdate;
//end;
procedure TfrmMain.GetFTPDirectories;
var
i: Integer;
begin
if (dm.Host='') or (dm.Username='') or (dm.Password='') then
begin
ShowMessage('Enter valid FTP server information, first.');
dm.actSettingsExecute(self);
exit;
end;
if FCurrentNode = nil then
begin
FCurrentNode := TFTPItem.Create(nil); // create root
if FCurrentNode.Folder = '' then
FCurrentNode.Folder := '/';
end;
Screen.Cursor := crHourGlass;
try
//WalkTree(FCurrentNode);
ListFiles;
UpdateUI;
finally
Screen.Cursor := crArrow;
end;
end;
//procedure TfrmMain.ConnectoToFTPServer;
//begin
// with dm do
// begin
// try
// if not ftp.Connected then
// begin
// Connect;
// if ftp.Connected then
// begin
// Log('Connected to '+dm.Host+' on port '+inttostr(dm.Port));
// UpdateUI;
// end;
// end;
// except
//// try
//// ftp.Disconnect(False);
//// except
//// end;
//// if ftp.IOHandler <> nil then ftp.IOHandler.InputBuffer.Clear;
// Log('Connectivity to the server has been lost.');
// end;
// end;
//end;
procedure TfrmMain.UpdateUI;
var
connected: boolean;
begin
try
connected := false;
connected := ftp.Connected;
except
end;
// if (FMapFiles.Count > 0) and connected then
// btnUpload.Enabled := true
// else
// btnUpload.Enabled := false;
if FCurrentNode <> nil then
begin
// lstFtpFiles.RootNodeCount := FCurrentNode.Children.Count;
// txtFolder.Text := ROOT + FCurrentNode.Folder;
end;
end;
function TfrmMain.CorrectTargetFolder: boolean;
var
i: Integer;
begin
result := false;
for i := 0 to FCurrentNode.Children.Count-1 do
begin
if uppercase(FCurrentNode.Children[i].Name) = 'VALVE.INF' then
begin
result := true;
exit;
end;
end;
end;
function TfrmMain.DoesFTpFileExist(fn: string): boolean;
var
i: Integer;
fileName: string;
FFoundFTPItem: TFTPItem;
begin
result := false;
fileName := uppercase(fn);
FFoundNode := nil;
FFoundFTPItem := SearchTree(FRoot, fileName);
if FFoundNode <> nil then
result := true;
end;
procedure TfrmMain.WalkTree(ftpItem: TFTPItem);
var
i: Integer;
// item: TIdFTPListItem;
ftpChild: TFTPItem;
// dl: TList<TIdFTPListItem>;
// col: TIdFTPListItems;
files: TStringList;
begin
Log('Retrieving '+ftpItem.Folder);
// Cwd(ftpItem.Folder);
// Dir;
Directory(ftpItem.Folder);
// files := TStringList.Create;
// dm.FTP.List(files,'',true);
//
// col := TIdFTPListItems.Create;
// dl := TList<TIdFTPListItem>.Create;
// for i := 0 to dm.FTP.DirectoryListing.Count-1 do
// begin
// item := TIdFTPListItem.Create(col);
// item.FileName := dm.FTP.DirectoryListing.Items[i].FileName;
// item.Size := dm.FTP.DirectoryListing.Items[i].Size;
// item.ItemType := dm.FTP.DirectoryListing.Items[i].ItemType;
// dl.Add(item);
// end;
//
// if dl.Count > 0 then
// begin
// for i := 0 to dl.Count-1 do
// begin
// item := dl[i];
// ftpChild := TFTPItem.Create(ftpItem);
// ftpChild.Folder := ftpItem.Folder;
// ftpChild.Name := item.FileName;
// ftpChild.Size := item.Size;
// ftpChild.ModifiedDate := trim(copy(files[i],1,24));
// if ftpChild.Folder[length(ftpChild.Folder)]<>'/' then
// ftpChild.FullPathWithFileName := uppercase(ftpItem.Folder + '/' + ftpChild.Name)
// else
// ftpChild.FullPathWithFileName := uppercase(ftpItem.Folder + ftpChild.Name);
// if item.ItemType = ditDirectory then
// begin
// if uppercase(ftpChild.Name) <> 'APPCACHE' then
// begin
// ftpChild.Dir := true;
// if ftpChild.Folder[length(ftpChild.Folder)]<>'/' then
// ftpChild.Folder := ftpChild.Folder + '/';
// ftpChild.Folder := ftpChild.Folder + item.FileName;
// WalkTree(ftpChild);
// end;
// end else
// begin
// ftpChild.Dir := false;
// end;
// //Log(ftpChild.Name);
// ftpItem.Children.Add(ftpChild);
// end;
// end;
end;
function TfrmMain.ChangeFolder(node: TFTPItem; folder: string): TFTPItem;
var
i: Integer;
begin
result := nil;
for i := 0 to node.Children.Count-1 do
begin
if (node.Children[i].Dir = true) and (node.Children[i].Folder = folder) then
begin
result := node.Children[i];
exit;
end;
end;
end;
function TfrmMain.GetRootNode(node: TFTPItem): TFTPItem;
begin
result := nil;
if node = nil then exit;
if node.Parent = nil then
begin
result := node;
exit;
end;
if node.Parent <> nil then
result := GetRootNode(node.Parent);
end;
function TfrmMain.SearchTree(node: TFTPItem; fn: string): TFTPItem;
var
i: Integer;
searchFor: string;
begin
if FFoundNode <> nil then
begin
result := FFoundNode;
exit;
end;
result := nil;
if fn[length(fn)] = '/' then
fn := copy(fn, 1, length(fn)-1);
searchFor := uppercase(fn);
for i := 0 to node.Children.Count-1 do
begin
if node.Children[i].FullPathWithFileName = searchFor then
begin
result := node.Children[i];
FFoundNode := result;
exit;
end;
if node.Children[i].Dir then
result := SearchTree(node.Children[i], searchFor);
end;
end;
procedure TfrmMain.UnZipperArchiveProgress(Sender: TObject; Progress: Byte; var Abort: Boolean);
begin
frmMain.sProgressBar1.Position := Progress;
end;
procedure TfrmMain.UpdateNode(folder: string; item: TAbArchiveItem; cleanFileName: string; ToStream: TMemoryStream;
zipFileName: string; fullPath: string);
var
ftpItem,existingFtpItem: TFTPItem;
// fullPath: string;
delimiterPos: integer;
parent: TFTPItem;
i,zipPos: Integer;
begin
if FDOD = nil then exit;
// 'sprites/obj_icons/dod_tiger2_b2/icon_obj_custom1_allies.spr'
// 'sound/dod_walmart/alliesscoreshort.wav'
// zipPos := pos(zipFileName+'/',uppercase(Item.FileName));
// if zipPos > 0 then
// fullPath := copy(Item.FileName,length(zipFileName+'/')+1)
// else
// fullPath := item.FileName;
delimiterPos := pos('/', fullPath);
if delimiterPos > 0 then
begin
// We have at least one folder in there
for i := length(fullPath)-1 downto 0 do
begin
if fullPath[i] = '/' then
begin
fullPath := copy(fullPath, 1, i-1);
break;
end;
end;
CreateFolders(fullPath, '');
// Get parent for the file/folder
FFoundNode := nil;
//parent := SearchTree(FDOD,FDOD.Folder + '/' + fullPath);
parent := SearchTree(FDOD,fullPath);
if parent <> nil then
begin
// Update our local tree with a new FTPItem
ftpItem := TFTPItem.Create(parent);
ftpItem.FullPathWithFileName := uppercase(parent.Folder + '/' + cleanFileName);
ftpItem.Folder := parent.Folder;
ftpItem.Name := cleanFileName;
ftpItem.Size := item.UncompressedSize;
ftpItem.Dir := false;
ftpItem.ModifiedDate := datetimetostr(item.LastModTimeAsDateTime);
parent.Children.Add(ftpItem);
Log('Changing to folder ' + parent.Folder);
FTP.HostDirName := parent.Folder;
dm.CurrentFileSize := ftpItem.Size;
Log('Uploading '+cleanFileName);
try
// dm.ftp.Put(ToStream,cleanFileName,false);
except
end;
end;
end else
begin
// Update our local tree with a new FTPItem
ftpItem := TFTPItem.Create(FDOD);
ftpItem.FullPathWithFileName := uppercase(FDOD.Folder + '/' + cleanFileName);
ftpItem.Folder := FDOD.Folder;
ftpItem.Name := cleanFileName;
ftpItem.Size := item.UncompressedSize;
ftpItem.Dir := false;
ftpItem.ModifiedDate := datetimetostr(item.LastModTimeAsDateTime);
FDOD.Children.Add(ftpItem);
Log('Changing to folder ' + FDOD.Folder);
FTP.HostDirName := FDOD.Folder;
Log('Uploading '+cleanFileName);
dm.CurrentFileSize := ftpItem.Size;
try
// dm.ftp.Put(ToStream,cleanFileName,false);
except
end;
end;
end;
procedure TfrmMain.CreateFolders(fullpath: string; currentFolder: string);
var
ftpItem,existingFtpItem: TFTPItem;
dir,path,leftDir: string;
delimiterPos: integer;
parent: TFTPItem;
begin
// 'sprites/obj_icons/dod_tiger2_b2/icon_obj_custom1_allies.spr'
if currentFolder = '' then
begin
// Get first folder from full path
if length(fullpath) > 0 then
begin
leftDir := uppercase(copy(fullpath,1,5));
if leftDir = '/DOD/' then
fullpath := copy(fullpath,6);
delimiterPos := pos('/', fullpath);
if delimiterPos > 0 then
begin
dir := copy(fullpath,1,delimiterPos-1);
FFoundNode := nil;
existingFtpItem := SearchTree(FDOD,FDOD.Folder + '/' + dir);
if existingFtpItem = nil then
begin
// Folder does not exist, create it on the server
Log('Creating folder '+FDOD.Folder + '/' + dir);
MkDir(FDOD.Folder, dir);
// Update our local tree with a new FTPItem
ftpItem := TFTPItem.Create(FDOD);
ftpItem.FullPathWithFileName := uppercase(FDOD.Folder + '/' + dir);
ftpItem.Folder := FDOD.Folder + '/' + dir;
ftpItem.Name := dir;
ftpItem.Size := 0;
ftpItem.Dir := true;
ftpItem.ModifiedDate := datetimetostr(now);
FDOD.Children.Add(ftpItem);
end;
CreateFolders(fullpath, dir);
end;
end;
end else
begin
// Keep going with the fullpath
leftDir := copy(fullPath,1,pos(currentFolder,fullPath)+length(currentFolder));
path := copy(fullPath,pos(currentFolder,fullPath)+length(currentFolder)+1);
//delimiterPos := pos('/', path);
//if delimiterPos > 0 then
if path <> '' then
begin
// We have at least one folder in there
delimiterPos := pos('/', path);
if delimiterPos > 0 then
dir := copy(path,1,delimiterPos-1)
else
dir := path;
FFoundNode := nil;
path := FDOD.Folder + '/' + leftDir + dir;
existingFtpItem := SearchTree(FDOD,path);
if existingFtpItem = nil then
begin
// Folder does not exist, create it on the server