-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathksAutoUpdater.pas
285 lines (252 loc) · 7.63 KB
/
ksAutoUpdater.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
unit ksAutoUpdater;
interface
type
TksUpdateAvailableEvent = reference to procedure(Sender: TObject; AETag, AVersion: string);
IksAutoUpdater = interface
function GetETag: string;
function GetUpdateUrl: string;
procedure SetUpdateUrl(const Value: string);
function CheckForUpdate: Boolean;
function UpdateAvailable: Boolean;
procedure DoUpdate(const AReplaceRunningExe: Boolean = False; const AParams: string = '');
property UpdateURL: string read GetUpdateUrl write SetUpdateUrl;
property ETag: string read GetETag;
end;
function CreateAutoUpdater(AUrl,
ACurrentETag: string;
AIntervalSeconds: integer;
AUpdateAvailableEvent: TksUpdateAvailableEvent;
const ACheckBuild: Boolean = True): IksAutoUpdater;
implementation
uses Windows, Messages, Forms, System.Net.HttpClient, ExtCtrls, Classes,
SysUtils, System.IOUtils, ShellAPi, IniFiles, DateUtils;
type
TksAutoUpdater = class(TInterfacedObject, IksAutoUpdater)
private
FTimer: TTimer;
FUpdateUrl: string;
FIntervalSeconds: integer;
FNewFile: string;
FUpdateAvailable: Boolean;
FETag: string;
FCheckBuild: Boolean;
FUpdateAvailableEvent: TksUpdateAvailableEvent;
function GetETag: string;
procedure CreateTimer;
function GetUpdateUrl: string;
procedure SetUpdateUrl(const Value: string);
procedure OnTimer(Sender: TObject);
public
constructor Create(AUpdateUrl: string;
ACheckIntervalSeconds: integer;
AETag: string;
ACheckBuild: Boolean;
AOnUpdateAvailable: TksUpdateAvailableEvent); virtual;
destructor Destroy; override;
function UpdateAvailable: Boolean;
function CheckForUpdate: Boolean;
procedure DoUpdate(const AReplaceRunningExe: Boolean = False; const AParams: string = '');
property UpdateUrl: string read GetUpdateUrl write SetUpdateUrl;
property ETag: string read GetETag;
end;
function CreateAutoUpdater(AUrl,
ACurrentETag: string;
AIntervalSeconds: integer;
AUpdateAvailableEvent: TksUpdateAvailableEvent;
const ACheckBuild: Boolean = True): IksAutoUpdater;
begin
Result := TksAutoUpdater.Create(AUrl, AIntervalSeconds, ACurrentETag, ACheckBuild, AUpdateAvailableEvent);
end;
procedure GetApplicationVersion(var AMajor, AMinor, ARelease, ABuild: integer; const AExe: string = '');
var
VerInfoSize, VerValueSize, DUMMY: DWORD;
VerInfo:pointer;
VerValue: PVSFixedFileInfo;
AFilename: string;
begin
try
AMajor := 0;
AMinor := 0;
ARelease := 0;
ABuild := 0;
AFileName := AExe;
if AFileName = '' then
AFileName := ParamStr(0);
if FileExists(AFilename) = False then
Exit;
VerInfoSize:=GetFileVersionInfoSize(Pchar(AFilename), DUMMY);
GetMem(verinfo, verinfosize);
GetFileVersionInfo(pchar(AFilename),0,VerInfoSize, VerInfo);
VerQueryValue(VerInfo,'\',Pointer(VerValue), VerValueSize);
With VerValue^ do
begin
AMajor := dwFileVersionMS shr 16; //Major
AMinor := dwFileVersionMS and $FFFF; //Minor
ARelease := dwFileVersionLS shr 16; //Release
ABuild := dwFileVersionLS and $FFFF; //Build
end;
FreeMem(VerInfo, VerInfoSize);
except
// corrupt *.exe?
end;
end;
function GetApplicationBuild(const AExe: string = ''): integer;
var
v1,v2,v3,v4: integer;
begin
GetApplicationVersion(v1, v2, v3, v4, AExe);
Result := v4;
end;
function GetBuildFromVersionStr(AVersionStr: string): integer;
var
AStrings: TStrings;
begin
Result := 0;
AStrings := TStringList.Create;
try
AStrings.Text := Trim(StringReplace(AVersionStr, '.', #13, [rfReplaceAll]));
if AStrings.Count = 0 then
Exit;
Result := StrToIntDef(AStrings[AStrings.Count-1], 0);
finally
AStrings.Free;
end;
end;
{ TksAutoUpdater }
constructor TksAutoUpdater.Create(//AAppID: string;
AUpdateUrl: string;
ACheckIntervalSeconds: integer;
AETag: string;
ACheckBuild: Boolean;
AOnUpdateAvailable: TksUpdateAvailableEvent);
begin
FETag := AETag;
FUpdateUrl := AUpdateUrl;
FUpdateAvailable := False;
FIntervalSeconds := ACheckIntervalSeconds;
FCheckBuild := ACheckBuild;
FUpdateAvailableEvent := AOnUpdateAvailable;
CreateTimer;
end;
destructor TksAutoUpdater.Destroy;
begin
FTimer.Free;
inherited;
end;
procedure TksAutoUpdater.DoUpdate(const AReplaceRunningExe: Boolean = False; const AParams: string = '');
begin
if AReplaceRunningExe then
begin
if FNewFile = '' then
begin
FUpdateAvailable := False;
Exit;
end;
DeleteFile(ChangeFileExt(ParamStr(0), '.old'));
RenameFile(ParamStr(0), ChangeFileExt(ParamStr(0), '.old'));
if CopyFile(PWideChar(FNewFile), PWideChar(ParamStr(0)), False) then
begin
Sleep(1000);
ShellExecute(0, nil, PChar(ParamStr(0)), PWideChar(AParams), nil, SW_SHOWNORMAL);
DeleteFile(FNewFile);
FUpdateAvailable := False;
FNewFile := '';
Sleep(1000);
PostMessage(Application.MainForm.Handle, WM_QUIT, 0, 0);
end;
end
else
begin
ShellExecute(0, nil, PChar(FNewFile), PChar('/SILENT'), nil, SW_SHOWNORMAL);
end;
end;
function TksAutoUpdater.GetETag: string;
begin
Result := FETag;
end;
function TksAutoUpdater.GetUpdateUrl: string;
begin
Result := FUpdateUrl;
end;
procedure TksAutoUpdater.SetUpdateUrl(const Value: string);
begin
FUpdateUrl := Value;
end;
function TksAutoUpdater.CheckForUpdate: Boolean;
var
AHttp: THTTPClient;
AResponse: IHTTPResponse;
AStream: TMemoryStream;
begin
Result := False;
try
AHttp := THTTPClient.Create;
try
AResponse := AHttp.Head(FUpdateUrl);
if (AResponse.StatusCode = 200) and (AResponse.HeaderValue['ETag'] <> FETag) then
begin
AStream := TMemoryStream.Create;
try
AResponse := AHttp.Get(FUpdateUrl, AStream);
if AResponse.StatusCode = 200 then
begin
FETag := AResponse.HeaderValue['ETag'];
FNewFile := ChangeFileExt(TPath.GetTempFileName, '.exe');
AStream.SaveToFile(FNewFile);
if (GetApplicationBuild(FNewFile) > GetApplicationBuild) or (FCheckBuild = False) then
begin
Result := True;
TThread.Queue(nil,
procedure
begin
if Assigned(FUpdateAvailableEvent) then
FUpdateAvailableEvent(Self, FETag, GetApplicationBuild(FNewFile).ToString);
end
);
end;
end;
finally
AStream.Free;
end;
end;
finally
AHttp.Free;
end;
except
//
end;
end;
procedure TksAutoUpdater.OnTimer(Sender: TObject);
begin
if FUpdateAvailable then
Exit;
FTimer.Enabled := False;
TThread.CreateAnonymousThread(
procedure
begin
FUpdateAvailable := CheckForUpdate;
if FUpdateAvailable = False then
begin
TThread.Synchronize(nil,
procedure
begin
FTimer.Interval := FIntervalSeconds*1000;
FTimer.Enabled := True;
end
);
end;
end
).Start;
end;
procedure TksAutoUpdater.CreateTimer;
begin
FTimer := TTimer.Create(nil);
FTimer.Interval := 3000;
FTimer.OnTimer := OnTimer;
FTimer.Enabled := True;
end;
function TksAutoUpdater.UpdateAvailable: Boolean;
begin
Result := FUpdateAvailable;
end;
end.