1 (* Copyright (C) DooM 2D:Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE a_modes.inc}
23 xparser
, xstreams
, utils
;
26 // ////////////////////////////////////////////////////////////////////////// //
33 TDynFieldList
= specialize TSimpleList
<TDynField
>;
34 TDynRecList
= specialize TSimpleList
<TDynRecord
>;
35 TDynEBSList
= specialize TSimpleList
<TDynEBS
>;
37 // this is base type for all scalars (and arrays)
41 TType
= (TBool
, TChar
, TByte
, TUByte
, TShort
, TUShort
, TInt
, TUInt
, TString
, TPoint
, TSize
, TList
, TTrigData
);
42 // TPoint: pair of Integers
43 // TSize: pair of UShorts
44 // TList: actually, array of records
45 // TTrigData: array of mMaxDim bytes, but internally a record (mRecRef)
46 // arrays of chars are pascal shortstrings (with counter in the first byte)
50 TEBS
= (TNone
, TRec
, TEnum
, TBitSet
);
57 mIVal
: Integer; // for all integer types
58 mIVal2
: Integer; // for point and size
59 mSVal
: AnsiString; // string; for byte and char arrays
60 mRVal
: TDynRecList
; // for list
61 mRecRef
: TDynRecord
; // for TEBS.TRec
62 mMaxDim
: Integer; // for byte and char arrays; <0: not an array; 0: impossible value
63 mBinOfs
: Integer; // offset in binary; <0 - none
64 mRecOfs
: Integer; // offset in record; <0 - none
65 mSepPosSize
: Boolean; // for points and sizes, use separate fields
66 mAsT
: Boolean; // for points and sizes, use separate fields, names starts with `t`
72 mBitSetUnique
: Boolean; // bitset can contain only one value
74 mDefUnparsed
: AnsiString;
75 mDefSVal
: AnsiString; // default string value
76 mDefIVal
, mDefIVal2
: Integer; // default integer values
77 mDefRecRef
: TDynRecord
;
78 mEBS
: TEBS
; // complex type type
79 mEBSTypeName
: AnsiString; // name of enum, bitset or record
80 mEBSType
: TObject
; // either TDynRecord or TDynEBS; nil means "simple type"; nil for `TTrigData` too
83 mRecRefId
: AnsiString;
88 procedure parseDef (pr
: TTextParser
);
90 procedure parseDefaultValue (); // parse `mDefUnparsed` to `mDefSVal`, `mDefIVal`, `mDefIVal2`, `mDefRecRef`
91 procedure fixDefaultValue (); // this will NOT clone `mDefRecRef`
92 function isDefaultValue (): Boolean;
95 constructor Create (const aname
: AnsiString; atype
: TType
);
96 constructor Create (pr
: TTextParser
);
97 destructor Destroy (); override;
99 class function getTypeName (t
: TType
): AnsiString;
101 function definition (): AnsiString;
102 function pasdef (): AnsiString;
104 function clone (newOwner
: TDynRecord
=nil): TDynField
;
106 procedure parseValue (pr
: TTextParser
);
107 procedure parseBinValue (st
: TStream
);
109 procedure writeTo (wr
: TTextWriter
);
110 procedure writeBinTo (st
: TStream
);
112 // won't work for lists
113 function isSimpleEqu (fld
: TDynField
): Boolean;
115 procedure setValue (const s
: AnsiString);
118 property pasname
: AnsiString read mPasName
;
119 property name
: AnsiString read mName
;
120 property baseType
: TType read mType
;
121 property defined
: Boolean read mDefined write mDefined
;
122 property internal
: Boolean read mInternal write mInternal
;
123 property ival
: Integer read mIVal
;
124 property sval
: AnsiString read mSVal
;
125 property hasDefault
: Boolean read mHasDefault
;
126 property defsval
: AnsiString read mDefSVal
;
127 property ebs
: TEBS read mEBS
;
128 property ebstype
: TObject read mEBSType
;
129 property ebstypename
: AnsiString read mEBSTypeName
; // enum/bitset name
130 property list
: TDynRecList read mRVal
; // for list
132 property x
: Integer read mIVal
;
133 property w
: Integer read mIVal
;
134 property y
: Integer read mIVal2
;
135 property h
: Integer read mIVal2
;
139 // "value" header record contains TList fields, with name equal to record type
144 mPasName
: AnsiString;
147 mFields
: TDynFieldList
;
148 mTrigTypes
: array of AnsiString; // if this is triggerdata, we'll hold list of triggers here
149 mHeader
: Boolean; // true for header record
150 mBinBlock
: Integer; // -1: none
151 mHeaderRec
: TDynRecord
; // for "value" records this is header record with data, for "type" records this is header type record
154 procedure parseDef (pr
: TTextParser
); // parse definition
156 function findByName (const aname
: AnsiString): Integer; inline;
157 function hasByName (const aname
: AnsiString): Boolean; inline;
158 function getFieldByName (const aname
: AnsiString): TDynField
; inline;
160 function getIsTrigData (): Boolean; inline;
161 function getIsForTrig (const aname
: AnsiString): Boolean; inline;
164 function findRecordByTypeId (const atypename
, aid
: AnsiString): TDynRecord
;
165 function findRecordNumByType (const atypename
: AnsiString; rc
: TDynRecord
): Integer;
166 procedure addRecordByType (const atypename
: AnsiString; rc
: TDynRecord
);
169 constructor Create ();
170 constructor Create (pr
: TTextParser
); // parse definition
171 destructor Destroy (); override;
173 function definition (): AnsiString;
174 function pasdef (): AnsiString;
176 function clone (): TDynRecord
;
178 function isSimpleEqu (rec
: TDynRecord
): Boolean;
180 procedure parseValue (pr
: TTextParser
; beginEaten
: Boolean=false);
181 procedure parseBinValue (st
: TStream
; forceData
: Boolean=false);
183 procedure writeTo (wr
: TTextWriter
; putHeader
: Boolean=true);
184 procedure writeBinTo (st
: TStream
; trigbufsz
: Integer=-1; onlyFields
: Boolean=false);
186 // find field with `TriggerType` type
187 function trigTypeField (): TDynField
;
189 // number of records of the given instance
190 function instanceCount (const typename
: AnsiString): Integer;
193 property id
: AnsiString read mId
; // for map parser
194 property pasname
: AnsiString read mPasName
;
195 property name
: AnsiString read mName
; // record name
196 property size
: Integer read mSize
; // size in bytes
197 property fields
: TDynFieldList read mFields
;
198 property has
[const aname
: AnsiString]: Boolean read hasByName
;
199 property field
[const aname
: AnsiString]: TDynField read getFieldByName
;
200 property isTrigData
: Boolean read getIsTrigData
;
201 property isForTrig
[const aname
: AnsiString]: Boolean read getIsForTrig
;
202 property headerType
: TDynRecord read mHeaderRec
;
203 property isHeader
: Boolean read mHeader
;
211 mIds
: array of AnsiString;
212 mVals
: array of Integer;
213 mMaxName
: AnsiString; // MAX field
214 mMaxVal
: Integer; // max value
217 procedure cleanup ();
219 procedure parseDef (pr
: TTextParser
); // parse definition
221 function findByName (const aname
: AnsiString): Integer; inline;
222 function hasByName (const aname
: AnsiString): Boolean; inline;
223 function getFieldByName (const aname
: AnsiString): Integer; inline;
226 constructor Create (pr
: TTextParser
); // parse definition
227 destructor Destroy (); override;
229 function definition (): AnsiString;
230 function pasdef (): AnsiString;
232 // return empty string if not found
233 function nameByValue (v
: Integer): AnsiString;
236 property name
: AnsiString read mName
; // record name
237 property isEnum
: Boolean read mIsEnum
;
238 property has
[const aname
: AnsiString]: Boolean read hasByName
;
239 property field
[const aname
: AnsiString]: Integer read getFieldByName
;
245 recTypes
: TDynRecList
; // [0] is always header
246 trigTypes
: TDynRecList
; // trigdata
247 ebsTypes
: TDynEBSList
; // enums, bitsets
250 procedure parseDef (pr
: TTextParser
);
252 function getHeaderRecType (): TDynRecord
; inline;
255 constructor Create (pr
: TTextParser
); // parses data definition
256 destructor Destroy (); override;
258 function findRecType (const aname
: AnsiString): TDynRecord
;
259 function findTrigFor (const aname
: AnsiString): TDynRecord
;
260 function findEBSType (const aname
: AnsiString): TDynEBS
;
262 function pasdef (): AnsiString;
264 // creates new header record
265 function parseMap (pr
: TTextParser
): TDynRecord
;
267 // creates new header record
268 function parseBinMap (st
: TStream
): TDynRecord
;
271 property headerType
: TDynRecord read getHeaderRecType
;
281 // ////////////////////////////////////////////////////////////////////////// //
282 function StrEqu (const a
, b
: AnsiString): Boolean; inline; begin result
:= (a
= b
); end;
285 // ////////////////////////////////////////////////////////////////////////// //
286 constructor TDynField
.Create (const aname
: AnsiString; atype
: TType
);
293 if (mType
= TType
.TList
) then mRVal
:= TDynRecList
.Create();
297 constructor TDynField
.Create (pr
: TTextParser
);
304 destructor TDynField
.Destroy ();
311 procedure TDynField
.cleanup ();
324 mSepPosSize
:= false;
326 mHasDefault
:= false;
338 mBitSetUnique
:= false;
341 if (mType
= TType
.TList
) then mRVal
:= TDynRecList
.Create();
345 function TDynField
.clone (newOwner
: TDynRecord
=nil): TDynField
;
349 result
:= TDynField
.Create(mName
, mType
);
350 result
.mOwner
:= mOwner
;
351 if (newOwner
<> nil) then result
.mOwner
:= newOwner
else result
.mOwner
:= mOwner
;
352 result
.mPasName
:= mPasName
;
353 result
.mName
:= mName
;
354 result
.mType
:= mType
;
355 result
.mIVal
:= mIVal
;
356 result
.mIVal2
:= mIVal2
;
357 result
.mSVal
:= mSVal
;
358 if (mRVal
<> nil) then
360 result
.mRVal
:= TDynRecList
.Create(mRVal
.count
);
361 for rec
in mRVal
do result
.mRVal
.append(rec
.clone());
365 if (mType
= TType
.TList
) then result
.mRVal
:= TDynRecList
.Create() else result
.mRVal
:= nil;
367 result
.mRecRef
:= mRecRef
;
368 result
.mMaxDim
:= mMaxDim
;
369 result
.mBinOfs
:= mBinOfs
;
370 result
.mRecOfs
:= mRecOfs
;
371 result
.mSepPosSize
:= mSepPosSize
;
373 result
.mDefined
:= mDefined
;
374 result
.mHasDefault
:= mHasDefault
;
375 result
.mOmitDef
:= mOmitDef
;
376 result
.mInternal
:= mInternal
;
377 result
.mNegBool
:= mNegBool
;
378 result
.mBitSetUnique
:= mBitSetUnique
;
379 result
.mDefUnparsed
:= mDefUnparsed
;
380 result
.mDefSVal
:= mDefSVal
;
381 result
.mDefIVal
:= mDefIVal
;
382 result
.mDefIVal2
:= mDefIVal2
;
383 result
.mDefRecRef
:= mDefRecRef
;
385 result
.mEBSTypeName
:= mEBSTypeName
;
386 result
.mEBSType
:= mEBSType
;
387 result
.mRecRefId
:= mRecRefId
;
391 // won't work for lists
392 function TDynField
.isSimpleEqu (fld
: TDynField
): Boolean;
394 if (fld
= nil) or (mType
<> fld
.mType
) then begin result
:= false; exit
; end;
396 TType
.TBool
: result
:= ((mIVal
<> 0) = (fld
.mIVal
<> 0));
397 TType
.TChar
: result
:= (mSVal
= fld
.mSVal
);
404 result
:= (mIVal
= fld
.mIVal
);
405 TType
.TString
: result
:= (mSVal
= fld
.mSVal
);
408 result
:= ((mIVal
= fld
.mIVal
) and (mIVal2
= fld
.mIVal2
));
409 TType
.TList
: result
:= false;
412 if (mRecRef
= nil) then begin result
:= (fld
.mRecRef
= nil); exit
; end;
413 result
:= mRecRef
.isSimpleEqu(fld
.mRecRef
);
415 else raise Exception
.Create('ketmar forgot to handle some field type');
420 procedure TDynField
.setValue (const s
: AnsiString);
424 stp
:= TStrTextParser
.Create(s
+';');
433 procedure TDynField
.parseDefaultValue ();
435 stp
: TTextParser
= nil;
437 oIVal
, oIVal2
: Integer;
441 if not mHasDefault
then
456 stp
:= TStrTextParser
.Create(mDefUnparsed
+';');
461 mDefRecRef
:= mRecRef
;
474 // default value should be parsed
475 procedure TDynField
.fixDefaultValue ();
477 if mDefined
then exit
;
478 if not mHasDefault
then
480 if mInternal
then exit
;
481 raise Exception
.Create(Format('field ''%s'' in record ''%s'' of record type ''%s'' is not set', [mName
, mOwner
.mId
, mOwner
.mName
]));
483 if (mEBS
= TEBS
.TRec
) then mRecRef
:= mDefRecRef
;
491 // default value should be parsed
492 function TDynField
.isDefaultValue (): Boolean;
494 if not mHasDefault
then begin result
:= false; exit
; end;
495 if (mEBS
= TEBS
.TRec
) then begin result
:= (mRecRef
= mDefRecRef
); exit
; end;
497 TType
.TChar
, TType
.TString
: result
:= (mSVal
= mDefSVal
);
498 TType
.TPoint
, TType
.TSize
: result
:= (mIVal
= mDefIVal2
) and (mIVal2
= mDefIVal2
);
499 TType
.TList
, TType
.TTrigData
: result
:= false; // no default values for those types
500 else result
:= (mIVal
= mDefIVal
);
505 class function TDynField
.getTypeName (t
: TType
): AnsiString;
508 TType
.TBool
: result
:= 'bool';
509 TType
.TChar
: result
:= 'char';
510 TType
.TByte
: result
:= 'byte';
511 TType
.TUByte
: result
:= 'ubyte';
512 TType
.TShort
: result
:= 'short';
513 TType
.TUShort
: result
:= 'ushort';
514 TType
.TInt
: result
:= 'int';
515 TType
.TUInt
: result
:= 'uint';
516 TType
.TString
: result
:= 'string';
517 TType
.TPoint
: result
:= 'point';
518 TType
.TSize
: result
:= 'size';
519 TType
.TList
: result
:= 'array';
520 TType
.TTrigData
: result
:= 'trigdata';
521 else raise Exception
.Create('ketmar forgot to handle some field type');
526 function TDynField
.definition (): AnsiString;
528 result
:= mPasName
+' is '+quoteStr(mName
)+' type ';
529 result
+= getTypeName(mType
);
530 if (mMaxDim
>= 0) then result
+= Format('[%d]', [mMaxDim
]);
531 if (mRecOfs
>= 0) then result
+= Format(' offset %d', [mRecOfs
]);
533 TEBS
.TNone
: begin end;
534 TEBS
.TRec
: result
+= ' '+mEBSTypeName
;
535 TEBS
.TEnum
: result
+= ' enum '+mEBSTypeName
;
536 TEBS
.TBitSet
: begin result
+= ' bitset '; if mBitSetUnique
then result
+= 'unique '; result
+= mEBSTypeName
; end;
538 if mHasDefault
and (Length(mDefUnparsed
) > 0) then result
+= ' default '+mDefUnparsed
;
541 if (mType
= TType
.TPoint
) then begin if (mAsT
) then result
+= ' as txy' else result
+= ' as xy'; end
542 else if (mType
= TType
.TSize
) then begin if (mAsT
) then result
+= ' as twh' else result
+= ' as wh'; end;
544 if mOmitDef
then result
+= ' omitdefault';
545 if mInternal
then result
+= ' internal';
549 function TDynField
.pasdef (): AnsiString;
551 result
:= mPasName
+': ';
553 TType
.TBool
: result
+= 'Boolean;';
554 TType
.TChar
: if (mMaxDim
> 0) then result
+= formatstrf('Char%d;', [mMaxDim
]) else result
+= 'Char;';
555 TType
.TByte
: result
+= 'ShortInt;';
556 TType
.TUByte
: result
+= 'Byte;';
557 TType
.TShort
: result
+= 'SmallInt;';
558 TType
.TUShort
: result
+= 'Word;';
559 TType
.TInt
: result
+= 'LongInt;';
560 TType
.TUInt
: result
+= 'LongWord;';
561 TType
.TString
: result
+= 'AnsiString;';
563 if mAsT
then result
:= 'tX, tY: Integer;'
564 else if mSepPosSize
then result
:= 'X, Y: Integer;'
565 else result
+= 'TDFPoint;';
567 if mAsT
then result
:= 'tWidth, tHeight: Word;'
568 else if mSepPosSize
then result
:= 'Width, Height: Word;'
569 else result
+= 'TSize;';
570 TType
.TList
: assert(false);
571 TType
.TTrigData
: result
+= formatstrf('Byte%d;', [mMaxDim
]);
572 else raise Exception
.Create('ketmar forgot to handle some field type');
577 procedure TDynField
.parseDef (pr
: TTextParser
);
582 fldrecname
: AnsiString;
583 fldpasname
: AnsiString;
584 asxy
, aswh
, ast
: Boolean;
593 lebs
: TDynField
.TEBS
;
613 lebs
:= TDynField
.TEBS
.TNone
;
615 fldpasname
:= pr
.expectId(); // pascal field name
618 fldname
:= pr
.expectStr();
621 fldtype
:= pr
.expectId();
624 if pr
.eatDelim('[') then
626 lmaxdim
:= pr
.expectInt();
627 if (lmaxdim
< 1) then raise Exception
.Create(Format('invalid field ''%s'' array size', [fldname
]));
631 while (pr
.tokType
<> pr
.TTSemi
) do
633 if pr
.eatId('offset') then
635 if (fldofs
>= 0) then raise Exception
.Create(Format('duplicate field ''%s'' offset', [fldname
]));
636 fldofs
:= pr
.expectInt();
637 if (fldofs
< 0) then raise Exception
.Create(Format('invalid field ''%s'' offset', [fldname
]));
641 if pr
.eatId('as') then
643 if pr
.eatId('xy') then asxy
:= true
644 else if pr
.eatId('wh') then aswh
:= true
645 else if pr
.eatId('txy') then begin asxy
:= true; ast
:= true; end
646 else if pr
.eatId('twh') then begin aswh
:= true; ast
:= true; end
647 else raise Exception
.Create(Format('invalid field ''%s'' as what?', [fldname
]));
651 if pr
.eatId('enum') then
653 lebs
:= TDynField
.TEBS
.TEnum
;
654 if (Length(fldrecname
) <> 0) then raise Exception
.Create(Format('field ''%s'' already typed as ''%s''', [fldname
, fldrecname
]));
655 fldrecname
:= pr
.expectId();
659 if pr
.eatId('bitset') then
661 lebs
:= TDynField
.TEBS
.TBitSet
;
662 if (Length(fldrecname
) <> 0) then raise Exception
.Create(Format('field ''%s'' already typed as ''%s''', [fldname
, fldrecname
]));
663 unique
:= pr
.eatId('unique');
664 fldrecname
:= pr
.expectId();
668 if pr
.eatId('default') then
670 if hasdefStr
or hasdefInt
or hasdefId
then raise Exception
.Create(Format('field ''%s'' has duplicate default', [fldname
]));
675 defstr
:= pr
.expectStr(true); // allow empty strings
680 defstr
:= pr
.expectId();
685 defint
:= pr
.expectInt();
688 raise Exception
.Create(Format('field ''%s'' has invalid default', [fldname
]));
693 if pr
.eatId('omitdefault') then
699 if pr
.eatId('internal') then
705 if (pr
.tokType
<> pr
.TTId
) then raise Exception
.Create(Format('field ''%s'' has something unexpected in definition', [fldname
]));
707 if (Length(fldrecname
) <> 0) then raise Exception
.Create(Format('field ''%s'' already typed as ''%s''', [fldname
, fldrecname
]));
708 fldrecname
:= pr
.expectId();
709 lebs
:= TDynField
.TEBS
.TRec
;
712 pr
.expectTT(pr
.TTSemi
);
716 if (fldtype
= 'bool') then mType
:= TType
.TBool
717 else if (fldtype
= 'negbool') then begin mType
:= TType
.TBool
; mNegBool
:= true; end
718 else if (fldtype
= 'char') then mType
:= TType
.TChar
719 else if (fldtype
= 'byte') then mType
:= TType
.TByte
720 else if (fldtype
= 'ubyte') then mType
:= TType
.TUByte
721 else if (fldtype
= 'short') then mType
:= TType
.TShort
722 else if (fldtype
= 'ushort') then mType
:= TType
.TUShort
723 else if (fldtype
= 'int') then mType
:= TType
.TInt
724 else if (fldtype
= 'uint') then mType
:= TType
.TUInt
725 else if (fldtype
= 'string') then mType
:= TType
.TString
726 else if (fldtype
= 'point') then mType
:= TType
.TPoint
727 else if (fldtype
= 'size') then mType
:= TType
.TSize
728 else if (fldtype
= 'trigdata') then mType
:= TType
.TTrigData
729 else raise Exception
.Create(Format('field ''%s'' has invalid type ''%s''', [fldname
, fldtype
]));
731 if (lmaxdim
> 0) and (mType
<> TType
.TChar
) and (mType
<> TType
.TTrigData
) then raise Exception
.Create(Format('field ''%s'' of type ''%s'' cannot be array', [fldname
, fldtype
]));
732 if (mType
= TType
.TTrigData
) then
734 if (lmaxdim
< 1) then raise Exception
.Create(Format('field ''%s'' of type ''%s'' cannot be array', [fldname
, fldtype
]));
735 if (Length(fldrecname
) > 0) then raise Exception
.Create(Format('field ''%s'' of type ''%s'' cannot have another type', [fldname
, fldtype
]));
736 lebs
:= TDynField
.TEBS
.TRec
;
739 if hasdefStr
then self
.mDefUnparsed
:= quoteStr(defstr
)
740 else if hasdefInt
then self
.mDefUnparsed
:= Format('%d', [defint
])
741 else if hasdefId
then self
.mDefUnparsed
:= defstr
;
743 self
.mHasDefault
:= (hasdefStr
or hasdefId
or hasdefInt
);
744 self
.mPasName
:= fldpasname
;
746 self
.mEBSTypeName
:= fldrecname
;
747 self
.mBitSetUnique
:= unique
;
748 self
.mMaxDim
:= lmaxdim
;
749 self
.mBinOfs
:= fldofs
;
750 self
.mRecOfs
:= fldofs
;
751 self
.mSepPosSize
:= (asxy
or aswh
);
753 self
.mOmitDef
:= omitdef
;
754 self
.mInternal
:= ainternal
;
758 procedure TDynField
.writeBinTo (st
: TStream
);
767 TEBS
.TNone
: begin end;
770 if (mMaxDim
>= 0) then
772 // this must be triggerdata
773 if (mType
<> TType
.TTrigData
) then
775 raise Exception
.Create(Format('record reference type ''%s'' in field ''%s'' cannot be written', [mEBSTypeName
, mName
]));
778 GetMem(buf
, mMaxDim
);
779 if (buf
= nil) then raise Exception
.Create(Format('record reference type ''%s'' in field ''%s'' cannot be written', [mEBSTypeName
, mName
]));
781 FillChar(buf
^, mMaxDim
, 0);
782 if (mRecRef
<> nil) then
784 ws
:= TSFSMemoryChunkStream
.Create(buf
, mMaxDim
);
785 mRecRef
.writeBinTo(ws
, mMaxDim
); // as trigdata
787 st
.WriteBuffer(buf
^, mMaxDim
);
790 if (buf
<> nil) then FreeMem(buf
);
796 TType
.TByte
: maxv
:= 127;
797 TType
.TUByte
: maxv
:= 254;
798 TType
.TShort
: maxv
:= 32767;
799 TType
.TUShort
: maxv
:= 65534;
800 TType
.TInt
: maxv
:= $7fffffff;
801 TType
.TUInt
: maxv
:= $7fffffff;
802 else raise Exception
.Create(Format('record reference type ''%s'' in field ''%s'' cannot be written', [mEBSTypeName
, mName
]));
804 // find record number
805 if (mRecRef
<> nil) then
807 f
:= mOwner
.findRecordNumByType(mEBSTypeName
, mRecRef
);
808 if (f
< 0) then raise Exception
.Create(Format('record reference type ''%s'' in field ''%s'' not found in record list', [mEBSTypeName
, mName
]));
809 if (f
> maxv
) then raise Exception
.Create(Format('record reference type ''%s'' in field ''%s'' has too big index', [mEBSTypeName
, mName
]));
816 TType
.TByte
, TType
.TUByte
: writeInt(st
, Byte(f
));
817 TType
.TShort
, TType
.TUShort
: writeInt(st
, SmallInt(f
));
818 TType
.TInt
, TType
.TUInt
: writeInt(st
, LongWord(f
));
819 else raise Exception
.Create(Format('record reference type ''%s'' in field ''%s'' cannot be written', [mEBSTypeName
, mName
]));
823 TEBS
.TEnum
: begin end;
824 TEBS
.TBitSet
: begin end;
825 else raise Exception
.Create('ketmar forgot to handle some EBS type');
833 if (mIVal
<> 0) then writeInt(st
, Byte(1)) else writeInt(st
, Byte(0));
837 if (mIVal
= 0) then writeInt(st
, Byte(1)) else writeInt(st
, Byte(0));
843 if (mMaxDim
= 0) then raise Exception
.Create(Format('invalid string size definition for field ''%s''', [mName
]));
844 if (mMaxDim
< 0) then
846 if (Length(mSVal
) <> 1) then raise Exception
.Create(Format('invalid string size definition for field ''%s''', [mName
]));
847 writeInt(st
, Byte(mSVal
[1]));
851 if (Length(mSVal
) > mMaxDim
) then raise Exception
.Create(Format('invalid string size definition for field ''%s''', [mName
]));
853 if (Length(s
) > 0) then st
.WriteBuffer(PChar(s
)^, Length(s
));
854 for f
:= Length(s
) to mMaxDim
do writeInt(st
, Byte(0));
861 // triggerdata array was processed earlier
862 if (mMaxDim
>= 0) then Exception
.Create(Format('byte array in field ''%s'' cannot be written', [mName
]));
863 writeInt(st
, Byte(mIVal
));
869 if (mMaxDim
>= 0) then raise Exception
.Create(Format('short array in field ''%s'' cannot be written', [mName
]));
870 writeInt(st
, Word(mIVal
));
876 if (mMaxDim
>= 0) then raise Exception
.Create(Format('int array in field ''%s'' cannot be written', [mName
]));
877 writeInt(st
, LongWord(mIVal
));
882 raise Exception
.Create(Format('cannot write string field ''%s''', [mName
]));
886 if (mMaxDim
>= 0) then raise Exception
.Create(Format('pos/size array in field ''%s'' cannot be written', [mName
]));
887 writeInt(st
, LongInt(mIVal
));
888 writeInt(st
, LongInt(mIVal2
));
893 if (mMaxDim
>= 0) then raise Exception
.Create(Format('pos/size array in field ''%s'' cannot be written', [mName
]));
894 writeInt(st
, Word(mIVal
));
895 writeInt(st
, Word(mIVal2
));
908 else raise Exception
.Create('ketmar forgot to handle some field type');
913 procedure TDynField
.writeTo (wr
: TTextWriter
);
917 first
, found
: Boolean;
922 TEBS
.TNone
: begin end;
925 if (mRecRef
= nil) then
927 if (mType
= TType
.TTrigData
) then wr
.put('{}'#10) else wr
.put('null;'#10);
929 else if (Length(mRecRef
.mId
) = 0) then
931 mRecRef
.writeTo(wr
, false); // only data, no header
942 //def := mOwner.mOwner;
943 //es := def.findEBSType(mEBSTypeName);
945 if (mEBSType
<> nil) and (mEBSType
is TDynEBS
) then es
:= (mEBSType
as TDynEBS
);
946 if (es
= nil) or (not es
.mIsEnum
) then raise Exception
.Create(Format('record enum type ''%s'' for field ''%s'' not found', [mEBSTypeName
, mName
]));
947 for f
:= 0 to High(es
.mVals
) do
949 if (es
.mVals
[f
] = mIVal
) then
956 raise Exception
.Create(Format('value %d in record enum type ''%s'' for field ''%s'' not found', [mIVal
, mEBSTypeName
, mName
]));
960 //def := mOwner.mOwner;
961 //es := def.findEBSType(mEBSTypeName);
963 if (mEBSType
<> nil) and (mEBSType
is TDynEBS
) then es
:= (mEBSType
as TDynEBS
);
964 if (es
= nil) or es
.mIsEnum
then raise Exception
.Create(Format('record bitset type ''%s'' for field ''%s'' not found', [mEBSTypeName
, mName
]));
968 for f
:= 0 to High(es
.mVals
) do
970 if (es
.mVals
[f
] = 0) then
977 raise Exception
.Create(Format('value %d in record bitset type ''%s'' for field ''%s'' not found', [0, mEBSTypeName
, mName
]));
984 if ((mIVal
and mask
) <> 0) then
987 for f
:= 0 to High(es
.mVals
) do
989 if (es
.mVals
[f
] = mask
) then
991 if not first
then wr
.put('+') else first
:= false;
997 if not found
then raise Exception
.Create(Format('value %d in record bitset type ''%s'' for field ''%s'' not found', [mask
, mEBSTypeName
, mName
]));
1004 else raise Exception
.Create('ketmar forgot to handle some EBS type');
1010 if (mIVal
= 0) then wr
.put('false;'#10) else wr
.put('true;'#10);
1015 if (mMaxDim
= 0) then raise Exception
.Create(Format('invalid string size definition for field ''%s''', [mName
]));
1016 wr
.put(quoteStr(mSVal
));
1027 wr
.put('%d;'#10, [mIVal
]);
1032 wr
.put(quoteStr(mSVal
));
1039 wr
.put('(%d %d);'#10, [mIVal
, mIVal2
]);
1052 else raise Exception
.Create('ketmar forgot to handle some field type');
1054 raise Exception
.Create(Format('cannot parse field ''%s'' yet', [mName
]));
1057 procedure TDynField
.parseBinValue (st
: TStream
);
1059 rec
, rc
: TDynRecord
;
1067 TEBS
.TNone
: begin end;
1070 // this must be triggerdata
1071 if (mType
= TType
.TTrigData
) then
1073 assert(mMaxDim
> 0);
1075 // find trigger definition
1076 tfld
:= rec
.trigTypeField();
1077 if (tfld
= nil) then raise Exception
.Create(Format('triggerdata value for field ''%s'' in record ''%s'' without TriggerType field', [mName
, rec
.mName
]));
1078 rc
:= mOwner
.mOwner
.findTrigFor(tfld
.mSVal
); // find in mapdef
1079 if (rc
= nil) then raise Exception
.Create(Format('triggerdata definition for field ''%s'' in record ''%s'' with type ''%s'' not found', [mName
, rec
.mName
, tfld
.mSVal
]));
1081 rc
.mHeaderRec
:= mOwner
.mHeaderRec
;
1083 rc
.parseBinValue(st
, true);
1094 // not a trigger data
1096 TType
.TByte
: f
:= readShortInt(st
);
1097 TType
.TUByte
: f
:= readByte(st
);
1098 TType
.TShort
: f
:= readSmallInt(st
);
1099 TType
.TUShort
: f
:= readWord(st
);
1100 TType
.TInt
: f
:= readLongInt(st
);
1101 TType
.TUInt
: f
:= readLongWord(st
);
1102 else raise Exception
.Create(Format('invalid non-numeric type ''%s'' for field ''%s'' of record ''%s''', [getTypeName(mType
), mName
, mEBSTypeName
]));
1104 if (f
< 0) then mRecRefId
:= '' else mRecRefId
:= Format('%s%d', [mEBSTypeName
, f
]);
1112 assert(mMaxDim
< 0);
1114 TType
.TByte
: f
:= readShortInt(st
);
1115 TType
.TUByte
: f
:= readByte(st
);
1116 TType
.TShort
: f
:= readSmallInt(st
);
1117 TType
.TUShort
: f
:= readWord(st
);
1118 TType
.TInt
: f
:= readLongInt(st
);
1119 TType
.TUInt
: f
:= readLongWord(st
);
1120 else raise Exception
.Create(Format('invalid non-numeric type ''%s'' for field ''%s'' of record ''%s''', [getTypeName(mType
), mName
, mEBSTypeName
]));
1123 if (mEBSType
<> nil) and (mEBSType
is TDynEBS
) then es
:= (mEBSType
as TDynEBS
);
1124 if (es
= nil) or (es
.mIsEnum
<> (mEBS
= TEBS
.TEnum
)) then raise Exception
.Create(Format('record enum type ''%s'' for field ''%s'' not found', [mEBSTypeName
, mName
]));
1126 // build enum/bitfield values
1127 if (mEBS
= TEBS
.TEnum
) then
1129 mSVal
:= es
.nameByValue(mIVal
);
1130 if (Length(mSVal
) = 0) then raise Exception
.Create(Format('record enum type ''%s'' for field ''%s'' has invalid value %d', [mEBSTypeName
, mName
, mIVal
]));
1134 // special for 'none'
1137 mSVal
:= es
.nameByValue(mIVal
);
1138 if (Length(mSVal
) = 0) then raise Exception
.Create(Format('record bitset type ''%s'' for field ''%s'' has invalid value %d', [mEBSTypeName
, mName
, mIVal
]));
1144 while (mask
<> 0) do
1146 if ((mIVal
and mask
) <> 0) then
1148 s
:= es
.nameByValue(mask
);
1149 if (Length(s
) = 0) then raise Exception
.Create(Format('record bitset type ''%s'' for field ''%s'' has invalid value %d', [mEBSTypeName
, mName
, mask
]));
1150 if (Length(mSVal
) <> 0) then mSVal
+= '+';
1157 //writeln('ebs <', es.mName, '>: ', mSVal);
1161 else raise Exception
.Create('ketmar forgot to handle some EBS type');
1168 if (f
<> 0) then f
:= 1;
1169 if mNegBool
then f
:= 1-f
;
1176 if (mMaxDim
< 0) then
1178 mIVal
:= readByte(st
);
1183 GetMem(tdata
, mMaxDim
);
1185 st
.ReadBuffer(tdata
^, mMaxDim
);
1187 while (f
< mMaxDim
) and (tdata
[f
] <> 0) do Inc(f
);
1190 SetLength(mSVal
, f
);
1191 Move(tdata
^, PChar(mSVal
)^, f
);
1192 mSVal
:= win2utf(mSVal
);
1201 TType
.TByte
: begin mIVal
:= readShortInt(st
); mDefined
:= true; exit
; end;
1202 TType
.TUByte
: begin mIVal
:= readByte(st
); mDefined
:= true; exit
; end;
1203 TType
.TShort
: begin mIVal
:= readSmallInt(st
); mDefined
:= true; exit
; end;
1204 TType
.TUShort
: begin mIVal
:= readWord(st
); mDefined
:= true; exit
; end;
1205 TType
.TInt
: begin mIVal
:= readLongInt(st
); mDefined
:= true; exit
; end;
1206 TType
.TUInt
: begin mIVal
:= readLongWord(st
); mDefined
:= true; exit
; end;
1209 raise Exception
.Create('cannot read strings from binaries yet');
1214 mIVal
:= readLongInt(st
);
1215 mIVal2
:= readLongInt(st
);
1221 mIVal
:= readWord(st
);
1222 mIVal2
:= readWord(st
);
1236 else raise Exception
.Create('ketmar forgot to handle some field type');
1238 raise Exception
.Create(Format('cannot parse field ''%s'' yet', [mName
]));
1242 procedure TDynField
.parseValue (pr
: TTextParser
);
1244 procedure parseInt (min
, max
: Integer);
1246 mIVal
:= pr
.expectInt();
1247 if (mIVal
< min
) or (mIVal
> max
) then raise Exception
.Create(Format('invalid %s value for field ''%s''', [getTypeName(mType
), mName
]));
1252 rec
, rc
: TDynRecord
;
1258 // if this field should contain struct, convert type and parse struct
1260 TEBS
.TNone
: begin end;
1263 // ugly hack. sorry.
1264 if (mType
= TType
.TTrigData
) then
1266 pr
.expectTT(pr
.TTBegin
);
1267 if (pr
.tokType
= pr
.TTEnd
) then
1271 pr
.expectTT(pr
.TTEnd
);
1276 // find trigger definition
1277 tfld
:= rec
.trigTypeField();
1278 if (tfld
= nil) then raise Exception
.Create(Format('triggerdata value for field ''%s'' in record ''%s'' without ''type'' field', [mName
, rec
.mName
]));
1279 rc
:= mOwner
.mOwner
.findTrigFor(tfld
.mSVal
); // find in mapdef
1280 if (rc
= nil) then raise Exception
.Create(Format('triggerdata definition for field ''%s'' in record ''%s'' with type ''%s'' not found', [mName
, rec
.mName
, tfld
.mSVal
]));
1282 rc
.mHeaderRec
:= mOwner
.mHeaderRec
;
1283 //writeln(rc.definition);
1285 rc
.parseValue(pr
, true);
1293 pr
.eatTT(pr
.TTSemi
); // hack: allow (but don't require) semicolon after inline records
1296 // other record types
1297 if (pr
.tokType
= pr
.TTId
) then
1299 if pr
.eatId('null') then
1305 rec
:= mOwner
.findRecordByTypeId(mEBSTypeName
, pr
.tokStr
);
1306 if (rec
= nil) then raise Exception
.Create(Format('record ''%s'' (%s) value for field ''%s'' not found', [pr
.tokStr
, mEBSTypeName
, mName
]));
1311 pr
.expectTT(pr
.TTSemi
);
1314 else if (pr
.tokType
= pr
.TTBegin
) then
1316 //rec := mOwner.mOwner.findRecType(mEBSTypeName); // find in mapdef
1318 if (mEBSType
<> nil) and (mEBSType
is TDynRecord
) then rec
:= (mEBSType
as TDynRecord
);
1319 if (rec
= nil) then raise Exception
.Create(Format('record type ''%s'' for field ''%s'' not found', [mEBSTypeName
, mName
]));
1321 rc
.mHeaderRec
:= mOwner
.mHeaderRec
;
1325 mOwner
.addRecordByType(mEBSTypeName
, rc
);
1326 pr
.eatTT(pr
.TTSemi
); // hack: allow (but don't require) semicolon after inline records
1329 pr
.expectTT(pr
.TTBegin
);
1333 //es := mOwner.mOwner.findEBSType(mEBSTypeName); // find in mapdef
1335 if (mEBSType
<> nil) and (mEBSType
is TDynEBS
) then es
:= (mEBSType
as TDynEBS
);
1336 if (es
= nil) or (not es
.mIsEnum
) then raise Exception
.Create(Format('record enum type ''%s'' for field ''%s'' not found', [mEBSTypeName
, mName
]));
1337 tk
:= pr
.expectId();
1338 if not es
.has
[tk
] then raise Exception
.Create(Format('record enum value ''%s'' of type ''%s'' for field ''%s'' not found', [tk
, mEBSTypeName
, mName
]));
1339 mIVal
:= es
.field
[tk
];
1341 //writeln('ENUM ', mEBSName, '; element <', mSVal, '> with value ', mIVal);
1343 pr
.expectTT(pr
.TTSemi
);
1348 //es := mOwner.mOwner.findEBSType(mEBSTypeName); // find in mapdef
1350 if (mEBSType
<> nil) and (mEBSType
is TDynEBS
) then es
:= (mEBSType
as TDynEBS
);
1351 if (es
= nil) or es
.mIsEnum
then raise Exception
.Create(Format('record bitset type ''%s'' for field ''%s'' not found', [mEBSTypeName
, mName
]));
1355 tk
:= pr
.expectId();
1356 if not es
.has
[tk
] then raise Exception
.Create(Format('record bitset value ''%s'' of type ''%s'' for field ''%s'' not found', [tk
, mEBSTypeName
, mName
]));
1357 mIVal
:= mIVal
or es
.field
[tk
];
1359 if (pr
.tokType
<> pr
.TTDelim
) or ((pr
.tokChar
<> '|') and (pr
.tokChar
<> '+')) then break
;
1360 if mBitSetUnique
then raise Exception
.Create(Format('record bitset of type ''%s'' for field ''%s'' expects only one value', [tk
, mEBSTypeName
, mName
]));
1361 //pr.expectDelim('|');
1362 pr
.skipToken(); // plus or pipe
1365 pr
.expectTT(pr
.TTSemi
);
1368 else raise Exception
.Create('ketmar forgot to handle some EBS type');
1374 if pr
.eatId('true') or pr
.eatId('tan') or pr
.eatId('yes') then mIVal
:= 1
1375 else if pr
.eatId('false') or pr
.eatId('ona') or pr
.eatId('no') then mIVal
:= 0
1376 else raise Exception
.Create(Format('invalid bool value for field ''%s''', [mName
]));
1378 pr
.expectTT(pr
.TTSemi
);
1383 if (mMaxDim
= 0) then raise Exception
.Create(Format('invalid string size definition for field ''%s''', [mName
]));
1384 mSVal
:= pr
.expectStr(true);
1385 if (mMaxDim
< 0) then
1388 if (Length(mSVal
) <> 1) then raise Exception
.Create(Format('invalid string size for field ''%s''', [mName
]));
1389 mIVal
:= Integer(mSVal
[1]);
1395 if (Length(mSVal
) > mMaxDim
) then raise Exception
.Create(Format('invalid string size for field ''%s''', [mName
]));
1398 pr
.expectTT(pr
.TTSemi
);
1403 parseInt(-128, 127);
1404 pr
.expectTT(pr
.TTSemi
);
1410 pr
.expectTT(pr
.TTSemi
);
1415 parseInt(-32768, 32768);
1416 pr
.expectTT(pr
.TTSemi
);
1422 pr
.expectTT(pr
.TTSemi
);
1427 parseInt(Integer($80000000), $7fffffff);
1428 pr
.expectTT(pr
.TTSemi
);
1433 parseInt(0, $7fffffff); //FIXME
1434 pr
.expectTT(pr
.TTSemi
);
1439 mSVal
:= pr
.expectStr(true);
1441 pr
.expectTT(pr
.TTSemi
);
1447 if pr
.eatDelim('[') then edim
:= ']' else begin pr
.expectDelim('('); edim
:= ')'; end;
1448 mIVal
:= pr
.expectInt();
1449 if (mType
= TType
.TSize
) then
1451 if (mIVal
< 0) or (mIVal
> 32767) then raise Exception
.Create(Format('invalid %s value for field ''%s''', [getTypeName(mType
), mName
]));
1453 mIVal2
:= pr
.expectInt();
1454 if (mType
= TType
.TSize
) then
1456 if (mIVal2
< 0) or (mIVal2
> 32767) then raise Exception
.Create(Format('invalid %s value for field ''%s''', [getTypeName(mType
), mName
]));
1459 pr
.expectDelim(edim
);
1460 pr
.expectTT(pr
.TTSemi
);
1473 else raise Exception
.Create('ketmar forgot to handle some field type');
1475 raise Exception
.Create(Format('cannot parse field ''%s'' yet', [mName
]));
1479 // ////////////////////////////////////////////////////////////////////////// //
1480 constructor TDynRecord
.Create (pr
: TTextParser
);
1482 if (pr
= nil) then raise Exception
.Create('cannot create record type without type definition');
1486 mFields
:= TDynFieldList
.Create();
1495 constructor TDynRecord
.Create ();
1499 mFields
:= TDynFieldList
.Create();
1506 destructor TDynRecord
.Destroy ();
1517 function TDynRecord
.findByName (const aname
: AnsiString): Integer; inline;
1520 while (result
< mFields
.count
) do
1522 if StrEqu(aname
, mFields
[result
].mName
) then exit
;
1529 function TDynRecord
.hasByName (const aname
: AnsiString): Boolean; inline;
1531 result
:= (findByName(aname
) >= 0);
1535 function TDynRecord
.getFieldByName (const aname
: AnsiString): TDynField
; inline;
1539 f
:= findByName(aname
);
1540 if (f
>= 0) then result
:= mFields
[f
] else result
:= nil;
1544 function TDynRecord
.getIsTrigData (): Boolean; inline;
1546 result
:= (Length(mTrigTypes
) > 0);
1550 function TDynRecord
.getIsForTrig (const aname
: AnsiString): Boolean; inline;
1555 for f
:= 0 to High(mTrigTypes
) do if StrEqu(mTrigTypes
[f
], aname
) then exit
;
1560 function TDynRecord
.clone (): TDynRecord
;
1565 result
:= TDynRecord
.Create();
1566 result
.mOwner
:= mOwner
;
1568 result
.mPasName
:= mPasName
;
1569 result
.mName
:= mName
;
1570 result
.mSize
:= mSize
;
1571 if (mFields
.count
> 0) then
1573 result
.mFields
.capacity
:= mFields
.count
;
1574 for fld
in mFields
do result
.mFields
.append(fld
.clone(result
));
1576 SetLength(result
.mTrigTypes
, Length(mTrigTypes
));
1577 for f
:= 0 to High(mTrigTypes
) do result
.mTrigTypes
[f
] := mTrigTypes
[f
];
1578 result
.mHeader
:= mHeader
;
1579 result
.mBinBlock
:= mBinBlock
;
1580 result
.mHeaderRec
:= mHeaderRec
;
1584 function TDynRecord
.findRecordByTypeId (const atypename
, aid
: AnsiString): TDynRecord
;
1590 if (Length(aid
) = 0) then exit
;
1592 fld
:= mHeaderRec
.field
[atypename
];
1593 if (fld
= nil) then exit
;
1594 if (fld
.mType
<> fld
.TType
.TList
) then raise Exception
.Create(Format('cannot get record of type ''%s'' due to name conflict with ordinary field', [atypename
]));
1596 if (fld
.mRVal
<> nil) then
1598 for rec
in fld
.mRVal
do
1600 if StrEqu(rec
.mId
, aid
) then begin result
:= rec
; exit
; end;
1607 function TDynRecord
.findRecordNumByType (const atypename
: AnsiString; rc
: TDynRecord
): Integer;
1614 fld
:= mHeaderRec
.field
[atypename
];
1615 if (fld
= nil) then exit
;
1616 if (fld
.mType
<> fld
.TType
.TList
) then raise Exception
.Create(Format('cannot get record of type ''%s'' due to name conflict with ordinary field', [atypename
]));
1618 if (fld
.mRVal
<> nil) then
1620 for f
:= 0 to fld
.mRVal
.count
-1 do
1622 if (fld
.mRVal
[f
] = rc
) then begin result
:= f
; exit
; end;
1629 procedure TDynRecord
.addRecordByType (const atypename
: AnsiString; rc
: TDynRecord
);
1634 fld
:= mHeaderRec
.field
[atypename
];
1638 fld
:= TDynField
.Create(atypename
, TDynField
.TType
.TList
);
1639 fld
.mOwner
:= mHeaderRec
;
1640 mHeaderRec
.mFields
.append(fld
);
1642 if (fld
.mType
<> fld
.TType
.TList
) then raise Exception
.Create(Format('cannot append record of type ''%s'' due to name conflict with ordinary field', [atypename
]));
1644 if (fld
.mRVal
= nil) then fld
.mRVal
:= TDynRecList
.Create();
1645 fld
.mRVal
.append(rc
);
1649 function TDynRecord
.isSimpleEqu (rec
: TDynRecord
): Boolean;
1653 if (rec
= nil) then begin result
:= false; exit
; end; // self.mRecRef can't be `nil` here
1654 if (rec
= self
) then begin result
:= true; exit
; end;
1655 if (mFields
.count
<> rec
.mFields
.count
) then begin result
:= false; exit
; end;
1657 for f
:= 0 to mFields
.count
-1 do
1659 if not mFields
[f
].isSimpleEqu(rec
.mFields
[f
]) then exit
;
1665 function TDynRecord
.trigTypeField (): TDynField
;
1670 for fld
in mFields
do
1672 if (fld
.mEBS
<> TDynField
.TEBS
.TEnum
) then continue
;
1673 if not (fld
.mEBSType
is TDynEBS
) then continue
;
1674 es
:= (fld
.mEBSType
as TDynEBS
);
1676 if StrEqu(es
.mName
, 'TriggerType') then begin result
:= fld
; exit
; end;
1682 // number of records of the given instance
1683 function TDynRecord
.instanceCount (const typename
: AnsiString): Integer;
1688 fld
:= field
[typename
];
1689 if (fld
<> nil) and (fld
.mType
= fld
.TType
.TList
) then result
:= fld
.mRVal
.count
;
1693 procedure TDynRecord
.parseDef (pr
: TTextParser
);
1698 if pr
.eatId('TriggerData') then
1701 if pr
.eatDelim('(') then
1705 while pr
.eatTT(pr
.TTComma
) do begin end;
1706 if pr
.eatDelim(')') then break
;
1707 tdn
:= pr
.expectId();
1708 if isForTrig
[tdn
] then raise Exception
.Create(Format('duplicate trigdata ''%s'' trigtype ''%s''', [mName
, tdn
]));
1709 SetLength(mTrigTypes
, Length(mTrigTypes
)+1);
1710 mTrigTypes
[High(mTrigTypes
)] := tdn
;
1715 tdn
:= pr
.expectId();
1716 SetLength(mTrigTypes
, 1);
1717 mTrigTypes
[0] := tdn
;
1719 mName
:= 'TriggerData';
1723 mPasName
:= pr
.expectId(); // pascal record name
1725 mName
:= pr
.expectStr();
1726 while (pr
.tokType
<> pr
.TTBegin
) do
1728 if pr
.eatId('header') then begin mHeader
:= true; continue
; end;
1729 if pr
.eatId('size') then
1731 if (mSize
> 0) then raise Exception
.Create(Format('duplicate `size` in record ''%s''', [mName
]));
1732 mSize
:= pr
.expectInt();
1733 if (mSize
< 1) then raise Exception
.Create(Format('invalid record ''%s'' size: %d', [mName
, mSize
]));
1734 pr
.expectId('bytes');
1737 if pr
.eatId('binblock') then
1739 if (mBinBlock
>= 0) then raise Exception
.Create(Format('duplicate `binblock` in record ''%s''', [mName
]));
1740 mBinBlock
:= pr
.expectInt();
1741 if (mBinBlock
< 1) then raise Exception
.Create(Format('invalid record ''%s'' binblock: %d', [mName
, mBinBlock
]));
1747 pr
.expectTT(pr
.TTBegin
);
1749 while (pr
.tokType
<> pr
.TTEnd
) do
1751 fld
:= TDynField
.Create(pr
);
1752 if hasByName(fld
.name
) then begin fld
.Free(); raise Exception
.Create(Format('duplicate field ''%s''', [fld
.name
])); end;
1755 mFields
.append(fld
);
1758 pr
.expectTT(pr
.TTEnd
);
1762 function TDynRecord
.pasdef (): AnsiString;
1774 result
:= ' '+mPasName
+' = packed record'#10;
1776 for fld
in mFields
do
1778 if fld
.mInternal
then continue
;
1779 if (fld
.mBinOfs
< 0) then continue
;
1780 result
+= ' '+fld
.pasdef
+#10;
1782 result
+= ' end;'#10;
1786 function TDynRecord
.definition (): AnsiString;
1793 result
:= 'TriggerData for ';
1794 if (Length(mTrigTypes
) > 1) then
1797 for f
:= 0 to High(mTrigTypes
) do
1799 if (f
<> 0) then result
+= ', ';
1800 result
+= mTrigTypes
[f
];
1806 result
+= mTrigTypes
[0];
1812 result
:= mPasName
+' is '+quoteStr(mName
);
1813 if (mSize
>= 0) then result
+= Format(' size %d bytes', [mSize
]);
1814 if mHeader
then result
+= ' header';
1817 for f
:= 0 to mFields
.count
-1 do
1820 result
+= mFields
[f
].definition
;
1827 procedure TDynRecord
.parseBinValue (st
: TStream
; forceData
: Boolean=false);
1833 loaded
: array[0..255] of Boolean;
1834 rec
, rect
: TDynRecord
;
1837 mst
: TSFSMemoryChunkStream
= nil;
1839 procedure linkNames (rec
: TDynRecord
);
1844 //writeln('*** rec: ', rec.mName, '.', rec.mId, ' (', rec.mFields.count, ')');
1845 for fld
in rec
.mFields
do
1847 if (fld
.mType
= TDynField
.TType
.TTrigData
) then
1849 if (fld
.mRecRef
<> nil) then linkNames(fld
.mRecRef
);
1852 if (Length(fld
.mRecRefId
) = 0) then continue
;
1853 assert(fld
.mEBSType
<> nil);
1854 rt
:= findRecordByTypeId(fld
.mEBSTypeName
, fld
.mRecRefId
);
1857 e_LogWritefln('record of type ''%s'' with id ''%s'' links to inexistant record of type ''%s'' with id ''%s''', [rec
.mName
, rec
.mId
, fld
.mEBSTypeName
, fld
.mRecRefId
], MSG_WARNING
);
1858 //raise Exception.Create(Format('record of type ''%s'' with id ''%s'' links to inexistant record of type ''%s'' with id ''%s''', [rec.mName, rec.mId, fld.mEBSTypeName, fld.mRecRefId]));
1860 //writeln(' ', rec.mName, '.', rec.mId, ':', fld.mName, ' -> ', rt.mName, '.', rt.mId, ' (', fld.mEBSTypeName, '.', fld.mRecRefId, ')');
1861 fld
.mRecRefId
:= '';
1863 fld
.mDefined
:= true;
1865 for fld
in rec
.mFields
do
1867 //writeln(' ', fld.mName);
1868 fld
.fixDefaultValue(); // just in case
1873 for f
:= 0 to High(loaded
) do loaded
[f
] := false;
1874 mst
:= TSFSMemoryChunkStream
.Create(nil, 0);
1876 if mHeader
and not forceData
then
1878 // parse map file as sequence of blocks
1880 st
.ReadBuffer(sign
[1], 4);
1881 if (sign
<> 'MAP'#1) then raise Exception
.Create('invalid binary map signature');
1883 while (st
.position
< st
.size
) do
1885 btype
:= readByte(st
);
1886 if (btype
= 0) then break
; // no more blocks
1887 readLongWord(st
); // reserved
1888 bsize
:= readLongInt(st
);
1889 {$IF DEFINED(D2D_XDYN_DEBUG)}writeln('btype=', btype
, '; bsize=', bsize
);{$ENDIF}
1890 if (bsize
< 0) or (bsize
> $1fffffff) then raise Exception
.Create(Format('block of type %d has invalid size %d', [btype
, bsize
]));
1891 if loaded
[btype
] then raise Exception
.Create(Format('block of type %d already loaded', [btype
]));
1892 loaded
[btype
] := true;
1893 // find record type for this block
1895 for rec
in mOwner
.recTypes
do if (rec
.mBinBlock
= btype
) then begin rect
:= rec
; break
; end;
1896 if (rect
= nil) then raise Exception
.Create(Format('block of type %d has no corresponding record', [btype
]));
1897 //writeln('found type ''', rec.mName, ''' for block type ', btype);
1898 if (rec
.mSize
= 0) or ((bsize
mod rec
.mSize
) <> 0) then raise Exception
.Create(Format('block of type %d has invalid number of records', [btype
]));
1900 if (rect
.mHeader
) then
1902 if (bsize
<> mSize
) then raise Exception
.Create(Format('header block of type %d has invalid number of records', [btype
]));
1904 st
.ReadBuffer(buf
^, bsize
);
1905 mst
.setup(buf
, mSize
);
1906 parseBinValue(mst
, true); // force parsing data
1910 // create list for this type
1911 fld
:= TDynField
.Create(rec
.mName
, TDynField
.TType
.TList
);
1913 mFields
.append(fld
);
1917 st
.ReadBuffer(buf
^, bsize
);
1918 for f
:= 0 to (bsize
div rec
.mSize
)-1 do
1920 mst
.setup(buf
+f
*rec
.mSize
, rec
.mSize
);
1921 rec
:= rect
.clone();
1922 rec
.mHeaderRec
:= self
;
1923 rec
.parseBinValue(mst
);
1924 rec
.mId
:= Format('%s%d', [rec
.mName
, f
]);
1925 fld
.mRVal
.append(rec
);
1926 //writeln('parsed ''', rec.mId, '''...');
1932 //st.position := st.position+bsize;
1935 for fld
in mFields
do
1937 if (fld
.mType
<> TDynField
.TType
.TList
) then continue
;
1938 for rec
in fld
.mRVal
do linkNames(rec
);
1944 if StrEqu(mName
, 'TriggerData') then mSize
:= Integer(st
.size
-st
.position
);
1945 if (mSize
< 1) then raise Exception
.Create(Format('cannot read record of type ''%s'' with unknown size', [mName
]));
1947 st
.ReadBuffer(buf
^, mSize
);
1948 for fld
in mFields
do
1950 if fld
.mInternal
then continue
;
1951 if (fld
.mBinOfs
< 0) then continue
;
1952 if (fld
.mBinOfs
>= st
.size
) then raise Exception
.Create(Format('record of type ''%s'' has invalid field ''%s''', [fld
.mName
]));
1953 mst
.setup(buf
+fld
.mBinOfs
, mSize
-fld
.mBinOfs
);
1954 //writeln('parsing ''', mName, '.', fld.mName, '''...');
1955 fld
.parseBinValue(mst
);
1959 if (buf
<> nil) then FreeMem(buf
);
1964 procedure TDynRecord
.writeBinTo (st
: TStream
; trigbufsz
: Integer=-1; onlyFields
: Boolean=false);
1967 rec
, rv
: TDynRecord
;
1970 blk
, blkmax
: Integer;
1975 if (trigbufsz
< 0) then
1977 if (mBinBlock
< 1) then raise Exception
.Create('cannot write binary record without block number');
1978 if (mSize
< 1) then raise Exception
.Create('cannot write binary record without size');
1987 FillChar(buf
^, bufsz
, 0);
1988 ws
:= TSFSMemoryChunkStream
.Create(buf
, bufsz
);
1990 // write normal fields
1991 for fld
in mFields
do
1994 if (fld
.mType
= fld
.TType
.TList
) then continue
; // later
1995 if fld
.mInternal
then continue
;
1996 if (fld
.mBinOfs
< 0) then continue
;
1997 if (fld
.mBinOfs
>= bufsz
) then raise Exception
.Create('binary value offset is outside of the buffer');
1998 TSFSMemoryChunkStream(ws
).setup(buf
+fld
.mBinOfs
, bufsz
-fld
.mBinOfs
);
1999 //writeln('writing field <', fld.mName, '>');
2003 // write block with normal fields
2004 if mHeader
and not onlyFields
then
2006 //writeln('writing header...');
2007 // signature and version
2008 writeIntBE(st
, LongWord($4D415001));
2009 writeInt(st
, Byte(mBinBlock
)); // type
2010 writeInt(st
, LongWord(0)); // reserved
2011 writeInt(st
, LongWord(bufsz
)); // size
2013 st
.WriteBuffer(buf
^, bufsz
);
2015 ws
.Free(); ws
:= nil;
2016 FreeMem(buf
); buf
:= nil;
2018 // write other blocks, if any
2019 if mHeader
and not onlyFields
then
2023 for fld
in mFields
do
2026 if (fld
.mType
= fld
.TType
.TList
) then
2028 if (fld
.mRVal
= nil) or (fld
.mRVal
.count
= 0) then continue
;
2029 rec
:= mOwner
.findRecType(fld
.mName
);
2030 if (rec
= nil) then continue
;
2031 if (rec
.mBinBlock
<= 0) then continue
;
2032 if (blkmax
< rec
.mBinBlock
) then blkmax
:= rec
.mBinBlock
;
2036 for blk
:= 1 to blkmax
do
2038 if (blk
= mBinBlock
) then continue
;
2040 for fld
in mFields
do
2043 if (fld
.mType
= fld
.TType
.TList
) then
2045 if (fld
.mRVal
= nil) or (fld
.mRVal
.count
= 0) then continue
;
2046 rec
:= mOwner
.findRecType(fld
.mName
);
2047 if (rec
= nil) then continue
;
2048 if (rec
.mBinBlock
<> blk
) then continue
;
2049 if (ws
= nil) then ws
:= TMemoryStream
.Create();
2050 for rv
in fld
.mRVal
do rv
.writeBinTo(ws
);
2056 blksz
:= Integer(ws
.position
);
2058 writeInt(st
, Byte(blk
)); // type
2059 writeInt(st
, LongWord(0)); // reserved
2060 writeInt(st
, LongWord(blksz
)); // size
2061 st
.CopyFrom(ws
, blksz
);
2067 writeInt(st
, Byte(0));
2068 writeInt(st
, LongWord(0));
2069 writeInt(st
, LongWord(0));
2073 if (buf
<> nil) then FreeMem(buf
);
2078 procedure TDynRecord
.writeTo (wr
: TTextWriter
; putHeader
: Boolean=true);
2086 if (Length(mId
) > 0) then begin wr
.put(' '); wr
.put(mId
); end;
2092 for fld
in mFields
do
2095 if (fld
.mType
= fld
.TType
.TList
) then
2097 if not mHeader
then raise Exception
.Create('record list in non-header record');
2098 if (fld
.mRVal
<> nil) then
2100 for rec
in fld
.mRVal
do
2102 if (Length(rec
.mId
) = 0) then continue
;
2104 rec
.writeTo(wr
, true);
2109 if fld
.mInternal
then continue
;
2110 if fld
.mOmitDef
and fld
.isDefaultValue
then continue
;
2122 procedure TDynRecord
.parseValue (pr
: TTextParser
; beginEaten
: Boolean=false);
2125 rec
, trc
, rv
: TDynRecord
;
2127 if (mOwner
= nil) then raise Exception
.Create(Format('can''t parse record ''%s'' value without owner', [mName
]));
2133 if (not beginEaten
) and (pr
.tokType
= pr
.TTId
) then mId
:= pr
.expectId();
2137 assert(mHeaderRec
= self
);
2140 //writeln('parsing record <', mName, '>');
2141 if not beginEaten
then pr
.expectTT(pr
.TTBegin
);
2142 while (pr
.tokType
<> pr
.TTEnd
) do
2144 if (pr
.tokType
<> pr
.TTId
) then raise Exception
.Create('identifier expected');
2145 //writeln('<', mName, '.', pr.tokStr, '>');
2150 // add records with this type (if any)
2151 trc
:= mOwner
.findRecType(pr
.tokStr
);
2152 if (trc
<> nil) then
2155 rec
.mHeaderRec
:= mHeaderRec
;
2159 if (Length(rec
.mId
) > 0) then
2161 fld
:= field
[pr
.tokStr
];
2162 if (fld
<> nil) and (fld
.mRVal
<> nil) then
2164 for rv
in fld
.mRVal
do
2166 if (Length(rv
.mId
) > 0) and StrEqu(rv
.mId
, rec
.mId
) then raise Exception
.Create(Format('duplicate thing ''%s'' in record ''%s''', [fld
.mName
, mName
]));
2170 addRecordByType(rec
.mName
, rec
);
2180 fld
:= field
[pr
.tokStr
];
2181 if (fld
<> nil) then
2183 if fld
.defined
then raise Exception
.Create(Format('duplicate field ''%s'' in record ''%s''', [fld
.mName
, mName
]));
2184 if fld
.internal
then raise Exception
.Create(Format('internal field ''%s'' in record ''%s''', [fld
.mName
, mName
]));
2190 // something is wrong
2191 raise Exception
.Create(Format('unknown field ''%s'' in record ''%s''', [pr
.tokStr
, mName
]));
2193 pr
.expectTT(pr
.TTEnd
);
2194 // fix field defaults
2195 for fld
in mFields
do fld
.fixDefaultValue();
2196 //writeln('done parsing record <', mName, '>');
2200 // ////////////////////////////////////////////////////////////////////////// //
2201 constructor TDynEBS
.Create (pr
: TTextParser
);
2208 destructor TDynEBS
.Destroy ();
2215 procedure TDynEBS
.cleanup ();
2226 function TDynEBS
.findByName (const aname
: AnsiString): Integer;
2229 while (result
< Length(mIds
)) do
2231 if StrEqu(aname
, mIds
[result
]) then exit
;
2238 function TDynEBS
.hasByName (const aname
: AnsiString): Boolean; inline;
2240 result
:= (findByName(aname
) >= 0);
2244 function TDynEBS
.getFieldByName (const aname
: AnsiString): Integer; inline;
2248 f
:= findByName(aname
);
2249 if (f
>= 0) then result
:= mVals
[f
] else result
:= 0;
2253 function TDynEBS
.definition (): AnsiString;
2257 if mIsEnum
then result
:='enum ' else result
:= 'bitset ';
2261 if mIsEnum
then cv
:= 0 else cv
:= 1;
2262 for f
:= 0 to High(mIds
) do
2264 if (mIds
[f
] = mMaxName
) then continue
;
2265 result
+= ' '+mIds
[f
];
2266 if (mVals
[f
] <> cv
) then
2268 result
+= Format(' = %d', [mVals
[f
]]);
2269 if mIsEnum
then cv
:= mVals
[f
];
2274 result
+= Format(', // %d'#10, [mVals
[f
]]);
2276 if mIsEnum
then Inc(cv
) else if (mVals
[f
] = cv
) then cv
:= cv
shl 1;
2279 if (Length(mMaxName
) > 0) then result
+= ' '+mMaxName
+' = MAX,'#10;
2284 function TDynEBS
.pasdef (): AnsiString;
2288 result
:= '// '+mName
+#10'const'#10;
2290 for f
:= 0 to High(mIds
) do
2292 result
+= formatstrf(' %s = %d;'#10, [mIds
[f
], mVals
[f
]]);
2297 function TDynEBS
.nameByValue (v
: Integer): AnsiString;
2301 for f
:= 0 to High(mVals
) do
2303 if (mVals
[f
] = v
) then begin result
:= mIds
[f
]; exit
; end;
2309 procedure TDynEBS
.parseDef (pr
: TTextParser
);
2317 if pr
.eatId('enum') then mIsEnum
:= true
2318 else if pr
.eatId('bitset') then mIsEnum
:= false
2319 else pr
.expectId('enum');
2320 mName
:= pr
.expectId();
2321 mMaxVal
:= Integer($80000000);
2322 if mIsEnum
then cv
:= 0 else cv
:= 1;
2323 pr
.expectTT(pr
.TTBegin
);
2324 while (pr
.tokType
<> pr
.TTEnd
) do
2326 idname
:= pr
.expectId();
2327 for f
:= 0 to High(mIds
) do
2329 if StrEqu(mIds
[f
], idname
) then raise Exception
.Create(Format('duplicate field ''%s'' in enum/bitset ''%s''', [idname
, mName
]));
2331 if StrEqu(mMaxName
, idname
) then raise Exception
.Create(Format('duplicate field ''%s'' in enum/bitset ''%s''', [idname
, mName
]));
2336 if pr
.eatDelim('=') then
2338 if pr
.eatId('MAX') then
2340 if (Length(mMaxName
) > 0) then raise Exception
.Create(Format('duplicate max field ''%s'' in enum/bitset ''%s''', [idname
, mName
]));
2346 v
:= pr
.expectInt();
2347 if mIsEnum
then cv
:= v
;
2355 if mIsEnum
or (not hasV
) then
2357 if (mMaxVal
< v
) then mMaxVal
:= v
;
2359 SetLength(mIds
, Length(mIds
)+1);
2360 mIds
[High(mIds
)] := idname
;
2361 SetLength(mVals
, Length(mIds
));
2362 mVals
[High(mVals
)] := v
;
2364 if mIsEnum
or (not hasV
) then
2366 if mIsEnum
then Inc(cv
) else cv
:= cv
shl 1;
2369 if (pr
.tokType
= pr
.TTEnd
) then break
;
2370 pr
.expectTT(pr
.TTComma
);
2371 while pr
.eatTT(pr
.TTComma
) do begin end;
2373 pr
.expectTT(pr
.TTEnd
);
2375 if (Length(mMaxName
) > 0) then
2377 SetLength(mIds
, Length(mIds
)+1);
2378 mIds
[High(mIds
)] := mMaxName
;
2379 SetLength(mVals
, Length(mIds
));
2380 mVals
[High(mVals
)] := mMaxVal
;
2385 // ////////////////////////////////////////////////////////////////////////// //
2386 constructor TDynMapDef
.Create (pr
: TTextParser
);
2388 recTypes
:= TDynRecList
.Create();
2389 trigTypes
:= TDynRecList
.Create();
2390 ebsTypes
:= TDynEBSList
.Create();
2395 destructor TDynMapDef
.Destroy ();
2400 for rec
in recTypes
do rec
.Free();
2401 for rec
in trigTypes
do rec
.Free();
2402 for ebs
in ebsTypes
do ebs
.Free();
2413 function TDynMapDef
.getHeaderRecType (): TDynRecord
; inline;
2415 if (recTypes
.count
= 0) then raise Exception
.Create('no header in empty mapdef');
2416 result
:= recTypes
[0];
2420 function TDynMapDef
.findRecType (const aname
: AnsiString): TDynRecord
;
2424 for rec
in recTypes
do
2426 if StrEqu(rec
.name
, aname
) then begin result
:= rec
; exit
; end;
2432 function TDynMapDef
.findTrigFor (const aname
: AnsiString): TDynRecord
;
2436 for rec
in trigTypes
do
2438 if (rec
.isForTrig
[aname
]) then begin result
:= rec
; exit
; end;
2444 function TDynMapDef
.findEBSType (const aname
: AnsiString): TDynEBS
;
2448 for ebs
in ebsTypes
do
2450 if StrEqu(ebs
.name
, aname
) then begin result
:= ebs
; exit
; end;
2456 procedure TDynMapDef
.parseDef (pr
: TTextParser
);
2458 rec
, hdr
: TDynRecord
;
2462 // setup header links and type links
2463 procedure linkRecord (rec
: TDynRecord
);
2467 rec
.mHeaderRec
:= recTypes
[0];
2468 for fld
in rec
.mFields
do
2470 if (fld
.mType
= fld
.TType
.TTrigData
) then continue
;
2472 TDynField
.TEBS
.TNone
: begin end;
2473 TDynField
.TEBS
.TRec
:
2475 fld
.mEBSType
:= findRecType(fld
.mEBSTypeName
);
2476 if (fld
.mEBSType
= nil) then raise Exception
.Create(Format('field ''%s'' of type ''%s'' has no correcponding record definition', [fld
.mName
, fld
.mEBSTypeName
]));
2478 TDynField
.TEBS
.TEnum
,
2479 TDynField
.TEBS
.TBitSet
:
2481 fld
.mEBSType
:= findEBSType(fld
.mEBSTypeName
);
2482 if (fld
.mEBSType
= nil) then raise Exception
.Create(Format('field ''%s'' of type ''%s'' has no correcponding enum/bitset', [fld
.mName
, fld
.mEBSTypeName
]));
2483 if ((fld
.mEBS
= TDynField
.TEBS
.TEnum
) <> (fld
.mEBSType
as TDynEBS
).mIsEnum
) then raise Exception
.Create(Format('field ''%s'' of type ''%s'' enum/bitset type conflict', [fld
.mName
, fld
.mEBSTypeName
]));
2489 // setup default values
2490 procedure fixRecordDefaults (rec
: TDynRecord
);
2494 for fld
in rec
.mFields
do if fld
.mHasDefault
then fld
.parseDefaultValue();
2501 if not pr
.skipBlanks() then break
;
2502 if (pr
.tokType
<> pr
.TTId
) then raise Exception
.Create('identifier expected');
2504 if (pr
.tokStr
= 'enum') or (pr
.tokStr
= 'bitset') then
2506 eb
:= TDynEBS
.Create(pr
);
2507 if (findEBSType(eb
.name
) <> nil) then
2510 raise Exception
.Create(Format('duplicate enum/bitset ''%s''', [eb
.name
]));
2513 ebsTypes
.append(eb
);
2514 //writeln(eb.definition); writeln;
2518 if (pr
.tokStr
= 'TriggerData') then
2520 rec
:= TDynRecord
.Create(pr
);
2521 for f
:= 0 to High(rec
.mTrigTypes
) do
2523 if (findTrigFor(rec
.mTrigTypes
[f
]) <> nil) then
2526 raise Exception
.Create(Format('duplicate trigdata ''%s''', [rec
.mTrigTypes
[f
]]));
2530 trigTypes
.append(rec
);
2531 //writeln(dr.definition); writeln;
2535 rec
:= TDynRecord
.Create(pr
);
2536 //writeln(dr.definition); writeln;
2537 if (findRecType(rec
.name
) <> nil) then begin rec
.Free(); raise Exception
.Create(Format('duplicate record ''%s''', [rec
.name
])); end;
2538 if (hdr
<> nil) and StrEqu(rec
.name
, hdr
.name
) then begin rec
.Free(); raise Exception
.Create(Format('duplicate record ''%s''', [rec
.name
])); end;
2542 if (hdr
<> nil) then begin rec
.Free(); raise Exception
.Create(Format('duplicate header record ''%s'' (previous is ''%s'')', [rec
.name
, hdr
.name
])); end;
2547 recTypes
.append(rec
);
2551 // put header record to top
2552 if (hdr
= nil) then raise Exception
.Create('header definition not found in mapdef');
2553 recTypes
.append(nil);
2554 for f
:= recTypes
.count
-1 downto 1 do recTypes
[f
] := recTypes
[f
-1];
2557 // setup header links and type links
2558 for rec
in recTypes
do linkRecord(rec
);
2559 for rec
in trigTypes
do linkRecord(rec
);
2561 // setup default values
2562 for rec
in recTypes
do fixRecordDefaults(rec
);
2563 for rec
in trigTypes
do fixRecordDefaults(rec
);
2567 // ////////////////////////////////////////////////////////////////////////// //
2568 function TDynMapDef
.parseMap (pr
: TTextParser
): TDynRecord
;
2570 res
: TDynRecord
= nil;
2574 pr
.expectId(headerType
.name
);
2575 res
:= headerType
.clone();
2576 res
.mHeaderRec
:= res
;
2580 except on E
: Exception
do
2589 function TDynMapDef
.parseBinMap (st
: TStream
): TDynRecord
;
2591 res
: TDynRecord
= nil;
2595 res
:= headerType
.clone();
2596 res
.mHeaderRec
:= res
;
2597 res
.parseBinValue(st
);
2600 except on E
: Exception
do
2609 function TDynMapDef
.pasdef (): AnsiString;
2618 result
+= '// ////////////////////////////////////////////////////////////////////////// //'#10;
2619 result
+= '// enums and bitsets'#10;
2620 for ebs
in ebsTypes
do result
+= #10+ebs
.pasdef();
2621 result
+= #10#10'// ////////////////////////////////////////////////////////////////////////// //'#10;
2622 result
+= '// records'#10'type'#10;
2623 for rec
in recTypes
do
2625 if (rec
.mSize
< 1) then continue
;
2626 result
+= rec
.pasdef();
2629 result
+= #10#10'// ////////////////////////////////////////////////////////////////////////// //'#10;
2630 result
+= '// triggerdata'#10'type'#10;
2631 result
+= ' TTriggerData = record'#10;
2632 result
+= ' case Byte of'#10;
2633 result
+= ' 0: (Default: Byte128);'#10;
2634 for rec
in trigTypes
do
2638 for tn
in rec
.mTrigTypes
do
2640 if needComma
then result
+= ', ' else needComma
:= true;
2644 for fld
in rec
.mFields
do
2646 if fld
.mInternal
then continue
;
2647 if (fld
.mBinOfs
< 0) then continue
;
2648 result
+= ' '+fld
.pasdef
+#10;
2652 result
+= ' end;'#10;