-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAutoUpdater.pas
More file actions
490 lines (424 loc) · 15.1 KB
/
AutoUpdater.pas
File metadata and controls
490 lines (424 loc) · 15.1 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
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
unit AutoUpdater;
interface
uses windows, sysutils, ExtActns, Common, constantes, classes, CMIni, utils, unitjoiner,
splashscreen, SkySQL, DriverHandler;
//Function StartUpdate(Ini: TCMIni): boolean;
Procedure ShowMessages(Modo: integer; pIni: PCMIni = nil);
implementation
uses UnitMatriz, FrameUpdates, StrUtils, Inicializador;
var
Joiner: TJoiner;
function WindowsDirectory: AnsiString;
var
WinDir: PChar;
begin
WinDir := StrAlloc(MAX_PATH);
GetWindowsDirectory(WinDir, MAX_PATH);
Result := StrPas(WinDir);
if Result[Length(Result)] <> '\' then
Result := Result + '\';
StrDispose(WinDir);
end;
function SystemDirectory: AnsiString;
var
SysDir: PChar;
begin
SysDir := StrAlloc(MAX_PATH);
GetSystemDirectory(SysDir, MAX_PATH);
Result := StrPas(SysDir);
if Result[Length(Result)] <> '\' then
Result := Result + '\';
StrDispose(SysDir);
end;
function GetTempDir: AnsiString;
var
TmpDir: PChar;
begin
TmpDir := StrAlloc(MAX_PATH);
GetTempPath(MAX_PATH, TmpDir);
Result := StrPas(TmpDir);
if Result[Length(Result)] <> '\' then
Result := Result + '\';
StrDispose(TmpDir);
end;
Function FixPath(valor: AnsiString; removeMask: boolean = false): AnsiString;
begin
if ( Pos('<root>', valor) = 0 ) and
( Pos('<windows>', valor) = 0 ) and
( Pos('<system>', valor) = 0 ) and
( Pos('<temp>', valor) = 0 ) then
begin
if not removeMask then
valor := AnsiString(ExtractFilePath(ParamStr(0))+valor)
end else
begin
if not removeMask then
begin
valor := AnsiReplaceStr(valor, '<root>', ExtractFilePath(ParamStr(0)));
valor := AnsiReplaceStr(valor, '<windows>', WindowsDirectory);
valor := AnsiReplaceStr(valor, '<system>', SystemDirectory);
valor := AnsiReplaceStr(valor, '<temp>', GetTempDir);
end else
begin
valor := AnsiReplaceStr(valor, '<root>', '');
valor := AnsiReplaceStr(valor, '<windows>', '');
valor := AnsiReplaceStr(valor, '<system>', '');
valor := AnsiReplaceStr(valor, '<temp>', '');
end;
end;
result := valor;
end;
Procedure ShowMessages(Modo: integer; pIni: PCMIni = nil);
var //ini: PCMIni;
UpdatePath: AnsiString;
Local: AnsiString;
Keys: TStrings;
i, j, k: integer;
Level: integer;
Function TraduzIcone(valor: AnsiString): integer;
begin
result := 0;
valor := trim(LowerCase(valor));
if( valor <> '' ) then
begin
if valor[1] = 'w' then result := MB_ICONWARNING
else
if valor[1] = 'a' then result := MB_ICONASTERISK
else
if valor[1] = 'e' then result := MB_ICONERROR
else
if valor[1] = 'x' then result := MB_ICONEXCLAMATION
else
if valor[1] = 'q' then result := MB_ICONQUESTION;
end;
end;
begin
Local := ExtractFilePath(ParamStr(0));
UpdatePath := Local+'Updates\';
Keys := TStringList.Create;
if(pIni = nil) then
begin
pIni := new(PCMIni);
pIni^ := TCMIni.create(GetHDCode, UpdatePath+'update.cmx');
end;
pIni^.ListKeys(Keys);
for i:= 0 to Keys.Count-1 do
begin
if( (keys[i][1]+keys[i][2]) <> '01' ) then
continue;
Level := pIni^.getvalue(Keys[i], 'Level', 0).AsInteger ;
j := pIni^.getvalue(Keys[i], 'ShowTime', 0).AsInteger ;
//Apenas para parte de Atualização
if (j <> Modo) or (j >= 2) then
continue;
//Pega mensagem no arquivo de update
j := pIni^.getvalue(Keys[i], 'Modo', '0').AsInteger;
case j of
0: MessageBox(0, PCHAR( trim(pIni^.getvalue(Keys[i], 'Message', '0').AsString ) ), PCHAR( trim(pIni^.getvalue(Keys[i], 'Titulo', '0').AsString )), TraduzIcone( pIni^.getvalue(Keys[i], 'Icone', '0').AsString ) );
1: Mensagem.Add(Mensagem.GetFreeName, trim(pIni^.getvalue(Keys[i], 'Message', '0').AsString) );
end;
//Avisa o arquivo de upload que a mensagem foi mostrada
k := UpIni.getvalue( Keys[i], 'qnt', '0' ).AsInteger;
UpIni.setvalue(Keys[i], 'ultimo', FormatDateTime('yyyy-mm-dd h:m:s', now));
UpIni.setvalue(Keys[i], 'level', Level);
UpIni.setvalue(Keys[i], 'qnt', k+1 );
UpIni.save;
UpIni.close;
//Deleta a mensagem do arquivo de update
pIni^.delete(keys[i]);
pIni^.save;
end;
end;
Function FixVersion(valor: AnsiString): integer;
begin
result := 0;
if trim(valor) = '' then
exit;
try
result := StrToInt(valor);
except
on e:exception do
result := 0;
end;
end;
Function SafeStrToInt(valor: AnsiString): integer;
begin
result := FixVersion(valor);
end;
Function IsUpdated(version: integer; arquivo: AnsiString):boolean;
var vers1: integer;
begin
result := false;
vers1 := FixVersion(Joiner.ExtractString(arquivo));
if (vers1 = 0) or (vers1 < version) then
exit;
result := true;
end;
type
TFileToDown = packed record
arquivo: AnsiString;
size: integer;
end;
Function IsVersionCorrect(arquivo: AnsiString; atual: integer): Boolean;
var
Version: AnsiString;
iVersion: integer;
begin
result := false;
//Pega versão do arquivo
Version := Joiner.ExtractString(arquivo, Constante(0063, false));
iVersion := FixVersion(Version);
if (atual = 0) or (iVersion = 0) or (iVersion < atual) then
exit;
result := true;
end;
Function CheckFileState(arq: AnsiString; data: TDateTime): integer;
var fData: TDateTime; cData: TDateTime;
Aplicacao : THandle;
FPath: AnsiString;
Info1,Info2,Info3: TFileTime;
Estrutura : SystemTime;
begin
FPath := ExtractFilePath( ParamStr(0) )+'Plugins\'+ arq;
if FileExists( FPath ) then
begin
try
fData := data;//StrToDateTime( trim(data) );
except
on e:exception do
fData := MinDateTime;
end;
try //formatdatetime('dd/mm/yyyy hh:mm:ss', cdata)+' - '+formatdatetime('dd/mm/yyyy hh:mm:ss', fdata)
Aplicacao := FileOpen(FPath, fmOpenRead or fmShareDenyNone);
if Aplicacao > 0 then
begin
GetFileTime( Aplicacao, @Info1, @Info2, @Info3);
if FileTimeToSystemTime(Info3, Estrutura) then
begin
cData := SystemTimeToDateTime(Estrutura);
end;
end;
finally
FileClose(Aplicacao);
end;
if cData < fData then
result := 1
else
result := 2;
end else
begin
result := 0;
end;
end;
Function GetDownloadSize(arq: AnsiString): int64;
var Post: TPost;
res: AnsiString;
param: AnsiString;
begin
Post := TPost.Create(false);
//if(frmMatriz.frameOptions1.edtConPort.Value = 0) then
// Post.Port := 80
//else
Post.Port := 8080;//frmMatriz.frameOptions1.edtConPort.Value;
Post.Host := AnsiString(Constante(0064, false)) + AnsiString(Constante(0067, false));
Post.Clear;
param := AnsiReplaceStr(arq,'\', '/');
Post.Add( IntToStr(9123+PostIndex), UrlEncode(param));
res := trim(Post.Execute);
try
result := StrToInt(res);
except
on e:exception do
result := 0;
end;
end;
(*
Function StartUpdate(Ini: TCMIni): boolean;
var Files: TStrings;
Values: TStrings;
i,j: integer;
Arquivo: AnsiString;
Key: AnsiString;
Local: AnsiString;
UpdateNow: Boolean;
Flag: Boolean;
IniVersion: integer;
UpdateFile: AnsiString;
UpdatePath: AnsiString;
FileName: AnsiString;
NewIni: TCMIni;
Donwloads: array of TFileToDown;
Messages: TStrings;
DataS: AnsiString;
DataD: TDateTime;
NewKeys: TStrings;
begin
try
ChangeSplash( StrPas(Constante(0167, true)) );
Joiner := TJoiner.create;
Local := ExtractFilePath(ParamStr(0));
UpdatePath := Local+StrPas(Constante(0076, false));
UpdateNow := false;
NewIni := TCMIni.create(GetHDCode, UpdatePath+StrPas(Constante(0083, false)));
//Deu erro na atualização anterior?
if(NewIni.ValueExists('Erro')) then
begin
if MessageBox(0, PChar(Constante(0169, true)), PChar(Constante(0171, true)),MB_ICONQUESTION+MB_YESNO) <> IDYES then
begin
MessageBox(0, PChar(Constante(0170, true)), PChar(Constante(0171, true)), MB_ICONINFORMATION+MB_OK);
ExitProcess(0);
end;
NewKeys := TStringList.Create;
try
NewIni.ListKeys(NewKeys);
for i := 0 to NewKeys.Count-1 do
begin
try
NewIni.delete(NewKeys[i], 'Erro');
except
on e:exception do
end;
end
finally
NewKeys.Free;
end;
end;
Files := TStringList.Create;
Values := TStringList.Create;
Messages := TStringList.Create;
//Ini eh de dados na memória, vindos do servidor; então precisamos salvar
//no disco no arquivo NewIni
Ini.ListKeys( Files );
Donwloads := nil;
//Verifica todos os arquivos do INI
for i := 0 to Files.Count-1 do
begin
Flag := false;
Key := Trim(Files.Strings[i]);
//Salva as mensagens
if( (Key[1] + Key[2]) = '01' ) then
begin
Ini.ListValues(Key, Messages);
for j := 0 to Messages.Count-1 do
NewIni.setvalue( Key, Messages[j], Ini.getvalue(Key, Messages[j]).AsString);
end;
//Se nao for key de atualização continua
if( (Key[1] + Key[2]) <> '02' ) then
continue;
//Nome do arquivo a ser atualizado
Arquivo := ini.getvalue(Files.Strings[i], 'Arquivo', '').AsString;
FileName := ExtractFileName(FixPath(Arquivo));
//Pega versão atual
IniVersion := Ini.getvalue(Files.Strings[i], 'Version', 0).AsInteger;
//Diretorio + Arquivo - Lembrando que o arquivo pode ser colocado
//em qualquer parte do PC, nao apenas no diretorio local, usando masks.
UpdateFile := FixPath(Arquivo); //Local+Arquivo;
//Se nao existir o arquivo no local certo ou na pasta updates, ou sea versão
//dos dois não estiver correta, seta o flag
if (not FileExists(UpdateFile)) or ( (FileExists(UpdateFile)) and ((not IsVersionCorrect(UpdateFile, IniVersion)) or (IniVersion = -1)) ) then
begin
//Tipo de arquivo que é atualizado dependendo da data de criação (versao = -1)
//Informar data em campo 'Data'
if(IniVersion = -1) then
begin
DataS := Ini.getvalue(Files.Strings[i], 'Data', 0).AsString;
if Length(DataS) < 10 then
continue;
try
DataD := StrToDateTime(DataS);
if not FileExists(UpdateFile) then
Flag := true
else
begin
//Está desatualizado
if CheckFileState(UpdateFile, DataD) = 1 then
begin
flag := true
end else
begin
//ini.delete
continue;
end;
end;
except
on e:exception do
continue;
end;
end else
begin
//O arquivo existe na pasta de atualizações (ja foi feito o download)
if FileExists(UpdatePath+FixPath(Arquivo, true) ) then
begin
//Verifica se o Ini de update está completo, apesar de o arquivo estar ae...
//Mas tem doido pra mexe em tudo neh..
if not NewIni.ValueExists(Arquivo) then
begin
//Ixi.. falei! Então agora vamos apenas restaurar o INI de update
Ini.ListValues(Files.Strings[i], Values);
for j := 0 to Values.Count-1 do
begin
NewIni.setvalue(Files.Strings[i], Values.Strings[j], Ini.getvalue(Files.Strings[i], Values.Strings[j]).AsString );
NewIni.save;
end;
end;
//Indica que o arquivo já existe
UpdateFile := UpdatePath+FixPath(Arquivo, true);
UpdateNow := true;
end else
Flag := true;
end;
end;
if not Flag then
begin
//Se a versão for inválida ou antiga seta o flag
if not IsVersionCorrect(UpdateFile, IniVersion) then
flag := true;
end;
//Donwloads := nil;
//Se o Flag estiver setado grava o arquivo ini temporário
if flag then
begin
UpdateNow := true;
Ini.ListValues(Files.Strings[i], Values);
for j := 0 to Values.Count-1 do
begin
NewIni.setvalue( Files.Strings[i], Values.Strings[j], Ini.getvalue(Files.Strings[i], Values.Strings[j], '').AsString );
NewIni.save;
end;
SetLength(Donwloads, Length(Donwloads)+1);
Donwloads[High(Donwloads)].arquivo := Arquivo;
Donwloads[High(Donwloads)].size := {SafeStrToInt( Ini.getvalue(Files.Strings[i], 'Size', '0').AsString );} GetDownloadSize(AnsiReplaceStr(FixPath(Arquivo, true), '\', '/'));//GetDownloadSize(Arquivo);
pTotalAtualn := pTotalAtualn + Donwloads[High(Donwloads)].size;
end;
end;
NewIni.save;
//Tudo ok agora.. Vamos baixar os arquivos
for i := 0 to length(Donwloads)-1 do
begin
Arquivo := AnsiReplaceStr(FixPath(Donwloads[i].arquivo, true), '\', '/');
FileName := ExtractFileName(FixPath(Arquivo));
//Cria arvore do diretório se não existir
ForceDirectories( ExtractFilePath(UpdatePath+FixPath(Donwloads[i].arquivo, true)) );
//Faz o download
//j := frmMatriz.DownloadURL_NOCache(Constante(0064, false)+'/cmx/CMUpdates/'+Arquivo, Donwloads[i].size, UpdatePath+FixPath(Donwloads[i].arquivo, true));
//frmMatriz.DownloadUpdateFile(Constante(0064, false)+'/cmx/CMUpdates/'+Arquivo, UpdatePath+FixPath(Donwloads[i].arquivo, true));
frmMatriz.DownloadUpdateFile(Arquivo, UpdatePath+FixPath(Donwloads[i].arquivo, true));
end;
pTotalAtualn := 0;
Files.Free;
Values.Free;
Messages.Free;
if UpdateNow then
//ExitCM;
ReloadCM;
except
on e:exception do
begin
ChangeSplash( StrPas(Constante(0166, true)) );
Delay2(3000);
ExitCM;
end;
end;
end;
*)
end.