-
Notifications
You must be signed in to change notification settings - Fork 14
/
Updater.dpr
301 lines (259 loc) · 6.72 KB
/
Updater.dpr
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
program Updater;
{$WEAKLINKRTTI ON}
{$IFDEF DEBUG}
{$APPTYPE CONSOLE}
{$ENDIF}
{$R *.res}
{$IFDEF WIN64}
{$R 'UpdaterFiles64.res' 'UpdaterFiles64.rc'}
{$ELSE}
{$R 'UpdaterFiles32.res' 'UpdaterFiles32.rc'}
{$ENDIF}
uses
Winapi.Windows,
Winapi.ShellAPI,
System.SysUtils,
System.Classes,
System.ZLib,
System.Generics.Collections,
System.Generics.Defaults,
AutoUpdate.Params in 'AutoUpdate\AutoUpdate.Params.pas';
{$SETPEFlAGS
IMAGE_FILE_DEBUG_STRIPPED or
IMAGE_FILE_LINE_NUMS_STRIPPED or
IMAGE_FILE_LOCAL_SYMS_STRIPPED or
IMAGE_FILE_RELOCS_STRIPPED}
const
FullFileNameFormat = '%0:s\%1:s';
BakFileNameFormat = '%0:s.bak';
FileResNameFormat = 'FileUpdate%0:u';
FileMainOldOffset = 8000;
FileMainNewOffset = 8001;
FileUpdateOffset = 8002;
MutexOffset = 8950;
RestartParamsFormat = '%0:s %1:s';
SilentParam = '-Silent';
type
TFiles = TList<string>;
function GetString(Index: Integer): string;
var
RetLength: Integer;
begin
RetLength := MAX_PATH;
SetLength(Result, RetLength);
RetLength := LoadString(HInstance, Index, LPTSTR(Result), RetLength);
SetLength(Result, RetLength);
end;
function ResourceExist(Index: Integer): Boolean;
var
ResStream: TResourceStream;
begin
Result := True;
try
ResStream := TResourceStream.Create(HInstance, Format(FileResNameFormat, [Index]), RT_RCDATA);
ResStream.Free;
except
Result := False;
end;
end;
function ExtractFile(Index: Integer; FileName: string): Boolean;
var
ResStream: TResourceStream;
DecompresStream: TDecompressionStream;
SaveStream: TFileStream;
begin
Result := True;
try
ResStream := TResourceStream.Create(HInstance, Format(FileResNameFormat, [Index]), RT_RCDATA);
try
ResStream.Position := 0;
DecompresStream := TDecompressionStream.Create(ResStream);
try
SaveStream := TFileStream.Create(FileName, fmCreate or fmOpenWrite or fmShareDenyWrite);
try
DecompresStream.Position := 0;
SaveStream.CopyFrom(DecompresStream, DecompresStream.Size);
finally
SaveStream.Free;
end;
finally
DecompresStream.Free;
end;
finally
ResStream.Free;
end;
except
Result := False;
end;
end;
function BackupFile(FileName: string): Boolean;
var
BakFileName: string;
begin
BakFileName := Format(BakFileNameFormat, [FileName]);
if FileExists(BakFileName) then
begin
Result := DeleteFile(BakFileName);
if not Result then Exit(False);
end;
Result := RenameFile(FileName, BakFileName);
end;
function RestoreFile(FileName: string): Boolean;
var
BakFileName: string;
begin
BakFileName := Format(BakFileNameFormat, [FileName]);
Result := FileExists(BakFileName);
if not Result then Exit(False);
if FileExists(FileName) then
begin
Result := DeleteFile(FileName);
if not Result then Exit(False);
end;
Result := RenameFile(BakFileName, FileName);
end;
function RestoreAllFile(const Files: TFiles): Boolean;
var
FileName: string;
begin
Result := True;
for FileName in Files do
Result := Result and RestoreFile(FileName);
end;
function DeleteAllBackup(const Files: TFiles): Boolean;
var
FileName: string;
BakFileName: string;
begin
Result := True;
for FileName in Files do
begin
BakFileName := Format(BakFileNameFormat, [FileName]);
Result := Result and DeleteFile(BakFileName);
end;
end;
function ReplaceFile(Index: Integer; Dir: string; var Backups: TFiles): Boolean;
var
FileName: string;
begin
FileName := Format(FullFileNameFormat, [Dir, GetString(FileUpdateOffset + Index)]);
if FileExists(FileName) then
begin
Result := BackupFile(FileName);
if not Result then Exit(False);
Backups.Add(FileName);
end;
Result := ExtractFile(Index, FileName);
end;
function ReplaceAllFile(Dir: string): Boolean;
var
I: Integer;
Backups: TFiles;
begin
Result := True;
Backups := TFiles.Create;
try
I := 0;
while ResourceExist(I) and Result do
begin
Result := ReplaceFile(I, Dir, Backups);
Inc(I);
end;
if not Result then
begin
if RestoreAllFile(Backups) then
DeleteAllBackup(Backups);
end else
DeleteAllBackup(Backups);
finally
Backups.Free;
end;
end;
procedure UpdateComplete(Dir: string);
var
FileName: string;
begin
FileName := Format(FullFileNameFormat, [Dir, GetString(FileMainNewOffset)]);
ShellExecute(0, LPTSTR('open'), LPTSTR(FileName), LPTSTR(StartParamUpdateComplete), LPTSTR(Dir), SW_NORMAL);
end;
procedure UpdateFail(Dir: string);
var
FileName: string;
begin
FileName := Format(FullFileNameFormat, [Dir, GetString(FileMainOldOffset)]);
ShellExecute(0, LPTSTR('open'), LPTSTR(FileName), LPTSTR(StartParamUpdateFail), LPTSTR(Dir), SW_NORMAL);
end;
procedure ShowHelp;
begin
MessageBox(0, 'CmdLine: "Directory of the updated program"', 'Using', MB_ICONINFORMATION or MB_OK);
end;
procedure WaitMutex;
const
WaitTimeout = 3000;
var
Mutex: THandle;
begin
Mutex := OpenMutex(SYNCHRONIZE, True, LPTSTR(GetString(MutexOffset)));
if Mutex = 0 then Exit;
WaitForSingleObject(Mutex, WaitTimeout);
CloseHandle(Mutex);
end;
var
Silent: Boolean;
ProgramDir: string;
Sei: TShellExecuteInfo;
lpExitCode: DWORD;
begin
if ParamCount < 1 then
begin
ShowHelp;
ExitProcess(1);
end;
ProgramDir := ParamStr(1);
if not DirectoryExists(ProgramDir) then
begin
UpdateFail('');
ExitProcess(1);
end;
if ParamCount >= 2 then
Silent := UpperCase(ParamStr(2)) = UpperCase(SilentParam)
else
Silent := False;
WaitMutex;
if ReplaceAllFile(ProgramDir) then
begin
if not Silent then UpdateComplete(ProgramDir);
ExitProcess(0);
end
else
begin
if Silent then ExitProcess(1);
ZeroMemory(@Sei, SizeOf(Sei));
Sei.cbSize := SizeOf(Sei);
Sei.fMask := SEE_MASK_NOCLOSEPROCESS;
Sei.lpVerb := LPTSTR('runas');
Sei.lpFile := LPTSTR(ParamStr(0));
Sei.nShow := SW_HIDE;
Sei.lpParameters := PChar(Format(RestartParamsFormat, [ProgramDir.QuotedString('"'), SilentParam]));
if ShellExecuteEx(@Sei) then
begin
WaitForSingleObject(Sei.hProcess, INFINITE);
if not GetExitCodeProcess(Sei.hProcess, lpExitCode) then
begin
UpdateFail(ProgramDir);
ExitProcess(1);
end;
if lpExitCode <> 0 then
begin
UpdateFail(ProgramDir);
ExitProcess(1);
end;
UpdateComplete(ProgramDir);
end
else
begin
UpdateFail(ProgramDir);
ExitProcess(1);
end;
end;
end.