-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfurqContext.pas
1668 lines (1511 loc) · 39.7 KB
/
furqContext.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
unit furqContext;
interface
uses
Classes,
Math,
MD5,
JclStringLists,
d2dClasses,
d2dUtils,
furqTypes,
furqFiler
;
type
TFURQCodeLoadProc = function (const aFilename: string): string of object;
IFURQCodePoint = interface
['{CE84C50E-929A-4E0A-B20F-4BCFB5F82086}']
function pm_GetLineNo: Integer;
function pm_GetSrcFile: Integer;
property LineNo: Integer read pm_GetLineNo;
property SrcFile: Integer read pm_GetSrcFile;
end;
TFURQCodePoint = class(TInterfacedObject, IFURQCodePoint)
private
f_File : Integer;
f_LineNo : Integer;
function pm_GetLineNo: Integer;
function pm_GetSrcFile: Integer;
public
constructor Create(aFile, aLineNo: Integer);
end;
// êîä, èñõîäíèê êâåñòà
TFURQCode = class
private
f_ActionList: IJclStringList;
f_Labels : IJclStringList;
f_Source : IJclStringList;
f_Files : IJclStringList;
f_SourceHash: TMD5Digest;
procedure CalcSourceHash;
function pm_GetLocCountVarName(Index: Integer): string;
public
constructor Create;
destructor Destroy; override;
procedure Load(const aFilename: string; aLoadProc: TFURQCodeLoadProc);
function GetSourcePointStr(aLineNo: Integer): string;
property ActionList: IJclStringList read f_ActionList;
property Labels: IJclStringList read f_Labels;
property LocCountVarName[Index: Integer]: string read pm_GetLocCountVarName;
property Source: IJclStringList read f_Source;
property SourceHash: TMD5Digest read f_SourceHash;
end;
type
// Îäèíî÷íûé îïåðàòîð, íà íèõ ðàñïàðñèâàåòñÿ ñòðîêà èñõîäíîãî êîäà
TFURQOperator = class
private
f_Next: TFURQOperator;
f_Source: string;
protected
f_OperatorType: Byte;
public
constructor Create(const aSource: string);
destructor Destroy; override;
procedure Load(const aFiler: TFURQFiler); virtual;
procedure Save(const aFiler: TFURQFiler); virtual;
property Next: TFURQOperator read f_Next write f_Next;
property Source: string read f_Source;
end;
type
// òî÷êà âûïîëíåíèÿ (äëÿ ðåàëèçàöèè ñòåêà)
TFURQExecutionPoint = class
private
f_CurOp: TFURQOperator;
f_Line: Integer;
f_Operators: TFURQOperator;
f_Prev: TFURQExecutionPoint;
public
constructor Create(aPrev: TFURQExecutionPoint);
destructor Destroy; override;
procedure ClearOperators;
procedure DropPrev;
procedure Load(const aFiler: TFURQFiler);
procedure Save(const aFiler: TFURQFiler);
property CurOp: TFURQOperator read f_CurOp write f_CurOp;
property Line: Integer read f_Line write f_Line;
property Operators: TFURQOperator read f_Operators write f_Operators;
property Prev: TFURQExecutionPoint read f_Prev;
end;
type
// äàííûå êíîïêè
IFURQActionData = interface
['{6C665DAF-91A6-4B76-8E17-3A990580B3CD}']
function pm_GetModifier: TFURQActionModifier;
function pm_GetLabelIdx: Integer;
function pm_GetParams: IJclStringList;
procedure Save(const aFiler: Td2dFiler);
property Modifier: TFURQActionModifier read pm_GetModifier;
property LabelIdx: Integer read pm_GetLabelIdx;
property Params: IJclStringList read pm_GetParams;
end;
TFURQActionData = class(TInterfacedObject, IFURQActionData)
private
f_LabelIdx : Integer;
f_Params : IJclStringList;
f_Modifier : TFURQActionModifier;
function pm_GetModifier: TFURQActionModifier;
function pm_GetLabelIdx: Integer;
function pm_GetParams: IJclStringList;
procedure Save(const aFiler: Td2dFiler);
public
constructor Create(aIdx: Integer; aParams: IJclStringList; aModifier: TFURQActionModifier = amNone);
constructor Load(const aFiler: Td2dFiler);
destructor Destroy; override;
end;
const
cMaxStateParams = 3;
type
// Êîíòåêñò. Õðàíèò ñîñòîÿíèå êâåñòà è âñå åãî ïåðåìåííûå.
TFURQContext = class(Td2dProtoObject)
private
f_Actions : IJclStringList;
f_MenuActions: IJclStringList;
f_Buttons: IJclStringList;
f_Code: TFURQCode;
f_Inventory: IJclStringList;
f_NumFormatString: string;
f_Stack: TFURQExecutionPoint;
f_BackupStack : TFURQExecutionPoint;
f_State: TFURQContextState;
f_BackupState: TFURQContextState;
f_ExecutionMode: TFURQContextExecutionMode;
f_Menu: IJclStringList;
f_StateParam: array[1..cMaxStateParams] of string;
f_StateResult: string;
f_Variables: IJclStringList;
function ButtonIdxByID(anActionID: string): Integer;
function pm_GetStateParam(aIndex: Integer): string;
function pm_GetVariables(aName: string): Variant;
procedure pm_SetStateParam(aIndex: Integer; const Value: string);
procedure pm_SetVariables(aName: string; const Value: Variant);
procedure SaveButtons(const aFiler: TFURQFiler);
procedure LoadButtons(const aFiler: TFURQFiler);
procedure SaveActions(const aFiler: TFURQFiler);
procedure LoadActions(const aFiler: TFURQFiler);
protected
procedure Cleanup; override;
function DoActionFinal(const anActionData: IFURQActionData): Boolean; virtual;
procedure DoLoadData(aFiler: TFURQFiler); virtual;
procedure DoSaveData(aFiler: TFURQFiler); virtual;
function GetVirtualVariable(const aName: string): Variant; virtual;
procedure InitSysVariables; virtual;
function IsSystemOrVirtualVariable(aVarName: string): Boolean; virtual;
function pm_GetActionsData(anID: string): IFURQActionData; virtual;
procedure SetSysVariables(aName: string; const Value: Variant); virtual;
function SetVirtualVariables(const aName: string; const aValue: Variant): Boolean; virtual;
public
constructor Create(aCode: TFURQCode);
function ActionIsFinal(const aActionID: string): Boolean;
function ActionIsMenu(const aActionID: string): Boolean;
function AddAction(const aData: IFURQActionData; const aActionList: IJclStringList = nil): string;
procedure AddButton(const aLabel: string; const aParams: IJclStringList; const aText: string; aModifier:
TFURQActionModifier);
procedure AddToInventory(aName: string; anAmount: Double);
function DoAction(const anActionData: IFURQActionData): Boolean;
procedure ClearButtons; virtual;
procedure ClearText; virtual;
procedure ClearInventory;
procedure ClearStack;
procedure ClearVariables;
procedure ClearStateParams;
function DeleteVariable(aVarName: string): Boolean;
procedure ExecuteProc(aLabelIdx: Integer);
procedure ExecuteProcAtLine(aLineNo: Integer);
procedure ForgetProcs;
function FormatNumber(aNumber: Extended): string;
function GetInventoryString(aIndex: Integer): string;
procedure IncLocationCount(const aLabelIdx: Integer);
function IsVariableExists(const aVarName: string): Boolean;
procedure JumpToLabelIdx(aIndex: Integer);
procedure JumpToLine(aLineNo: Integer);
procedure LoadFromFile(aFilename: string; aIgnoreHash: Boolean = False);
procedure LoadFromStream(aStream: TStream; aIgnoreHash: Boolean = False);
procedure MusicPlay(const aFilename: string; aFadeTime: Integer); virtual;
procedure MusicStop(aFadeTime: Integer); virtual;
procedure OutText(const aStr: string); virtual; abstract;
procedure Restart; virtual;
procedure ReturnFromProc;
procedure SaveToFile(const aFilename, aLocation: string; const aDescription: string = '');
procedure SaveToStream(aLocation: string; aStream: TStream; const aDescription: string = '');
procedure SoundPlay(const aFilename: string; aVolume: Integer; aLooped: Boolean); virtual;
procedure SoundStop(const aFilename: string); virtual;
procedure StartMenuBuild(aKeepMenuActions: Boolean);
procedure EndMenuBuild;
function SystemProc(aProcName: string; aParams: IJclStringList): Boolean; virtual;
property ActionsData[anID: string]: IFURQActionData read pm_GetActionsData;
property Buttons: IJclStringList read f_Buttons;
property Menu: IJclStringList read f_Menu;
property Code: TFURQCode read f_Code;
property ExecutionMode: TFURQContextExecutionMode read f_ExecutionMode;
property Inventory: IJclStringList read f_Inventory;
property Stack: TFURQExecutionPoint read f_Stack;
property State: TFURQContextState read f_State write f_State;
property StateParam[aIndex: Integer]: string read pm_GetStateParam write pm_SetStateParam;
property StateResult: string read f_StateResult write f_StateResult;
property Variables[aName: string]: Variant read pm_GetVariables write pm_SetVariables;
end;
TFURQCondition = class(TFURQOperator)
private
f_ThenOp: TFURQOperator;
f_ElseOp: TFURQOperator;
public
constructor Create(const aCondition: string; aThenOp, aElseOp: TFURQOperator);
destructor Destroy; override;
procedure Load(const aFiler: TFURQFiler); override;
procedure Save(const aFiler: TFURQFiler); override;
property ThenOp: TFURQOperator read f_ThenOp;
property ElseOp: TFURQOperator read f_ElseOp;
end;
TFURQWinContext = class(TFURQContext)
private
f_Filename: string;
f_TextBuffer: string;
protected
procedure InitSysVariables; override;
public
constructor Create(aCode: TFURQCode; aFilename: string);
procedure ClearTextBuffer;
procedure OutText(const aStr: string);
procedure Restart; override;
property TextBuffer: string read f_TextBuffer;
end;
const
sDefaultActName = 'Îñìîòðåòü';
sDefaultActTag = '_defact_';
sVarSaveSuccess = 'save_success';
function FURQReadSaveInfo(const aFileName: string; out theDescription: string; out theDateTime: TDateTime): Boolean;
function ActionDataHash(const aData: IFURQActionData): string;
implementation
uses
Windows,
mmSystem,
SysUtils,
StrUtils,
Variants,
furqUtils
;
const
// START resource string wizard section
c_sUsePrefix = 'use_';
c_sInclude = '%include ';
c_sEnd = 'end';
c_sCountPrefix = 'count_';
c_sIdispPrefix = 'idisp_';
c_sInvPrefix = 'inv_';
c_sFp_prec = 'fp_prec';
c_s_result = '_result';
// math
c_s_sin = '_sin';
c_s_cos = '_cos';
c_s_tan = '_tan';
c_s_arcsin = '_arcsin';
c_s_arccos = '_arccos';
c_s_arctan = '_arctan';
c_s_sqrt = '_sqrt';
c_s_power = '_power';
c_s_int = '_int';
c_s_tohex = '_tohex';
c_s_trim = '_trim';
c_s_scopy = '_scopy';
c_sInvname = 'invname';
c_sGametitle = 'gametitle';
c_sTimeVar = 'time';
// END resource string wizard section
sCorruptedSaveFile = 'Ôàéë ñîõðàíåíèÿ èñïîð÷åí';
sBadVersionSaveFile = 'Ôàéë ñîõðàíåíèÿ îò äðóãîé âåðñèè èãðû';
otOperator = 1;
otCondition = 2;
function ActionDataHash(const aData: IFURQActionData): string;
var
I: Integer;
l_Source: string;
l_MD5 : TMD5Digest;
begin
Result := '';
if aData <> nil then
begin
case aData.Modifier of
amNone : l_Source := '0-';
amNonFinal : l_Source := '1-';
amMenu : l_Source := '2-';
end;
l_Source := l_Source + IntToStr(aData.LabelIdx);
if aData.Params <> nil then
begin
for I := 0 to aData.Params.LastIndex do
l_Source := l_Source + '-' + aData.Params[I];
end;
l_MD5 := MD5String(l_Source);
Result := MD5DigestToStr(l_MD5);
end;
end;
function FURQReadSaveInfo(const aFileName: string; out theDescription: string; out theDateTime: TDateTime): Boolean;
var
l_FS : TFileStream;
l_R : TFURQFiler;
l_Signature : string;
begin
theDescription := '';
theDateTime := 0;
Result := False;
if FileExists(aFileName) then
begin
l_FS := TFileStream.Create(aFilename, fmOpenRead);
try
l_R := TFURQFiler.Create(l_FS);
try
l_Signature := l_R.ReadString;
if l_Signature = cSaveFileSignature then
begin
theDescription := l_R.ReadString; // îïèñàíèå ñîõðàí¸íêè
theDateTime := l_R.ReadDouble; // äàòà è âðåìÿ
Result := True;
end;
finally
l_R.Free;
end;
finally
l_FS.Free;
end;
end;
end;
procedure SaveOperators(const aFirstOp: TFURQOperator; const aFiler: TFURQFiler); //çàïèñûâàåò ÖÅÏÎ×ÊÓ îïåðàòîðîâ (ñ ó÷¸òîì Next)
var
l_Op: TFURQOperator;
begin
if aFirstOp <> nil then
begin
aFiler.WriteBoolean(True);
l_Op := aFirstOp;
while l_Op <> nil do
begin
l_Op.Save(aFiler);
l_Op := l_Op.Next;
aFiler.WriteBoolean(l_Op <> nil);
end;
end
else
aFiler.WriteBoolean(False);
end;
function LoadOperators(const aFiler: TFURQFiler): TFURQOperator; // ôàáðèêà, çàãðóæàåò ÖÅÏÎ×ÊÓ îïåðàòîðîâ
function LoadOne: TFURQOperator;
var
l_Type: Byte;
begin
l_Type := aFiler.ReadByte;
case l_Type of
otOperator : Result := TFURQOperator.Create('');
otCondition : Result := TFURQCondition.Create('', nil, nil);
else
raise EFURQRunTimeError.Create(sCorruptedSaveFile);
end;
Result.Load(aFiler);
end;
var
l_Op: TFURQOperator;
begin
if aFiler.ReadBoolean then
begin
Result := LoadOne;
l_Op := Result;
while aFiler.ReadBoolean do
begin
l_Op.Next := LoadOne;
l_Op := l_Op.Next;
end;
end
else
Result := nil;
end;
constructor TFURQCode.Create;
begin
inherited;
f_Source := JclStringList;
f_Files := JclStringList;
f_Files.CaseSensitive := False;
f_Labels := JclStringList;
//f_Labels.Sorted := True;
f_Labels.Duplicates := dupIgnore;
f_Labels.CaseSensitive := False;
f_ActionList := JclStringList;
f_ActionList.CaseSensitive := False;
end;
destructor TFURQCode.Destroy;
begin
f_Source := nil;
f_Files := nil;
f_Labels := nil;
f_ActionList := nil;
inherited;
end;
procedure TFURQCode.CalcSourceHash;
var
I: Integer;
l_MD5C : TMD5Context;
l_Str: string;
begin
MD5Init(l_MD5C);
for I := 0 to f_Source.LastIndex do
begin
l_Str := f_Source[I];
if l_Str <> '' then
MD5Update(l_MD5C, MD5.PByteArray(@l_Str[1]), Length(l_Str));
end;
MD5Final(f_SourceHash, l_MD5C);
end;
function TFURQCode.GetSourcePointStr(aLineNo: Integer): string;
var
l_CP: IFURQCodePoint;
begin
l_CP := f_Source.Interfaces[aLineNo] as IFURQCodePoint;
if l_CP <> nil then
Result := Format('%s ñòðîêà %d', [f_Files[l_CP.SrcFile], l_CP.LineNo])
else
Result := '';
end;
procedure TFURQCode.Load(const aFilename: string; aLoadProc: TFURQCodeLoadProc);
var
l_BasePath: string;
procedure DeleteComments(const aList: IJclStringList);
var
I : Integer;
l_OpenPos, l_ClosePos: Cardinal;
l_Pos, l_Pos2 : Cardinal;
l_Str: string;
l_IsInComment : Boolean;
begin
// óäàëÿåì êîììåíòàðèè âèäà /* ... */
l_IsInComment := False;
for I := 0 to aList.Count - 1 do
begin
l_Str := aList[I];
if l_IsInComment then
begin
l_Pos := Pos('*/', l_Str);
if l_Pos > 0 then
begin
Delete(l_Str, 1, l_Pos+1);
l_Str := '_' + l_Str;
l_IsInComment := False;
end
else
begin
aList[I] := '_';
Continue;
end;
end;
while true do
begin
l_Pos := Pos('/*', l_Str);
if l_Pos > 0 then
begin
l_Pos2 := PosEx('*/', l_Str, l_Pos+2);
if l_Pos2 > 0 then
begin
Delete(l_Str, l_Pos, l_Pos2 - l_Pos + 2);
Continue;
end
else
begin
Delete(l_Str, l_Pos, MaxInt);
l_IsInComment := True;
Break;
end;
end
else
Break;
end;
aList[I] := l_Str;
end;
// îòðåçàåì êîììåíòàðèè, íà÷èíàþùèåñÿ ñ ";"
for I := 0 to Pred(aList.Count) do
begin
l_Pos := Pos(';', aList[I]);
if l_Pos > 0 then
aList[I] := Copy(aList[I], 1, l_Pos-1);
end;
end;
procedure TrimLines;
var
I, J : Integer;
l_Str: string;
l_Len: Integer;
begin
for I := 0 to Pred(f_Source.Count) do
begin
l_Str := AnsiReplaceStr(f_Source[I], #9, ' ');
l_Len := Length(l_Str);
for J := 1 to l_Len do
if l_Str[J] < #32 then l_Str[J] := #32;
f_Source[I] := TrimLeft(l_Str);
end;
end;
procedure ParseLocations; // èùåì ëîêàöèè (ìåòêè)
var
I: Integer;
l_Str: string;
l_Idx: Integer;
begin
for I := 0 to Pred(f_Source.Count) do
begin
l_Str := f_Source[I];
if AnsiStartsStr(':', l_Str) then
begin
Delete(l_Str, 1, 1);
l_Str := Trim(l_Str);
if f_Labels.IndexOf(l_Str) = -1 then // ÷òîáû ðàáîòàëà òîëüêî ïåðâàÿ ìåòêà èç îäèíàêîâûõ
begin
l_Idx := f_Labels.Add(l_Str);
f_Labels.Variants[l_Idx] := I;
end;
end;
end;
end;
procedure FillActions; // èùåì äîñòóïíûå äåéñòâèÿ
var
I: Integer;
l_Name, l_ActionName: string;
l_Pos: Integer;
l_Label: string;
l_Idx: Integer;
l_ActIdx: Integer;
begin
for I := 0 to f_Labels.LastIndex do
begin
l_Label := f_Labels[I];
if AnsiStartsText(c_sUsePrefix, l_Label) then
begin
l_Name := Copy(l_Label, 5, MaxInt);
l_ActIdx := f_ActionList.Add(l_Name);
f_ActionList.Variants[l_ActIdx] := l_Label;
end;
end;
end;
procedure IndexActionLabels;
var
I : Integer;
begin
for I := 0 to f_ActionList.LastIndex do
f_ActionList.Variants[I] := f_Labels.IndexOf(f_ActionList.Variants[I]);
end;
procedure LoadSingleFile(const aOneFilename: string);
var
I: Integer;
l_Buf: IJclStringList;
l_Includes: IJclStringList;
l_CodePoint: IFURQCodePoint;
l_File: string;
l_FileID: string;
l_Idx: Integer;
l_MainIdx: Integer;
l_Str: string;
l_Path: string;
begin
l_FileID := ExtractRelativePath(l_BasePath, aOneFilename);
if f_Files.IndexOf(l_FileID) < 0 then
begin
l_Buf := JclStringList;
l_Includes := JclStringList;
l_Path := ExtractFilePath(aOneFilename);
l_Buf.Text := aLoadProc(aOneFileName);
DeleteComments(l_Buf);
l_Idx := f_Files.Add(l_FileID);
for I := 0 to Pred(l_Buf.Count) do
begin
l_Str := Trim(l_Buf[I]);
if AnsiStartsText(c_sInclude, l_Str) then
begin
l_File := Trim(Copy(l_Str, 10, MaxInt));
l_Includes.Add(l_File);
l_Buf[I] := '';
end;
end;
for I := 0 to Pred(l_Buf.Count) do
begin
l_MainIdx := f_Source.Add(l_Buf[I]);
l_CodePoint := TFURQCodePoint.Create(l_Idx, I+1);
f_Source.Interfaces[l_MainIdx] := l_CodePoint;
end;
l_MainIdx := f_Source.Add(c_sEnd);
l_CodePoint := TFURQCodePoint.Create(l_Idx, l_Buf.Count+1);
f_Source.Interfaces[l_MainIdx] := l_CodePoint;
for I := 0 to Pred(l_Includes.Count) do
LoadSingleFile(l_Path + l_Includes[I]);
end;
end;
begin
f_Source.Clear;
f_Labels.Clear;
f_Files.Clear;
f_Labels.Sorted := False; // ÷òîáû íå ñîðòèðîâàòü ïðèíóäèòåëüíî äåéñòâèÿ èíâåíòàðÿ
f_ActionList.Clear;
l_BasePath := ExtractFilePath(aFilename);
LoadSingleFile(aFilename);
TrimLines;
ParseLocations;
FillActions;
f_Labels.Sorted := True;
IndexActionLabels;
CalcSourceHash;
end;
function TFURQCode.pm_GetLocCountVarName(Index: Integer): string;
begin
Result := c_sCountPrefix+Labels[Index];
end;
constructor TFURQOperator.Create(const aSource: string);
begin
inherited Create;
f_OperatorType := otOperator;
f_Source := aSource;
end;
destructor TFURQOperator.Destroy;
begin
FreeAndNil(f_Next);
inherited;
end;
procedure TFURQOperator.Load(const aFiler: TFURQFiler);
begin
// f_OperatorType íå ÷èòàåì, îí ÷èòàåòñÿ ôàáðèêîé
f_Source := aFiler.ReadString;
end;
procedure TFURQOperator.Save(const aFiler: TFURQFiler);
begin
aFiler.WriteByte(f_OperatorType);
aFiler.WriteString(f_Source);
end;
constructor TFURQExecutionPoint.Create(aPrev: TFURQExecutionPoint);
begin
inherited Create;
f_Prev := aPrev;
end;
destructor TFURQExecutionPoint.Destroy;
begin
ClearOperators;
inherited;
end;
procedure TFURQExecutionPoint.ClearOperators;
begin
FreeAndNil(f_Operators);
f_CurOp := nil;
end;
procedure TFURQExecutionPoint.DropPrev;
begin
if f_Prev <> nil then
begin
f_Prev.DropPrev;
FreeAndNil(f_Prev);
end;
end;
procedure TFURQExecutionPoint.Load(const aFiler: TFURQFiler);
begin
DropPrev;
FreeAndNil(f_Operators);
f_Line := aFiler.ReadInteger;
f_CurOp := LoadOperators(aFiler);
f_Operators := f_CurOp; // ñòðîêà ïîëíîñòüþ íèêîìó íå íóæíà
if aFiler.ReadBoolean then // åñòü ñòåê
begin
f_Prev := TFURQExecutionPoint.Create(nil);
f_Prev.Load(aFiler);
end;
end;
procedure TFURQExecutionPoint.Save(const aFiler: TFURQFiler);
var
l_HasPrev: Boolean;
begin
aFiler.WriteInteger(f_Line);
SaveOperators(CurOp, aFiler);
l_HasPrev := f_Prev <> nil;
aFiler.WriteBoolean(l_HasPrev);
if l_HasPrev then
f_Prev.Save(aFiler);
end;
constructor TFURQContext.Create(aCode: TFURQCode);
begin
inherited Create;
f_Code := aCode;
f_Actions := JclStringList;
f_Actions.Sorted := True;
f_Actions.Duplicates := dupIgnore;
f_MenuActions := JclStringList;
f_MenuActions.Sorted := True;
f_MenuActions.Duplicates := DUPIGNORE;
f_Menu := JclStringList;
f_Buttons := JclStringList;
f_Variables := JclStringList;
f_Variables.Sorted := True;
f_Variables.CaseSensitive := False;
f_Inventory := JclStringList;
f_Inventory.CaseSensitive := False;
f_ExecutionMode := emNormal;
Restart;
end;
procedure TFURQContext.Cleanup;
begin
ClearStack;
FreeAndNil(f_Stack);
inherited;
end;
function TFURQContext.ActionIsFinal(const aActionID: string): Boolean;
var
l_Data: IFURQActionData;
begin
Result := True;
l_Data := ActionsData[aActionID];
if l_Data <> nil then
Result := l_Data.Modifier = amNone;
end;
function TFURQContext.ActionIsMenu(const aActionID: string): Boolean;
var
l_Data: IFURQActionData;
begin
Result := False;
l_Data := ActionsData[aActionID];
if l_Data <> nil then
Result := l_Data.Modifier = amMenu;
end;
function TFURQContext.AddAction(const aData: IFURQActionData; const aActionList: IJclStringList = nil): string;
var
l_Idx: Integer;
l_ID : string;
l_CurActions: IJclStringList;
begin
l_ID := ActionDataHash(aData);
Result := l_ID;
if (ExecutionMode = emMenuBuild) then
l_CurActions := f_MenuActions
else
if aActionList = nil then
l_CurActions := f_Actions
else
l_CurActions := aActionList;
l_Idx := l_CurActions.IndexOf(l_ID);
if l_Idx < 0 then
begin
l_Idx := l_CurActions.Add(l_ID);
l_CurActions.Interfaces[l_Idx] := aData;
end;
end;
procedure TFURQContext.AddButton(const aLabel: string; const aParams: IJclStringList; const aText: string; aModifier: TFURQActionModifier);
var
l_LIdx, l_Idx: Integer;
l_BData: IFURQActionData;
l_NewAID: string;
l_CurButtons: IJclStringList;
begin
l_LIdx := f_Code.Labels.IndexOf(aLabel);
if (l_LIdx = -1) then
begin
if (ExecutionMode <> emMenuBuild) and (EnsureReal(Variables[cVarHidePhantoms]) <> 0) then
Exit;
l_NewAID := '';
end
else
begin
l_BData := TFURQActionData.Create(l_LIdx, aParams, aModifier);
l_NewAID := AddAction(l_BData);
end;
if ExecutionMode = emMenuBuild then
l_CurButtons := f_Menu
else
l_CurButtons := f_Buttons;
l_Idx := l_CurButtons.Add(aText);
l_CurButtons.Variants[l_Idx] := l_NewAID;
end;
procedure TFURQContext.AddToInventory(aName: string; anAmount: Double);
var
l_Idx: Integer;
begin
l_Idx := f_Inventory.IndexOf(aName);
if l_Idx < 0 then
begin
if anAmount <= 0 then
Exit;
l_Idx := f_Inventory.Add(aName);
end;
f_Inventory.Variants[l_Idx] := f_Inventory.Variants[l_Idx] + anAmount;
if f_Inventory.Variants[l_Idx] <= 0.0 then
f_Inventory.Delete(l_Idx);
end;
function TFURQContext.ButtonIdxByID(anActionID: string): Integer;
var
I: Integer;
begin
Result := -1;
for I := 0 to f_Buttons.LastIndex do
if f_Buttons.Variants[I] = anActionID then
begin
Result := I;
Break;
end;
end;
function TFURQContext.DoAction(const anActionData: IFURQActionData): Boolean;
begin
Result := False;
if anActionData <> nil then
begin
if anActionData.LabelIdx > -1 then
begin
if anActionData.Modifier = amNone then
Result := DoActionFinal(anActionData)
else
begin
if State = csPause then
ExecuteProc(anActionData.LabelIdx)
else
JumpToLabelIdx(anActionData.LabelIdx);
Result := True;
end;
end;
end;
end;
procedure TFURQContext.ClearButtons;
begin
f_Buttons.Clear;
end;
procedure TFURQContext.ClearText;
begin
// does nothing in base class
end;
procedure TFURQContext.ClearInventory;
begin
f_Inventory.Clear;
end;
procedure TFURQContext.ClearStack;
var
l_TempEP: TFURQExecutionPoint;
begin
if f_Stack = nil then
f_Stack := TFURQExecutionPoint.Create(nil)
else
begin
f_Stack.DropPrev;
f_Stack.ClearOperators;
end;
end;
procedure TFURQContext.ClearStateParams;
var
I: Integer;
begin
for I := 1 to cMaxStateParams do
f_StateParam[I] := '';
end;
procedure TFURQContext.ClearVariables;
begin
f_Variables.Clear;
InitSysVariables;
end;
function TFURQContext.DeleteVariable(aVarName: string): Boolean;
var
l_Idx: Integer;
begin
l_Idx := f_Variables.IndexOf(aVarName);
Result := l_Idx >= 0;
if Result then
f_Variables.Delete(l_Idx);
end;
function TFURQContext.DoActionFinal(const anActionData: IFURQActionData): Boolean;
var
l_CommonStr: string;
l_Idx: Integer;
begin
Result := False;
try
IncLocationCount(anActionData.LabelIdx);
JumpToLabelIdx(anActionData.LabelIdx);
// current_loc è previous_loc
Variables[cVarPreviousLoc] := Variables[cVarCurrentLoc];
Variables[cVarCurrentLoc] := Code.Labels[anActionData.LabelIdx];
// îáðàáàòûâàåì common-ëîêàöèþ
if Variables[cVarCommon] <> 0 then
l_CommonStr := cLocCommonPrefix + FormatNumber(EnsureReal(Variables[cVarCommon]))
else
l_CommonStr := cLocCommon;
l_Idx := Code.Labels.IndexOf(l_CommonStr);
if l_Idx <> -1 then
ExecuteProc(l_Idx);
Result := True;
finally
ClearButtons;
f_Actions.Clear;
f_MenuActions.Clear;
f_Menu.Clear;
end;
end;
procedure TFURQContext.DoLoadData(aFiler: TFURQFiler);
var
l_Num: Integer;
I: Integer;
l_Name: string;
l_Value: Variant;
begin
l_Num := aFiler.ReadInteger;
for I := 1 to l_Num do
begin
l_Name := aFiler.ReadString;
l_Value := aFiler.ReadVarValue;
Variables[l_Name] := l_Value;
end;
l_Num := aFiler.ReadInteger;
for I := 1 to l_Num do
begin
l_Name := aFiler.ReadString;
l_Value := aFiler.ReadDouble;
AddToInventory(l_Name, l_Value);
end;
f_Stack.Load(aFiler);
LoadActions(aFiler);
LoadButtons(aFiler);
end;
procedure TFURQContext.DoSaveData(aFiler: TFURQFiler);
var
I: Integer;
begin
aFiler.WriteInteger(f_Variables.Count);
for I := 0 to f_Variables.LastIndex do
begin