-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpGetUnit.pas
More file actions
409 lines (341 loc) · 12.2 KB
/
HttpGetUnit.pas
File metadata and controls
409 lines (341 loc) · 12.2 KB
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
unit HttpGetUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OverbyteIcsHttpProt, StdCtrls, Buttons, ComCtrls, MainUnit, Misc, ShellAPI,
SpTBXControls, SpTBXItem,StrUtils, SevenZipVCL,Utility,
OverbyteIcsWndControl,IniFiles;
type
THttpGetForm = class(TForm)
SpTBXTitleBar1: TSpTBXTitleBar;
HttpCli1: THttpCli;
SevenZip1: TSevenZip;
pnlMain: TSpTBXPanel;
Label1: TSpTBXLabel;
StatusLabel: TSpTBXLabel;
Label2: TSpTBXLabel;
FileNameLabel: TSpTBXLabel;
ProgressBar: TSpTBXProgressBar;
ReceivedLabel: TSpTBXLabel;
CancelButton: TSpTBXSpeedButton;
procedure CreateParams(var Params: TCreateParams); override;
function StartDownload(URL: string): Boolean;
procedure CancelDownload;
procedure UpdateReceivedStatus;
procedure OnStartDownloadMessage(var Msg: TMessage); message WM_STARTDOWNLOAD;
procedure OnStartDownloadMessage2(var Msg: TMessage); message WM_STARTDOWNLOAD2;
procedure FormCreate(Sender: TObject);
procedure HttpCli1DocData(Sender: TObject; Buffer: Pointer;
Len: Integer);
procedure CancelButtonClick(Sender: TObject);
procedure HttpCli1HeaderData(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure HttpCli1DocBegin(Sender: TObject);
procedure CloseButtonClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
HttpGetForm: THttpGetForm;
DownloadFile: // fields must be initialized by sender of WM_STARTDOWNLOAD message
record
URL: string;
FileName: string; // to where the file from url should be saved
ServerOptions: Integer;
HeaderReceived: boolean;
end;
FileStream: TFileStream;
DownloadStatus:
record
Downloading: Boolean;
Success: Boolean;
end;
implementation
uses
PreferencesFormUnit, BattleFormUnit, SpringDownloaderFormUnit, gnugettext;
{$R *.dfm}
procedure THttpGetForm.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
{with Params do begin
ExStyle := ExStyle or WS_EX_APPWINDOW;
WndParent := GetDesktopWindow;
end;}
{if not PreferencesFormUnit.Preferences.TaskbarButtons then Exit;
if Preferences.TaskbarButtons then HttpGetForm.FormStyle := fsNormal
else HttpGetForm.FormStyle := fsStayOnTop;
}
end;
function THttpGetForm.StartDownload(URL: string): Boolean;
var
i:integer;
paramList: TStringList;
appExePath: string;
e: integer;
errorList: TStringList;
Ini : TIniFile;
deleteFileName: String;
regExpDelete: string;
deleteWholeDirName: string;
defaultRegExp: string;
begin
Result := False;
ReceivedLabel.Visible := True;
if (DownloadFile.ServerOptions and 4) = 4 then
HttpGetForm.Caption := _('Auto-updating lobby ...')
else if (DownloadFile.ServerOptions and 8) = 8 then
HttpGetForm.Caption := _('Downloading map ...')
else
HttpGetForm.Caption := _('File download');
SpTBXTitleBar1.Caption := HttpGetForm.Caption;
if DownloadStatus.Downloading then Exit;
UpdateReceivedStatus;
DownloadStatus.Downloading := True;
DownloadStatus.Success := False;
FixURL(URL);
HttpCli1.URL := URL;
{if Preferences.UseProxy then
begin
HttpCli1.Proxy := Preferences.ProxyAddress;
HttpCli1.ProxyPort := IntToStr(Preferences.ProxyPort);
HttpCli1.ProxyUsername := Preferences.ProxyUsername;
HttpCli1.ProxyPassword := Preferences.ProxyPassword;
end
else }HttpCli1.Proxy := '';
CancelButton.Enabled := True;
StatusLabel.Caption := _('Loading ');
try
try
HttpCli1.Get;
StatusLabel.Caption := _('Done.');
DownloadStatus.Success := True;
except
on E: EHttpException do
begin
StatusLabel.Caption := _('Failed : ') + IntToStr(HttpCli1.StatusCode) + ' ' + HttpCli1.ReasonPhrase;
end
else
raise;
end;
finally
CancelButton.Enabled := False;
try
HttpCli1.RcvdStream.Destroy;
except
end;
HttpCli1.RcvdStream := nil;
DownloadStatus.Downloading := False;
end;
if DownloadStatus.Success then
begin
// try to open the file:
if (DownloadFile.ServerOptions and $1) = 1 then
begin
if MessageDlg(_('Server says you should now open this file. Would you like to do that now?'), mtInformation, [mbYes, mbNo], 0) = mrYes then
begin
ShellExecute(MainForm.Handle, 'open', PChar(AnsiString(DownloadFile.FileName)), nil, PChar(ExtractFilePath(Application.ExeName)), SW_SHOWNORMAL);
if (DownloadFile.ServerOptions and $2) = 2 then MainForm.Close;
end;
end
else if (DownloadFile.ServerOptions and $4) = 4 then
begin
//StatusLabel.Caption := _('Download complete, extracting new files ...');
ReceivedLabel.Visible := False;
appExePath := Application.ExeName;
SevenZip1.SZFileName := DownloadFile.FileName;
DelTree(ExtractFilePath(appExePath)+'TASClientUpdateTemp\');
SevenZip1.ExtrBaseDir := ExtractFilePath(appExePath)+'TASClientUpdateTemp\';
e := SevenZip1.Extract(false);
DeleteFile(DownloadFile.FileName);
if e=0 then
begin
// delete tasclient.exe.old
if FileExists(Application.ExeName+'.old') then
DeleteFile(Application.ExeName+'.old');
// renames tasclient.exe
if not RenameFile(Application.ExeName,Application.ExeName+'.old') then
begin
MessageDlg('Error : cannot rename '+Application.ExeName, mtError, [mbOK], 0);
Exit;
end;
// unloads the 7z dll
SevenZip1.SZFileName := '';
SevenZip1.Destroy;
// closes springdownloader
CloseSpringDownloader;
// change translation to default
UseLanguage('en_us');
// uninit unitsync
Utility.DeInitLib;
if FileExists(ExtractFilePath(appExePath)+'TASClientUpdateTemp\update.ini') then
begin
Ini := TIniFile.Create(ExtractFilePath(appExePath)+'TASClientUpdateTemp\update.ini');
if Ini.ValueExists('Warning','Message') then
MessageDlg(Ini.ReadString('Warning','Message',_('TASClient is about to update and delete some files in the lobby folder, please backup everything you don''t want to loose')),mtWarning,[mbOK],0);
// delete whole dir
i := 1;
while Ini.ValueExists('DeleteWholeDir','Dir'+IntToStr(i)) do
begin
deleteWholeDirName := Ini.ReadString('DeleteWholeDir','Dir'+IntToStr(i),'');
if (deleteWholeDirName <> '') and (Pos('..',deleteWholeDirName) = 0) then // reverse path is forbidden for security issue
Misc.DelTree(ExtractFilePath(appExePath)+'Lobby\'+deleteWholeDirName);
Inc(i);
end;
// delete outdated files
i := 1;
while Ini.ValueExists('Delete','File'+IntToStr(i)) do
begin
deleteFileName := Ini.ReadString('Delete','File'+IntToStr(i),'');
if Pos('..',deleteFileName) = 0 then // reverse path is forbidden for security issue
begin
deleteFileName := ExtractFilePath(appExePath)+'Lobby\'+deleteFileName;
if FileExists(deleteFileName) then
DeleteFile(deleteFileName);
end;
Inc(i);
end;
defaultRegExp := '^'+StringReplace(LowerCase(ExtractFilePath(appExePath)+'Lobby\'),'\','\\',[rfReplaceAll]);
// regex delete outdated files
i := 1;
while Ini.ValueExists('RegExDelete','RegEx'+IntToStr(i)) do
begin
regExpDelete := Ini.ReadString('RegExDelete','RegEx'+IntToStr(i),'');
if regExpDelete <> '' then
Misc.DeleteFilesRegExp(ExtractFilePath(appExePath)+'Lobby',defaultRegExp+regExpDelete);
Inc(i);
end;
DeleteFile(ExtractFilePath(appExePath)+'TASClientUpdateTemp\update.ini');
end;
// updates files
MoveTree(ExtractFilePath(appExePath)+'TASClientUpdateTemp\',ExtractFilePath(appExePath));
// reconstructs the parameters string
paramList := TStringList.Create;
for i:=1 to ParamCount do
paramList.Add(ParamStr(i));
// executes the new tasclient.exe
ShellExecute(0,'open',PChar(appExePath),PChar(JoinStringList(paramList,' ')),PChar(ExtractFilePath(appExePath)), SW_SHOWNORMAL);
MainForm.Close;
end
else
begin
if (SevenZip1.LastError >= 0) and (SevenZip1.LastError <= 11) then
MessageDlg(_('Error ')+IntToStr(e) + ','+IntToStr(SevenZip1.LastError)+' : '+c7zipResMsg[SevenZip1.LastError], mtError, [mbOK], 0)
else
MessageDlg(_('Error ')+IntToStr(e) + ','+IntToStr(SevenZip1.LastError), mtError, [mbOK], 0);
end;
end
else if (DownloadFile.ServerOptions and 8) = 8 then
begin
BattleForm.ReloadMapListButtonClick(nil);
HttpGetForm.Close;
end
else
MessageDlg(_('Download complete!'), mtInformation, [mbOK], 0);
end;
Result := True;
end;
procedure THttpGetForm.CancelDownload;
begin
HttpCli1.Abort;
end;
procedure THttpGetForm.UpdateReceivedStatus;
var
currentSize: integer;
fileSize: integer;
begin
if not DownloadStatus.Downloading then
begin
ReceivedLabel.Caption := _('Received 0 byte');
ProgressBar.Position := 0;
Exit;
end;
currentSize := HttpCli1.RcvdStream.Size;
fileSize := HttpCli1.ContentLength;
try
ReceivedLabel.Caption := Format(_('Received %s (%d%%)'),[Misc.FormatFileSize3([currentSize, fileSize],'%s/%s %u'),Round(currentSize / fileSize * 100)])
except
ReceivedLabel.Caption := Format('Received %s (%d%%)',[Misc.FormatFileSize3([currentSize, fileSize],'%s/%s %u'),Round(currentSize / fileSize * 100)])
end;
if fileSize = 0 then
ProgressBar.Position := 0
else
ProgressBar.Position := Round(currentSize / fileSize * 100);
end;
procedure THttpGetForm.FormCreate(Sender: TObject);
begin
TranslateComponent(self);
HttpCli1.SocksLevel := '5';
DownloadStatus.Downloading := False;
DownloadStatus.Success := False;
end;
procedure THttpGetForm.OnStartDownloadMessage(var Msg: TMessage); // responds to WM_STARTDOWNLOAD message
begin
if (DownloadFile.ServerOptions and 4) = 4 then
HttpGetForm.ShowModal
else
HttpGetForm.Show;
end;
procedure THttpGetForm.OnStartDownloadMessage2(var Msg: TMessage); // responds to WM_STARTDOWNLOAD2 message
begin
StartDownload(DownloadFile.URL);
end;
procedure THttpGetForm.HttpCli1DocData(Sender: TObject; Buffer: Pointer;
Len: Integer);
begin
UpdateReceivedStatus;
end;
procedure THttpGetForm.CancelButtonClick(Sender: TObject);
begin
CancelDownload;
end;
procedure THttpGetForm.HttpCli1HeaderData(Sender: TObject);
begin
StatusLabel.Caption := StatusLabel.Caption + '.';
end;
procedure THttpGetForm.FormShow(Sender: TObject);
begin
FileNameLabel.Caption := '?';
PostMessage(Handle, WM_STARTDOWNLOAD2, 0, 0);
end;
procedure THttpGetForm.HttpCli1DocBegin(Sender: TObject);
begin
StatusLabel.Caption := _('Transfering ...');
try
if not DownloadFile.HeaderReceived then
begin
if DownloadFile.FileName = '*' then
DownloadFile.FileName := ExtractFilePath(Application.ExeName) + HttpCli1.DocName
else
DownloadFile.FileName := ExtractFilePath(Application.ExeName) + DownloadFile.FileName;
DownloadFile.HeaderReceived := True;
end;
FileNameLabel.Caption := ExtractFileName(DownloadFile.FileName);
FileStream := TFileStream.Create(DownloadFile.FileName, fmCreate);
HttpCli1.RcvdStream := FileStream;
except
HttpCli1.Abort;
MessageDlg(_('Unable to create file: ') + DownloadFile.FileName + _('. Download cancelled!'), mtWarning, [mbOK], 0);
Exit;
end;
end;
procedure THttpGetForm.CloseButtonClick(Sender: TObject);
begin
Close;
end;
procedure THttpGetForm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
if DownloadStatus.Downloading then
begin
if MessageDlg(_('If you close this window your download will be cancelled. Are you sure you wish to do that?'), mtConfirmation, [mbYes, mbNo], 0) = mrNo then
begin
Action := caNone;
Exit;
end;
CancelDownload;
end;
end;
end.