-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLoading.pas
More file actions
221 lines (196 loc) · 6.42 KB
/
Loading.pas
File metadata and controls
221 lines (196 loc) · 6.42 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
unit Loading;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, sLabel,
Vcl.Imaging.GIFImg, Vcl.ExtCtrls, acPNG, Vcl.ComCtrls;
type
TFormLoading = class(TForm)
sLabelFX1: TsLabelFX;
ImageDialing: TImage;
LabelState: TLabel;
LabelVer: TLabel;
ProgressBar: TProgressBar;
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure FormShow(Sender: TObject);
private
UpdateLog:TStringList; //Ëîã îáíîâëåíèÿ
procedure ULog(Value:string);
public
class procedure SplashShow;
class procedure SplashClose;
procedure UpdateApp;
procedure RunApp;
procedure UpdateLoading(UpdateState:Integer);
end;
var
FormLoading: TFormLoading;
aLink:string;
procedure SetLoadState(str:string);
implementation
uses Main, Main.CommonFunc;
{$R *.dfm}
procedure TFormLoading.ULog(Value:string);
begin
UpdateLog.Insert(0, DateTimeToStr(Now)+': '+Value+' with _> "'+SysErrorMessage(GetLastError)+'"');
if not DirectoryExists(ExtractFilePath(Application.ExeName)+'Logs') then CreateDir(ExtractFilePath(Application.ExeName)+'Logs');
if DirectoryExists(ExtractFilePath(Application.ExeName)+'Logs') then
begin
try
UpdateLog.SaveToFile(ExtractFilePath(Application.ExeName)+'Logs\'+LogName);
except
end;
end;
end;
procedure TFormLoading.UpdateLoading(UpdateState:Integer);
begin
if UpdateState > 0 then
begin
ProgressBar.Position:=UpdateState;
ProgressBar.State:=pbsNormal;
SetLoadState('Çàãðóçêà îáíîâëåíèé...');
end
else
begin
ProgressBar.Position:=1;
ProgressBar.State:=pbsError;
SetLoadState('Íåò ñîåäèíåíèÿ');
end
end;
procedure TFormLoading.RunApp;
begin
WinExec(PAnsiChar(AnsiString(ExtractFilePath(Application.ExeName)+InternalExe)), SW_NORMAL);
end;
class procedure TFormLoading.SplashClose;
begin
if Assigned(FormLoading) then FormLoading.Close;
end;
class procedure TFormLoading.SplashShow;
begin
FormLoading:=TFormLoading.Create(nil);
FormLoading.Repaint;
FormLoading.Show;
Application.ProcessMessages;
SetLoadState('Çàïóñê...');
end;
procedure TFormLoading.UpdateApp;
var TryInc:Integer;
begin
LabelVer.Caption:='New';
SetLoadState('Çàãðóçêà îáíîâëåíèé...');
ULog('Íà÷àòî îáíîâëåíèå');
ProgressBar.Show;
Application.ProcessMessages;
aLink:='';
try
aLink:=ParamStr(1);
except
begin
aLink:='';
ULog('except aLink:=ParamStr(1)');
end;
end;
if aLink = '' then
begin
ULog('aLink = "", Exit');
MessageBox(Application.Handle, 'Â ïàðàìåòðàõ çàïóñêà íå óêàçàíà ññûëêà íà ôàéë îáíîâëåíèé!', 'Îøèáêà', MB_ICONERROR or MB_OK);
Exit;
end;
StopUpdate:=False;
TryInc:=1;
while (not Application.Terminated) and (not StopUpdate) and (TryInc < 10) do
begin
SetLoadState('Çàãðóçêà. Ïîïûòêà ¹'+IntToStr(TryInc));
if FileExists(UpdateFile) then
begin
ULog('DeleteFile(UpdateFile) = '+IntToStr(Ord(DeleteFile(UpdateFile))));
end;
Application.ProcessMessages;
ULog('GetInetFile(aLink, UpdateFile)');
if GetInetFile(aLink, UpdateFile) then Break;
end;
Application.ProcessMessages;
if StopUpdate then
begin
ULog('Îáíîâëåíèå îòìåíåíî, Exit');
SetLoadState('Îáíîâëåíèå îòìåíåíî');
ProgressBar.State:=pbsError;
Application.ProcessMessages;
Sleep(3000);
Exit;
end;
if Application.Terminated then Exit;
ULog('Çàêðûòèå êîíôëèêò. ïðîãðàìì...');
SetLoadState('Çàêðûòèå êîíôëèêò. ïðîãðàìì...');
WinExec(PAnsiChar(AnsiString('taskkill /f /im '+UpdateExe)), SW_HIDE);
//Çàêðûâàåì çàïóùåííûå ïðîãðàììû AsterLine
ULog('Çàêðûâàåì AsterLine...');
SetLoadState('Çàêðûâàåì AsterLine...');
SendNotifyMessage(HWND_BROADCAST, WM_MSGCLOSE, 0, 0);
//Ïîäîæä¸ì ïîêà çàâåðøàòñÿ ðàáî÷èå ïðîãðàììû AsterLine.exe
Sleep(3000);
//Óáèâàåì âðó÷íóþ, åñëè ÷òî
ULog('taskkill /f /im '+InternalExe);
WinExec(PAnsiChar(AnsiString('taskkill /f /im '+InternalExe)), SW_HIDE);
Sleep(2000);
//Óäàëÿåì ðàáî÷èé ôàéë (òîò, êîòîðûé çàìåíÿåì)
ULog('Îáíîâëåíèå ôàéëîâ... 1/3 DeleteFile(ExtractFilePath(Application.ExeName)+InternalExe)');
SetLoadState('Îáíîâëåíèå ôàéëîâ... 1/3');
if FileExists(ExtractFilePath(Application.ExeName)+InternalExe) then
while not DeleteFile(ExtractFilePath(Application.ExeName)+InternalExe) do
begin
case MessageBox(Application.Handle, PChar('Íå ñìîã óäàëèòü ôàéë "'#13#10+ExtractFilePath(Application.ExeName)+InternalExe+'"'#13#10'Ïîâòîðèòü ïîïûòêó?'), PWideChar(AppDesc+' - Âíèìàíèå'), MB_ICONWARNING or MB_YESNOCANCEL) of
ID_CANCEL:Exit;
ID_NO:Break;
end;
end;
ULog('Îáíîâëåíèå ôàéëîâ... 2/3 if FileExists(ExtractFilePath(Application.ExeName)+InternalExe) then Exit');
SetLoadState('Îáíîâëåíèå ôàéëîâ... 2/3');
if FileExists(ExtractFilePath(Application.ExeName)+InternalExe) then
begin
MessageBox(Application.Handle, 'Ïðîèçîøëà îøèáêà ïðè îáíîâëåíèè ïðîãðàììû.'+#13#10+'Íå ñìîã óäàëèòü ôàéë.', PWideChar(AppDesc+' - Âíèìàíèå'), MB_ICONWARNING or MB_OK);
Exit;
end;
ULog('Îáíîâëåíèå ôàéëîâ... 3/3 CopyFile(PWideChar(UpdateFile), PWideChar(ExtractFilePath(Application.ExeName)+InternalExe), True)');
SetLoadState('Îáíîâëåíèå ôàéëîâ... 3/3');
if not CopyFile(PWideChar(UpdateFile), PWideChar(ExtractFilePath(Application.ExeName)+InternalExe), True) then
begin
MessageBox(Application.Handle, 'Ïðîèçîøëà îøèáêà ïðè îáíîâëåíèè ïðîãðàììû.'+#13#10+'Íå ñìîã ñêîïèðîâàòü ôàéë.', PWideChar(AppDesc+' - Âíèìàíèå'), MB_ICONWARNING or MB_OK);
Exit;
end;
//Çàïóñêàåì íîâóþ âåðñèþ
ULog('Ñîçäàíèå ÿðëûêà '+ExtractFilePath(Application.ExeName)+InternalExe);
SetLoadState('Ñîçäàíèå ÿðëûêà');
CreateShortcut(ExtractFilePath(Application.ExeName)+InternalExe);
end;
procedure SetLoadState(str:string);
begin
if not Assigned(FormLoading) then Exit;
FormLoading.LabelState.Caption:=str;
FormLoading.Update;
FormLoading.Refresh;
end;
procedure TFormLoading.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
StopUpdate:=True;
CanClose:=True;
end;
procedure TFormLoading.FormCreate(Sender: TObject);
begin
UpdateLog:=TStringList.Create;
sLabelFX1.Caption:=AppDesc;
LabelVer.Caption:=GetAppVersionStr;
Color:=ColorDarker(GetAeroColor);
end;
procedure TFormLoading.FormPaint(Sender: TObject);
begin
Canvas.Pen.Color:=ColorDarker(Color, 60);
Canvas.Rectangle(0, 0, ClientWidth, ClientHeight);
end;
procedure TFormLoading.FormShow(Sender: TObject);
begin
SetForegroundWindow(Handle);
end;
end.