forked from yavfast/dbg-spider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuUpdateInfo.pas
246 lines (199 loc) · 5.44 KB
/
uUpdateInfo.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
unit uUpdateInfo;
interface
uses Classes, SysUtils, XMLDoc, XMLIntf, System.Generics.Collections;
type
TChangeLogItemType = (cliInfo = 0, cliFix, cliAdd, cliUpdate);
TChangeLogItem = class
ItemType: TChangeLogItemType;
ItemText: String;
class function StrToItemType(const Str: String): TChangeLogItemType; static;
class function ItemTypeAsStr(const ItemType: TChangeLogItemType): String; static;
end;
TChangeLogVersionInfo = class(TObjectList<TChangeLogItem>)
private
FVersion: String;
FDate: TDateTime;
function GetData: String;
procedure SetDate(const Value: String);
public
property Version: String read FVersion write FVersion;
property Date: String read GetData write SetDate;
end;
TUpdateInfo = class
private
FXML: IXMLDocument;
FCurVer: String;
function GetLastVersion: String;
public
constructor Create;
destructor Destroy; override;
function Load: Boolean;
function GetAllVersions(var SL: TStringList): Boolean;
function GetVersionInfo(const Version: String; var Info: TChangeLogVersionInfo): Boolean;
property LastVersion: String read GetLastVersion;
property CurrentVersion: String read FCurVer;
end;
var
gvUpdateInfo: TUpdateInfo = nil;
implementation
uses
Forms, IdHTTP, ClassUtils, WinAPIUtils;
const
_UpdateInfoURL = 'http://dbg-spider.net/update_info.txt';
(*
<?xml version="1.0" encoding="utf-8"?>
<spider last_version="1.0.3.0">
<change_log>
<release version="1.0.3.0">
<item type="fix"></item>
<item type="add"></item>
</release>
</change_log>
</spider>
*)
{ TUpdateInfo }
constructor TUpdateInfo.Create;
begin
inherited Create;
FXML := nil;
FCurVer := GetFileVersion(Application.ExeName)
end;
destructor TUpdateInfo.Destroy;
begin
FXML := nil;
inherited;
end;
function TUpdateInfo.GetAllVersions(var SL: TStringList): Boolean;
var
I: Integer;
ChangeLogNode: IXMLNode;
ReleaseNode: IXMLNode;
Ver: String;
begin
Result := False;
SL.Clear;
if Assigned(FXML) then
begin
ChangeLogNode := FXML.DocumentElement.ChildNodes.Nodes['change_log'];
if Assigned(ChangeLogNode) then
begin
for I := 0 to ChangeLogNode.ChildNodes.Count - 1 do
begin
ReleaseNode := ChangeLogNode.ChildNodes[I];
Ver := ReleaseNode.Attributes['version'];
if Ver <> '' then
SL.Add(Ver);
end;
Result := True;
end;
end;
end;
function TUpdateInfo.GetLastVersion: String;
begin
Result := '';
if Assigned(FXML) then
begin
Result := FXML.DocumentElement.Attributes['last_version'];
end;
end;
function TUpdateInfo.GetVersionInfo(const Version: String; var Info: TChangeLogVersionInfo): Boolean;
var
I, J: Integer;
ChangeLogNode: IXMLNode;
ReleaseNode: IXMLNode;
ItemNode: IXMLNode;
Ver: String;
ChangeLogItem: TChangeLogItem;
begin
Result := False;
Info.Clear;
Info.Version := Version;
if Assigned(FXML) then
begin
ChangeLogNode := FXML.DocumentElement.ChildNodes.Nodes['change_log'];
if Assigned(ChangeLogNode) then
begin
for I := 0 to ChangeLogNode.ChildNodes.Count - 1 do
begin
ReleaseNode := ChangeLogNode.ChildNodes[I];
Ver := ReleaseNode.Attributes['version'];
if Ver = Version then
begin
Info.Date := ReleaseNode.Attributes['date'];
for J := 0 to ReleaseNode.ChildNodes.Count - 1 do
begin
ItemNode := ReleaseNode.ChildNodes[J];
ChangeLogItem := TChangeLogItem.Create;
ChangeLogItem.ItemType := TChangeLogItem.StrToItemType(ItemNode.Attributes['type']);
if ItemNode.IsTextElement then
ChangeLogItem.ItemText := ItemNode.Text;
Info.Add(ChangeLogItem);
end;
end;
end;
Result := True;
end;
end;
end;
function TUpdateInfo.Load: Boolean;
var
IdHttp: TIdHTTP;
Res: String;
begin
Result := False;
FXML := nil;
IdHttp := TIdHTTP.Create(nil);
try
try
Res := IdHttp.Get(_UpdateInfoURL);
if Res <> '' then
begin
FXML := TXMLDocument.Create(nil);
FXML.LoadFromXML(Res);
Result := FXML.Active;
end;
except
FXML := Nil;
end;
finally
FreeAndNil(IdHttp);
end;
end;
{ TChangeLogItem }
class function TChangeLogItem.ItemTypeAsStr(const ItemType: TChangeLogItemType): String;
begin
case ItemType of
cliInfo: Result := 'Info';
cliFix: Result := 'Fix';
cliAdd: Result := 'Add';
cliUpdate: Result := 'Update';
else
Result := '';
end;
end;
class function TChangeLogItem.StrToItemType(const Str: String): TChangeLogItemType;
begin
for Result := Low(TChangeLogItemType) to High(TChangeLogItemType) do
if SameText(Str, ItemTypeAsStr(Result)) then
Exit;
Result := cliInfo;
end;
{ TChangeLogVersionInfo }
function TChangeLogVersionInfo.GetData: String;
begin
Result := DateToStr(FDate);
end;
procedure TChangeLogVersionInfo.SetDate(const Value: String);
var
FS: TFormatSettings;
begin
FS.ShortDateFormat := 'yyyy-mm-dd';
FS.DateSeparator := '-';
FS.LongDateFormat := 'yyyy-mm-dd';
TryStrToDate(Value, FDate, FS);
end;
initialization
gvUpdateInfo := TUpdateInfo.Create;
finalization
FreeAndNil(gvUpdateInfo);
end.