-
Notifications
You must be signed in to change notification settings - Fork 12
/
bgradnetdeserial.pas
1560 lines (1414 loc) · 46.6 KB
/
bgradnetdeserial.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ ***************************************************************************
* *
* This file is part of BGRABitmap library which is distributed under the *
* modified LGPL. *
* *
* See the file COPYING.modifiedLGPL.txt, included in this distribution, *
* for details about the copyright. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* *
************************* BGRABitmap library ******************************
- Drawing routines with transparency and antialiasing with Lazarus.
Offers also various transforms.
- These routines allow to manipulate 32bit images in BGRA format or RGBA
format (depending on the platform).
- This code is under modified LGPL (see COPYING.modifiedLGPL.txt).
This means that you can link this library inside your programs for any purpose.
Only the included part of the code must remain LGPL.
- If you make some improvements to this library, please notify here:
http://www.lazarus.freepascal.org/index.php/topic,12037.0.html
********************* Contact : Circular at operamail.com *******************
******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | [email protected]
(Compatibility with FPC ($Mode objfpc/delphi) and delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
Unit BGRADNetDeserial;
{$i bgrabitmap.inc}{$H+}{$H+}
interface
{ This unit allow to read .Net serialized classes with BinaryFormatter of
namespace System.Runtime.Serialization.Formatters.Binary.
Serialization is a process by which objects in memory are saved according
to their structure.
This unit is used by BGRAPaintNet to read Paint.NET images. }
uses
Classes, BGRATypes,{$IFNDEF FPC}Types, GraphType, BGRAGraphics,{$ENDIF} SysUtils;
type
arrayOfLongword = array of BGRALongWord;
TTypeCategory = (ftPrimitiveType = 0, ftString = 1, ftObjectType =
2, ftRuntimeType = 3,
ftGenericType = 4, ftArrayOfObject = 5, ftArrayOfString = 6,
ftArrayOfPrimitiveType = 7);
TPrimitiveType = (ptNone = 0, ptBoolean = 1, ptByte = 2, ptChar = 3, ptDecimal = 5,
ptDouble = 6, ptInt16 = 7, ptInt32 = 8, ptInt64 = 9, ptSByte = 10, ptSingle = 11,
ptDateTime = 13, ptUInt16 = 14, ptUInt32 = 15, ptUInt64 = 16, ptString = 18);
TGenericArrayType = (gatSingleDimension, gatJagged, gatMultidimensional);
TDotNetDeserialization = class;
ArrayOfNameValue = array of record
Name: string;
Value, valueType: string;
end;
TFieldType = record
category: TTypeCategory;
primitiveType: TPrimitiveType;
refAssembly: BGRALongWord;
Name: string;
end;
TSerializedType = record
ClassName: string;
nbFields: integer;
fieldNames: array of string;
fieldTypes: array of TFieldType;
refAssembly: BGRALongWord;
end;
TAssemblyReference = record
idAssembly: BGRALongWord;
Name: string;
end;
{ TCustomSerializedObject }
TCustomSerializedObject = class
protected
FContainer: TDotNetDeserialization;
function GetTypeAsString: string; virtual; abstract;
function GetFieldAsString(Index: BGRALongWord): string; overload; virtual; abstract;
function GetFieldAsString(Name: string): string; overload;
function GetFieldCount: BGRALongWord; virtual; abstract;
function GetFieldName(Index: BGRALongWord): string; virtual; abstract;
function GetFieldTypeAsString(Index: BGRALongWord): string; virtual; abstract;
function IsReferenceType(index: BGRALongWord): boolean; virtual; abstract;
public
idObject: BGRALongWord;
refCount: integer;
inToString: boolean;
constructor Create(container: TDotNetDeserialization); virtual;
property FieldCount: BGRALongWord read GetFieldCount;
property FieldName[Index: BGRALongWord]:string read GetFieldName;
property FieldAsString[Index: BGRALongWord]: string read GetFieldAsString;
property FieldByNameAsString[Name: string]: string read GetFieldAsString;
property FieldTypeAsString[Index: BGRALongWord]: string read GetFieldTypeAsString;
property TypeAsString: string read GetTypeAsString;
function GetFieldIndex(Name: string): integer;
end;
{ TSerializedClass }
TSerializedClass = class(TCustomSerializedObject)
protected
function GetFieldAsString(Index: BGRALongWord): string; override;
function GetFieldCount: BGRALongWord; override;
function GetFieldName(Index: BGRALongWord): string; override;
function GetFieldTypeAsString(Index: BGRALongWord): string; override;
function IsReferenceType(index: BGRALongWord): boolean; override;
function GetTypeAsString: string; override;
public
numType: integer;
fields: ArrayOfNameValue;
end;
{ TSerializedArray }
TSerializedArray = class(TCustomSerializedObject)
private
data: pointer;
FItemSize: BGRALongWord;
function GetItemPtr(Index: BGRALongWord): pointer;
procedure InitData;
protected
FArrayType: TGenericArrayType;
function GetFieldAsString(Index: BGRALongWord): string; override;
function GetFieldCount: BGRALongWord; override;
function GetFieldName(Index: BGRALongWord): string; override;
function GetFieldTypeAsString(Index: BGRALongWord): string; override;
function IsReferenceType(index: BGRALongWord): boolean; override;
function GetTypeAsString: string; override;
public
dimensions: array of BGRALongWord;
itemType: TFieldType;
nbItems: BGRALongWord;
constructor Create(AContainer: TDotNetDeserialization; AItemType: TFieldType; ALength: BGRALongWord); overload;
constructor Create(AContainer: TDotNetDeserialization; AArrayType: TGenericArrayType; AItemType: TFieldType; ADimensions: arrayOfLongword); overload;
destructor Destroy; override;
property ItemPtr[Index:BGRALongWord]: pointer read GetItemPtr;
property ItemSize: BGRALongWord read FItemSize;
end;
{ TSerializedValue }
TSerializedValue = class(TSerializedArray)
protected
function GetIsReferenceType: boolean;
function GetValueAsString: string;
function GetTypeAsString: string; override;
public
constructor Create(AContainer: TDotNetDeserialization; AItemType: TFieldType); overload;
property ValueAsString: string read GetValueAsString;
property IsReferenceType: boolean read GetIsReferenceType;
end;
{ TDotNetDeserialization }
TDotNetDeserialization = class
objectTypes: array of TSerializedType;
assemblies: array of TAssemblyReference;
objects: array of TCustomSerializedObject;
function FindClass(typeName: string): TSerializedClass;
function FindObject(typeName: string): TCustomSerializedObject;
function GetSimpleField(obj: TCustomSerializedObject; Name: string): string; overload;
function GetObjectField(obj: TCustomSerializedObject; Name: string): TCustomSerializedObject; overload;
function GetObjectField(obj: TCustomSerializedObject; index: integer): TCustomSerializedObject; overload;
function GetObject(id: string): TCustomSerializedObject; overload;
function GetObject(id: BGRALongWord): TCustomSerializedObject; overload;
function IsBoxedValue(obj: TCustomSerializedObject; index: integer): boolean;
function GetBoxedValue(obj: TCustomSerializedObject; index: integer): string;
procedure LoadFromStream(Stream: TStream);
procedure LoadFromFile(filename: string);
procedure LoadFromFileUTF8(filenameUTF8: string);
function ToString: string; override;
constructor Create;
destructor Destroy; override;
function GetTypeOfClassObject(idObject: BGRALongWord): integer;
private
EndOfStream: boolean;
ArrayFillerCount: BGRALongWord;
currentAutoObjectValue: BGRALongWord;
function nextAutoObjectId: BGRALongWord;
function LoadNextFromStream(Stream: TStream): BGRALongWord;
function LoadStringFromStream(Stream: TStream): string;
function LoadDotNetCharFromStream(Stream: TStream): string;
function LoadTypeFromStream(Stream: TStream; IsRuntimeType: boolean): integer;
function LoadValuesFromStream(Stream: TStream; numType: integer): ArrayOfNameValue;
function LoadValueFromStream(Stream: TStream; const fieldType: TFieldType): string;
function LoadFieldType(Stream: TStream; category: TTypeCategory): TFieldType;
end;
function WinReadByte(stream: TStream): byte;
function WinReadWord(Stream: TStream): BGRAWord;
function WinReadSmallInt(Stream: TStream): smallint;
function WinReadLongint(Stream: TStream): BGRALongInt;
function WinReadLongword(Stream: TStream): BGRALongWord;
function WinReadInt64(Stream: TStream): BGRAInt64;
function WinReadQWord(Stream: TStream): BGRAQWord;
implementation
uses BGRAUTF8;
const
//block types
btRefTypeObject = 1;
btRuntimeObject = 4;
btExternalObject = 5;
btString = 6;
btGenericArray = 7;
btBoxedPrimitiveTypeValue = 8;
btObjectReference = 9;
btNullValue = 10;
btEndOfStream = 11;
btAssembly = 12;
btArrayFiller8b = 13;
btArrayFiller32b = 14;
btArrayOfPrimitiveType = 15;
btArrayOfObject = 16;
btArrayOfString = 17;
btMethodCall = 21;
btMethodResponse = 22;
idArrayFiller = $80000000;
{$hints off}
function WinReadByte(stream: TStream): byte;
begin
stream.Read(Result, sizeof(Result));
end;
function WinReadWord(Stream: TStream): BGRAWord;
begin
stream.Read(Result, sizeof(Result));
{$IFNDEF BDS}Result := LEtoN(Result);{$ENDIF}
end;
function WinReadSmallInt(Stream: TStream): smallint;
begin
stream.Read(Result, sizeof(Result));
{$IFNDEF BDS}Result := LEtoN(Result);{$ENDIF}
end;
function WinReadLongint(Stream: TStream): BGRALongInt;
begin
stream.Read(Result, sizeof(Result));
{$IFNDEF BDS}Result := LEtoN(Result);{$ENDIF}
end;
function WinReadLongword(Stream: TStream): BGRALongWord;
begin
stream.Read(Result, sizeof(Result));
{$IFNDEF BDS}Result := LEtoN(Result);{$ENDIF}
end;
function WinReadInt64(Stream: TStream): BGRAInt64;
begin
stream.Read(Result, sizeof(Result));
{$IFNDEF BDS}Result := LEtoN(Result);{$ENDIF}
end;
function WinReadQWord(Stream: TStream): BGRAQWord;
begin
stream.Read(Result, sizeof(Result));
{$IFNDEF BDS}Result := LEtoN(Result);{$ENDIF}
end;
{$hints on}
function GetFieldTypeSize(const fieldType: TFieldType): BGRALongWord;
begin
case fieldType.category of
ftPrimitiveType:
case fieldType.primitiveType of
ptBoolean, ptByte,ptSByte: result := 1;
ptChar,ptString, ptDecimal: Result := sizeof(string);
ptsingle: result := sizeof(single);
ptDouble: result := sizeof(double);
ptInt16,ptUInt16: result := 2;
ptInt32,ptUInt32: result := 4;
ptInt64,ptUInt64,ptDateTime: result := 8;
else
raise Exception.Create('Unknown primitive type (' + IntToStr(
byte(fieldType.primitiveType)) + ')');
end;
ftString, ftObjectType, ftRuntimeType, ftGenericType, ftArrayOfObject,
ftArrayOfString, ftArrayOfPrimitiveType: result := 4;
else
raise Exception.Create('Unknown field type (' + IntToStr(
byte(fieldType.category)) + ')');
end;
end;
function IsDotNetTypeStoredAsString(const fieldType: TFieldType): boolean;
begin
result := (fieldType.category = ftPrimitiveType) and
(fieldType.primitiveType in [ptChar,ptString,ptDecimal]);
end;
function DotNetValueToString(var value; const fieldType: TFieldType): string;
var
tempByte: byte;
value2bytes: record
case byte of
2: (tempWord: BGRAWord);
3: (tempInt16: smallint);
end;
value4bytes: record
case byte of
1: (tempsingle: single);
2: (tempLongWord: BGRALongWord);
3: (tempLongInt: BGRALongInt);
end;
value8bytes: record
case byte of
1: (tempDouble: double);
2: (tempInt64: BGRAInt64);
{$IFDEF OBJ}
2: (tempUInt64: BGRAQWord);
{$ELSE}
3: (tempUInt64: BGRAQWord);
{$ENDIF}
end;
tempIdObject: BGRALongWord;
begin
if IsDotNetTypeStoredAsString(fieldType) then
begin
Result := pstring(@value)^;
exit;
end;
case fieldType.category of
ftPrimitiveType: case fieldType.primitiveType of
ptBoolean:
begin
{$hints off}
move(value,tempByte,sizeof(tempByte));
{$hints on}
if tempByte = 0 then
Result := 'False'
else
if tempByte = 1 then
Result := 'True'
else
raise Exception.Create('Invalid boolean value (' +
IntToStr(tempByte) + ')');
end;
ptByte: Result := inttostr(pbyte(@value)^);
ptSByte: Result := inttostr(pshortint(@value)^);
ptInt16,ptUInt16:
begin
{$hints off}
move(value, value2bytes.tempWord,sizeof(BGRAWord));
{$hints on}
{$IFNDEF BDS}value2bytes.tempWord := LEtoN(value2bytes.tempWord);{$ENDIF}
if fieldType.primitiveType = ptInt16 then
Result := IntToStr(value2bytes.tempInt16)
else
Result := IntToStr(value2bytes.tempWord);
end;
ptInt32,ptUInt32,ptsingle:
begin
{$hints off}
move(value, value4bytes.tempLongWord,sizeof(BGRALongWord));
{$hints on}
{$IFNDEF BDS}value4bytes.tempLongWord := LEtoN(value4bytes.tempLongWord);{$ENDIF}
if fieldType.primitiveType = ptInt32 then
Result := IntToStr(value4bytes.tempLongInt)
else if fieldType.primitiveType = ptUInt32 then
Result := IntToStr(value4bytes.tempLongWord)
else
result := FloatToStr(value4bytes.tempsingle);
end;
ptInt64,ptUInt64,ptDouble,ptDateTime:
begin
{$hints off}
move(value, value8bytes.tempUInt64,8);
{$hints on}
{$IFNDEF BDS}value8bytes.tempUInt64 := LEtoN(value8bytes.tempUInt64);{$ENDIF}
if fieldType.primitiveType = ptInt64 then
Result := IntToStr(value8bytes.tempInt64)
else if fieldType.primitiveType = ptUInt64 then
Result := IntToStr(value8bytes.tempUInt64)
else if fieldType.primitiveType = ptDouble then
result := FloatToStr(value8bytes.tempDouble)
else
Result := DateTimeToStr(
(value8bytes.tempUInt64 and $7FFFFFFFFFFFFFFF - 599264352000000000) / 864000000000);
end;
else
raise Exception.Create('Unknown primitive type (' + IntToStr(
byte(fieldType.primitiveType)) + ')');
end;
ftString, ftObjectType, ftRuntimeType, ftGenericType, ftArrayOfObject,
ftArrayOfString, ftArrayOfPrimitiveType:
begin
{$hints off}
move(value,tempIdObject,sizeof(tempIdObject));
{$hints on}
result := '#' + IntToStr(tempIdObject);
end;
else
raise Exception.Create('Unknown field type (' + IntToStr(
byte(fieldType.category)) + ')');
end;
end;
function PrimitiveTypeName(pt: TPrimitiveType): string;
begin
case pt of
ptBoolean: Result := 'Boolean';
ptByte: Result := 'Byte';
ptChar: Result := 'Char';
ptDecimal: Result := 'Decimal';
ptDouble: Result := 'Double';
ptInt16: Result := 'Int16';
ptInt32: Result := 'Int32';
ptInt64: Result := 'BGRAInt64';
ptSByte: Result := 'SByte';
ptsingle: Result := 'single';
ptDateTime: Result := 'DateTime';
ptUInt16: Result := 'UInt16';
ptUInt32: Result := 'UInt32';
ptUInt64: Result := 'BGRAUInt64';
ptString: Result := 'String';
else
raise Exception.Create('Unknown primitive type (' + IntToStr(integer(pt)) + ')');
end;
end;
Function DotNetTypeToString(ft: TFieldType): string;
begin
if ft.category = ftPrimitiveType then
result := PrimitiveTypeName(ft.primitiveType)
else
case ft.category of
ftString: result := 'String';
ftObjectType: result := 'Object';
ftRuntimeType: result := 'RuntimeType';
ftGenericType: result := 'GenericType';
ftArrayOfObject: result := 'Object[]';
ftArrayOfString: result := 'String[]';
ftArrayOfPrimitiveType: result := 'PrimitiveType[]';
else
raise Exception.Create('Unknown field type (' + IntToStr(
byte(ft.category)) + ')');
end;
end;
{ TCustomSerializedObject }
function TCustomSerializedObject.GetFieldAsString(Name: string): string;
begin
result := GetFieldAsString(GetFieldIndex(Name));
end;
constructor TCustomSerializedObject.Create(container: TDotNetDeserialization);
begin
FContainer := container;
refCount := 0;
end;
function TCustomSerializedObject.GetFieldIndex(Name: string): integer;
var
i: integer;
fn: string;
begin
if FieldCount = 0 then
begin
result := -1;
exit;
end;
//case sensitive
for i := 0 to FieldCount-1 do
if FieldName[i] = Name then
begin
Result := i;
exit;
end;
//case insensitive
for i := 0 to FieldCount-1 do
if compareText(FieldName[i], Name) = 0 then
begin
Result := i;
exit;
end;
//case sensitive inner member
for i := 0 to FieldCount-1 do
begin
fn := FieldName[i];
if (length(Name) < length(fn)) and
(copy(fn, length(fn) - length(Name),
length(Name) + 1) = '+' + Name) then
begin
Result := i;
exit;
end;
end;
//case insensitive inner member
for i := 0 to FieldCount-1 do
begin
fn := FieldName[i];
if (length(Name) < length(fn)) and
(compareText(copy(fn, length(fn) -
length(Name), length(Name) + 1), '+' + Name) = 0) then
begin
Result := i;
exit;
end;
end;
Result := -1;
end;
{ TSerializedClass }
function TSerializedClass.GetFieldAsString(Index: BGRALongWord): string;
begin
result := fields[Index].Value;
end;
function TSerializedClass.GetFieldCount: BGRALongWord;
begin
Result:= length(fields);
end;
function TSerializedClass.GetFieldName(Index: BGRALongWord): string;
begin
result := fields[Index].Name;
end;
function TSerializedClass.GetFieldTypeAsString(Index: BGRALongWord): string;
begin
result := fields[Index].valueType;
end;
function TSerializedClass.IsReferenceType(index: BGRALongWord): boolean;
begin
Result:= FContainer.objectTypes[numType].fieldTypes[index].category <> ftPrimitiveType;
end;
function TSerializedClass.GetTypeAsString: string;
begin
Result:= FContainer.objectTypes[numType].ClassName;
end;
{ TSerializedArray }
procedure TSerializedArray.InitData;
begin
FItemSize := GetFieldTypeSize(itemType);
getmem(data, itemSize*nbItems);
fillchar(data^, itemSize*nbItems, 0);
end;
function TSerializedArray.GetItemPtr(Index: BGRALongWord): pointer;
begin
if index >= nbItems then
raise exception.Create('Index out of bounds');
result := pointer(pbyte(data)+Index*itemsize);
end;
function TSerializedArray.GetFieldAsString(Index: BGRALongWord): string;
begin
if data = nil then
result := ''
else
result := DotNetValueToString(ItemPtr[index]^, itemType);
end;
function TSerializedArray.GetFieldCount: BGRALongWord;
begin
Result:= nbItems;
end;
function TSerializedArray.GetFieldName(Index: BGRALongWord): string;
var
r: BGRALongWord;
begin
result := '[';
for r := 1 to length(dimensions) do
begin
if r <> 1 then result:=result+',';
result := result +inttostr(index mod dimensions[r-1]);
index := index div dimensions[r-1];
end;
result := result +']';
end;
{$hints off}
function TSerializedArray.GetFieldTypeAsString(Index: BGRALongWord): string;
begin
Result:= DotNetTypeToString(itemType);
end;
{$hints on}
{$hints off}
function TSerializedArray.IsReferenceType(index: BGRALongWord): boolean;
begin
Result:= itemType.category <> ftPrimitiveType;
end;
{$hints on}
function TSerializedArray.GetTypeAsString: string;
var
i: Integer;
begin
Result:= DotNetTypeToString(itemType)+'[';
for i := 2 to length(dimensions) do
result := result +',';
result := result +']';
end;
constructor TSerializedArray.Create(AContainer: TDotNetDeserialization; AItemType: TFieldType; ALength: BGRALongWord);
begin
inherited Create(AContainer);
setlength(dimensions,1);
dimensions[0] := ALength;
nbItems := ALength;
FArrayType := gatSingleDimension;
itemType := AItemType;
InitData;
end;
constructor TSerializedArray.Create(AContainer: TDotNetDeserialization; AArrayType: TGenericArrayType; AItemType: TFieldType;
ADimensions: arrayOfLongword);
var n: BGRALongWord;
begin
inherited Create(AContainer);
setlength(dimensions, length(ADimensions));
nbItems := 1;
if length(ADimensions) <> 0 then
for n := 0 to length(ADimensions)-1 do
begin
dimensions[n] := ADimensions[n];
nbItems := nbItems *ADimensions[n];
end;
FArrayType := AArrayType;
itemType := AItemType;
InitData;
end;
destructor TSerializedArray.Destroy;
var ps: PString;
n: BGRALongWord;
begin
if IsDotNetTypeStoredAsString(itemType) and (nbItems <> 0) then
begin
ps := PString(data);
for n := 1 to nbItems do
begin
ps^ := '';
inc(ps);
end;
end;
freemem(data);
inherited Destroy;
end;
{ TSerializedValue }
function TSerializedValue.GetIsReferenceType: boolean;
begin
result := inherited IsReferenceType(0);
end;
function TSerializedValue.GetValueAsString: string;
begin
result := GetFieldAsString(0);
end;
function TSerializedValue.GetTypeAsString: string;
begin
Result:= GetFieldTypeAsString(0);
end;
constructor TSerializedValue.Create(AContainer: TDotNetDeserialization;
AItemType: TFieldType);
begin
inherited Create(AContainer,AItemType,1);
end;
{ TDotNetDeserialization }
function TDotNetDeserialization.FindClass(typeName: string): TSerializedClass;
var obj: TCustomSerializedObject;
begin
obj := FindObject(typeName);
if obj is TSerializedClass then
result := obj as TSerializedClass
else
raise exception.Create('FindClass: found object is not a class');
end;
function TDotNetDeserialization.FindObject(typeName: string): TCustomSerializedObject;
var
i: integer;
comparedType: string;
begin
for i := 0 to high(objects) do
begin
comparedType := objects[i].TypeAsString;
if (comparedType = typeName) or
( (length(typeName) < length(comparedType) ) and
(copy(comparedType, length(comparedType) - length(typeName),
length(typeName) + 1) = '.' + typeName) ) then
begin
Result := objects[i];
exit;
end;
end;
Result := nil;
end;
function TDotNetDeserialization.GetSimpleField(obj: TCustomSerializedObject;
Name: string): string;
var
i,idxSlash: integer;
tempSub: TCustomSerializedObject;
begin
i := obj.GetFieldIndex(Name);
if i = -1 then
begin
idxSlash := pos('\',name);
if idxSlash <> 0 then
begin
tempSub := GetObjectField(obj,copy(name,1,idxSlash-1));
if tempSub <> nil then
begin
result := GetSimpleField(tempSub,copy(name,idxSlash+1,length(name)-idxSlash));
exit;
end;
end;
Result := ''
end
else
begin
if IsBoxedValue(obj, i) then
Result := GetBoxedValue(obj, i)
else
Result := obj.FieldAsString[i];
end;
end;
function TDotNetDeserialization.GetObjectField(obj: TCustomSerializedObject;
Name: string): TCustomSerializedObject;
var
i: integer;
idxSlash: BGRALongInt;
tempSub: TCustomSerializedObject;
begin
i := obj.GetFieldIndex(Name);
if i = -1 then
begin
idxSlash := pos('\',name);
if idxSlash <> 0 then
begin
tempSub := GetObjectField(obj,copy(name,1,idxSlash-1));
if tempSub <> nil then
begin
result := GetObjectField(tempSub,copy(name,idxSlash+1,length(name)-idxSlash));
exit;
end;
end;
Result := nil
end
else
begin
if not obj.IsReferenceType(i) then
raise Exception.Create('GetObjectField: Not a reference type');
Result := GetObject(obj.FieldAsString[i]);
end;
end;
function TDotNetDeserialization.GetObjectField(obj: TCustomSerializedObject;
index: integer): TCustomSerializedObject;
begin
if not obj.IsReferenceType(index) then
raise Exception.Create('GetObjectField: Not a reference type');
Result := GetObject(obj.FieldAsString[index]);
end;
function TDotNetDeserialization.GetObject(id: string): TCustomSerializedObject;
var
idObj: BGRALongWord;
begin
if copy(id, 1, 1) = '#' then
Delete(id, 1, 1);
idObj := StrToInt64(id);
Result := GetObject(idObj);
end;
function TDotNetDeserialization.GetObject(id: BGRALongWord): TCustomSerializedObject;
var
i: integer;
begin
for i := 0 to high(objects) do
if objects[i].idObject = id then
begin
Result := objects[i];
exit;
end;
Result := nil;
end;
function TDotNetDeserialization.IsBoxedValue(obj: TCustomSerializedObject;
index: integer): boolean;
var
subObj: TCustomSerializedObject;
begin
if not obj.IsReferenceType(index) then
begin
Result := False;
exit;
end;
subObj := GetObject(obj.FieldAsString[index]);
if subObj = nil then //suppose Nothing is a boxed value
begin
Result := True;
exit;
end;
Result := subObj is TSerializedValue;
end;
function TDotNetDeserialization.GetBoxedValue(obj: TCustomSerializedObject;
index: integer): string;
var
subObj: TCustomSerializedObject;
begin
if not obj.IsReferenceType(index) then
raise Exception.Create('GetBoxedValue: Not a reference type');
subObj := GetObject(obj.FieldAsString[index]);
if subObj = nil then
begin
Result := ''; //empty value
exit;
end;
if (subObj is TSerializedValue) and not (subObj as TSerializedValue).IsReferenceType then
Result := (subObj as TSerializedValue).ValueAsString
else
raise Exception.Create('GetBoxedValue: Not a primitive type');
end;
procedure TDotNetDeserialization.LoadFromStream(Stream: TStream);
var
header: packed record
blockId: byte;
value1, value2, value3, value4: BGRALongInt;
end;
curStreamPosition, prevStreamPosition: BGRAInt64;
begin
{$hints off}
if Stream.Read(header, sizeof(header)) <> sizeof(header) then
raise Exception.Create('Invalid header size');
if (header.blockId <> 0) or (header.value1 <> 1) or (header.value2 <> -1) or
(header.value3 <> 1) or (header.value4 <> 0) then
raise Exception.Create('Invalid header format');
{$hints on}
EndOfStream := False;
curStreamPosition := Stream.Position;
try
while (Stream.Position < Stream.Size) and not EndOfStream do
begin
prevStreamPosition := curStreamPosition;
curStreamPosition := Stream.Position;
LoadNextFromStream(Stream);
end;
except
on ex: Exception do
raise Exception.Create('Error while loading serialized data at position ' +
IntToStr(stream.Position) + ' (block starting at ' +
IntToStr(curStreamPosition) + ', previous block at ' +
IntToStr(prevStreamPosition) + '). ' + ex.message);
end;
end;
procedure TDotNetDeserialization.LoadFromFile(filename: string);
var
stream: TFileStream;
begin
stream := TFileStream.Create(filename, fmOpenRead);
try
LoadFromStream(stream);
finally
stream.Free;
end;
end;
procedure TDotNetDeserialization.LoadFromFileUTF8(filenameUTF8: string);
var
stream: TFileStreamUTF8;
begin
stream := TFileStreamUTF8.Create(filenameUTF8, fmOpenRead);
try
LoadFromStream(stream);
finally
stream.Free;
end;
end;
function TDotNetDeserialization.ToString: string;
function ObjectToString(num: integer; expectedType: string;
tab: string; main: boolean): string;
var
j, k: integer;
subId: BGRALongWord;
subNum: integer;
objType, subExpectedType: string;
fieldTypeStr: string;
begin
Result := '';
if (num < 0) or (num > high(objects)) then
raise Exception.Create('Index out of bounds');
with objects[num] do //here array is not changed so it won't move
begin
if inToString then
begin
if main then
Result := ''
else
Result := '#' + IntToStr(idObject) + LineEnding;
exit;
end;
inToString := True;
objType := TypeAsString;
if main then
begin
Result := result +tab + 'Object';
Result := result +' #' + IntToStr(idObject);
if (objType = '') or (objType = expectedType) then
Result := result +' = '
else
Result := result +' As ' + objType + ' = ';
end
else
begin
if (objType = '') or (objType = expectedType) then
Result := ''
else
Result := '(' + objType + ') ';
if (idObject < idArrayFiller) and (refCount > 0) then
Result := result +'#' + IntToStr(idObject) + ' = ';
end;
if (length(objType) > 2) and (copy(objType, length(objType) - 1, 2) = '[]') then
subExpectedType := copy(objType, 1, length(objType) - 2)
else
subExpectedType := '';
if not main and (objects[num] is TSerializedValue) then
begin
Result := result +(objects[num] as TSerializedValue).ValueAsString + LineEnding;
end
else
if (FieldCount = 0) then
begin
Result := result +'{}' + LineEnding;
end
else
begin
Result := result +'{' + LineEnding;
for j := 0 to FieldCount-1 do
begin
Result := result +tab + ' ' + FieldName[j];
fieldTypeStr := FieldTypeAsString[j];
if (fieldTypeStr <> '') and (fieldTypeStr <> subExpectedType) and
not ((subExpectedType = '') and ((fieldTypeStr = 'Int32') or
(fieldTypeStr = 'Boolean') or (fieldTypeStr = 'Double'))) then
Result := result +' As ' + fieldTypeStr;
Result := result +' = ';
if not IsReferenceType(j) then
Result := result + FieldAsString[j] + lineending
else
begin
try
subId := StrToInt64(copy(fieldAsString[j], 2, length(fieldAsString[j]) - 1));
if subId = 0 then result := result + 'null'+LineEnding else
begin
begin