forked from groupsc10-zz/WBot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwbot_utils.pas
531 lines (495 loc) · 14.3 KB
/
wbot_utils.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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
{
_ _ _ _____
| | | || | |_ _| /|
| | | || |___ ___ | | / |__
| |/\| || _ |/ _ \| | /_ /
| / \ || (_) | (_) | | | /
|__/\__||_____|\___/|_| |/
}
unit WBot_Utils;
{$i wbot.inc}
interface
uses
Classes, SysUtils, TypInfo, fpjson, jsonparser, jsonscanner, base64, LResources,
// Wbot
WBot_Model;
function StringToFile(const AString: string; const AFileName: TFileName;
const ASafely: boolean = True): boolean;
function FileToString(const AFileName: TFileName;
const ASafely: boolean = True): string;
function JSONParse(const AString: TJSONStringType;
const ASafely: boolean = True): TJSONData;
procedure JSONToObject(const AJSON: TJSONStringType;
const AObject: TObject); overload;
procedure JSONToObject(const AJSON: TJSONData;
const AObject: TObject); overload;
function ObjectToJSON(const AObject: TObject): TJSONData;
procedure Base64ToStream(const AString: string; const AStream: TStream;
const AMode: TBase64DecodingMode = bdmMIME);
function StreamToBase64(const AStream: TStream): string;
function ResourceToString(const AResourceName: string): string;
function ReplaceVAR(const AScript, AVar, ANewValue: string): string;
function NormalizeString(const AValue: string): string;
function StringToActionType(const AValue: string): TActionType;
function ActionTypeToString(const AValue: TActionType): string;
implementation
function StringToFile(const AString: string; const AFileName: TFileName;
const ASafely: boolean): boolean;
var
VStream: TFileStream;
begin
Result := False;
try
VStream := TFileStream.Create(AFileName, fmCreate);
try
VStream.Write(Pointer(AString)^, Length(AString));
finally
FreeAndNil(VStream);
end;
Result := True;
except
if (not (ASafely)) then
begin
raise;
end;
end;
end;
function FileToString(const AFileName: TFileName;
const ASafely: boolean): string;
var
VStream: TFileStream;
VLength: NativeInt;
begin
Result := EmptyStr;
if (FileExists(AFileName)) then
begin
try
VStream := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);
try
VLength := VStream.Size;
if (VLength > 0) then
begin
SetLength(Result, VLength);
VStream.Read(Pointer(Result)^, VLength);
end;
finally
FreeAndNil(VStream);
end;
except
if (not (ASafely)) then
begin
raise;
end;
end;
end;
end;
function JSONParse(const AString: TJSONStringType;
const ASafely: boolean): TJSONData;
var
VParser: TJSONParser;
begin
Result := nil;
VParser := TJSONParser.Create(AString, [joUTF8]);
try
try
Result := VParser.Parse;
except
FreeAndNil(Result);
if (not (ASafely)) then
begin
raise;
end;
end;
finally
FreeAndNil(VParser);
end;
end;
procedure JSONToStrings(const AJSON: TJSONArray; const AStrings: TStrings);
var
VIndex: NativeInt;
VJSON: TJSONData;
begin
if (Assigned(AStrings)) and (Assigned(AJSON)) then
begin
AStrings.Clear;
for VIndex := 0 to (AJSON.Count - 1) do
begin
VJSON := AJSON[VIndex];
if (Assigned(VJSON)) and (VJSON.JSONType = jtString) then
begin
AStrings.Add(VJSON.AsString);
end;
end;
end;
end;
procedure JSONToModelList(const AJSON: TJSONArray;
const AModelList: TModelList);
var
VIndex: NativeInt;
VJSON: TJSONData;
VModel: TModel;
VModelClass: TModelClass;
begin
if (Assigned(AModelList)) and (Assigned(AJSON)) then
begin
AModelList.Clear;
VModelClass := AModelList.ItemClass;
if (Assigned(VModelClass)) then
begin
for VIndex := 0 to (AJSON.Count - 1) do
begin
VJSON := AJSON[VIndex];
if (Assigned(VJSON)) and (VJSON.JSONType = jtObject) then
begin
VModel := VModelClass.Create;
JSONToObject(VJSON, VModel);
AModelList.Add(VModel);
end;
end;
end;
end;
end;
procedure JSONToObject(const AJSON: TJSONStringType; const AObject: TObject);
var
VJSON: TJSONData;
begin
if (Assigned(AObject)) then
begin
VJSON := JSONParse(AJSON);
if (Assigned(VJSON)) then
begin
try
if (VJSON.JSONType = jtArray) then
begin
if (AObject.InheritsFrom(TStrings)) then
begin
JSONToStrings(VJSON as TJSONArray, AObject as TStrings);
end
else
if (AObject.InheritsFrom(TModelList)) then
begin
JSONToModelList(VJSON as TJSONArray, AObject as TModelList);
end;
end
else
if (VJSON.JSONType = jtString) then
begin
JSONToObject(VJSON.AsString, AObject);
end
else
begin
JSONToObject(VJSON, AObject);
end;
finally
FreeAndNil(VJSON);
end;
end;
end;
end;
procedure JSONToObject(const AJSON: TJSONData; const AObject: TObject);
var
VProp: PPropInfo;
VProps: PPropList;
VPropsCount: NativeInt;
VIndex: NativeInt;
VObject: TObject;
VJSON: TJSONData;
VJSONIndex: NativeInt;
begin
if (Assigned(AObject)) and
(Assigned(AJSON)) and (AJSON.JSONType = jtObject) then
begin
try
VPropsCount := GetPropList(AObject, VProps);
for VIndex := 0 to (VPropsCount - 1) do
begin
VProp := VProps^[VIndex];
if (VProp <> nil) and (VProp^.GetProc <> nil) then
begin
VJSONIndex := (AJSON as TJSONObject).IndexOfName(VProp^.Name, True);
if (VJSONIndex > -1) then
begin
VJSON := (AJSON as TJSONObject).Items[VJSONIndex];
if (Assigned(VJSON)) and (VJSON.JSONType <> jtNull) then
begin
case VProp^.PropType^.Kind of
tkAString:
begin
if (VProp^.SetProc <> nil) and
(VJSON.JSONType in [jtBoolean, jtNumber, jtString]) then
begin
SetStrProp(AObject, VProp, VJSON.AsString);
end
else
if (VJSON.JSONType in [jtArray, jtObject]) then
begin
SetStrProp(AObject, VProp,
VJSON.FormatJSON(AsCompressedJSON));
end;
end;
tkFloat:
begin
if (VProp^.SetProc <> nil) and
(VJSON.JSONType in [jtBoolean, jtNumber]) then
begin
SetFloatProp(AObject, VProp, VJSON.AsFloat);
end;
end;
tkInteger:
begin
if (VProp^.SetProc <> nil) and
(VJSON.JSONType in [jtBoolean, jtNumber]) then
begin
SetOrdProp(AObject, VProp, VJSON.AsInteger);
end;
end;
tkInt64,
tkQWord:
begin
if (VProp^.SetProc <> nil) and
(VJSON.JSONType in [jtBoolean, jtNumber]) then
begin
SetInt64Prop(AObject, VProp, VJSON.AsInt64);
end;
end;
tkBool:
begin
if (VJSON.JSONType in [jtBoolean, jtNumber]) then
begin
SetOrdProp(AObject, VProp, Ord(VJSON.AsBoolean));
end;
end;
tkClass:
begin
VObject := GetObjectProp(AObject, VProp);
if (Assigned(VObject)) then
begin
if (VJSON.JSONType = jtArray) then
begin
if (VObject.InheritsFrom(TStrings)) then
begin
JSONToStrings(VJSON as TJSONArray, VObject as TStrings);
end
else
if (VObject is TModelList) then
begin
JSONToModelList(VJSON as TJSONArray,
VObject as TModelList);
end;
end
else
if (VJSON.JSONType = jtString) then
begin
JSONToObject(VJSON.AsString, VObject);
end
else
begin
JSONToObject(VJSON, VObject);
end;
end;
end;
end;
end;
end;
end;
end;
finally
FreeMemAndNil(VProps);
end;
end;
end;
function StringsToJSON(const AStrings: TStrings): TJSONArray;
var
VIndex: NativeInt;
begin
Result := TJSONArray.Create([]);
for VIndex := 0 to (AStrings.Count - 1) do
begin
Result.Add(AStrings[VIndex]);
end;
end;
function ModelListToJSON(const AModelList: TModelList): TJSONArray;
var
VIndex: NativeInt;
begin
Result := TJSONArray.Create([]);
for VIndex := 0 to (AModelList.Count - 1) do
begin
Result.Add(ObjectToJSON(AModelList[VIndex]));
end;
end;
function ObjectToJSON(const AObject: TObject): TJSONData;
var
VProp: PPropInfo;
VProps: PPropList;
VPropsCount: NativeInt;
VIndex: NativeInt;
VObject: TObject;
begin
Result := TJSONObject.Create([]);
if (Assigned(AObject)) then
begin
try
VPropsCount := GetPropList(AObject, VProps);
for VIndex := 0 to (VPropsCount - 1) do
begin
VProp := VProps^[VIndex];
if (VProp <> nil) and (VProp^.GetProc <> nil) then
begin
case VProp^.PropType^.Kind of
tkAString:
(Result as TJSONObject).Add(VProp^.Name,
GetStrProp(AObject, VProp));
tkFloat:
(Result as TJSONObject).Add(VProp^.Name,
GetFloatProp(AObject, VProp));
tkInteger:
(Result as TJSONObject).Add(VProp^.Name,
GetOrdProp(AObject, VProp));
tkInt64,
tkQWord:
(Result as TJSONObject).Add(VProp^.Name,
GetInt64Prop(AObject, VProp));
tkBool:
(Result as TJSONObject).Add(VProp^.Name,
GetOrdProp(AObject, VProp) <> 0);
tkClass:
begin
VObject := GetObjectProp(AObject, VProp);
if (Assigned(VObject)) then
begin
if (VObject.InheritsFrom(TStrings)) then
begin
(Result as TJSONObject).Add(VProp^.Name,
StringsToJSON(VObject as TStrings));
end
else
if (VObject.InheritsFrom(TModelList)) then
begin
(Result as TJSONObject).Add(VProp^.Name,
ModelListToJSON(VObject as TModelList));
end
else
begin
(Result as TJSONObject).Add(VProp^.Name,
ObjectToJSON(VObject));
end;
end;
end;
end;
end;
end
finally
FreeMemAndNil(VProps);
end;
end;
end;
procedure Base64ToStream(const AString: string; const AStream: TStream;
const AMode: TBase64DecodingMode);
var
VDecoder: TBase64DecodingStream;
VStream: TStringStream;
begin
if (AString <> EmptyStr) then
begin
VStream := TStringStream.Create(AString);
try
VDecoder := TBase64DecodingStream.Create(VStream, AMode);
try
AStream.CopyFrom(VDecoder, VDecoder.Size);
finally
VDecoder.Free;
end;
finally
VStream.Free;
end;
end;
end;
function StreamToBase64(const AStream: TStream): string;
var
VStream: TStringStream;
VEncoding: TBase64EncodingStream;
begin
Result := EmptyStr;
if (Assigned(AStream)) and (AStream.Size > 0) then
begin
AStream.Position := 0;
VStream := TStringStream.Create(EmptyStr);
try
VEncoding := TBase64EncodingStream.Create(VStream);
try
VEncoding.CopyFrom(AStream, AStream.Size);
finally
VEncoding.Free;
end;
Result := VStream.DataString;
finally
VStream.Free;
end;
end;
end;
function ResourceToString(const AResourceName: string): string;
var
VStream: TStringStream;
VResource: TLazarusResourceStream;
begin
Result := EmptyStr;
if (AResourceName <> EmptyStr) then
begin
VStream := TStringStream.Create(EmptyStr);
try
try
VResource := TLazarusResourceStream.Create(AResourceName, nil);
VResource.SaveToStream(VStream);
Result := VStream.DataString;
except
end;
finally
FreeAndNil(VStream);
FreeAndNil(VResource);
end;
end;
end;
function ReplaceVAR(const AScript, AVar, ANewValue: string): string;
var
VVar: string;
begin
VVar := AVar;
Result := VVar;
if (Length(VVar) > 2) then
begin
Result := StringReplace(AScript, Trim(VVar), Trim(ANewValue),
[rfReplaceAll, rfIgnoreCase]);
end;
end;
function NormalizeString(const AValue: string): string;
begin
Result := StringReplace(AValue, LineEnding, '\n', [rfReplaceAll]);
Result := StringReplace(Result, #13, '', [rfReplaceAll]);
Result := StringReplace(Result, '"', '\"', [rfReplaceAll]);
Result := StringReplace(Result, #$A, '', [rfReplaceAll]);
end;
function StringToActionType(const AValue: string): TActionType;
var
VType: TActionType;
VName: string;
begin
Result := atNone;
for VType in TActionType do
begin
VName := GetEnumName(TypeInfo(TActionType), Ord(VType));
if (LowerCase(VName) = LowerCase('at' + AValue)) then
begin
Result := VType;
Break;
end;
end;
end;
function ActionTypeToString(const AValue: TActionType): string;
var
VName: string;
begin
VName := GetEnumName(TypeInfo(TActionType), Ord(AValue));
Result := Copy(VName, 3, Length(VName) - 2);
end;
end.