-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLThread.pas
151 lines (120 loc) · 3.86 KB
/
DLThread.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
{
XVM Updater - World of Tanks's XVM one click installer/updater
Copyright (C) 2013 - 2014 Edgar 'LaCourgette' Fournival
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
}
unit DLThread;
interface
uses
SysUtils, Classes, IdComponent;
type
TDownloadCallback = procedure(Data: TMemoryStream) of Object;
TDLThread = class(TThread)
constructor Create(cURL: String; cShowErrors: Boolean); overload;
constructor Create(cURL: String; cCallback: TDownloadCallback); overload;
public
Data: TMemoryStream;
Failed: Boolean;
private
Callback: TDownloadCallback;
ShowErrors: Boolean;
protected
URL: String;
procedure Execute; override;
procedure DWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
procedure DWorkBegin(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Int64);
procedure Redirect(Sender: TObject; var dest: String; var NumRedirect: Integer; var Handled: Boolean; var VMethod: string);
procedure HandleException(Error: String);
end;
implementation
uses
Languages, Main, Windows, IdHTTP, ComCtrls;
constructor TDLThread.Create(cURL: String; cShowErrors: Boolean);
begin
FreeOnTerminate := False;
URL := cURL;
ShowErrors := cShowErrors;
Failed := False;
Data := TMemoryStream.Create;
inherited Create(true);
end;
constructor TDLThread.Create(cURL: String; cCallback: TDownloadCallback);
begin
FreeOnTerminate := True; // Free after callback.
URL := cURL;
Callback := cCallback;
ShowErrors := False;
Failed := False;
Data := TMemoryStream.Create;
inherited Create(false);
end;
procedure TDLThread.DWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
begin
if Terminated then Abort;
Synchronize(procedure
begin
fWindow.Status.SetProgress(AWorkCount);
end);
end;
procedure TDLThread.DWorkBegin(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Int64);
begin
Synchronize(procedure
begin
fWindow.Status.SetNormal(AWorkCountMax);
end);
end;
procedure TDLThread.Redirect(Sender: TObject; var dest: String; var NumRedirect: Integer; var Handled: Boolean; var VMethod: string);
begin
Handled := True;
end;
procedure TDLThread.HandleException(Error: String);
begin
Failed := True;
if not(ShowErrors) or Terminated then Exit; // Fail silently.
Synchronize(procedure
begin
fWindow.Status.SetError;
MessageBox(0,
PChar(sFailDownload[fWindow.CurrentLanguage]+#13#10+Error),
'XVM Updater',
+mb_OK +mb_ICONWARNING);
end);
end;
procedure TDLThread.Execute;
begin
with TIdHTTP.Create(nil) do
begin
try
try
if not Assigned(Callback) then
begin
OnWork := Self.DWork;
OnWorkBegin := Self.DWorkBegin;
end;
OnRedirect := Self.Redirect;
HandleRedirects := True;
Get(Self.URL, Data);
finally
Free;
end;
except
on E : Exception do HandleException(E.Message);
end;
end;
if Assigned(Callback) then
begin
Callback(Data);
Data.Free;
end;
Terminate;
end;
end.