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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 2008 by Giulio Bernardi
Base classes for resource handling
See the file COPYING.FPC, 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.
**********************************************************************}
unit resource;
{$MODE OBJFPC} {$H+}
interface
uses
Classes, Sysutils;
const
RT_CURSOR = 1; //Hardware-dependent cursor resource.
RT_BITMAP = 2; //Bitmap resource.
RT_ICON = 3; //Hardware-dependent icon resource.
RT_MENU = 4; //Menu resource.
RT_DIALOG = 5; //Dialog box.
RT_STRING = 6; //String-table entry.
RT_FONTDIR = 7; //Font directory resource.
RT_FONT = 8; //Font resource.
RT_ACCELERATOR = 9; //Accelerator table.
RT_RCDATA = 10; //Application-defined resource (raw data).
RT_MESSAGETABLE = 11; //Message-table entry.
RT_GROUP_CURSOR = 12; //Hardware-independent cursor resource.
RT_GROUP_ICON = 14; //Hardware-independent icon resource.
RT_VERSION = 16; //Version resource.
RT_DLGINCLUDE = 17; //Never present in compiled form
RT_PLUGPLAY = 19; //Plug and Play resource.
RT_VXD = 20; //VXD.
RT_ANICURSOR = 21; //Animated cursor.
RT_ANIICON = 22; //Animated icon.
RT_HTML = 23; //HTML.
RT_MANIFEST = 24; //Microsoft Windows XP: Side-by-Side Assembly XML Manifest.
CREATEPROCESS_MANIFEST_RESOURCE_ID = 1;
ISOLATIONAWARE_MANIFEST_RESOURCE_ID = 2;
ISOLATIONAWARE_NOSTATICIMPORT_MANIFEST_RESOURCE_ID = 3;
MINIMUM_RESERVED_MANIFEST_RESOURCE_ID = 1; //inclusive
MAXIMUM_RESERVED_MANIFEST_RESOURCE_ID = 16; //inclusive
const
MF_MOVEABLE = $0010;
MF_PURE = $0020;
MF_PRELOAD = $0040;
MF_DISCARDABLE = $1000;
resourcestring
SReaderNotFoundExt = 'Cannot find resource reader for extension ''%s''';
SReaderNotFoundProbe = 'Cannot find a resource reader: unknown format.';
SWriterNotFoundExt = 'Cannot find resource writer for extension ''%s''';
SDescChangeNotAllowed = 'Cannot modify %s resource description';
SLangIDChangeNotAllowed = 'Cannot modify %s resource language ID';
SResDuplicate = 'Duplicate resource: Type = %s, Name = %s, Lang ID = %.4x';
SResourceNotFound = 'Cannot find resource: Type = %s, Name = %s';
SResourceNotFoundLang = 'Cannot find resource: Type = %s, Name = %s, Lang ID = %.4x';
type
TLangID = word;
TResName = string;
TResID = LongWord;
TDescType = (dtName, dtID);
type
EResourceException = class(Exception);
EResourceDescTypeException = class(EResourceException);
EResourceDescChangeNotAllowedException = class(EResourceException);
EResourceLangIDChangeNotAllowedException = class(EResourceException);
EResourceDuplicateException = class(EResourceException);
EResourceNotFoundException = class(EResourceException);
ENoMoreFreeIDsException = class(EResourceException);
EResourceReaderException = class(EResourceException);
EResourceReaderNotFoundException = class(EResourceReaderException);
EResourceReaderWrongFormatException = class(EResourceReaderException);
EResourceReaderUnexpectedEndOfStreamException = class (EResourceReaderException);
EResourceWriterException = class(EResourceException);
EResourceWriterNotFoundException = class(EResourceWriterException);
type
TAbstractResource = class;
{ TResourceDesc }
TResourceDesc = class
private
fName : TResName;
fID : TResID;
fDescType : TDescType;
fOwner : TAbstractresource;
function GetID : TResID;
function GetName : TResName;
procedure SetID(const aID : TResID);
procedure SetName(const aName : TResName);
procedure CanChangeType(newType : TDescType);
procedure CanChangeValue;
protected
procedure SetOwner(aOwner : TAbstractResource);
public
constructor Create; overload;
constructor Create(const aID : TResID); overload;
constructor Create(const aName : TResName); overload;
procedure Assign(aResourceDesc : TResourceDesc);
function Equals(aResDesc : TResourceDesc) : boolean;
property Name : TResName read GetName write SetName;
property ID : TResID read GetID write SetID;
property DescType : TDescType read fDescType;
end;
TResources = class;
{ TAbstractResource }
TAbstractResource = class
private
fLangId : TLangID;
fDataSize : longword;
fHeaderSize : longword;
fDataVersion : longword;
fMemoryFlags : word;
fVersion : longword;
fCharacteristics : longword;
fDataOffset : longword;
fCodePage : longword;
fRawData : TStream;
fOwnerList : TResources;
fOwner : TAbstractResource;
function GetRawData : TStream;
function GetCacheData : boolean;
procedure SetCacheData(const aValue : boolean);
function GetDataSize : longword;
procedure SetLangID(aLangID : TLangID);
protected
procedure SetDescOwner(aDesc : TResourceDesc);
procedure SetOwnerList(aResources : TResources); virtual;
procedure SetChildOwner(aChild : TAbstractResource);
function GetType : TResourceDesc; virtual; abstract;
function GetName : TResourceDesc; virtual; abstract;
function ChangeDescTypeAllowed(aDesc : TResourceDesc) : boolean; virtual; abstract;
function ChangeDescValueAllowed(aDesc : TResourceDesc) : boolean; virtual; abstract;
procedure NotifyResourcesLoaded; virtual; abstract;
constructor Create; virtual; overload;
public
constructor Create(aType,aName : TResourceDesc); virtual; abstract; overload;
destructor Destroy; override;
function CompareContents(aResource: TAbstractResource): boolean; virtual;
procedure UpdateRawData; virtual; abstract;
procedure SetCustomRawDataStream(aStream : TStream);
property _Type : TResourceDesc read GetType;
property Name : TResourceDesc read GetName;
property LangID : TLangID read fLangID write SetLangID;
property DataSize : longword read GetDataSize;
property HeaderSize : longword read fHeaderSize;
property DataVersion : longword read fDataVersion write fDataVersion;
property MemoryFlags : word read fMemoryFlags write fMemoryFlags;
property Version : longword read fVersion write fVersion;
property Characteristics : longword read fCharacteristics write fCharacteristics;
property DataOffset : longword read fDataOffset;
property CodePage : longword read fCodePage write fCodePage;
property RawData : TStream read GetRawData;
property CacheData : boolean read GetCacheData write SetCacheData;
property OwnerList : TResources read fOwnerList;
property Owner : TAbstractResource read fOwner;
end;
TResourceClass = class of TAbstractResource;
{ TGenericResource }
TGenericResource = class(TAbstractResource)
private
fType : TResourceDesc;
fName : TResourceDesc;
protected
function GetType : TResourceDesc; override;
function GetName : TResourceDesc; override;
function ChangeDescTypeAllowed(aDesc : TResourceDesc) : boolean; override;
function ChangeDescValueAllowed(aDesc : TResourceDesc) : boolean; override;
procedure NotifyResourcesLoaded; override;
public
constructor Create(aType,aName : TResourceDesc); override;
destructor Destroy; override;
procedure UpdateRawData; override;
end;
type
TAbstractResourceReader = class;
TAbstractResourceWriter = class;
TResourceReaderClass = class of TAbstractResourceReader;
TResourceWriterClass = class of TAbstractResourceWriter;
{ TResources }
TResources = class
private
fList : TFPList;
fTree : TObject;
dummyType : TResourceDesc;
dummyName : TResourceDesc;
fCacheData : boolean;
fMoveFromCount : integer;
fRemovedCount : integer;
function GetItem(index : integer) : TAbstractResource;
function GetCount : longword;
procedure SetCacheData(const aValue : boolean);
procedure NotifyLoaded;
// protected
private
fTempRStream : TStream;
class procedure InitReaderList;
class procedure InitWriterList;
class procedure DisposeStreamerList(aList : TFPList);
class procedure DisposeReaderList;
class procedure DisposeWriterList;
class function FindWriterClass(aExtension : string) : TResourceWriterClass;
class procedure RegisterStreamer(aList : TFPList; aExtension : string; aClass : TClass);
procedure SendUpdateRawData;
procedure InternalRemove(aResource: TAbstractResource);
procedure InternalRemove(aIndex : integer);
procedure QuietRemove(aResource : TAbstractResource; aIndex : integer; aIndexValid : boolean);
procedure InternalClear;
procedure InternalAdd(aResource : TAbstractResource; prevIdx : integer; prevIdxValid : boolean);
procedure AddNoTree(aResource : TAbstractResource);
function InternalFind(aType, aName : TResourceDesc; const aLangID : TLangID) : TAbstractResource; overload;
function InternalFind(aType, aName : TResourceDesc) : TAbstractResource; overload;
procedure BeginMoveFrom;
procedure EndMoveFrom;
public
constructor Create;
destructor Destroy; override;
procedure Add(aResource : TAbstractResource);
function AddAutoID(aResource : TAbstractResource) : TResID;
procedure Clear;
function Find(aType, aName : TResourceDesc; const aLangID : TLangID) : TAbstractResource; overload;
function Find(aType, aName : TResourceDesc) : TAbstractResource; overload;
function Find(const aType : TResName; const aName : TResName; const aLangID : TLangID) : TAbstractResource; overload;
function Find(const aType : TResName; const aName : TResID; const aLangID : TLangID) : TAbstractResource; overload;
function Find(const aType : TResID; const aName : TResName; const aLangID : TLangID) : TAbstractResource; overload;
function Find(const aType : TResID; const aName : TResID; const aLangID : TLangID) : TAbstractResource; overload;
function Find(const aType : TResName; const aName : TResName) : TAbstractResource; overload;
function Find(const aType : TResName; const aName : TResID) : TAbstractResource; overload;
function Find(const aType : TResID; const aName : TResName) : TAbstractResource; overload;
function Find(const aType : TResID; const aName : TResID) : TAbstractResource; overload;
class function FindReader(aStream: TStream; aExtension: string) : TAbstractResourceReader;
class function FindReader(aStream : TStream) : TAbstractResourceReader;
procedure MoveFrom(aResources : TResources);
function Remove(aType,aName : TResourceDesc; const aLangID : TLangID) : TAbstractResource; overload;
function Remove(aType,aName : TResourceDesc) : TAbstractResource; overload;
function Remove(aResource : TAbstractResource) : TAbstractResource; overload;
function Remove(aIndex : integer) : TAbstractResource; overload;
procedure LoadFromStream(aStream : TStream); overload;
procedure LoadFromStream(aStream : TStream; aReader : TAbstractResourceReader); overload;
procedure LoadFromFile(aFileName : string); overload;
procedure LoadFromFile(aFileName : string; aReader : TAbstractResourceReader); overload;
class procedure RegisterReader(const aExtension : string; aClass : TResourceReaderClass);
class procedure RegisterWriter(const aExtension : string; aClass : TResourceWriterClass);
procedure WriteToStream(aStream : TStream; aWriter : TAbstractResourceWriter);
procedure WriteToFile(aFileName : string); overload;
procedure WriteToFile(aFileName : string; aWriter : TAbstractResourceWriter); overload;
property Count : longword read GetCount;
property Items[Index : integer] : TAbstractResource read GetItem; default;
property CacheData : boolean read fCacheData write SetCacheData;
end;
{ TAbstractResourceReader }
TAbstractResourceReader = class
private
protected
procedure SetDataSize(aResource : TAbstractResource; aValue : longword);
procedure SetHeaderSize(aResource : TAbstractResource; aValue : longword);
procedure SetDataOffset(aResource : TAbstractResource; aValue : longword);
procedure SetRawData(aResource : TAbstractResource; aStream : TStream);
procedure CallSubReaderLoad(aReader: TAbstractResourceReader; aResources : TResources; aStream : TStream);
procedure AddNoTree(aResources : TResources; aResource: TAbstractResource);
function GetTree(aResources : TResources) : TObject;
function GetExtensions : string; virtual; abstract;
function GetDescription : string; virtual; abstract;
procedure Load(aResources : TResources; aStream : TStream); virtual; abstract;
function CheckMagic(aStream : TStream) : boolean; virtual; abstract;
public
constructor Create; virtual; abstract;
property Extensions : string read GetExtensions;
property Description : string read GetDescription;
end;
{ TAbstractResourceWriter }
TAbstractResourceWriter = class
private
protected
function GetTree(aResources : TResources) : TObject;
function GetExtensions : string; virtual; abstract;
function GetDescription : string; virtual; abstract;
procedure Write(aResources : TResources; aStream : TStream); virtual; abstract;
public
constructor Create; virtual; abstract;
property Extensions : string read GetExtensions;
property Description : string read GetDescription;
end;
implementation
uses resdatastream, resourcetree, resmerger;
type
PRegisteredStreamerEntry = ^TRegisteredStreamerEntry;
TRegisteredStreamerEntry = record
ext : shortstring;
_class : TClass;
next : PRegisteredStreamerEntry;
end;
var RegisteredReaders : TFPList = nil;
RegisteredWriters : TFPList = nil;
{ TResourceDesc }
function TResourceDesc.GetID: TResID;
begin
if fDescType<>dtId then
raise EResourceDescTypeException.Create('');
Result:=fId;
end;
function TResourceDesc.GetName: TResName;
begin
if fDescType = dtName then
Result:=fName
else Result:=IntToStr(fId);
end;
procedure TResourceDesc.CanChangeType(newType : TDescType);
begin
if fOwner=nil then exit;
if newType=fDescType then exit;
if (fOwner.OwnerList<>nil) or (not fOwner.ChangeDescTypeAllowed(self)) then
raise EResourceDescChangeNotAllowedException.CreateFmt(SDescChangeNotAllowed,[Name]);
end;
procedure TResourceDesc.CanChangeValue;
begin
if fOwner=nil then exit;
if (fOwner.OwnerList<>nil) or (not fOwner.ChangeDescValueAllowed(self)) then
raise EResourceDescChangeNotAllowedException.CreateFmt(SDescChangeNotAllowed,[Name]);
end;
procedure TResourceDesc.SetOwner(aOwner: TAbstractResource);
begin
fOwner:=aOwner;
end;
procedure TResourceDesc.SetID(const aID: TResID);
begin
CanChangeType(dtID);
CanChangeValue;
fDescType:=dtID;
fId:=aID;
end;
procedure TResourceDesc.SetName(const aName: TResName);
begin
CanChangeType(dtName);
CanChangeValue;
fDescType:=dtName;
fName:=UpperCase(aName);
end;
constructor TResourceDesc.Create;
begin
fName:='';
fID:=0;
fDescType:=dtName;
fOwner:=nil;
end;
constructor TResourceDesc.Create(const aID: TResID);
begin
Create;
SetID(aID);
end;
constructor TResourceDesc.Create(const aName: TResName);
begin
Create;
SetName(aName);
end;
procedure TResourceDesc.Assign(aResourceDesc: TResourceDesc);
begin
CanChangeType(aResourceDesc.fDescType);
CanChangeValue;
fDescType:=aResourceDesc.fDescType;
case fDescType of
dtID : begin fID:=aResourceDesc.fID; fName:=''; end;
dtName : begin fName:=aResourceDesc.fName; fID:=0; end;
end;
end;
function TResourceDesc.Equals(aResDesc: TResourceDesc): boolean;
begin
Result:=aResDesc.DescType=fDescType;
if not Result then exit;
case fDescType of
dtName : Result:=aResDesc.Name=fName;
dtID : Result:=aResDesc.ID=fID;
end;
end;
{ TAbstractResource }
function TAbstractResource.GetRawData: TStream;
begin
if fRawData = nil then
fRawData:=TResourceDataStream.Create(nil,self,DataSize,TCachedResourceDataStream);
Result:=fRawData;
end;
function TAbstractResource.GetCacheData: boolean;
begin
Result:=TResourceDataStream(RawData).Cached;
end;
procedure TAbstractResource.SetCacheData(const aValue: boolean);
begin
TResourceDataStream(RawData).Cached:=aValue;
end;
function TAbstractResource.GetDataSize: longword;
begin
if fRawData=nil then Result:=fDataSize
else Result:=fRawData.Size;
end;
procedure TAbstractResource.SetLangID(aLangID: TLangID);
begin
if OwnerList<>nil then
raise EResourceLangIDChangeNotAllowedException.CreateFmt(SLangIDChangeNotAllowed,[Name]);
fLangId:=aLangID;
end;
procedure TAbstractResource.SetDescOwner(aDesc: TResourceDesc);
begin
aDesc.SetOwner(self);
end;
procedure TAbstractResource.SetOwnerList(aResources: TResources);
begin
fOwnerList:=aResources;
end;
procedure TAbstractResource.SetChildOwner(aChild: TAbstractResource);
begin
aChild.fOwner:=self;
end;
constructor TAbstractResource.Create;
begin
fLangID:=0;
fDataSize:=0;
fHeaderSize:=0;
fDataVersion:=0;
fMemoryFlags:=MF_MOVEABLE or MF_DISCARDABLE;
fVersion:=0;
fCharacteristics:=0;
fDataOffset:=0;
fCodePage:=0;
fRawData:=nil;
fOwnerList:=nil;
fOwner:=nil;
end;
destructor TAbstractResource.Destroy;
begin
if fRawData<>nil then fRawData.Free;
end;
function TAbstractResource.CompareContents(aResource: TAbstractResource
): boolean;
begin
Result:=TResourceDataStream(RawData).Compare(aResource.RawData);
end;
procedure TAbstractResource.SetCustomRawDataStream(aStream: TStream);
begin
TResourceDataStream(RawData).SetCustomStream(aStream);
end;
{ TResources }
function TResources.GetItem(index: integer): TAbstractResource;
begin
Result:=TAbstractResource(fList[index]);
end;
function TResources.GetCount: longword;
begin
Result:=fList.Count;
end;
procedure TResources.SetCacheData(const aValue: boolean);
var i : integer;
begin
if aValue=fCacheData then exit;
fCacheData:=aValue;
if fCacheData then exit; //single resources cache data by default
//don't cache data: load everything and free the temporary stream.
for i:=0 to Count-1 do
Items[i].CacheData:=aValue;
if fTempRStream<>nil then FreeAndNil(fTempRStream);
end;
procedure TResources.NotifyLoaded;
var i : integer;
begin
for i:=0 to Count-1 do
Items[i].NotifyResourcesLoaded;
end;
class procedure TResources.InitReaderList;
begin
if RegisteredReaders=nil then
RegisteredReaders:=TFPList.Create;
end;
class procedure TResources.InitWriterList;
begin
if RegisteredWriters=nil then
RegisteredWriters:=TFPList.Create;
end;
class procedure TResources.DisposeStreamerList(aList: TFPList);
var p,p2 : PRegisteredStreamerEntry;
i : integer;
begin
if aList=nil then exit;
for i:=0 to aList.Count-1 do
begin
p:=PRegisteredStreamerEntry(aList[i]);
while p<>nil do
begin
p2:=p^.next;
Freemem(p);
p:=p2;
end;
end;
end;
class procedure TResources.DisposeReaderList;
begin
DisposeStreamerList(RegisteredReaders);
FreeAndNil(RegisteredReaders);
end;
class procedure TResources.DisposeWriterList;
begin
DisposeStreamerList(RegisteredWriters);
FreeAndNil(RegisteredWriters);
end;
class function TResources.FindReader(aStream: TStream; aExtension: string) :
TAbstractResourceReader;
var i : integer;
p : PRegisteredStreamerEntry;
position : int64;
found : boolean;
begin
Result:=nil;
InitReaderList;
position:=aStream.Position;
aExtension:=lowercase(aExtension);
for i:=0 to RegisteredReaders.Count-1 do
begin
p:=PRegisteredStreamerEntry(RegisteredReaders[i]);
if p^.ext=aExtension then //try all readers registered for this extension
begin
while p<>nil do
begin
Result:=TResourceReaderClass(p^._class).Create;
found:=Result.CheckMagic(aStream);
aStream.Position:=position; //rewind
if found then exit;
FreeAndNil(Result);
p:=p^.next;
end;
// There are readers for this extension, but no one seems to be able
// to read the file.
// So, return the first reader, and it will fail later.
p:=PRegisteredStreamerEntry(RegisteredReaders[i]);
Result:=TResourceReaderClass(p^._class).Create;
exit;
end;
end;
raise EResourceReaderNotFoundException.Create(Format(SReaderNotFoundExt,[aExtension]));
end;
class function TResources.FindReader(aStream: TStream
): TAbstractResourceReader;
var i : integer;
p : PRegisteredStreamerEntry;
position : int64;
found : boolean;
begin
Result:=nil;
InitReaderList;
position:=aStream.Position;
for i:=0 to RegisteredReaders.Count-1 do
begin
p:=PRegisteredStreamerEntry(RegisteredReaders[i]);
while p<>nil do
begin
Result:=TResourceReaderClass(p^._class).Create;
found:=Result.CheckMagic(aStream);
aStream.Position:=position; //rewind
if found then exit;
FreeAndNil(Result);
p:=p^.next;
end;
end;
raise EResourceReaderNotFoundException.Create(SReaderNotFoundProbe);
end;
procedure TResources.MoveFrom(aResources: TResources);
var res : TAbstractResource;
i : integer;
begin
aResources.BeginMoveFrom;
try
for i:=0 to aResources.Count-1 do
begin
res:=aResources.Items[i];
if res=nil then continue;
if res.Owner<>nil then //If we are adding an owned resource, add
InternalAdd(res.Owner,0,false) //the owner resource instead (it will take
else //care of adding its sub-resources)
InternalAdd(res,i,true);
end;
finally
aResources.EndMoveFrom;
end;
end;
class function TResources.FindWriterClass(aExtension: string
): TResourceWriterClass;
var i : integer;
p : PRegisteredStreamerEntry;
begin
Result:=nil;
InitWriterList;
aExtension:=lowercase(aExtension);
for i:=0 to RegisteredWriters.Count-1 do
begin
p:=PRegisteredStreamerEntry(RegisteredWriters[i]);
if p^.ext=aExtension then
begin
Result:=TResourceWriterClass(p^._class);
exit;
end;
end;
raise EResourceWriterNotFoundException.Create(Format(SWriterNotFoundExt,[aExtension]));
end;
procedure TResources.InternalAdd(aResource : TAbstractResource; prevIdx :
integer; prevIdxValid : boolean);
var resold : TAbstractResource;
begin
resold:=InternalFind(aResource._Type,aResource.Name,aResource.LangID);
if resold<>nil then
begin
if TResourceMerger.Merge(resold,aResource) then exit;
raise EResourceDuplicateException.CreateFmt(SResDuplicate,[aResource._Type.Name,aResource.Name.Name,aResource.LangID]);
end;
fList.Add(aResource);
TRootResTreeNode(fTree).Add(aResource);
if aResource.OwnerList<>nil then
aResource.OwnerList.QuietRemove(aResource,prevIdx,prevIdxValid);
aResource.SetOwnerList(self);
aResource.CacheData:=fCacheData;
end;
procedure TResources.AddNoTree(aResource: TAbstractResource);
begin
fList.Add(aResource);
aResource.SetOwnerList(self);
aResource.CacheData:=fCacheData;
end;
function TResources.InternalFind(aType, aName: TResourceDesc;
const aLangID: TLangID): TAbstractResource;
begin
Result:=TRootResTreeNode(fTree).Find(aType,aName,aLangID);
end;
function TResources.InternalFind(aType, aName: TResourceDesc
): TAbstractResource;
begin
Result:=TRootResTreeNode(fTree).Find(aType,aName);
end;
procedure TResources.BeginMoveFrom;
begin
inc(fMoveFromCount);
fRemovedCount:=0;
end;
procedure TResources.EndMoveFrom;
begin
dec(fMoveFromCount);
if fMoveFromCount=0 then
if fRemovedCount=fList.Count then //all items removed: clear the list
fList.Clear
else
fList.Pack; //for some reason, not all items were removed. remove only nils
end;
procedure TResources.Add(aResource: TAbstractResource);
begin
InternalAdd(aResource,0,false);
end;
function TResources.AddAutoID(aResource: TAbstractResource): TResID;
var newid : TResID;
begin
newid:=TRootResTreeNode(fTree).FindFreeID(aResource._Type);
//if we reached this point, ENoMoreFreeIDsException hasn't been raised.
if aResource.OwnerList<>nil then aResource.OwnerList.Remove(aResource);
aResource.Name.ID:=newid;
InternalAdd(aResource,0,false);
Result:=newid;
end;
//clear without freeing fTempRStream
procedure TResources.InternalClear;
var i : integer;
begin
TRootResTreeNode(fTree).Clear;
for i:=0 to Count-1 do
TAbstractResource(fList[i]).Free;
fList.Clear;
end;
procedure TResources.Clear;
begin
InternalClear;
if fTempRStream<>nil then FreeAndNil(fTempRStream);
end;
function TResources.Find(aType, aName: TResourceDesc; const aLangID : TLangID):
TAbstractResource;
begin
Result:=TRootResTreeNode(fTree).Find(aType,aName,aLangID);
if Result=nil then
raise EResourceNotFoundException.CreateFmt(SResourceNotFoundLang,[aType.Name,aName.Name,aLangID]);
end;
function TResources.Find(aType, aName: TResourceDesc):
TAbstractResource;
begin
Result:=TRootResTreeNode(fTree).Find(aType,aName);
if Result=nil then
raise EResourceNotFoundException.CreateFmt(SResourceNotFound,[aType.Name,aName.Name]);
end;
function TResources.Find(const aType: TResName; const aName: TResName; const
aLangID : TLangID): TAbstractResource;
begin
dummyType.Name:=aType;
dummyName.Name:=aName;
Result:=Find(dummyType,dummyName,aLangID);
end;
function TResources.Find(const aType: TResName; const aName: TResID; const
aLangID : TLangID ): TAbstractResource;
begin
dummyType.Name:=aType;
dummyName.ID:=aName;
Result:=Find(dummyType,dummyName,aLangID);
end;
function TResources.Find(const aType: TResID; const aName: TResName; const
aLangID : TLangID ): TAbstractResource;
begin
dummyType.ID:=aType;
dummyName.Name:=aName;
Result:=Find(dummyType,dummyName,aLangID);
end;
function TResources.Find(const aType: TResID; const aName: TResID; const
aLangID : TLangID ): TAbstractResource;
begin
dummyType.ID:=aType;
dummyName.ID:=aName;
Result:=Find(dummyType,dummyName,aLangID);
end;
function TResources.Find(const aType: TResName; const aName: TResName
): TAbstractResource;
begin
dummyType.Name:=aType;
dummyName.Name:=aName;
Result:=Find(dummyType,dummyName);
end;
function TResources.Find(const aType: TResName; const aName: TResID
): TAbstractResource;
begin
dummyType.Name:=aType;
dummyName.ID:=aName;
Result:=Find(dummyType,dummyName);
end;
function TResources.Find(const aType: TResID; const aName: TResName
): TAbstractResource;
begin
dummyType.ID:=aType;
dummyName.Name:=aName;
Result:=Find(dummyType,dummyName);
end;
function TResources.Find(const aType: TResID; const aName: TResID
): TAbstractResource;
begin
dummyType.ID:=aType;
dummyName.ID:=aName;
Result:=Find(dummyType,dummyName);
end;
function TResources.Remove(aType,aName : TResourceDesc;
const aLangID : TLangID) : TAbstractResource;
begin
Result:=TRootResTreeNode(fTree).Remove(aType,aName,aLangID);
InternalRemove(Result);
Result.SetOwnerList(nil);
end;
function TResources.Remove(aType,aName : TResourceDesc) : TAbstractResource;
begin
Result:=TRootResTreeNode(fTree).Remove(aType,aName);
InternalRemove(Result);
Result.SetOwnerList(nil);
end;
function TResources.Remove(aResource: TAbstractResource) : TAbstractResource;
begin
InternalRemove(aResource);
Result:=TRootResTreeNode(fTree).Remove(aResource._Type,aResource.Name,aResource.LangID);
Result.SetOwnerList(nil);
end;
function TResources.Remove(aIndex: integer): TAbstractResource;
begin
Result:=Items[aIndex];
InternalRemove(aIndex);
Result:=TRootResTreeNode(fTree).Remove(Result._Type,Result.Name,Result.LangID);
Result.SetOwnerList(nil);
end;
procedure TResources.InternalRemove(aResource: TAbstractResource);
var idx : integer;
begin
if aResource=nil then exit;
idx:=fList.IndexOf(aResource);
if idx=-1 then
raise EResourceNotFoundException.CreateFmt(SResourceNotFoundLang,[
aResource._Type.Name,aResource.Name.Name,aResource.LangID]);
if fMoveFromCount>0 then fList[idx]:=nil
else fList.Delete(idx);
inc(fRemovedCount);
end;
procedure TResources.InternalRemove(aIndex: integer);
begin
if fMoveFromCount>0 then fList[aIndex]:=nil
else fList.Delete(aIndex);
inc(fRemovedCount);
end;
//removes without calling setownerlist
procedure TResources.QuietRemove(aResource : TAbstractResource; aIndex :
integer; aIndexValid : boolean);
begin
if aIndexValid then InternalRemove(aIndex)
else InternalRemove(aResource);
TRootResTreeNode(fTree).Remove(aResource._Type,aResource.Name,aResource.LangID);
end;
procedure TResources.LoadFromStream(aStream: TStream);
var aReader : TAbstractResourceReader;
begin
aReader:=FindReader(aStream);
try
LoadFromStream(aStream,aReader);
finally
aReader.Free;
end;
end;
procedure TResources.LoadFromStream(aStream: TStream;
aReader: TAbstractResourceReader);
begin
InternalClear;
aReader.Load(self,aStream);
NotifyLoaded;
end;
procedure TResources.LoadFromFile(aFileName: string);
var ext : string;
aReader : TAbstractResourceReader;
begin
ext:=ExtractFileExt(aFileName);
if fTempRStream<>nil then FreeAndNil(fTempRStream);
fTempRStream:=TFileStream.Create(aFileName,fmOpenRead or fmShareDenyWrite);
aReader:=FindReader(fTempRStream,ext);
try
LoadFromStream(fTempRStream,aReader);
finally
aReader.Free;
if not fCacheData then FreeAndNil(fTempRStream);
end;
end;
procedure TResources.LoadFromFile(aFileName: string;
aReader: TAbstractResourceReader);
begin
if fTempRStream<>nil then FreeAndNil(fTempRStream);
fTempRStream:=TFileStream.Create(aFileName,fmOpenRead or fmShareDenyWrite);
try
LoadFromStream(fTempRStream,aReader);
finally
if not fCacheData then FreeAndNil(fTempRStream);
end;
end;
class procedure TResources.RegisterStreamer(aList : TFPList; aExtension :
string; aClass : TClass);
var newp,p : PRegisteredStreamerEntry;
i : integer;
begin
aExtension:=lowercase(aExtension);
newp:=GetMem(sizeof(TRegisteredStreamerEntry));
newp^.next:=nil;
newp^.ext:=aExtension;
newp^._class:=aClass;
for i:=0 to aList.Count-1 do
begin
p:=PRegisteredStreamerEntry(aList[i]);
if p^.ext=aExtension then
begin
while p^.next<>nil do
p:=p^.next;
p^.next:=newp;
exit;
end;
end;
aList.Add(newp);
end;
procedure TResources.SendUpdateRawData;
var i : integer;
begin
for i:=0 to Count-1 do
Items[i].UpdateRawData;
end;
class procedure TResources.RegisterReader(const aExtension : string;
aClass: TResourceReaderClass);
begin
InitReaderList;
RegisterStreamer(RegisteredReaders,aExtension,aClass);
end;
class procedure TResources.RegisterWriter(const aExtension : string;
aClass: TResourceWriterClass);
begin
InitWriterList;
RegisterStreamer(RegisteredWriters,aExtension,aClass);
end;
procedure TResources.WriteToStream(aStream: TStream;
aWriter: TAbstractResourceWriter);
begin
SendUpdateRawData;
aWriter.Write(self,aStream);
end;
procedure TResources.WriteToFile(aFileName: string);
var ext : string;
aWriter : TAbstractResourceWriter;
begin
ext:=ExtractFileExt(aFileName);
aWriter:=FindWriterClass(ext).Create;
try
WriteToFile(aFileName,aWriter);
finally
aWriter.Free;
end;
end;
procedure TResources.WriteToFile(aFileName: string;
aWriter: TAbstractResourceWriter);
var OutStream : TFileStream;
begin
OutStream:=TFileStream.Create(aFileName,fmCreate or fmShareDenyWrite);
try
WriteToStream(OutStream,aWriter);
finally
OutStream.Free;
end;
end;
constructor TResources.Create;
begin
fList:=TFPList.Create;
fTree:=TRootResTreeNode.Create;
dummyType:=TResourceDesc.Create;
dummyName:=TResourceDesc.Create;
fTempRStream:=nil;
fCacheData:=true;
fMoveFromCount:=0;
fRemovedCount:=0;
end;
destructor TResources.Destroy;
begin
Clear;
fList.Free;
fTree.Free;
dummyType.Free;
dummyName.Free;
end;
{ TAbstractResourceReader }
procedure TAbstractResourceReader.SetDataSize(aResource: TAbstractResource;
aValue: longword);
begin
aResource.fDataSize:=aValue;
end;
procedure TAbstractResourceReader.SetHeaderSize(aResource: TAbstractResource;
aValue: longword);
begin
aResource.fHeaderSize:=aValue;
end;
procedure TAbstractResourceReader.SetDataOffset(aResource: TAbstractResource;
aValue: longword);
begin
aResource.fDataOffset:=aValue;
end;
procedure TAbstractResourceReader.SetRawData(aResource: TAbstractResource;
aStream: TStream);
begin
if aResource.fRawData<>nil then aResource.fRawData.Free; //should never happen!
aResource.fRawData:=aStream;
end;
procedure TAbstractResourceReader.CallSubReaderLoad(
aReader: TAbstractResourceReader; aResources: TResources; aStream: TStream);
begin
aReader.Load(aResources,aStream);
end;
procedure TAbstractResourceReader.AddNoTree(aResources: TResources;
aResource: TAbstractResource);
begin
aResources.AddNoTree(aResource);
end;
function TAbstractResourceReader.GetTree(aResources: TResources): TObject;
begin
Result:=aResources.fTree;
end;
{ TGenericResource }
function TGenericResource.ChangeDescTypeAllowed(aDesc: TResourceDesc): boolean;
begin
Result:=true;
end;
function TGenericResource.ChangeDescValueAllowed(aDesc: TResourceDesc
): boolean;
begin
Result:=true;
end;
procedure TGenericResource.NotifyResourcesLoaded;
begin
end;
procedure TGenericResource.UpdateRawData;
begin
end;
function TGenericResource.GetType : TResourceDesc;
begin
Result:=fType;
end;
function TGenericResource.GetName : TResourceDesc;
begin
Result:=fName;
end;
constructor TGenericResource.Create(aType, aName: TResourceDesc);
begin
Create;
fType:=TResourceDesc.Create;
fType.Assign(aType);
fName:=TResourceDesc.Create;
fName.Assign(aName);
SetDescOwner(fType);
SetDescOwner(fName);
end;
destructor TGenericResource.Destroy;
begin
fName.Free;
fType.Free;
inherited Destroy;
end;
{ TAbstractResourceWriter }
function TAbstractResourceWriter.GetTree(aResources: TResources): TObject;
begin
Result:=aResources.fTree;
end;
finalization
TResources.DisposeReaderList;
TResources.DisposeWriterList;
end.
|