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}
17 {.$DEFINE XDYNREC_USE_FIELDHASH} // actually, it is SLOWER with this
24 xparser
, xstreams
, utils
, hashtable
;
27 // ////////////////////////////////////////////////////////////////////////// //
34 TDynFieldList
= specialize TSimpleList
<TDynField
>;
35 TDynRecList
= specialize TSimpleList
<TDynRecord
>;
36 TDynEBSList
= specialize TSimpleList
<TDynEBS
>;
38 // this is base type for all scalars (and arrays)
42 TType
= (TBool
, TChar
, TByte
, TUByte
, TShort
, TUShort
, TInt
, TUInt
, TString
, TPoint
, TSize
, TList
, TTrigData
);
43 // TPoint: pair of Integers
44 // TSize: pair of UShorts
45 // TList: actually, array of records
46 // TTrigData: array of mMaxDim bytes, but internally a record (mRecRef)
47 // arrays of chars are pascal shortstrings (with counter in the first byte)
51 TEBS
= (TNone
, TRec
, TEnum
, TBitSet
);
58 mIVal
: Integer; // for all integer types
59 mIVal2
: Integer; // for point and size
60 mSVal
: AnsiString; // string; for byte and char arrays
61 mRVal
: TDynRecList
; // for list
62 mRHash
: THashStrInt
; // id -> index in mRVal
63 mRecRef
: TDynRecord
; // for TEBS.TRec
64 mMaxDim
: Integer; // for byte and char arrays; <0: not an array; 0: impossible value
65 mBinOfs
: Integer; // offset in binary; <0 - none
66 mSepPosSize
: Boolean; // for points and sizes, use separate fields
67 mAsT
: Boolean; // for points and sizes, use separate fields, names starts with `t`
73 mBitSetUnique
: Boolean; // bitset can contain only one value
74 mAsMonsterId
: Boolean; // special hack for triggers: monster record number+1 in binary (so 0 means "none")
76 mDefUnparsed
: AnsiString;
77 mDefSVal
: AnsiString; // default string value
78 mDefIVal
, mDefIVal2
: Integer; // default integer values
79 mDefRecRef
: TDynRecord
;
80 mEBS
: TEBS
; // complex type type
81 mEBSTypeName
: AnsiString; // name of enum, bitset or record
82 mEBSType
: TObject
; // either TDynRecord or TDynEBS; nil means "simple type"; nil for `TTrigData` too
85 mRecRefId
: AnsiString;
94 procedure parseDef (pr
: TTextParser
);
96 procedure parseDefaultValue (); // parse `mDefUnparsed` to `mDefSVal`, `mDefIVal`, `mDefIVal2`, `mDefRecRef`
97 procedure fixDefaultValue (); // this will NOT clone `mDefRecRef`
98 function isDefaultValue (): Boolean;
100 function getListCount (): Integer; inline;
101 function getListItem (idx
: Integer): TDynRecord
; inline; overload
;
102 function getListItem (const aname
: AnsiString): TDynRecord
; inline; overload
;
104 function getRecRefIndex (): Integer;
106 procedure setIVal (v
: Integer); inline;
109 // returns `true` for duplicate record id
110 function addListItem (rec
: TDynRecord
): Boolean; inline;
115 TListEnumerator = record
120 constructor Create (alist: TDynRecList);
121 function MoveNext (): Boolean; inline;
122 function getCurrent (): TDynRecord; inline;
123 property Current: TDynRecord read getCurrent;
128 constructor Create (const aname
: AnsiString; atype
: TType
);
129 constructor Create (pr
: TTextParser
);
130 destructor Destroy (); override;
132 class function getTypeName (t
: TType
): AnsiString;
134 function definition (): AnsiString;
135 function pasdef (): AnsiString;
137 function clone (newOwner
: TDynRecord
=nil): TDynField
;
139 procedure parseValue (pr
: TTextParser
);
140 procedure parseBinValue (st
: TStream
);
142 procedure writeTo (wr
: TTextWriter
);
143 procedure writeBinTo (st
: TStream
);
145 // won't work for lists
146 function isSimpleEqu (fld
: TDynField
): Boolean;
148 procedure setValue (const s
: AnsiString);
150 function GetEnumerator (): TDynRecList
.TEnumerator
; inline;
153 property pasname
: AnsiString read mPasName
;
154 property name
: AnsiString read mName
;
155 property baseType
: TType read mType
;
156 property negbool
: Boolean read mNegBool
;
157 property defined
: Boolean read mDefined write mDefined
;
158 property internal
: Boolean read mInternal write mInternal
;
159 property hasTPrefix
: Boolean read mAsT
;
160 property separatePasFields
: Boolean read mSepPosSize
;
161 property binOfs
: Integer read mBinOfs
;
162 property ival
: Integer read mIVal write setIVal
;
163 property ival2
: Integer read mIVal2
;
164 property sval
: AnsiString read mSVal
;
165 property hasDefault
: Boolean read mHasDefault
;
166 property defsval
: AnsiString read mDefSVal
;
167 property ebs
: TEBS read mEBS
;
168 property ebstype
: TObject read mEBSType
;
169 property ebstypename
: AnsiString read mEBSTypeName
; // enum/bitset name
170 property recref
: TDynRecord read mRecRef write mRecRef
; //FIXME: writing is a hack!
171 property recrefIndex
: Integer read getRecRefIndex
; // search for this record in header; -1: not found
173 property count
: Integer read getListCount
;
174 property item
[idx
: Integer]: TDynRecord read getListItem
;
175 property items
[const aname
: AnsiString]: TDynRecord read getListItem
; default
; // alas, FPC 3+ lost property overloading feature
177 property tagInt
: Integer read mTagInt write mTagInt
;
178 property tagPtr
: Pointer read mTagPtr write mTagPtr
;
182 // "value" header record contains TList fields, with name equal to record type
187 mPasName
: AnsiString;
190 mFields
: TDynFieldList
;
191 {$IF DEFINED(XDYNREC_USE_FIELDHASH)}
192 mFieldsHash
: THashStrInt
; // id -> index in mRVal
194 mTrigTypes
: array of AnsiString; // if this is triggerdata, we'll hold list of triggers here
195 mHeader
: Boolean; // true for header record
196 mBinBlock
: Integer; // -1: none
197 mHeaderRec
: TDynRecord
; // for "value" records this is header record with data, for "type" records this is header type record
204 procedure parseDef (pr
: TTextParser
); // parse definition
206 function findByName (const aname
: AnsiString): Integer; inline;
207 function hasByName (const aname
: AnsiString): Boolean; inline;
208 function getFieldByName (const aname
: AnsiString): TDynField
; inline;
209 function getFieldAt (idx
: Integer): TDynField
; inline;
210 function getCount (): Integer; inline;
212 function getIsTrigData (): Boolean; inline;
213 function getIsForTrig (const aname
: AnsiString): Boolean; inline;
215 function getForTrigCount (): Integer; inline;
216 function getForTrigAt (idx
: Integer): AnsiString; inline;
219 function findRecordByTypeId (const atypename
, aid
: AnsiString): TDynRecord
;
220 function findRecordNumByType (const atypename
: AnsiString; rc
: TDynRecord
): Integer;
221 function addRecordByType (const atypename
: AnsiString; rc
: TDynRecord
): Boolean; // `true`: duplicate record id
223 procedure addField (fld
: TDynField
); inline;
224 function addFieldChecked (fld
: TDynField
): Boolean; inline; // `true`: duplicate name
227 constructor Create ();
228 constructor Create (pr
: TTextParser
); // parse definition
229 destructor Destroy (); override;
231 function definition (): AnsiString;
232 function pasdef (): AnsiString;
234 function clone (): TDynRecord
;
236 function isSimpleEqu (rec
: TDynRecord
): Boolean;
238 procedure parseValue (pr
: TTextParser
; beginEaten
: Boolean=false);
239 procedure parseBinValue (st
: TStream
; forceData
: Boolean=false);
241 procedure writeTo (wr
: TTextWriter
; putHeader
: Boolean=true);
242 procedure writeBinTo (st
: TStream
; trigbufsz
: Integer=-1; onlyFields
: Boolean=false);
244 // find field with `TriggerType` type
245 function trigTypeField (): TDynField
;
247 // number of records of the given instance
248 function instanceCount (const typename
: AnsiString): Integer;
250 procedure setUserField (const fldname
: AnsiString; v
: LongInt);
251 procedure setUserField (const fldname
: AnsiString; v
: AnsiString);
254 property id
: AnsiString read mId
; // for map parser
255 property pasname
: AnsiString read mPasName
;
256 property name
: AnsiString read mName
; // record name
257 property size
: Integer read mSize
; // size in bytes
258 //property fields: TDynFieldList read mFields;
259 property has
[const aname
: AnsiString]: Boolean read hasByName
;
260 property count
: Integer read getCount
;
261 property field
[const aname
: AnsiString]: TDynField read getFieldByName
; default
;
262 property fieldAt
[idx
: Integer]: TDynField read getFieldAt
;
263 property isTrigData
: Boolean read getIsTrigData
;
264 property isForTrig
[const aname
: AnsiString]: Boolean read getIsForTrig
;
265 property forTrigCount
: Integer read getForTrigCount
;
266 property forTrigAt
[idx
: Integer]: AnsiString read getForTrigAt
;
267 property headerRec
: TDynRecord read mHeaderRec
;
268 property isHeader
: Boolean read mHeader
;
270 property tagInt
: Integer read mTagInt write mTagInt
;
271 property tagPtr
: Pointer read mTagPtr write mTagPtr
;
279 mIds
: array of AnsiString;
280 mVals
: array of Integer;
281 mMaxName
: AnsiString; // MAX field
282 mMaxVal
: Integer; // max value
285 procedure cleanup ();
287 procedure parseDef (pr
: TTextParser
); // parse definition
289 function findByName (const aname
: AnsiString): Integer; inline;
290 function hasByName (const aname
: AnsiString): Boolean; inline;
291 function getFieldByName (const aname
: AnsiString): Integer; inline;
294 constructor Create (pr
: TTextParser
); // parse definition
295 destructor Destroy (); override;
297 function definition (): AnsiString;
298 function pasdef (): AnsiString;
300 // return empty string if not found
301 function nameByValue (v
: Integer): AnsiString;
304 property name
: AnsiString read mName
; // record name
305 property isEnum
: Boolean read mIsEnum
;
306 property has
[const aname
: AnsiString]: Boolean read hasByName
;
307 property field
[const aname
: AnsiString]: Integer read getFieldByName
;
313 recTypes
: TDynRecList
; // [0] is always header
314 trigTypes
: TDynRecList
; // trigdata
315 ebsTypes
: TDynEBSList
; // enums, bitsets
318 procedure parseDef (pr
: TTextParser
);
320 function getHeaderRecType (): TDynRecord
; inline;
322 function getTrigTypeCount (): Integer; inline;
323 function getTrigTypeAt (idx
: Integer): TDynRecord
; inline;
326 constructor Create (pr
: TTextParser
); // parses data definition
327 destructor Destroy (); override;
329 function findRecType (const aname
: AnsiString): TDynRecord
;
330 function findTrigFor (const aname
: AnsiString): TDynRecord
;
331 function findEBSType (const aname
: AnsiString): TDynEBS
;
333 function pasdef (): AnsiString;
334 function pasdefconst (): AnsiString;
336 // creates new header record
337 function parseMap (pr
: TTextParser
): TDynRecord
;
339 // creates new header record
340 function parseBinMap (st
: TStream
): TDynRecord
;
343 property headerType
: TDynRecord read getHeaderRecType
;
344 property trigTypeCount
: Integer read getTrigTypeCount
;
345 property trigType
[idx
: Integer]: TDynRecord read getTrigTypeAt
;
349 {$IF DEFINED(D2D_DYNREC_PROFILER)}
350 procedure xdynDumpProfiles ();
358 {$IF DEFINED(D2D_DYNREC_PROFILER)},xprofiler
{$ENDIF};
361 // ////////////////////////////////////////////////////////////////////////// //
362 function StrEqu (const a
, b
: AnsiString): Boolean; inline; begin result
:= (a
= b
); end;
365 // ////////////////////////////////////////////////////////////////////////// //
367 constructor TDynField.TListEnumerator.Create (alist: TDynRecList);
374 function TDynField.TListEnumerator.MoveNext (): Boolean; inline;
377 result := (mList <> nil) and (mCurIdx < mList.count);
381 function TDynField.TListEnumerator.getCurrent (): TDynRecord; inline;
383 result := mList[mCurIdx];
388 function TDynField
.GetEnumerator (): TDynRecList
.TEnumerator
; inline;
390 //result := TListEnumerator.Create(mRVal);
391 if (mRVal
<> nil) then result
:= mRVal
.GetEnumerator
else result
:= TDynRecList
.TEnumerator
.Create(nil, 0);
395 // ////////////////////////////////////////////////////////////////////////// //
396 constructor TDynField
.Create (const aname
: AnsiString; atype
: TType
);
404 if (mType
= TType
.TList
) then
406 mRVal
:= TDynRecList
.Create();
407 mRHash
:= hashNewStrInt();
412 constructor TDynField
.Create (pr
: TTextParser
);
419 destructor TDynField
.Destroy ();
426 procedure TDynField
.cleanup ();
440 mSepPosSize
:= false;
442 mHasDefault
:= false;
454 mBitSetUnique
:= false;
455 mAsMonsterId
:= false;
463 function TDynField
.clone (newOwner
: TDynRecord
=nil): TDynField
;
467 result
:= TDynField
.Create(mName
, mType
);
468 result
.mOwner
:= mOwner
;
469 if (newOwner
<> nil) then result
.mOwner
:= newOwner
else result
.mOwner
:= mOwner
;
470 result
.mPasName
:= mPasName
;
471 result
.mName
:= mName
;
472 result
.mType
:= mType
;
473 result
.mIVal
:= mIVal
;
474 result
.mIVal2
:= mIVal2
;
475 result
.mSVal
:= mSVal
;
476 if (mRVal
<> nil) then
478 if (result
.mRVal
= nil) then result
.mRVal
:= TDynRecList
.Create(mRVal
.count
);
479 if (result
.mRHash
= nil) then result
.mRHash
:= hashNewStrInt();
480 for rec
in mRVal
do result
.addListItem(rec
.clone());
482 result
.mRecRef
:= mRecRef
;
483 result
.mMaxDim
:= mMaxDim
;
484 result
.mBinOfs
:= mBinOfs
;
485 result
.mSepPosSize
:= mSepPosSize
;
487 result
.mDefined
:= mDefined
;
488 result
.mHasDefault
:= mHasDefault
;
489 result
.mOmitDef
:= mOmitDef
;
490 result
.mInternal
:= mInternal
;
491 result
.mNegBool
:= mNegBool
;
492 result
.mBitSetUnique
:= mBitSetUnique
;
493 result
.mAsMonsterId
:= mAsMonsterId
;
494 result
.mDefUnparsed
:= mDefUnparsed
;
495 result
.mDefSVal
:= mDefSVal
;
496 result
.mDefIVal
:= mDefIVal
;
497 result
.mDefIVal2
:= mDefIVal2
;
498 result
.mDefRecRef
:= mDefRecRef
;
500 result
.mEBSTypeName
:= mEBSTypeName
;
501 result
.mEBSType
:= mEBSType
;
502 result
.mRecRefId
:= mRecRefId
;
503 result
.mTagInt
:= mTagInt
;
504 result
.mTagPtr
:= mTagPtr
;
508 procedure TDynField
.setIVal (v
: Integer); inline;
516 // won't work for lists
517 function TDynField
.isSimpleEqu (fld
: TDynField
): Boolean;
519 if (fld
= nil) or (mType
<> fld
.mType
) then begin result
:= false; exit
; end;
521 TType
.TBool
: result
:= ((mIVal
<> 0) = (fld
.mIVal
<> 0));
522 TType
.TChar
: result
:= (mSVal
= fld
.mSVal
);
529 result
:= (mIVal
= fld
.mIVal
);
530 TType
.TString
: result
:= (mSVal
= fld
.mSVal
);
533 result
:= ((mIVal
= fld
.mIVal
) and (mIVal2
= fld
.mIVal2
));
534 TType
.TList
: result
:= false;
537 if (mRecRef
= nil) then begin result
:= (fld
.mRecRef
= nil); exit
; end;
538 result
:= mRecRef
.isSimpleEqu(fld
.mRecRef
);
540 else raise Exception
.Create('ketmar forgot to handle some field type');
545 procedure TDynField
.setValue (const s
: AnsiString);
549 stp
:= TStrTextParser
.Create(s
+';');
558 procedure TDynField
.parseDefaultValue ();
560 stp
: TTextParser
= nil;
562 oIVal
, oIVal2
: Integer;
566 if not mHasDefault
then
581 stp
:= TStrTextParser
.Create(mDefUnparsed
+';');
586 mDefRecRef
:= mRecRef
;
599 // default value should be parsed
600 procedure TDynField
.fixDefaultValue ();
602 if mDefined
then exit
;
603 if not mHasDefault
then
605 if mInternal
then exit
;
606 raise Exception
.Create(Format('field ''%s'' in record ''%s'' of record type ''%s'' is not set', [mName
, mOwner
.mId
, mOwner
.mName
]));
608 if (mEBS
= TEBS
.TRec
) then mRecRef
:= mDefRecRef
;
616 // default value should be parsed
617 function TDynField
.isDefaultValue (): Boolean;
619 if not mHasDefault
then begin result
:= false; exit
; end;
620 if (mEBS
= TEBS
.TRec
) then begin result
:= (mRecRef
= mDefRecRef
); exit
; end;
622 TType
.TChar
, TType
.TString
: result
:= (mSVal
= mDefSVal
);
623 TType
.TPoint
, TType
.TSize
: result
:= (mIVal
= mDefIVal2
) and (mIVal2
= mDefIVal2
);
624 TType
.TList
, TType
.TTrigData
: result
:= false; // no default values for those types
625 else result
:= (mIVal
= mDefIVal
);
630 function TDynField
.getListCount (): Integer; inline;
632 if (mRVal
<> nil) then result
:= mRVal
.count
else result
:= 0;
636 function TDynField
.getListItem (idx
: Integer): TDynRecord
; inline; overload
;
638 if (mRVal
<> nil) and (idx
>= 0) and (idx
< mRVal
.count
) then result
:= mRVal
[idx
] else result
:= nil;
642 function TDynField
.getListItem (const aname
: AnsiString): TDynRecord
; inline; overload
;
646 if (mRVal
<> nil) and mRHash
.get(aname
, idx
) then result
:= mRVal
[idx
] else result
:= nil;
650 function TDynField
.addListItem (rec
: TDynRecord
): Boolean; inline;
653 if (mRVal
<> nil) then
656 if (Length(rec
.mId
) > 0) then result
:= mRHash
.put(rec
.mId
, mRVal
.count
-1);
661 class function TDynField
.getTypeName (t
: TType
): AnsiString;
664 TType
.TBool
: result
:= 'bool';
665 TType
.TChar
: result
:= 'char';
666 TType
.TByte
: result
:= 'byte';
667 TType
.TUByte
: result
:= 'ubyte';
668 TType
.TShort
: result
:= 'short';
669 TType
.TUShort
: result
:= 'ushort';
670 TType
.TInt
: result
:= 'int';
671 TType
.TUInt
: result
:= 'uint';
672 TType
.TString
: result
:= 'string';
673 TType
.TPoint
: result
:= 'point';
674 TType
.TSize
: result
:= 'size';
675 TType
.TList
: result
:= 'array';
676 TType
.TTrigData
: result
:= 'trigdata';
677 else raise Exception
.Create('ketmar forgot to handle some field type');
682 function TDynField
.definition (): AnsiString;
684 result
:= mPasName
+' is '+quoteStr(mName
)+' type ';
685 result
+= getTypeName(mType
);
686 if (mMaxDim
>= 0) then result
+= Format('[%d]', [mMaxDim
]);
687 if (mBinOfs
>= 0) then result
+= Format(' offset %d', [mBinOfs
]);
689 TEBS
.TNone
: begin end;
690 TEBS
.TRec
: result
+= ' '+mEBSTypeName
;
691 TEBS
.TEnum
: result
+= ' enum '+mEBSTypeName
;
692 TEBS
.TBitSet
: begin result
+= ' bitset '; if mBitSetUnique
then result
+= 'unique '; result
+= mEBSTypeName
; end;
694 if mAsMonsterId
then result
+= ' as monsterid';
695 if mHasDefault
and (Length(mDefUnparsed
) > 0) then result
+= ' default '+mDefUnparsed
;
698 if (mType
= TType
.TPoint
) then begin if (mAsT
) then result
+= ' as txy' else result
+= ' as xy'; end
699 else if (mType
= TType
.TSize
) then begin if (mAsT
) then result
+= ' as twh' else result
+= ' as wh'; end;
701 if mOmitDef
then result
+= ' omitdefault';
702 if mInternal
then result
+= ' internal';
706 function TDynField
.pasdef (): AnsiString;
708 result
:= mPasName
+': ';
710 TType
.TBool
: result
+= 'Boolean;';
711 TType
.TChar
: if (mMaxDim
> 0) then result
+= formatstrf('Char%d;', [mMaxDim
]) else result
+= 'Char;';
712 TType
.TByte
: result
+= 'ShortInt;';
713 TType
.TUByte
: result
+= 'Byte;';
714 TType
.TShort
: result
+= 'SmallInt;';
715 TType
.TUShort
: result
+= 'Word;';
716 TType
.TInt
: result
+= 'LongInt;';
717 TType
.TUInt
: result
+= 'LongWord;';
718 TType
.TString
: result
+= 'AnsiString;';
720 if mAsT
then result
:= 'tX, tY: Integer;'
721 else if mSepPosSize
then result
:= 'X, Y: Integer;'
722 else result
+= 'TDFPoint;';
724 if mAsT
then result
:= 'tWidth, tHeight: Word;'
725 else if mSepPosSize
then result
:= 'Width, Height: Word;'
726 else result
+= 'TSize;';
727 TType
.TList
: assert(false);
728 TType
.TTrigData
: result
+= formatstrf('Byte%d;', [mMaxDim
]);
729 else raise Exception
.Create('ketmar forgot to handle some field type');
734 procedure TDynField
.parseDef (pr
: TTextParser
);
739 fldrecname
: AnsiString;
740 fldpasname
: AnsiString;
741 asxy
, aswh
, ast
: Boolean;
750 lebs
: TDynField
.TEBS
;
772 lebs
:= TDynField.TEBS.TNone
;
774 fldpasname
:= pr.expectId
(); // pascal field name
777 fldname
:= pr.expectStr
();
780 fldtype
:= pr.expectId
();
783 if pr.eatDelim
('[') then
785 lmaxdim
:= pr.expectInt
();
786 if
(lmaxdim
< 1) then raise Exception.Create
(Format
('invalid field ''%s'' array size', [fldname
]));
790 while (pr
.tokType
<> pr
.TTSemi
) do
792 if pr
.eatId('offset') then
794 if (fldofs
>= 0) then raise Exception
.Create(Format('duplicate field ''%s'' offset', [fldname
]));
795 fldofs
:= pr
.expectInt();
796 if (fldofs
< 0) then raise Exception
.Create(Format('invalid field ''%s'' offset', [fldname
]));
800 if pr
.eatId('as') then
802 if pr
.eatId('xy') then asxy
:= true
803 else if pr
.eatId('wh') then aswh
:= true
804 else if pr
.eatId('txy') then begin asxy
:= true; ast
:= true; end
805 else if pr
.eatId('twh') then begin aswh
:= true; ast
:= true; end
806 else if pr
.eatId('monsterid') then begin asmonid
:= true
; end
807 else raise Exception.Create
(Format
('invalid field ''%s'' as what?', [fldname
]));
811 if pr
.eatId('enum') then
813 lebs
:= TDynField
.TEBS
.TEnum
;
814 if (Length(fldrecname
) <> 0) then raise Exception
.Create(Format('field ''%s'' already typed as ''%s''', [fldname
, fldrecname
]));
815 fldrecname
:= pr
.expectId();
819 if pr
.eatId('bitset') then
821 lebs
:= TDynField
.TEBS
.TBitSet
;
822 if (Length(fldrecname
) <> 0) then raise Exception
.Create(Format('field ''%s'' already typed as ''%s''', [fldname
, fldrecname
]));
823 unique
:= pr
.eatId('unique');
824 fldrecname
:= pr
.expectId();
828 if pr
.eatId('default') then
830 if hasdefStr
or hasdefInt
or hasdefId
then raise Exception
.Create(Format('field ''%s'' has duplicate default', [fldname
]));
835 defstr
:= pr
.expectStr(true); // allow empty strings
840 defstr
:= pr
.expectId();
845 defint
:= pr
.expectInt();
848 raise Exception
.Create(Format('field ''%s'' has invalid default', [fldname
]));
853 if pr
.eatId('omitdefault') then
859 if pr
.eatId('internal') then
865 if (pr
.tokType
<> pr
.TTId
) then raise Exception
.Create(Format('field ''%s'' has something unexpected in definition', [fldname
]));
867 if (Length(fldrecname
) <> 0) then raise Exception
.Create(Format('field ''%s'' already typed as ''%s''', [fldname
, fldrecname
]));
868 fldrecname
:= pr
.expectId();
869 lebs
:= TDynField
.TEBS
.TRec
;
872 pr
.expectTT(pr
.TTSemi
);
876 if (fldtype
= 'bool') then mType
:= TType
.TBool
877 else if (fldtype
= 'negbool') then begin mType
:= TType
.TBool
; mNegBool
:= true; end
878 else if (fldtype
= 'char') then mType
:= TType
.TChar
879 else if (fldtype
= 'byte') then mType
:= TType
.TByte
880 else if (fldtype
= 'ubyte') then mType
:= TType
.TUByte
881 else if (fldtype
= 'short') then mType
:= TType
.TShort
882 else if (fldtype
= 'ushort') then mType
:= TType
.TUShort
883 else if (fldtype
= 'int') then mType
:= TType
.TInt
884 else if (fldtype
= 'uint') then mType
:= TType
.TUInt
885 else if (fldtype
= 'string') then mType
:= TType
.TString
886 else if (fldtype
= 'point') then mType
:= TType
.TPoint
887 else if (fldtype
= 'size') then mType
:= TType
.TSize
888 else if (fldtype
= 'trigdata') then mType
:= TType
.TTrigData
889 else raise Exception
.Create(Format('field ''%s'' has invalid type ''%s''', [fldname
, fldtype
]));
891 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
]));
892 if (mType
= TType
.TTrigData
) then
894 if (lmaxdim
< 1) then raise Exception
.Create(Format('field ''%s'' of type ''%s'' cannot be array', [fldname
, fldtype
]));
895 if (Length(fldrecname
) > 0) then raise Exception
.Create(Format('field ''%s'' of type ''%s'' cannot have another type', [fldname
, fldtype
]));
896 lebs
:= TDynField
.TEBS
.TRec
;
899 if hasdefStr
then self
.mDefUnparsed
:= quoteStr(defstr
)
900 else if hasdefInt
then self
.mDefUnparsed
:= Format('%d', [defint
])
901 else if hasdefId
then self
.mDefUnparsed
:= defstr
;
903 self
.mHasDefault
:= (hasdefStr
or hasdefId
or hasdefInt
);
904 self
.mPasName
:= fldpasname
;
906 self
.mEBSTypeName
:= fldrecname
;
907 self
.mBitSetUnique
:= unique
;
908 self
.mAsMonsterId
:= asmonid
;
909 self.mMaxDim
:= lmaxdim
;
910 self.mBinOfs
:= fldofs
;
911 self.mSepPosSize
:= (asxy
or aswh
);
913 self.mOmitDef
:= omitdef
;
914 self.mInternal
:= ainternal
;
918 function TDynField
.getRecRefIndex (): Integer;
920 if (mRecRef
= nil) then begin result
:= -1; exit
; end;
921 result
:= mOwner
.findRecordNumByType(mEBSTypeName
, mRecRef
);
925 procedure TDynField
.writeBinTo (st
: TStream
);
934 TEBS
.TNone
: begin end;
937 if (mMaxDim
>= 0) then
939 // this must be triggerdata
940 if (mType
<> TType
.TTrigData
) then
942 raise Exception
.Create(Format('record reference type ''%s'' in field ''%s'' cannot be written', [mEBSTypeName
, mName
]));
945 GetMem(buf
, mMaxDim
);
946 if (buf
= nil) then raise Exception
.Create(Format('record reference type ''%s'' in field ''%s'' cannot be written', [mEBSTypeName
, mName
]));
948 FillChar(buf
^, mMaxDim
, 0);
949 if (mRecRef
<> nil) then
951 ws
:= TSFSMemoryChunkStream
.Create(buf
, mMaxDim
);
952 mRecRef
.writeBinTo(ws
, mMaxDim
); // as trigdata
954 st
.WriteBuffer(buf
^, mMaxDim
);
957 if (buf
<> nil) then FreeMem(buf
);
963 TType
.TByte
: maxv
:= 127;
964 TType
.TUByte
: maxv
:= 254;
965 TType
.TShort
: maxv
:= 32767;
966 TType
.TUShort
: maxv
:= 65534;
967 TType
.TInt
: maxv
:= $7fffffff;
968 TType
.TUInt
: maxv
:= $7fffffff;
969 else raise Exception
.Create(Format('record reference type ''%s'' in field ''%s'' cannot be written', [mEBSTypeName
, mName
]));
971 // find record number
972 if (mRecRef
<> nil) then
974 f
:= mOwner
.findRecordNumByType(mEBSTypeName
, mRecRef
);
975 if (f
< 0) then raise Exception
.Create(Format('record reference type ''%s'' in field ''%s'' not found in record list', [mEBSTypeName
, mName
]));
976 if mAsMonsterId
then Inc(f
);
977 if (f
> maxv
) then raise Exception
.Create(Format('record reference type ''%s'' in field ''%s'' has too big index', [mEBSTypeName
, mName
]));
981 if mAsMonsterId
then f
:= 0 else f
:= -1;
984 TType
.TByte
, TType
.TUByte
: writeInt(st
, Byte(f
));
985 TType
.TShort
, TType
.TUShort
: writeInt(st
, SmallInt(f
));
986 TType
.TInt
, TType
.TUInt
: writeInt(st
, LongWord(f
));
987 else raise Exception
.Create(Format('record reference type ''%s'' in field ''%s'' cannot be written', [mEBSTypeName
, mName
]));
991 TEBS
.TEnum
: begin end;
992 TEBS
.TBitSet
: begin end;
993 else raise Exception
.Create('ketmar forgot to handle some EBS type');
1001 if (mIVal
<> 0) then writeInt(st
, Byte(1)) else writeInt(st
, Byte(0));
1005 if (mIVal
= 0) then writeInt(st
, Byte(1)) else writeInt(st
, Byte(0));
1011 if (mMaxDim
= 0) then raise Exception
.Create(Format('invalid string size definition for field ''%s''', [mName
]));
1012 if (mMaxDim
< 0) then
1014 if (Length(mSVal
) <> 1) then raise Exception
.Create(Format('invalid string size definition for field ''%s''', [mName
]));
1015 writeInt(st
, Byte(mSVal
[1]));
1019 if (Length(mSVal
) > mMaxDim
) then raise Exception
.Create(Format('invalid string size definition for field ''%s''', [mName
]));
1020 s
:= utf2win(mSVal
);
1021 if (Length(s
) > 0) then st
.WriteBuffer(PChar(s
)^, Length(s
));
1022 for f
:= Length(s
) to mMaxDim
do writeInt(st
, Byte(0));
1029 // triggerdata array was processed earlier
1030 if (mMaxDim
>= 0) then Exception
.Create(Format('byte array in field ''%s'' cannot be written', [mName
]));
1031 writeInt(st
, Byte(mIVal
));
1037 if (mMaxDim
>= 0) then raise Exception
.Create(Format('short array in field ''%s'' cannot be written', [mName
]));
1038 writeInt(st
, Word(mIVal
));
1044 if (mMaxDim
>= 0) then raise Exception
.Create(Format('int array in field ''%s'' cannot be written', [mName
]));
1045 writeInt(st
, LongWord(mIVal
));
1050 raise Exception
.Create(Format('cannot write string field ''%s''', [mName
]));
1054 if (mMaxDim
>= 0) then raise Exception
.Create(Format('pos/size array in field ''%s'' cannot be written', [mName
]));
1055 writeInt(st
, LongInt(mIVal
));
1056 writeInt(st
, LongInt(mIVal2
));
1061 if (mMaxDim
>= 0) then raise Exception
.Create(Format('pos/size array in field ''%s'' cannot be written', [mName
]));
1062 writeInt(st
, Word(mIVal
));
1063 writeInt(st
, Word(mIVal2
));
1076 else raise Exception
.Create('ketmar forgot to handle some field type');
1081 procedure TDynField
.writeTo (wr
: TTextWriter
);
1085 first
, found
: Boolean;
1090 TEBS
.TNone
: begin end;
1093 if (mRecRef
= nil) then
1095 if (mType
= TType
.TTrigData
) then wr
.put('{}'#10) else wr
.put('null;'#10);
1097 else if (Length(mRecRef
.mId
) = 0) then
1099 mRecRef
.writeTo(wr
, false); // only data, no header
1103 wr
.put(mRecRef
.mId
);
1110 //def := mOwner.mOwner;
1111 //es := def.findEBSType(mEBSTypeName);
1113 if (mEBSType
<> nil) and (mEBSType
is TDynEBS
) then es
:= (mEBSType
as TDynEBS
);
1114 if (es
= nil) or (not es
.mIsEnum
) then raise Exception
.Create(Format('record enum type ''%s'' for field ''%s'' not found', [mEBSTypeName
, mName
]));
1115 for f
:= 0 to High(es
.mVals
) do
1117 if (es
.mVals
[f
] = mIVal
) then
1124 raise Exception
.Create(Format('value %d in record enum type ''%s'' for field ''%s'' not found', [mIVal
, mEBSTypeName
, mName
]));
1128 //def := mOwner.mOwner;
1129 //es := def.findEBSType(mEBSTypeName);
1131 if (mEBSType
<> nil) and (mEBSType
is TDynEBS
) then es
:= (mEBSType
as TDynEBS
);
1132 if (es
= nil) or es
.mIsEnum
then raise Exception
.Create(Format('record bitset type ''%s'' for field ''%s'' not found', [mEBSTypeName
, mName
]));
1136 for f
:= 0 to High(es
.mVals
) do
1138 if (es
.mVals
[f
] = 0) then
1145 raise Exception
.Create(Format('value %d in record bitset type ''%s'' for field ''%s'' not found', [0, mEBSTypeName
, mName
]));
1150 while (mask
<> 0) do
1152 if ((mIVal
and mask
) <> 0) then
1155 for f
:= 0 to High(es
.mVals
) do
1157 if (es
.mVals
[f
] = mask
) then
1159 if not first
then wr
.put('+') else first
:= false;
1165 if not found
then raise Exception
.Create(Format('value %d in record bitset type ''%s'' for field ''%s'' not found', [mask
, mEBSTypeName
, mName
]));
1172 else raise Exception
.Create('ketmar forgot to handle some EBS type');
1178 if (mIVal
= 0) then wr
.put('false;'#10) else wr
.put('true;'#10);
1183 if (mMaxDim
= 0) then raise Exception
.Create(Format('invalid string size definition for field ''%s''', [mName
]));
1184 wr
.put(quoteStr(mSVal
));
1195 wr
.put('%d;'#10, [mIVal
]);
1200 wr
.put(quoteStr(mSVal
));
1207 wr
.put('(%d %d);'#10, [mIVal
, mIVal2
]);
1220 else raise Exception
.Create('ketmar forgot to handle some field type');
1222 raise Exception
.Create(Format('cannot parse field ''%s'' yet', [mName
]));
1226 procedure TDynField
.parseBinValue (st
: TStream
);
1228 rec
, rc
: TDynRecord
;
1236 TEBS
.TNone
: begin end;
1239 // this must be triggerdata
1240 if (mType
= TType
.TTrigData
) then
1242 assert(mMaxDim
> 0);
1244 // find trigger definition
1245 tfld
:= rec
.trigTypeField();
1246 if (tfld
= nil) then raise Exception
.Create(Format('triggerdata value for field ''%s'' in record ''%s'' without TriggerType field', [mName
, rec
.mName
]));
1247 rc
:= mOwner
.mOwner
.findTrigFor(tfld
.mSVal
); // find in mapdef
1248 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
]));
1250 rc
.mHeaderRec
:= mOwner
.mHeaderRec
;
1252 rc
.parseBinValue(st
, true);
1263 // not a trigger data
1265 TType
.TByte
: f
:= readShortInt(st
);
1266 TType
.TUByte
: f
:= readByte(st
);
1267 TType
.TShort
: f
:= readSmallInt(st
);
1268 TType
.TUShort
: f
:= readWord(st
);
1269 TType
.TInt
: f
:= readLongInt(st
);
1270 TType
.TUInt
: f
:= readLongWord(st
);
1271 else raise Exception
.Create(Format('invalid non-numeric type ''%s'' for field ''%s'' of record ''%s''', [getTypeName(mType
), mName
, mEBSTypeName
]));
1273 if mAsMonsterId
then Dec(f
);
1274 if (f
< 0) then mRecRefId
:= '' else mRecRefId
:= Format('%s%d', [mEBSTypeName
, f
]);
1282 assert(mMaxDim
< 0);
1284 TType
.TByte
: f
:= readShortInt(st
);
1285 TType
.TUByte
: f
:= readByte(st
);
1286 TType
.TShort
: f
:= readSmallInt(st
);
1287 TType
.TUShort
: f
:= readWord(st
);
1288 TType
.TInt
: f
:= readLongInt(st
);
1289 TType
.TUInt
: f
:= readLongWord(st
);
1290 else raise Exception
.Create(Format('invalid non-numeric type ''%s'' for field ''%s'' of record ''%s''', [getTypeName(mType
), mName
, mEBSTypeName
]));
1293 if (mEBSType
<> nil) and (mEBSType
is TDynEBS
) then es
:= (mEBSType
as TDynEBS
);
1294 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
]));
1296 // build enum/bitfield values
1297 if (mEBS
= TEBS
.TEnum
) then
1299 mSVal
:= es
.nameByValue(mIVal
);
1300 if (Length(mSVal
) = 0) then raise Exception
.Create(Format('record enum type ''%s'' for field ''%s'' has invalid value %d', [mEBSTypeName
, mName
, mIVal
]));
1304 // special for 'none'
1307 mSVal
:= es
.nameByValue(mIVal
);
1308 if (Length(mSVal
) = 0) then raise Exception
.Create(Format('record bitset type ''%s'' for field ''%s'' has invalid value %d', [mEBSTypeName
, mName
, mIVal
]));
1314 while (mask
<> 0) do
1316 if ((mIVal
and mask
) <> 0) then
1318 s
:= es
.nameByValue(mask
);
1319 if (Length(s
) = 0) then raise Exception
.Create(Format('record bitset type ''%s'' for field ''%s'' has invalid value %d', [mEBSTypeName
, mName
, mask
]));
1320 if (Length(mSVal
) <> 0) then mSVal
+= '+';
1327 //writeln('ebs <', es.mName, '>: ', mSVal);
1331 else raise Exception
.Create('ketmar forgot to handle some EBS type');
1338 if (f
<> 0) then f
:= 1;
1339 if mNegBool
then f
:= 1-f
;
1346 if (mMaxDim
< 0) then
1348 mIVal
:= readByte(st
);
1353 GetMem(tdata
, mMaxDim
);
1355 st
.ReadBuffer(tdata
^, mMaxDim
);
1357 while (f
< mMaxDim
) and (tdata
[f
] <> 0) do Inc(f
);
1360 SetLength(mSVal
, f
);
1361 Move(tdata
^, PChar(mSVal
)^, f
);
1362 mSVal
:= win2utf(mSVal
);
1371 TType
.TByte
: begin mIVal
:= readShortInt(st
); mDefined
:= true; exit
; end;
1372 TType
.TUByte
: begin mIVal
:= readByte(st
); mDefined
:= true; exit
; end;
1373 TType
.TShort
: begin mIVal
:= readSmallInt(st
); mDefined
:= true; exit
; end;
1374 TType
.TUShort
: begin mIVal
:= readWord(st
); mDefined
:= true; exit
; end;
1375 TType
.TInt
: begin mIVal
:= readLongInt(st
); mDefined
:= true; exit
; end;
1376 TType
.TUInt
: begin mIVal
:= readLongWord(st
); mDefined
:= true; exit
; end;
1379 raise Exception
.Create('cannot read strings from binaries yet');
1384 mIVal
:= readLongInt(st
);
1385 mIVal2
:= readLongInt(st
);
1391 mIVal
:= readWord(st
);
1392 mIVal2
:= readWord(st
);
1406 else raise Exception
.Create('ketmar forgot to handle some field type');
1408 raise Exception
.Create(Format('cannot parse field ''%s'' yet', [mName
]));
1412 procedure TDynField
.parseValue (pr
: TTextParser
);
1414 procedure parseInt (min
, max
: Integer);
1416 mIVal
:= pr
.expectInt();
1417 if (mIVal
< min
) or (mIVal
> max
) then raise Exception
.Create(Format('invalid %s value for field ''%s''', [getTypeName(mType
), mName
]));
1422 rec
, rc
: TDynRecord
;
1428 // if this field should contain struct, convert type and parse struct
1430 TEBS
.TNone
: begin end;
1433 // ugly hack. sorry.
1434 if (mType
= TType
.TTrigData
) then
1436 pr
.expectTT(pr
.TTBegin
);
1437 if (pr
.tokType
= pr
.TTEnd
) then
1441 pr
.expectTT(pr
.TTEnd
);
1446 // find trigger definition
1447 tfld
:= rec
.trigTypeField();
1448 if (tfld
= nil) then raise Exception
.Create(Format('triggerdata value for field ''%s'' in record ''%s'' without ''type'' field', [mName
, rec
.mName
]));
1449 rc
:= mOwner
.mOwner
.findTrigFor(tfld
.mSVal
); // find in mapdef
1450 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
]));
1452 rc
.mHeaderRec
:= mOwner
.mHeaderRec
;
1453 //writeln(rc.definition);
1455 rc
.parseValue(pr
, true);
1463 pr
.eatTT(pr
.TTSemi
); // hack: allow (but don't require) semicolon after inline records
1466 // other record types
1467 if (pr
.tokType
= pr
.TTId
) then
1469 if pr
.eatId('null') then
1475 rec
:= mOwner
.findRecordByTypeId(mEBSTypeName
, pr
.tokStr
);
1476 if (rec
= nil) then raise Exception
.Create(Format('record ''%s'' (%s) value for field ''%s'' not found', [pr
.tokStr
, mEBSTypeName
, mName
]));
1481 pr
.expectTT(pr
.TTSemi
);
1484 else if (pr
.tokType
= pr
.TTBegin
) then
1486 //rec := mOwner.mOwner.findRecType(mEBSTypeName); // find in mapdef
1488 if (mEBSType
<> nil) and (mEBSType
is TDynRecord
) then rec
:= (mEBSType
as TDynRecord
);
1489 if (rec
= nil) then raise Exception
.Create(Format('record type ''%s'' for field ''%s'' not found', [mEBSTypeName
, mName
]));
1491 rc
.mHeaderRec
:= mOwner
.mHeaderRec
;
1495 if mOwner
.addRecordByType(mEBSTypeName
, rc
) then
1497 //raise Exception.Create(Format('record type ''%s'' for field ''%s'' not found', [mEBSTypeName, mName]));
1498 e_LogWritefln('duplicate record with id ''%s'' for field ''%s'' in record ''%s''', [rc
.mId
, mName
, mOwner
.mName
]);
1500 pr
.eatTT(pr
.TTSemi
); // hack: allow (but don't require) semicolon after inline records
1503 pr
.expectTT(pr
.TTBegin
);
1507 //es := mOwner.mOwner.findEBSType(mEBSTypeName); // find in mapdef
1509 if (mEBSType
<> nil) and (mEBSType
is TDynEBS
) then es
:= (mEBSType
as TDynEBS
);
1510 if (es
= nil) or (not es
.mIsEnum
) then raise Exception
.Create(Format('record enum type ''%s'' for field ''%s'' not found', [mEBSTypeName
, mName
]));
1511 tk
:= pr
.expectId();
1512 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
]));
1513 mIVal
:= es
.field
[tk
];
1515 //writeln('ENUM ', mEBSName, '; element <', mSVal, '> with value ', mIVal);
1517 pr
.expectTT(pr
.TTSemi
);
1522 //es := mOwner.mOwner.findEBSType(mEBSTypeName); // find in mapdef
1524 if (mEBSType
<> nil) and (mEBSType
is TDynEBS
) then es
:= (mEBSType
as TDynEBS
);
1525 if (es
= nil) or es
.mIsEnum
then raise Exception
.Create(Format('record bitset type ''%s'' for field ''%s'' not found', [mEBSTypeName
, mName
]));
1529 tk
:= pr
.expectId();
1530 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
]));
1531 mIVal
:= mIVal
or es
.field
[tk
];
1533 if (pr
.tokType
<> pr
.TTDelim
) or ((pr
.tokChar
<> '|') and (pr
.tokChar
<> '+')) then break
;
1534 if mBitSetUnique
then raise Exception
.Create(Format('record bitset of type ''%s'' for field ''%s'' expects only one value', [tk
, mEBSTypeName
, mName
]));
1535 //pr.expectDelim('|');
1536 pr
.skipToken(); // plus or pipe
1539 pr
.expectTT(pr
.TTSemi
);
1542 else raise Exception
.Create('ketmar forgot to handle some EBS type');
1548 if pr
.eatId('true') or pr
.eatId('tan') or pr
.eatId('yes') then mIVal
:= 1
1549 else if pr
.eatId('false') or pr
.eatId('ona') or pr
.eatId('no') then mIVal
:= 0
1550 else raise Exception
.Create(Format('invalid bool value for field ''%s''', [mName
]));
1552 pr
.expectTT(pr
.TTSemi
);
1557 if (mMaxDim
= 0) then raise Exception
.Create(Format('invalid string size definition for field ''%s''', [mName
]));
1558 mSVal
:= pr
.expectStr(true);
1559 if (mMaxDim
< 0) then
1562 if (Length(mSVal
) <> 1) then raise Exception
.Create(Format('invalid string size for field ''%s''', [mName
]));
1563 mIVal
:= Integer(mSVal
[1]);
1569 if (Length(mSVal
) > mMaxDim
) then raise Exception
.Create(Format('invalid string size for field ''%s''', [mName
]));
1572 pr
.expectTT(pr
.TTSemi
);
1577 parseInt(-128, 127);
1578 pr
.expectTT(pr
.TTSemi
);
1584 pr
.expectTT(pr
.TTSemi
);
1589 parseInt(-32768, 32768);
1590 pr
.expectTT(pr
.TTSemi
);
1596 pr
.expectTT(pr
.TTSemi
);
1601 parseInt(Integer($80000000), $7fffffff);
1602 pr
.expectTT(pr
.TTSemi
);
1607 parseInt(0, $7fffffff); //FIXME
1608 pr
.expectTT(pr
.TTSemi
);
1613 mSVal
:= pr
.expectStr(true);
1615 pr
.expectTT(pr
.TTSemi
);
1621 if pr
.eatDelim('[') then edim
:= ']' else begin pr
.expectDelim('('); edim
:= ')'; end;
1622 mIVal
:= pr
.expectInt();
1623 if (mType
= TType
.TSize
) then
1625 if (mIVal
< 0) or (mIVal
> 32767) then raise Exception
.Create(Format('invalid %s value for field ''%s''', [getTypeName(mType
), mName
]));
1627 mIVal2
:= pr
.expectInt();
1628 if (mType
= TType
.TSize
) then
1630 if (mIVal2
< 0) or (mIVal2
> 32767) then raise Exception
.Create(Format('invalid %s value for field ''%s''', [getTypeName(mType
), mName
]));
1633 pr
.expectDelim(edim
);
1634 pr
.expectTT(pr
.TTSemi
);
1647 else raise Exception
.Create('ketmar forgot to handle some field type');
1649 raise Exception
.Create(Format('cannot parse field ''%s'' yet', [mName
]));
1653 // ////////////////////////////////////////////////////////////////////////// //
1654 constructor TDynRecord
.Create (pr
: TTextParser
);
1656 if (pr
= nil) then raise Exception
.Create('cannot create record type without type definition');
1660 mFields
:= TDynFieldList
.Create();
1661 {$IF DEFINED(XDYNREC_USE_FIELDHASH)}
1662 mFieldsHash
:= hashNewStrInt();
1674 constructor TDynRecord
.Create ();
1678 mFields
:= TDynFieldList
.Create();
1679 {$IF DEFINED(XDYNREC_USE_FIELDHASH)}
1680 mFieldsHash
:= hashNewStrInt();
1690 destructor TDynRecord
.Destroy ();
1695 {$IF DEFINED(XDYNREC_USE_FIELDHASH)}
1707 procedure TDynRecord
.addField (fld
: TDynField
); inline;
1709 if (fld
= nil) then raise Exception
.Create('cannot append nil field to record');
1710 mFields
.append(fld
);
1711 {$IF DEFINED(XDYNREC_USE_FIELDHASH)}
1712 if (Length(fld
.mName
) > 0) then mFieldsHash
.put(fld
.mName
, mFields
.count
-1);
1717 function TDynRecord
.addFieldChecked (fld
: TDynField
): Boolean; inline; // `true`: duplicate name
1720 if (fld
= nil) then raise Exception
.Create('cannot append nil field to record');
1721 {$IF not DEFINED(XDYNREC_USE_FIELDHASH)}
1722 if (Length(fld
.mName
) > 0) then result
:= hasByName(fld
.mName
);
1724 mFields
.append(fld
);
1725 {$IF DEFINED(XDYNREC_USE_FIELDHASH)}
1726 if (Length(fld
.mName
) > 0) then result
:= mFieldsHash
.put(fld
.mName
, mFields
.count
-1);
1731 function TDynRecord
.findByName (const aname
: AnsiString): Integer; inline;
1733 {$IF DEFINED(XDYNREC_USE_FIELDHASH)}
1734 if not mFieldsHash
.get(aname
, result
) then result
:= -1;
1737 while (result
< mFields
.count
) do
1739 if StrEqu(aname
, mFields
[result
].mName
) then exit
;
1747 function TDynRecord
.hasByName (const aname
: AnsiString): Boolean; inline;
1749 result
:= (findByName(aname
) >= 0);
1753 function TDynRecord
.getFieldByName (const aname
: AnsiString): TDynField
; inline;
1757 f
:= findByName(aname
);
1758 if (f
>= 0) then result
:= mFields
[f
] else result
:= nil;
1762 function TDynRecord
.getFieldAt (idx
: Integer): TDynField
; inline;
1764 if (idx
>= 0) and (idx
< mFields
.count
) then result
:= mFields
[idx
] else result
:= nil;
1768 function TDynRecord
.getCount (): Integer; inline;
1770 result
:= mFields
.count
;
1774 function TDynRecord
.getIsTrigData (): Boolean; inline;
1776 result
:= (Length(mTrigTypes
) > 0);
1780 function TDynRecord
.getIsForTrig (const aname
: AnsiString): Boolean; inline;
1785 for f
:= 0 to High(mTrigTypes
) do if StrEqu(mTrigTypes
[f
], aname
) then exit
;
1790 function TDynRecord
.getForTrigCount (): Integer; inline;
1792 result
:= Length(mTrigTypes
);
1796 function TDynRecord
.getForTrigAt (idx
: Integer): AnsiString; inline;
1798 if (idx
>= 0) and (idx
< Length(mTrigTypes
)) then result
:= mTrigTypes
[idx
] else result
:= '';
1802 function TDynRecord
.clone (): TDynRecord
;
1807 result
:= TDynRecord
.Create();
1808 result
.mOwner
:= mOwner
;
1810 result
.mPasName
:= mPasName
;
1811 result
.mName
:= mName
;
1812 result
.mSize
:= mSize
;
1813 if (mFields
.count
> 0) then
1815 result
.mFields
.capacity
:= mFields
.count
;
1816 for fld
in mFields
do result
.addField(fld
.clone(result
));
1818 SetLength(result
.mTrigTypes
, Length(mTrigTypes
));
1819 for f
:= 0 to High(mTrigTypes
) do result
.mTrigTypes
[f
] := mTrigTypes
[f
];
1820 result
.mHeader
:= mHeader
;
1821 result
.mBinBlock
:= mBinBlock
;
1822 result
.mHeaderRec
:= mHeaderRec
;
1823 result
.mTagInt
:= mTagInt
;
1824 result
.mTagPtr
:= mTagPtr
;
1828 function TDynRecord
.findRecordByTypeId (const atypename
, aid
: AnsiString): TDynRecord
;
1834 if (Length(aid
) = 0) then exit
;
1836 fld
:= mHeaderRec
.field
[atypename
];
1837 if (fld
= nil) then exit
;
1838 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
]));
1840 if (fld
.mRVal
<> nil) then
1842 if fld
.mRHash
.get(aid
, idx
) then begin result
:= fld
.mRVal
[idx
]; exit
; end;
1848 function TDynRecord
.findRecordNumByType (const atypename
: AnsiString; rc
: TDynRecord
): Integer;
1855 fld
:= mHeaderRec
.field
[atypename
];
1856 if (fld
= nil) then exit
;
1857 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
]));
1859 if (fld
.mRVal
<> nil) then
1861 for idx
:= 0 to fld
.mRVal
.count
-1 do
1863 if (fld
.mRVal
[idx
] = rc
) then begin result
:= idx
; exit
; end;
1870 function TDynRecord
.addRecordByType (const atypename
: AnsiString; rc
: TDynRecord
): Boolean;
1875 fld
:= mHeaderRec
.field
[atypename
];
1879 fld
:= TDynField
.Create(atypename
, TDynField
.TType
.TList
);
1880 fld
.mOwner
:= mHeaderRec
;
1881 mHeaderRec
.addField(fld
);
1883 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
]));
1885 if (fld
.mRVal
= nil) then
1887 fld
.mRVal
:= TDynRecList
.Create();
1888 fld
.mRHash
:= hashNewStrInt();
1890 result
:= fld
.addListItem(rc
);
1894 function TDynRecord
.isSimpleEqu (rec
: TDynRecord
): Boolean;
1898 if (rec
= nil) then begin result
:= false; exit
; end; // self.mRecRef can't be `nil` here
1899 if (rec
= self
) then begin result
:= true; exit
; end;
1900 if (mFields
.count
<> rec
.mFields
.count
) then begin result
:= false; exit
; end;
1902 for f
:= 0 to mFields
.count
-1 do
1904 if not mFields
[f
].isSimpleEqu(rec
.mFields
[f
]) then exit
;
1910 function TDynRecord
.trigTypeField (): TDynField
;
1915 for fld
in mFields
do
1917 if (fld
.mEBS
<> TDynField
.TEBS
.TEnum
) then continue
;
1918 if not (fld
.mEBSType
is TDynEBS
) then continue
;
1919 es
:= (fld
.mEBSType
as TDynEBS
);
1921 if StrEqu(es
.mName
, 'TriggerType') then begin result
:= fld
; exit
; end;
1927 // number of records of the given instance
1928 function TDynRecord
.instanceCount (const typename
: AnsiString): Integer;
1933 fld
:= field
[typename
];
1934 if (fld
<> nil) and (fld
.mType
= fld
.TType
.TList
) then result
:= fld
.mRVal
.count
;
1938 procedure TDynRecord
.setUserField (const fldname
: AnsiString; v
: LongInt);
1942 if (Length(fldname
) = 0) then exit
;
1943 fld
:= field
[fldname
];
1944 if (fld
<> nil) then
1946 if (fld
.mType
<> fld
.TType
.TInt
) or (fld
.mEBS
<> fld
.TEBS
.TNone
) then
1948 raise Exception
.Create(Format('invalid user field ''%s'' type', [fld
.name
]));
1953 fld
:= TDynField
.Create(fldname
, fld
.TType
.TInt
);
1956 fld
.mInternal
:= true;
1957 fld
.mDefined
:= true;
1963 procedure TDynRecord
.setUserField (const fldname
: AnsiString; v
: AnsiString);
1967 if (Length(fldname
) = 0) then exit
;
1968 fld
:= field
[fldname
];
1969 if (fld
<> nil) then
1971 if (fld
.mType
<> fld
.TType
.TString
) or (fld
.mEBS
<> fld
.TEBS
.TNone
) then
1973 raise Exception
.Create(Format('invalid user field ''%s'' type', [fld
.name
]));
1978 fld
:= TDynField
.Create(fldname
, fld
.TType
.TString
);
1981 fld
.mInternal
:= true;
1982 fld
.mDefined
:= true;
1988 procedure TDynRecord
.parseDef (pr
: TTextParser
);
1993 if pr
.eatId('TriggerData') then
1996 if pr
.eatDelim('(') then
2000 while pr
.eatTT(pr
.TTComma
) do begin end;
2001 if pr
.eatDelim(')') then break
;
2002 tdn
:= pr
.expectId();
2003 if isForTrig
[tdn
] then raise Exception
.Create(Format('duplicate trigdata ''%s'' trigtype ''%s''', [mName
, tdn
]));
2004 SetLength(mTrigTypes
, Length(mTrigTypes
)+1);
2005 mTrigTypes
[High(mTrigTypes
)] := tdn
;
2010 tdn
:= pr
.expectId();
2011 SetLength(mTrigTypes
, 1);
2012 mTrigTypes
[0] := tdn
;
2014 mName
:= 'TriggerData';
2018 mPasName
:= pr
.expectId(); // pascal record name
2020 mName
:= pr
.expectStr();
2021 while (pr
.tokType
<> pr
.TTBegin
) do
2023 if pr
.eatId('header') then begin mHeader
:= true; continue
; end;
2024 if pr
.eatId('size') then
2026 if (mSize
> 0) then raise Exception
.Create(Format('duplicate `size` in record ''%s''', [mName
]));
2027 mSize
:= pr
.expectInt();
2028 if (mSize
< 1) then raise Exception
.Create(Format('invalid record ''%s'' size: %d', [mName
, mSize
]));
2029 pr
.expectId('bytes');
2032 if pr
.eatId('binblock') then
2034 if (mBinBlock
>= 0) then raise Exception
.Create(Format('duplicate `binblock` in record ''%s''', [mName
]));
2035 mBinBlock
:= pr
.expectInt();
2036 if (mBinBlock
< 1) then raise Exception
.Create(Format('invalid record ''%s'' binblock: %d', [mName
, mBinBlock
]));
2042 pr
.expectTT(pr
.TTBegin
);
2044 while (pr
.tokType
<> pr
.TTEnd
) do
2046 fld
:= TDynField
.Create(pr
);
2047 //if hasByName(fld.name) then begin fld.Free(); raise Exception.Create(Format('duplicate field ''%s''', [fld.name])); end;
2050 if addFieldChecked(fld
) then
2053 raise Exception
.Create(Format('duplicate field ''%s''', [fld
.name
]));
2057 pr
.expectTT(pr
.TTEnd
);
2061 function TDynRecord
.pasdef (): AnsiString;
2073 result
:= ' '+mPasName
+' = packed record'#10;
2075 for fld
in mFields
do
2077 if fld
.mInternal
then continue
;
2078 if (fld
.mBinOfs
< 0) then continue
;
2079 result
+= ' '+fld
.pasdef
+#10;
2081 result
+= ' end;'#10;
2085 function TDynRecord
.definition (): AnsiString;
2092 result
:= 'TriggerData for ';
2093 if (Length(mTrigTypes
) > 1) then
2096 for f
:= 0 to High(mTrigTypes
) do
2098 if (f
<> 0) then result
+= ', ';
2099 result
+= mTrigTypes
[f
];
2105 result
+= mTrigTypes
[0];
2111 result
:= mPasName
+' is '+quoteStr(mName
);
2112 if (mSize
>= 0) then result
+= Format(' size %d bytes', [mSize
]);
2113 if mHeader
then result
+= ' header';
2116 for f
:= 0 to mFields
.count
-1 do
2119 result
+= mFields
[f
].definition
;
2126 procedure TDynRecord
.parseBinValue (st
: TStream
; forceData
: Boolean=false);
2132 loaded
: array[0..255] of Boolean;
2133 rec
, rect
: TDynRecord
;
2136 mst
: TSFSMemoryChunkStream
= nil;
2138 procedure linkNames (rec
: TDynRecord
);
2143 //writeln('*** rec: ', rec.mName, '.', rec.mId, ' (', rec.mFields.count, ')');
2144 for fld
in rec
.mFields
do
2146 if (fld
.mType
= TDynField
.TType
.TTrigData
) then
2148 if (fld
.mRecRef
<> nil) then linkNames(fld
.mRecRef
);
2151 if (Length(fld
.mRecRefId
) = 0) then continue
;
2152 assert(fld
.mEBSType
<> nil);
2153 rt
:= findRecordByTypeId(fld
.mEBSTypeName
, fld
.mRecRefId
);
2156 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
);
2157 //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]));
2159 //writeln(' ', rec.mName, '.', rec.mId, ':', fld.mName, ' -> ', rt.mName, '.', rt.mId, ' (', fld.mEBSTypeName, '.', fld.mRecRefId, ')');
2160 fld
.mRecRefId
:= '';
2162 fld
.mDefined
:= true;
2164 for fld
in rec
.mFields
do
2166 //writeln(' ', fld.mName);
2167 fld
.fixDefaultValue(); // just in case
2172 for f
:= 0 to High(loaded
) do loaded
[f
] := false;
2173 mst
:= TSFSMemoryChunkStream
.Create(nil, 0);
2175 if mHeader
and not forceData
then
2177 // parse map file as sequence of blocks
2179 st
.ReadBuffer(sign
[1], 4);
2180 if (sign
<> 'MAP'#1) then raise Exception
.Create('invalid binary map signature');
2182 while (st
.position
< st
.size
) do
2184 btype
:= readByte(st
);
2185 if (btype
= 0) then break
; // no more blocks
2186 readLongWord(st
); // reserved
2187 bsize
:= readLongInt(st
);
2188 {$IF DEFINED(D2D_XDYN_DEBUG)}writeln('btype=', btype
, '; bsize=', bsize
);{$ENDIF}
2189 if (bsize
< 0) or (bsize
> $1fffffff) then raise Exception
.Create(Format('block of type %d has invalid size %d', [btype
, bsize
]));
2190 if loaded
[btype
] then raise Exception
.Create(Format('block of type %d already loaded', [btype
]));
2191 loaded
[btype
] := true;
2192 // find record type for this block
2194 for rec
in mOwner
.recTypes
do if (rec
.mBinBlock
= btype
) then begin rect
:= rec
; break
; end;
2195 if (rect
= nil) then raise Exception
.Create(Format('block of type %d has no corresponding record', [btype
]));
2196 //writeln('found type ''', rec.mName, ''' for block type ', btype);
2197 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
]));
2199 if (rect
.mHeader
) then
2201 if (bsize
<> mSize
) then raise Exception
.Create(Format('header block of type %d has invalid number of records', [btype
]));
2203 st
.ReadBuffer(buf
^, bsize
);
2204 mst
.setup(buf
, mSize
);
2205 parseBinValue(mst
, true); // force parsing data
2209 // create list for this type
2210 fld
:= TDynField
.Create(rec
.mName
, TDynField
.TType
.TList
);
2216 st
.ReadBuffer(buf
^, bsize
);
2217 for f
:= 0 to (bsize
div rec
.mSize
)-1 do
2219 mst
.setup(buf
+f
*rec
.mSize
, rec
.mSize
);
2220 rec
:= rect
.clone();
2221 rec
.mHeaderRec
:= self
;
2222 rec
.parseBinValue(mst
);
2223 rec
.mId
:= Format('%s%d', [rec
.mName
, f
]);
2224 fld
.addListItem(rec
);
2225 //writeln('parsed ''', rec.mId, '''...');
2231 //st.position := st.position+bsize;
2234 for fld
in mFields
do
2236 if (fld
.mType
<> TDynField
.TType
.TList
) then continue
;
2237 for rec
in fld
.mRVal
do linkNames(rec
);
2243 if StrEqu(mName
, 'TriggerData') then mSize
:= Integer(st
.size
-st
.position
);
2244 if (mSize
< 1) then raise Exception
.Create(Format('cannot read record of type ''%s'' with unknown size', [mName
]));
2246 st
.ReadBuffer(buf
^, mSize
);
2247 for fld
in mFields
do
2249 if fld
.mInternal
then continue
;
2250 if (fld
.mBinOfs
< 0) then continue
;
2251 if (fld
.mBinOfs
>= st
.size
) then raise Exception
.Create(Format('record of type ''%s'' has invalid field ''%s''', [fld
.mName
]));
2252 mst
.setup(buf
+fld
.mBinOfs
, mSize
-fld
.mBinOfs
);
2253 //writeln('parsing ''', mName, '.', fld.mName, '''...');
2254 fld
.parseBinValue(mst
);
2258 if (buf
<> nil) then FreeMem(buf
);
2263 procedure TDynRecord
.writeBinTo (st
: TStream
; trigbufsz
: Integer=-1; onlyFields
: Boolean=false);
2266 rec
, rv
: TDynRecord
;
2269 blk
, blkmax
: Integer;
2274 if (trigbufsz
< 0) then
2276 if (mBinBlock
< 1) then raise Exception
.Create('cannot write binary record without block number');
2277 if (mSize
< 1) then raise Exception
.Create('cannot write binary record without size');
2286 FillChar(buf
^, bufsz
, 0);
2287 ws
:= TSFSMemoryChunkStream
.Create(buf
, bufsz
);
2289 // write normal fields
2290 for fld
in mFields
do
2293 if (fld
.mType
= fld
.TType
.TList
) then continue
; // later
2294 if fld
.mInternal
then continue
;
2295 if (fld
.mBinOfs
< 0) then continue
;
2296 if (fld
.mBinOfs
>= bufsz
) then raise Exception
.Create('binary value offset is outside of the buffer');
2297 TSFSMemoryChunkStream(ws
).setup(buf
+fld
.mBinOfs
, bufsz
-fld
.mBinOfs
);
2298 //writeln('writing field <', fld.mName, '>');
2302 // write block with normal fields
2303 if mHeader
and not onlyFields
then
2305 //writeln('writing header...');
2306 // signature and version
2307 writeIntBE(st
, LongWord($4D415001));
2308 writeInt(st
, Byte(mBinBlock
)); // type
2309 writeInt(st
, LongWord(0)); // reserved
2310 writeInt(st
, LongWord(bufsz
)); // size
2312 st
.WriteBuffer(buf
^, bufsz
);
2314 ws
.Free(); ws
:= nil;
2315 FreeMem(buf
); buf
:= nil;
2317 // write other blocks, if any
2318 if mHeader
and not onlyFields
then
2322 for fld
in mFields
do
2325 if (fld
.mType
= fld
.TType
.TList
) then
2327 if (fld
.mRVal
= nil) or (fld
.mRVal
.count
= 0) then continue
;
2328 rec
:= mOwner
.findRecType(fld
.mName
);
2329 if (rec
= nil) then continue
;
2330 if (rec
.mBinBlock
<= 0) then continue
;
2331 if (blkmax
< rec
.mBinBlock
) then blkmax
:= rec
.mBinBlock
;
2335 for blk
:= 1 to blkmax
do
2337 if (blk
= mBinBlock
) then continue
;
2339 for fld
in mFields
do
2342 if (fld
.mType
= fld
.TType
.TList
) then
2344 if (fld
.mRVal
= nil) or (fld
.mRVal
.count
= 0) then continue
;
2345 rec
:= mOwner
.findRecType(fld
.mName
);
2346 if (rec
= nil) then continue
;
2347 if (rec
.mBinBlock
<> blk
) then continue
;
2348 if (ws
= nil) then ws
:= TMemoryStream
.Create();
2349 for rv
in fld
.mRVal
do rv
.writeBinTo(ws
);
2355 blksz
:= Integer(ws
.position
);
2357 writeInt(st
, Byte(blk
)); // type
2358 writeInt(st
, LongWord(0)); // reserved
2359 writeInt(st
, LongWord(blksz
)); // size
2360 st
.CopyFrom(ws
, blksz
);
2366 writeInt(st
, Byte(0));
2367 writeInt(st
, LongWord(0));
2368 writeInt(st
, LongWord(0));
2372 if (buf
<> nil) then FreeMem(buf
);
2377 procedure TDynRecord
.writeTo (wr
: TTextWriter
; putHeader
: Boolean=true);
2385 if (Length(mId
) > 0) then begin wr
.put(' '); wr
.put(mId
); end;
2391 for fld
in mFields
do
2394 if (fld
.mType
= fld
.TType
.TList
) then
2396 if not mHeader
then raise Exception
.Create('record list in non-header record');
2397 if (fld
.mRVal
<> nil) then
2399 for rec
in fld
.mRVal
do
2401 if (Length(rec
.mId
) = 0) then continue
;
2403 rec
.writeTo(wr
, true);
2408 if fld
.mInternal
then continue
;
2409 if fld
.mOmitDef
and fld
.isDefaultValue
then continue
;
2421 {$IF DEFINED(D2D_DYNREC_PROFILER)}
2423 profCloneRec
: UInt64 = 0;
2424 profFindRecType
: UInt64 = 0;
2425 profFieldSearching
: UInt64 = 0;
2426 profListDupChecking
: UInt64 = 0;
2427 profAddRecByType
: UInt64 = 0;
2428 profFieldValParsing
: UInt64 = 0;
2429 profFixDefaults
: UInt64 = 0;
2430 profRecValParse
: UInt64 = 0;
2432 procedure xdynDumpProfiles ();
2434 writeln('=== XDYNREC PROFILES ===');
2435 writeln('record cloning: ', profCloneRec
div 1000, '.', profCloneRec
mod 1000, ' milliseconds');
2436 writeln('findRecType : ', profFindRecType
div 1000, '.', profFindRecType
mod 1000, ' milliseconds');
2437 writeln('field[] : ', profFieldSearching
div 1000, '.', profFieldSearching
mod 1000, ' milliseconds');
2438 writeln('list dup check: ', profListDupChecking
div 1000, '.', profListDupChecking
mod 1000, ' milliseconds');
2439 writeln('addRecByType : ', profAddRecByType
div 1000, '.', profAddRecByType
mod 1000, ' milliseconds');
2440 writeln('field valparse: ', profFieldValParsing
div 1000, '.', profFieldValParsing
mod 1000, ' milliseconds');
2441 writeln('fix defaults : ', profFixDefaults
div 1000, '.', profFixDefaults
mod 1000, ' milliseconds');
2442 writeln('recvalparse : ', profRecValParse
div 1000, '.', profRecValParse
mod 1000, ' milliseconds');
2447 procedure TDynRecord
.parseValue (pr
: TTextParser
; beginEaten
: Boolean=false);
2450 rec
: TDynRecord
= nil;
2451 trc
{, rv}: TDynRecord
;
2452 {$IF DEFINED(D2D_DYNREC_PROFILER)}
2456 if (mOwner
= nil) then raise Exception
.Create(Format('can''t parse record ''%s'' value without owner', [mName
]));
2458 {$IF DEFINED(D2D_DYNREC_PROFILER)}stall
:= curTimeMicro();{$ENDIF}
2464 if (not beginEaten
) and (pr
.tokType
= pr
.TTId
) then mId
:= pr
.expectId();
2468 assert(mHeaderRec
= self
);
2471 //writeln('parsing record <', mName, '>');
2472 if not beginEaten
then pr
.expectTT(pr
.TTBegin
);
2473 while (pr
.tokType
<> pr
.TTEnd
) do
2475 if (pr
.tokType
<> pr
.TTId
) then raise Exception
.Create('identifier expected');
2476 //writeln('<', mName, '.', pr.tokStr, '>');
2481 // add records with this type (if any)
2482 {$IF DEFINED(D2D_DYNREC_PROFILER)}stt
:= curTimeMicro();{$ENDIF}
2483 trc
:= mOwner
.findRecType(pr
.tokStr
);
2484 {$IF DEFINED(D2D_DYNREC_PROFILER)}profFindRecType
:= curTimeMicro()-stt
;{$ENDIF}
2485 if (trc
<> nil) then
2487 {$IF DEFINED(D2D_DYNREC_PROFILER)}stt
:= curTimeMicro();{$ENDIF}
2489 {$IF DEFINED(D2D_DYNREC_PROFILER)}profCloneRec
:= curTimeMicro()-stt
;{$ENDIF}
2490 rec
.mHeaderRec
:= mHeaderRec
;
2495 if (Length(rec.mId) > 0) then
2497 {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
2498 fld := field[pr.tokStr];
2499 {$IF DEFINED(D2D_DYNREC_PROFILER)}profFieldSearching := curTimeMicro()-stt;{$ENDIF}
2501 if (fld <> nil) and (fld.mRVal <> nil) then
2503 {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
2504 //idtmp := trc.mName+':'+rec.mId;
2505 //if ids.put(idtmp, 1) then raise Exception.Create(Format('duplicate thing ''%s'' in record ''%s''', [fld.mName, mName]));
2506 if fld.mRHash.has(rec.mId) then raise Exception.Create(Format('duplicate thing ''%s'' in record ''%s''', [fld.mName, mName]));
2507 {$IF DEFINED(D2D_DYNREC_PROFILER)}profListDupChecking := curTimeMicro()-stt;{$ENDIF}
2511 {$IF DEFINED(D2D_DYNREC_PROFILER)}stt
:= curTimeMicro();{$ENDIF}
2512 addRecordByType(rec
.mName
, rec
);
2513 {$IF DEFINED(D2D_DYNREC_PROFILER)}profAddRecByType
:= curTimeMicro()-stt
;{$ENDIF}
2523 {$IF DEFINED(D2D_DYNREC_PROFILER)}stt
:= curTimeMicro();{$ENDIF}
2524 fld
:= field
[pr
.tokStr
];
2525 {$IF DEFINED(D2D_DYNREC_PROFILER)}profFieldSearching
:= curTimeMicro()-stt
;{$ENDIF}
2526 if (fld
<> nil) then
2528 if fld
.defined
then raise Exception
.Create(Format('duplicate field ''%s'' in record ''%s''', [fld
.mName
, mName
]));
2529 if fld
.internal
then raise Exception
.Create(Format('internal field ''%s'' in record ''%s''', [fld
.mName
, mName
]));
2531 {$IF DEFINED(D2D_DYNREC_PROFILER)}stt
:= curTimeMicro();{$ENDIF}
2533 {$IF DEFINED(D2D_DYNREC_PROFILER)}profFieldValParsing
:= curTimeMicro()-stt
;{$ENDIF}
2537 // something is wrong
2538 raise Exception
.Create(Format('unknown field ''%s'' in record ''%s''', [pr
.tokStr
, mName
]));
2540 pr
.expectTT(pr
.TTEnd
);
2541 // fix field defaults
2542 {$IF DEFINED(D2D_DYNREC_PROFILER)}stt
:= curTimeMicro();{$ENDIF}
2543 for fld
in mFields
do fld
.fixDefaultValue();
2544 {$IF DEFINED(D2D_DYNREC_PROFILER)}profFixDefaults
:= curTimeMicro()-stt
;{$ENDIF}
2545 //writeln('done parsing record <', mName, '>');
2546 //{$IF DEFINED(D2D_DYNREC_PROFILER)}writeln('stall: ', curTimeMicro()-stall);{$ENDIF}
2547 {$IF DEFINED(D2D_DYNREC_PROFILER)}profRecValParse
:= curTimeMicro()-stall
;{$ENDIF}
2551 // ////////////////////////////////////////////////////////////////////////// //
2552 constructor TDynEBS
.Create (pr
: TTextParser
);
2559 destructor TDynEBS
.Destroy ();
2566 procedure TDynEBS
.cleanup ();
2577 function TDynEBS
.findByName (const aname
: AnsiString): Integer;
2580 while (result
< Length(mIds
)) do
2582 if StrEqu(aname
, mIds
[result
]) then exit
;
2589 function TDynEBS
.hasByName (const aname
: AnsiString): Boolean; inline;
2591 result
:= (findByName(aname
) >= 0);
2595 function TDynEBS
.getFieldByName (const aname
: AnsiString): Integer; inline;
2599 f
:= findByName(aname
);
2600 if (f
>= 0) then result
:= mVals
[f
] else result
:= 0;
2604 function TDynEBS
.definition (): AnsiString;
2608 if mIsEnum
then result
:='enum ' else result
:= 'bitset ';
2612 if mIsEnum
then cv
:= 0 else cv
:= 1;
2613 for f
:= 0 to High(mIds
) do
2615 if (mIds
[f
] = mMaxName
) then continue
;
2616 result
+= ' '+mIds
[f
];
2617 if (mVals
[f
] <> cv
) then
2619 result
+= Format(' = %d', [mVals
[f
]]);
2620 if mIsEnum
then cv
:= mVals
[f
];
2625 result
+= Format(', // %d'#10, [mVals
[f
]]);
2627 if mIsEnum
then Inc(cv
) else if (mVals
[f
] = cv
) then cv
:= cv
shl 1;
2630 if (Length(mMaxName
) > 0) then result
+= ' '+mMaxName
+' = MAX,'#10;
2635 function TDynEBS
.pasdef (): AnsiString;
2639 result
:= '// '+mName
+#10'const'#10;
2641 for f
:= 0 to High(mIds
) do
2643 result
+= formatstrf(' %s = %d;'#10, [mIds
[f
], mVals
[f
]]);
2648 function TDynEBS
.nameByValue (v
: Integer): AnsiString;
2652 for f
:= 0 to High(mVals
) do
2654 if (mVals
[f
] = v
) then begin result
:= mIds
[f
]; exit
; end;
2660 procedure TDynEBS
.parseDef (pr
: TTextParser
);
2668 if pr
.eatId('enum') then mIsEnum
:= true
2669 else if pr
.eatId('bitset') then mIsEnum
:= false
2670 else pr
.expectId('enum');
2671 mName
:= pr
.expectId();
2672 mMaxVal
:= Integer($80000000);
2673 if mIsEnum
then cv
:= 0 else cv
:= 1;
2674 pr
.expectTT(pr
.TTBegin
);
2675 while (pr
.tokType
<> pr
.TTEnd
) do
2677 idname
:= pr
.expectId();
2678 for f
:= 0 to High(mIds
) do
2680 if StrEqu(mIds
[f
], idname
) then raise Exception
.Create(Format('duplicate field ''%s'' in enum/bitset ''%s''', [idname
, mName
]));
2682 if StrEqu(mMaxName
, idname
) then raise Exception
.Create(Format('duplicate field ''%s'' in enum/bitset ''%s''', [idname
, mName
]));
2687 if pr
.eatDelim('=') then
2689 if pr
.eatId('MAX') then
2691 if (Length(mMaxName
) > 0) then raise Exception
.Create(Format('duplicate max field ''%s'' in enum/bitset ''%s''', [idname
, mName
]));
2697 v
:= pr
.expectInt();
2698 if mIsEnum
then cv
:= v
;
2706 if mIsEnum
or (not hasV
) then
2708 if (mMaxVal
< v
) then mMaxVal
:= v
;
2710 SetLength(mIds
, Length(mIds
)+1);
2711 mIds
[High(mIds
)] := idname
;
2712 SetLength(mVals
, Length(mIds
));
2713 mVals
[High(mVals
)] := v
;
2715 if mIsEnum
or (not hasV
) then
2717 if mIsEnum
then Inc(cv
) else cv
:= cv
shl 1;
2720 if (pr
.tokType
= pr
.TTEnd
) then break
;
2721 pr
.expectTT(pr
.TTComma
);
2722 while pr
.eatTT(pr
.TTComma
) do begin end;
2724 pr
.expectTT(pr
.TTEnd
);
2726 if (Length(mMaxName
) > 0) then
2728 SetLength(mIds
, Length(mIds
)+1);
2729 mIds
[High(mIds
)] := mMaxName
;
2730 SetLength(mVals
, Length(mIds
));
2731 mVals
[High(mVals
)] := mMaxVal
;
2736 // ////////////////////////////////////////////////////////////////////////// //
2737 constructor TDynMapDef
.Create (pr
: TTextParser
);
2739 recTypes
:= TDynRecList
.Create();
2740 trigTypes
:= TDynRecList
.Create();
2741 ebsTypes
:= TDynEBSList
.Create();
2746 destructor TDynMapDef
.Destroy ();
2751 for rec
in recTypes
do rec
.Free();
2752 for rec
in trigTypes
do rec
.Free();
2753 for ebs
in ebsTypes
do ebs
.Free();
2764 function TDynMapDef
.getHeaderRecType (): TDynRecord
; inline;
2766 if (recTypes
.count
= 0) then raise Exception
.Create('no header in empty mapdef');
2767 result
:= recTypes
[0];
2771 function TDynMapDef
.findRecType (const aname
: AnsiString): TDynRecord
;
2775 for rec
in recTypes
do
2777 if StrEqu(rec
.name
, aname
) then begin result
:= rec
; exit
; end;
2783 function TDynMapDef
.findTrigFor (const aname
: AnsiString): TDynRecord
;
2787 for rec
in trigTypes
do
2789 if (rec
.isForTrig
[aname
]) then begin result
:= rec
; exit
; end;
2795 function TDynMapDef
.findEBSType (const aname
: AnsiString): TDynEBS
;
2799 for ebs
in ebsTypes
do
2801 if StrEqu(ebs
.name
, aname
) then begin result
:= ebs
; exit
; end;
2807 procedure TDynMapDef
.parseDef (pr
: TTextParser
);
2809 rec
, hdr
: TDynRecord
;
2813 // setup header links and type links
2814 procedure linkRecord (rec
: TDynRecord
);
2818 rec
.mHeaderRec
:= recTypes
[0];
2819 for fld
in rec
.mFields
do
2821 if (fld
.mType
= fld
.TType
.TTrigData
) then continue
;
2823 TDynField
.TEBS
.TNone
: begin end;
2824 TDynField
.TEBS
.TRec
:
2826 fld
.mEBSType
:= findRecType(fld
.mEBSTypeName
);
2827 if (fld
.mEBSType
= nil) then raise Exception
.Create(Format('field ''%s'' of type ''%s'' has no correcponding record definition', [fld
.mName
, fld
.mEBSTypeName
]));
2829 TDynField
.TEBS
.TEnum
,
2830 TDynField
.TEBS
.TBitSet
:
2832 fld
.mEBSType
:= findEBSType(fld
.mEBSTypeName
);
2833 if (fld
.mEBSType
= nil) then raise Exception
.Create(Format('field ''%s'' of type ''%s'' has no correcponding enum/bitset', [fld
.mName
, fld
.mEBSTypeName
]));
2834 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
]));
2840 // setup default values
2841 procedure fixRecordDefaults (rec
: TDynRecord
);
2845 for fld
in rec
.mFields
do if fld
.mHasDefault
then fld
.parseDefaultValue();
2852 if not pr
.skipBlanks() then break
;
2853 if (pr
.tokType
<> pr
.TTId
) then raise Exception
.Create('identifier expected');
2855 if (pr
.tokStr
= 'enum') or (pr
.tokStr
= 'bitset') then
2857 eb
:= TDynEBS
.Create(pr
);
2858 if (findEBSType(eb
.name
) <> nil) then
2861 raise Exception
.Create(Format('duplicate enum/bitset ''%s''', [eb
.name
]));
2864 ebsTypes
.append(eb
);
2865 //writeln(eb.definition); writeln;
2869 if (pr
.tokStr
= 'TriggerData') then
2871 rec
:= TDynRecord
.Create(pr
);
2872 for f
:= 0 to High(rec
.mTrigTypes
) do
2874 if (findTrigFor(rec
.mTrigTypes
[f
]) <> nil) then
2877 raise Exception
.Create(Format('duplicate trigdata ''%s''', [rec
.mTrigTypes
[f
]]));
2881 trigTypes
.append(rec
);
2882 //writeln(dr.definition); writeln;
2886 rec
:= TDynRecord
.Create(pr
);
2887 //writeln(dr.definition); writeln;
2888 if (findRecType(rec
.name
) <> nil) then begin rec
.Free(); raise Exception
.Create(Format('duplicate record ''%s''', [rec
.name
])); end;
2889 if (hdr
<> nil) and StrEqu(rec
.name
, hdr
.name
) then begin rec
.Free(); raise Exception
.Create(Format('duplicate record ''%s''', [rec
.name
])); end;
2893 if (hdr
<> nil) then begin rec
.Free(); raise Exception
.Create(Format('duplicate header record ''%s'' (previous is ''%s'')', [rec
.name
, hdr
.name
])); end;
2898 recTypes
.append(rec
);
2902 // put header record to top
2903 if (hdr
= nil) then raise Exception
.Create('header definition not found in mapdef');
2904 recTypes
.append(nil);
2905 for f
:= recTypes
.count
-1 downto 1 do recTypes
[f
] := recTypes
[f
-1];
2908 // setup header links and type links
2909 for rec
in recTypes
do linkRecord(rec
);
2910 for rec
in trigTypes
do linkRecord(rec
);
2912 // setup default values
2913 for rec
in recTypes
do fixRecordDefaults(rec
);
2914 for rec
in trigTypes
do fixRecordDefaults(rec
);
2918 // ////////////////////////////////////////////////////////////////////////// //
2919 function TDynMapDef
.parseMap (pr
: TTextParser
): TDynRecord
;
2921 res
: TDynRecord
= nil;
2925 pr
.expectId(headerType
.name
);
2926 res
:= headerType
.clone();
2927 res
.mHeaderRec
:= res
;
2937 function TDynMapDef
.parseBinMap (st
: TStream
): TDynRecord
;
2939 res
: TDynRecord
= nil;
2943 res
:= headerType
.clone();
2944 res
.mHeaderRec
:= res
;
2945 res
.parseBinValue(st
);
2954 function TDynMapDef
.pasdef (): AnsiString;
2963 result
+= '// ////////////////////////////////////////////////////////////////////////// //'#10;
2964 result
+= '// enums and bitsets'#10;
2965 for ebs
in ebsTypes
do result
+= #10+ebs
.pasdef();
2966 result
+= #10#10'// ////////////////////////////////////////////////////////////////////////// //'#10;
2967 result
+= '// records'#10'type'#10;
2968 for rec
in recTypes
do
2970 if (rec
.mSize
< 1) then continue
;
2971 result
+= rec
.pasdef();
2974 result
+= #10#10'// ////////////////////////////////////////////////////////////////////////// //'#10;
2975 result
+= '// triggerdata'#10'type'#10;
2976 result
+= ' TTriggerData = record'#10;
2977 result
+= ' case Byte of'#10;
2978 result
+= ' 0: (Default: Byte128);'#10;
2979 for rec
in trigTypes
do
2983 for tn
in rec
.mTrigTypes
do
2985 if needComma
then result
+= ', ' else needComma
:= true;
2989 for fld
in rec
.mFields
do
2991 if fld
.mInternal
then continue
;
2992 if (fld
.mBinOfs
< 0) then continue
;
2993 result
+= ' '+fld
.pasdef
+#10;
2997 result
+= ' end;'#10;
3001 function TDynMapDef
.pasdefconst (): AnsiString;
3006 result
+= '// ////////////////////////////////////////////////////////////////////////// //'#10;
3007 result
+= '// enums and bitsets'#10;
3008 for ebs
in ebsTypes
do result
+= #10+ebs
.pasdef();
3012 function TDynMapDef
.getTrigTypeCount (): Integer; inline; begin result
:= trigTypes
.count
; end;
3013 function TDynMapDef
.getTrigTypeAt (idx
: Integer): TDynRecord
; inline; begin if (idx
>= 0) and (idx
< trigTypes
.count
) then result
:= trigTypes
[idx
] else result
:= nil; end;