-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmextension.pp
1346 lines (1129 loc) · 44 KB
/
gmextension.pp
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
unit GmExtension;
{
gmextension.pas
Support for GameMaker GED/GEX/DAT file formats.
Part of the "GEX Editor" project.
Written by Dmitry D. Chernov aka Black Doomer.
}
{$MODE OBJFPC}
{$LONGSTRINGS ON}
{$INLINE ON}
{$ASSERTIONS ON}
{$RANGECHECKS ON}
{$WARN CONSTRUCTING_ABSTRACT OFF}
// TODO: Add consistency checks for TGmExtValueType, TGmExtInvokeType
// and (maybe) TGmExtContentKind, since $RANGECHECKS doesn't emit
// an exception if a value is out of range on typecast (why?).
// TODO: Mark some classes with 'abstract' and 'sealed' specifiers?
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
interface
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
uses
SysUtils, Classes, fgl, ZStream,
GmKryptStream;
const
cGmExtVersionDefault = 700;
cGmExtVersionGEX = 701;
////////////////////////////////////////////////////////////////////////////////////////////////////
type
// This is the base hierarchy class for this unit. Its goal is to provide correct class names for
// validation asserts, avoiding the necessity to use the ugly helper class for the whole TObject,
// and create the ability for user to inherit its own classes if necessary.
TGmExtObject = class
strict protected
class procedure CommonAssert( aCondition: Boolean; const aMessage: String ); inline;
class function WrapStreamChain( var aStream: TStream ): TGmKryptStream; inline;
public
procedure Reset(); inline;
end;
// This is the abstract interface class for any extension data that can be read or written into
// the TStream. It serves to avoid the necessity for unit-wide friendship (i.e. 'private' and
// 'protected' over their 'strict' versions) between partial entry classes (actually the
// TGmExtFileEntry and TGmExtContent). With this approach we need to cast instances explicitly to
// TGmExtEntity / TGmExtEntry every time we want to access their protected methods.
TGmExtEntity = class( TGmExtObject )
strict protected
procedure ReadEntityDefault( aStream: TStream ); virtual; abstract;
procedure WriteEntityDefault( aStream: TStream; aOptimize: Boolean ); virtual; abstract;
procedure ReadEntityGEX( aStream: TStream ); virtual; abstract;
procedure WriteEntityGEX( aStream: TStream; aOptimize: Boolean ); virtual; abstract;
end;
TGmExtEntry = class( TGmExtEntity )
strict protected
fRevision : LongInt;
public
constructor Create();
procedure LoadFromStream( aStream: TStream );
procedure SaveToStream( aStream: TStream; aForceOptimize: Boolean );
procedure Serialize( aStream: TStream; aForceRevision: LongInt );
property Revision: LongInt read fRevision write fRevision;
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
TGmExtValueType = (
gex_vtString = 1,
gex_vtReal = 2
);
TGmExtInvokeType = (
gex_itStdcall = 11,
gex_itCdecl = 12
);
RGmExtFuncArgCount = 0..16;
TGmExtFunctionClass = class of TGmExtFunction;
TGmExtFunction = class( TGmExtEntry )
strict private
fName : String;
fSymbol : String; // if empty, GM uses function name instead
fHelpLine : String;
fHidden : Boolean;
strict protected
fArgCount : RGmExtFuncArgCount;
procedure ReadInvokeType( aStream: TStream ); virtual; abstract;
procedure WriteInvokeType( aStream: TStream ); virtual; abstract;
procedure ReadArgCount( aStream: TStream ); virtual; abstract;
procedure WriteArgCount( aStream: TStream ); virtual; abstract;
procedure ReadEntityDefault( aStream: TStream ); override;
procedure WriteEntityDefault( aStream: TStream; aOptimize: Boolean ); override;
public
procedure AfterConstruction(); override;
property Name: String read fName write fName;
property Symbol: String read fSymbol write fSymbol;
property HelpLine: String read fHelpLine write fHelpLine;
property Hidden: Boolean read fHidden write fHidden;
property ArgCount: RGmExtFuncArgCount read fArgCount write fArgCount;
end;
TGmExtFuncNative = class( TGmExtFunction )
strict private
fArgTypes : array[RGmExtFuncArgCount] of TGmExtValueType;
fInvokeType : TGmExtInvokeType;
fResultType : TGmExtValueType;
function GetArgType( aIndex: RGmExtFuncArgCount ): TGmExtValueType;
procedure SetArgType( aIndex: RGmExtFuncArgCount; aValue: TGmExtValueType );
strict protected
procedure ReadInvokeType( aStream: TStream ); override;
procedure WriteInvokeType( aStream: TStream ); override;
procedure ReadArgCount( aStream: TStream ); override;
procedure WriteArgCount( aStream: TStream ); override;
procedure ReadEntityDefault( aStream: TStream ); override;
procedure WriteEntityDefault( aStream: TStream; aOptimize: Boolean ); override;
public
procedure AfterConstruction(); override;
property ResultType: TGmExtValueType read fResultType write fResultType;
property InvokeType: TGmExtInvokeType read fInvokeType write fInvokeType;
property ArgTypes[i: RGmExtFuncArgCount]: TGmExtValueType
read GetArgType write SetArgType;
end;
TGmExtFuncScript = class( TGmExtFunction )
strict private
fArgCountAny : Boolean;
strict protected
procedure ReadInvokeType( aStream: TStream ); override;
procedure WriteInvokeType( aStream: TStream ); override;
procedure ReadArgCount( aStream: TStream ); override;
procedure WriteArgCount( aStream: TStream ); override;
procedure ReadEntityDefault( aStream: TStream ); override;
procedure WriteEntityDefault( aStream: TStream; aOptimize: Boolean ); override;
public
property ArgCountAny: Boolean read fArgCountAny write fArgCountAny;
end;
TGmExtConstant = class( TGmExtEntry )
strict private
fName : String;
fValue : String;
fHidden : Boolean;
strict protected
procedure ReadEntityDefault( aStream: TStream ); override;
procedure WriteEntityDefault( aStream: TStream; aOptimize: Boolean ); override;
public
property Name: String read fName write fName;
property Value: String read fValue write fValue;
property Hidden: Boolean read fHidden write fHidden;
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
// FIXME: that's ugly, RGmExtContentKind should be actually
// defined as Low(TGmExtContentKind)..High(TGmExtContentKind)
RGmExtContentKind = 1..4;
TGmExtContentKind = (
gex_ckLibraryNative = Low(RGmExtContentKind),
gex_ckLibraryScript,
gex_ckBinaryPlugin,
gex_ckBinarySimple
);
TGmExtFunctionList = specialize TFPGObjectList<TGmExtFunction>;
TGmExtConstantList = specialize TFPGObjectList<TGmExtConstant>;
TGmExtContentClass = class of TGmExtContent;
TGmExtContent = class;
TGmExtDataEntry = class( TGmExtEntry )
strict private
fName : String;
fSource : String; // not necessarily an URI (URL / URN), but any string specifying the source
fContent : TGmExtContent;
strict protected
procedure ReadEntityDefault( aStream: TStream ); override;
procedure WriteEntityDefault( aStream: TStream; aOptimize: Boolean ); override;
public
constructor Create( aContent: TGmExtContent = nil );
property Name: String read fName write fName;
property Source: String read fSource write fSource;
property Content: TGmExtContent read fContent;
end;
TGmExtContent = class( TGmExtEntity )
strict private
fEntry : TGmExtDataEntry;
public
constructor Create( aEntry: TGmExtDataEntry = nil );
destructor Destroy(); override;
class function Kind(): TGmExtContentKind; virtual; abstract;
property Entry: TGmExtDataEntry read fEntry;
end;
// Base class for native libraries and GM scripts
TGmExtLibrary = class( TGmExtContent )
strict private
fInitFunction : String;
fExitFunction : String;
fFunctionList : TGmExtFunctionList;
fConstantList : TGmExtConstantList;
strict protected
procedure ReadEntityDefault( aStream: TStream ); override;
procedure WriteEntityDefault( aStream: TStream; aOptimize: Boolean ); override;
public
procedure AfterConstruction(); override;
procedure BeforeDestruction(); override;
class function FunctionClass(): TGmExtFunctionClass; virtual; abstract;
property InitFunction: String read fInitFunction write fInitFunction;
property ExitFunction: String read fExitFunction write fExitFunction;
property Functions: TGmExtFunctionList read fFunctionList;
property Constants: TGmExtConstantList read fConstantList;
end;
// Base class for GM action libraries and simple files
TGmExtBinary = class( TGmExtContent )
strict protected
procedure ReadEntityDefault( aStream: TStream ); override;
procedure WriteEntityDefault( aStream: TStream; aOptimize: Boolean ); override;
end;
// Kind: SYS
TGmExtLibNative = class( TGmExtLibrary )
public
class function Kind(): TGmExtContentKind; override;
class function FunctionClass(): TGmExtFunctionClass; override;
end;
// Kind: GML
TGmExtLibScript = class( TGmExtLibrary )
public
class function Kind(): TGmExtContentKind; override;
class function FunctionClass(): TGmExtFunctionClass; override;
end;
// Kind: LIB
TGmExtBinPlugin = class( TGmExtBinary )
public
class function Kind(): TGmExtContentKind; override;
end;
// Kind: BIN
TGmExtBinSimple = class( TGmExtBinary )
public
class function Kind(): TGmExtContentKind; override;
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
TGmExtContentList = specialize TFPGObjectList<TGmExtContent>;
TGmExtPrototype = class( TGmExtEntry )
strict private
fName : String;
fAuthor : String;
fVersion : String;
fDate : String;
fLicense : String;
fDescription : String;
fHelpFile : String;
fTempFolder : String;
fDependencies : TStringList;
fHidden : Boolean;
fEditable : Boolean;
fContentList : TGmExtContentList;
strict protected
procedure ReadEntityDefault( aStream: TStream ); override;
procedure WriteEntityDefault( aStream: TStream; aOptimize: Boolean ); override;
public
procedure AfterConstruction(); override;
procedure BeforeDestruction(); override;
property Name: String read fName write fName;
property Author: String read fAuthor write fAuthor;
property Version: String read fVersion write fVersion;
property Date: String read fDate write fDate;
property License: String read fLicense write fLicense;
property Description: String read fDescription write fDescription;
property HelpFile: String read fHelpFile write fHelpFile;
property TempFolder: String read fTempFolder write fTempFolder;
property Dependencies: TStringList read fDependencies;
property Hidden: Boolean read fHidden write fHidden;
property Editable: Boolean read fEditable write fEditable;
property Contents: TGmExtContentList read fContentList;
end;
TGmExtPackage = class( TGmExtEntry )
strict private
fPrototype : TGmExtPrototype;
fKeySeed : LongInt;
strict protected
procedure ReadEntityGEX( aStream: TStream ); override;
procedure WriteEntityGEX( aStream: TStream; aOptimize: Boolean ); override;
public
constructor Create();
procedure AfterConstruction(); override;
procedure BeforeDestruction(); override;
property Prototype: TGmExtPrototype read fPrototype;
property KeySeed: LongInt read fKeySeed write fKeySeed;
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
TGmExtStreamList = specialize TFPGObjectList<TStream>;
// The ownership of the returned TStream will be transferred to the calling class. If you want to
// avoid this (e.g. to read from an already opened and being in-use network connection), you
// should wrap it in TOwnerStream with .SourceOwner parameter set to False and return it instead.
TGmExtStreamBuilderProc = function(
aName: String;
var aSource: String
): TStream; // return 'nil' to skip
TGmExtFile = class( TGmExtObject )
strict protected
fStreamList : TGmExtStreamList;
procedure ReadStreams( aSource: TStream );
procedure WriteStreams( aTarget: TStream; aZlibLevel: TCompressionLevel );
public
procedure AfterConstruction(); override;
procedure BeforeDestruction(); override;
property StreamList: TGmExtStreamList read fStreamList;
end;
TGmExtFileDAT = class( TGmExtFile )
strict private
fKeySeed : LongInt;
public
procedure AfterConstruction(); override;
procedure LoadFromStream( aStream: TStream );
procedure SaveToStream( aStream: TStream; aZlibLevel: TCompressionLevel = clDefault );
property KeySeed: LongInt read fKeySeed write fKeySeed;
end;
TGmExtFileGEX = class( TGmExtFile )
strict private
fPackage : TGmExtPackage;
function AppendStream( aName: String; aSource: String;
cbStreamBuilder: TGmExtStreamBuilderProc ): String; inline;
public
procedure AfterConstruction(); override;
procedure BeforeDestruction(); override;
procedure LoadFromStream( aStream: TStream; cbStreamBuilder: TGmExtStreamBuilderProc );
procedure SaveToStream( aStream: TStream; cbStreamBuilder: TGmExtStreamBuilderProc;
aForceOptimize: Boolean = False; aZlibLevel: TCompressionLevel = clDefault );
property Package: TGmExtPackage read fPackage;
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
TGmExtStreamHelper = class helper for TStream
// Methods are 'protected' so that they can be called from this module without cluttering up the
// global TStream namespace. If for some unknown reason you want to use these methods in your own
// code, then inherit another helper from this one.
protected
function ReadGmInteger(): LongInt; inline;
function ReadGmString(): String; inline;
procedure WriteGmInteger( aValue: LongInt ); inline;
procedure WriteGmInteger( aValue: LongInt; aSkip: Boolean; aRequired: Boolean;
aFallback: LongInt = 0 ); inline; overload;
procedure WriteGmString( aValue: String ); inline;
procedure WriteGmString( aValue: String; aSkip: Boolean; aRequired: Boolean;
aFallback: String = '' ); inline; overload;
end;
const
cGmExtContentTypes : array[RGmExtContentKind] of TGmExtContentClass = (
TGmExtLibNative, TGmExtLibScript, TGmExtBinPlugin, TGmExtBinSimple
);
function GmExtRandomTempFolder(): String;
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
implementation
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
uses
LazFileUtils;
const
cGmExtSignatureGEX = 1234321;
cGmExtArgNumArbitrary = -1;
cGmExtScriptInvokeType = 2;
////////////////////////////////////////////////////////////////////////////////////////////////////
function GmExtRandomTempFolder(): String;
begin
Result := Format( 'temp%.3d', [Random(1000)] );
end;
// this is awkward and not thread-safe
function ExtractFileExtDelphi( const aFileName: String ): String; inline;
var
CurrentState : Boolean;
begin
CurrentState := FirstDotAtFileNameStartIsExtension;
FirstDotAtFileNameStartIsExtension := True;
Result := ExtractFileExt( aFileName );
FirstDotAtFileNameStartIsExtension := CurrentState;
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
// TGmExtObject
////////////////////////////////////////////////////////////////////////////////////////////////////
class procedure TGmExtObject.CommonAssert( aCondition: Boolean; const aMessage: String );
begin
Assert( aCondition, Format( '%s.'+LineEnding+'%s', [aMessage, ClassName()] ) );
end;
// We need this approach because GEX format is somewhat inconsistent: we need to encrypt / decrypt
// both the entries hierarchy and compressed data, but the key seed is specified in the header of
// the root entry (!) after the entry revision number (!!). Therefore, we create a crypto stream
// with an identical state, read / write the key seed from / to it and then initialize the cipher.
// This allows us to continue to use this stream to read / write the compressed data after
// processing the entries.
class function TGmExtObject.WrapStreamChain( var aStream: TStream ): TGmKryptStream;
begin
if aStream is TGmKryptStream then begin
Result := TGmKryptStream( aStream );
aStream := nil;
end else begin
Result := TGmKryptStream.Create( aStream );
aStream := Result;
end;
end;
// Important design decision. To reset any non-essential data in the object, it should be enough
// to call .BeforeDestruction() followed by .AfterConstruction(). Therefore, we use constructors
// and destructors to initialize and release the essential data ONLY, e.g. the revision number of
// a TGmExtEntry (otherwise in TGmExtEntry.LoadFromStream() its value would be erased on reset) or
// the linked entry of a TGmExtContent.
procedure TGmExtObject.Reset();
begin
BeforeDestruction();
AfterConstruction();
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
// TGmExtEntry
////////////////////////////////////////////////////////////////////////////////////////////////////
constructor TGmExtEntry.Create();
begin
inherited;
fRevision := cGmExtVersionDefault;
end;
procedure TGmExtEntry.LoadFromStream( aStream: TStream );
var
KnownVersion : Boolean = True;
begin
Reset();
fRevision := aStream.ReadGmInteger();
try
case fRevision of
cGmExtVersionDefault:
ReadEntityDefault( aStream );
cGmExtVersionGEX:
ReadEntityGEX( aStream );
else
KnownVersion := False;
end;
except
on EAbstractError do
KnownVersion := False;
end;
CommonAssert( KnownVersion, Format( 'Unsupported entry version (%d)', [fRevision] ) );
end;
procedure TGmExtEntry.SaveToStream( aStream: TStream; aForceOptimize: Boolean );
var
RevisionValue : LongInt;
begin
if aForceOptimize then
RevisionValue := -Abs( fRevision )
else
RevisionValue := fRevision;
Serialize( aStream, RevisionValue );
end;
procedure TGmExtEntry.Serialize( aStream: TStream; aForceRevision: LongInt );
var
OptimizeEntry : Boolean;
KnownVersion : Boolean = True;
begin
OptimizeEntry := aForceRevision < 0;
if OptimizeEntry then aForceRevision := -aForceRevision;
aStream.WriteGmInteger( aForceRevision );
try
case aForceRevision of
cGmExtVersionDefault:
WriteEntityDefault( aStream, OptimizeEntry );
cGmExtVersionGEX:
WriteEntityGEX( aStream, OptimizeEntry );
else
KnownVersion := False;
end;
except
on EAbstractError do
KnownVersion := False;
end;
CommonAssert( KnownVersion, Format( 'Unknown entry version (%d)', [aForceRevision] ) );
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
// TGmExtFunction
////////////////////////////////////////////////////////////////////////////////////////////////////
procedure TGmExtFunction.AfterConstruction();
begin
inherited;
fArgCount := Low(RGmExtFuncArgCount);
end;
procedure TGmExtFunction.ReadEntityDefault( aStream: TStream );
begin
fName := aStream.ReadAnsiString();
fSymbol := aStream.ReadAnsiString();
ReadInvokeType( aStream );
fHelpLine := aStream.ReadAnsiString();
fHidden := Boolean( aStream.ReadGmInteger() );
ReadArgCount( aStream );
end;
procedure TGmExtFunction.WriteEntityDefault( aStream: TStream; aOptimize: Boolean );
begin
aStream.WriteGmString( fName );
aStream.WriteGmString( fSymbol, aOptimize, fName <> fSymbol );
WriteInvokeType( aStream );
aStream.WriteGmString( fHelpLine, aOptimize, not fHidden );
aStream.WriteGmInteger( LongInt(fHidden) );
WriteArgCount( aStream );
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
// TGmExtFuncNative
////////////////////////////////////////////////////////////////////////////////////////////////////
procedure TGmExtFuncNative.AfterConstruction();
var
i : RGmExtFuncArgCount;
begin
inherited;
for i in RGmExtFuncArgCount do
fArgTypes[i] := gex_vtReal;
fInvokeType := gex_itStdcall;
fResultType := gex_vtReal;
end;
function TGmExtFuncNative.GetArgType( aIndex: RGmExtFuncArgCount ): TGmExtValueType;
begin
Result := fArgTypes[aIndex];
end;
procedure TGmExtFuncNative.SetArgType( aIndex: RGmExtFuncArgCount; aValue: TGmExtValueType );
begin
fArgTypes[aIndex] := aValue;
end;
procedure TGmExtFuncNative.ReadInvokeType( aStream: TStream );
begin
fInvokeType := TGmExtInvokeType( aStream.ReadGmInteger() );
end;
procedure TGmExtFuncNative.WriteInvokeType( aStream: TStream );
begin
aStream.WriteGmInteger( LongInt(fInvokeType) );
end;
procedure TGmExtFuncNative.ReadArgCount( aStream: TStream );
begin
fArgCount := aStream.ReadGmInteger();
end;
procedure TGmExtFuncNative.WriteArgCount( aStream: TStream );
begin
aStream.WriteGmInteger( LongInt(fArgCount) );
end;
procedure TGmExtFuncNative.ReadEntityDefault( aStream: TStream );
var
i : RGmExtFuncArgCount;
begin
inherited;
// Note: there are fields for 17 arguments, but only the first 4 are actually used.
for i in RGmExtFuncArgCount do
fArgTypes[i] := TGmExtValueType( aStream.ReadGmInteger() );
fResultType := TGmExtValueType( aStream.ReadGmInteger() );
end;
procedure TGmExtFuncNative.WriteEntityDefault( aStream: TStream; aOptimize: Boolean );
var
i : RGmExtFuncArgCount;
begin
inherited;
for i in RGmExtFuncArgCount do
aStream.WriteGmInteger( LongInt(fArgTypes[i]) );
aStream.WriteGmInteger( LongInt(fResultType) );
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
// TGmExtFuncScript
////////////////////////////////////////////////////////////////////////////////////////////////////
procedure TGmExtFuncScript.ReadInvokeType( aStream: TStream );
var
InvokeTypeValue : LongInt;
begin
InvokeTypeValue := aStream.ReadGmInteger();
// this fails for non-editable GM Windows Dialogs.ged from GameMaker 8
//CommonAssert( InvokeTypeValue = cGmExtScriptInvokeType,
// Format( 'Unsupported calling convention for a GML script (%d)', [InvokeTypeValue] ) );
end;
procedure TGmExtFuncScript.WriteInvokeType( aStream: TStream );
begin
aStream.WriteGmInteger( cGmExtScriptInvokeType );
end;
procedure TGmExtFuncScript.ReadArgCount( aStream: TStream );
var
ArgNumValue : LongInt;
begin
ArgNumValue := aStream.ReadGmInteger();
fArgCountAny := ArgNumValue = cGmExtArgNumArbitrary;
if fArgCountAny then
ArgNumValue := Low(RGmExtFuncArgCount);
fArgCount := ArgNumValue;
end;
procedure TGmExtFuncScript.WriteArgCount( aStream: TStream );
begin
if fArgCountAny then
aStream.WriteGmInteger( cGmExtArgNumArbitrary )
else
aStream.WriteGmInteger( LongInt(fArgCount) );
end;
procedure TGmExtFuncScript.ReadEntityDefault( aStream: TStream );
var
i : RGmExtFuncArgCount;
begin
inherited;
for i in RGmExtFuncArgCount do
aStream.ReadGmInteger();
aStream.ReadGmInteger();
end;
procedure TGmExtFuncScript.WriteEntityDefault( aStream: TStream; aOptimize: Boolean );
var
i : RGmExtFuncArgCount;
begin
inherited;
for i in RGmExtFuncArgCount do
aStream.WriteGmInteger( LongInt(gex_vtReal) );
aStream.WriteGmInteger( LongInt(gex_vtReal) );
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
// TGmExtConstant
////////////////////////////////////////////////////////////////////////////////////////////////////
procedure TGmExtConstant.ReadEntityDefault( aStream: TStream );
begin
fName := aStream.ReadAnsiString();
fValue := aStream.ReadAnsiString();
fHidden := Boolean( aStream.ReadGmInteger() );
end;
procedure TGmExtConstant.WriteEntityDefault( aStream: TStream; aOptimize: Boolean );
begin
aStream.WriteGmString( fName );
aStream.WriteGmString( fValue );
aStream.WriteGmInteger( LongInt(fHidden) );
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
// TGmExtDataEntry
////////////////////////////////////////////////////////////////////////////////////////////////////
constructor TGmExtDataEntry.Create( aContent: TGmExtContent );
begin
inherited Create();
fContent := aContent;
end;
// GM4HTML5 1.0.218 (the last classic version) adds files into GEX in a somewhat strange way.
// First of all, it doesn't allow to specify file kind (like original ExtMaker does), instead
// trying by itself to determine their contents by filename extensions. But resulting kinds
// doesn't correspond neither to ExtMaker ones, nor to Resource Tree icons in the IDE. Next,
// GM4HTML5 allows to specify functions and constants for ANY file. So I provide a table here:
// =============================================================================================
// File: .DLL .DYLIB .SO .A .JS .GML .LIB .CHM .HTM .HTML .TXT .RTF | note: kind
// Kind: 1 0 0 0 5* 2 3 6* 0 0 0 0 | 4 is unused
// ---------------------------------------------------------------------------------------------
// Kind: 0 1 2 3 4 5 6 7,8,9,etc. | Resource Tree's
// Icon: no "DLL" "GML" "LIB" no "?" "JS" no | icon labels
// ---------------------------------------------------------------------------------------------
// 5* : but in 39js.gex all kinds of .js files are 0, which's weird (maybe made on old GM4HTML5?)
// 6* : possibly related to "?" icon; filename also stated in <helpfile> XML field in .gmx file;
// GM4HTML5 puts the .CHM into compiled .EXE 2 times: as a help file and as a regular file.
// =============================================================================================
// All of this information was obtained experimentally by adding files with different filename
// extensions, and manual editing of extension's .gmx file in the project tree.
// I decided to convert kind 0 to "Native library" because in case of a simple file, it wiil (at
// least, it should) contain zero values in library-related fields, so GM runner will trait it
// just like a regular simple file. But if it's a .JS like in 39js.gex (see note 5*), then we'll
// parse it correctly without losing any important data.
procedure TGmExtDataEntry.ReadEntityDefault( aStream: TStream );
var
ContentKindValue : LongInt;
begin
fName := aStream.ReadAnsiString();
fSource := aStream.ReadAnsiString();
ContentKindValue := aStream.ReadGmInteger();
case ContentKindValue of
0, 5: ContentKindValue := LongInt(gex_ckLibraryNative);
6: ContentKindValue := LongInt(gex_ckBinarySimple);
end;
fContent := cGmExtContentTypes[RGmExtContentKind(ContentKindValue)].Create( Self );
try
// why FPC (Delphi?) can't guess about the common ancestor? :<
(fContent as TGmExtEntity).ReadEntityDefault( aStream );
except
fContent.Destroy();
raise;
end;
end;
procedure TGmExtDataEntry.WriteEntityDefault( aStream: TStream; aOptimize: Boolean );
begin
aStream.WriteGmString( fName );
aStream.WriteGmString( fSource, aOptimize, False );
aStream.WriteGmInteger( LongInt( fContent.Kind() ) );
(fContent as TGmExtEntity).WriteEntityDefault( aStream, aOptimize );
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
// TGmExtContent
////////////////////////////////////////////////////////////////////////////////////////////////////
constructor TGmExtContent.Create( aEntry: TGmExtDataEntry );
begin
inherited Create();
if aEntry = nil then
aEntry := TGmExtDataEntry.Create( Self );
fEntry := aEntry;
end;
destructor TGmExtContent.Destroy();
begin
fEntry.Free();
inherited;
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
// TGmExtBinary
////////////////////////////////////////////////////////////////////////////////////////////////////
procedure TGmExtBinary.ReadEntityDefault( aStream: TStream );
begin
CommonAssert( aStream.ReadAnsiString() = '', 'Initialization function is not empty' );
CommonAssert( aStream.ReadAnsiString() = '', 'Release function is not empty' );
CommonAssert( aStream.ReadGmInteger() = 0, 'Number of functions is not 0' );
CommonAssert( aStream.ReadGmInteger() = 0, 'Number of constants is not 0' );
end;
procedure TGmExtBinary.WriteEntityDefault( aStream: TStream; aOptimize: Boolean );
begin
aStream.WriteGmString('');
aStream.WriteGmString('');
aStream.WriteGmInteger(0);
aStream.WriteGmInteger(0);
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
// TGmExtLibrary
////////////////////////////////////////////////////////////////////////////////////////////////////
procedure TGmExtLibrary.AfterConstruction();
begin
inherited;
fFunctionList := TGmExtFunctionList.Create();
fConstantList := TGmExtConstantList.Create();
end;
procedure TGmExtLibrary.BeforeDestruction();
begin
FreeAndNil( fFunctionList );
FreeAndNil( fConstantList );
inherited;
end;
procedure TGmExtLibrary.ReadEntityDefault( aStream: TStream );
var
NewFunc : TGmExtFunction;
NewConst : TGmExtConstant;
FuncCount : LongInt;
ConstCount : LongInt;
i : SizeInt;
begin
fInitFunction := aStream.ReadAnsiString();
fExitFunction := aStream.ReadAnsiString();
// We add functions and constants before actual loading to
// prevent possible memory leak on exception when doing that.
FuncCount := aStream.ReadGmInteger();
for i := 1 to FuncCount do begin
NewFunc := FunctionClass().Create();
fFunctionList.Add( NewFunc );
NewFunc.LoadFromStream( aStream );
end;
ConstCount := aStream.ReadGmInteger();
for i := 1 to ConstCount do begin
NewConst := TGmExtConstant.Create();
fConstantList.Add( NewConst );
NewConst.LoadFromStream( aStream );
end;
end;
procedure TGmExtLibrary.WriteEntityDefault( aStream: TStream; aOptimize: Boolean );
var
iFunction : TGmExtFunction;
iConstant : TGmExtConstant;
begin
aStream.WriteGmString( fInitFunction );
aStream.WriteGmString( fExitFunction );
aStream.WriteGmInteger( fFunctionList.Count );
for iFunction in fFunctionList do
iFunction.SaveToStream( aStream, aOptimize );
aStream.WriteGmInteger( fConstantList.Count );
for iConstant in fConstantList do
iConstant.SaveToStream( aStream, aOptimize );
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
// TGmExtLibNative
////////////////////////////////////////////////////////////////////////////////////////////////////
class function TGmExtLibNative.Kind(): TGmExtContentKind;
begin
Result := gex_ckLibraryNative;
end;
class function TGmExtLibNative.FunctionClass(): TGmExtFunctionClass;
begin
Result := TGmExtFuncNative;
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
// TGmExtLibScript
////////////////////////////////////////////////////////////////////////////////////////////////////
class function TGmExtLibScript.Kind(): TGmExtContentKind;
begin
Result := gex_ckLibraryScript;
end;
class function TGmExtLibScript.FunctionClass(): TGmExtFunctionClass;
begin
Result := TGmExtFuncScript;
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
// TGmExtBinPlugin
////////////////////////////////////////////////////////////////////////////////////////////////////
class function TGmExtBinPlugin.Kind(): TGmExtContentKind;
begin
Result := gex_ckBinaryPlugin;
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
// TGmExtBinSimple
////////////////////////////////////////////////////////////////////////////////////////////////////
class function TGmExtBinSimple.Kind(): TGmExtContentKind;
begin
Result := gex_ckBinarySimple;
end;
////////////////////////////////////////////////////////////////////////////////////////////////////
// TGmExtPrototype
////////////////////////////////////////////////////////////////////////////////////////////////////
procedure TGmExtPrototype.AfterConstruction();
begin
inherited;
fEditable := True;
//fTempFolder := GmExtRandomTempFolder();
fDependencies := TStringList.Create();
fContentList := TGmExtContentList.Create();
end;
procedure TGmExtPrototype.BeforeDestruction();
begin
FreeAndNil( fDependencies );
FreeAndNil( fContentList );
inherited;
end;
procedure TGmExtPrototype.ReadEntityDefault( aStream: TStream );
var
UsesCount : LongInt;
FileCount : LongInt;
FileEntry : TGmExtDataEntry;
i : Integer;
begin
fEditable := Boolean( aStream.ReadGmInteger() );
fName := aStream.ReadAnsiString();
fTempFolder := aStream.ReadAnsiString();
fVersion := aStream.ReadAnsiString();
fAuthor := aStream.ReadAnsiString();
fDate := aStream.ReadAnsiString();
fLicense := aStream.ReadAnsiString();
fDescription := aStream.ReadAnsiString();
fHelpFile := aStream.ReadAnsiString();
fHidden := Boolean( aStream.ReadGmInteger() );
UsesCount := aStream.ReadGmInteger();
for i := 1 to UsesCount do
fDependencies.Add( aStream.ReadAnsiString() );
FileCount := aStream.ReadGmInteger();
for i := 1 to FileCount do begin
FileEntry := TGmExtDataEntry.Create( nil );
FileEntry.LoadFromStream( aStream );
fContentList.Add( FileEntry.Content );
end;
end;
procedure TGmExtPrototype.WriteEntityDefault( aStream: TStream; aOptimize: Boolean );
var
iDependency : String;
iContent : TGmExtContent;
begin
aStream.WriteGmInteger( LongInt(fEditable), aOptimize, False, LongInt(False) );
aStream.WriteGmString( fName );
aStream.WriteGmString( fTempFolder );
aStream.WriteGmString( fVersion );
aStream.WriteGmString( fAuthor );
aStream.WriteGmString( fDate );
aStream.WriteGmString( fLicense );
aStream.WriteGmString( fDescription );
// GameMaker IDE opens help file through shell command, so original file extension is important.
aStream.WriteGmString( fHelpFile, aOptimize, False, ExtractFileExtDelphi(fHelpFile) );