DEADSOFTWARE

textmap: dramatically increased text map parsing speed (text map now parses in the...
[d2df-sdl.git] / src / shared / xdynrec.pas
index 087724d7949217d016d2a03006c6470a6422e324..3e9ba66b1b9f4c3d6608857e7edf95d3842c907d 100644 (file)
@@ -45,9 +45,6 @@ type
       // TTrigData: array of mMaxDim bytes, but internally a record (mRecRef)
       // arrays of chars are pascal shortstrings (with counter in the first byte)
 
-      TDynFieldArray = array of TDynField;
-      TDynRecordArray = array of TDynRecord;
-
   private
     type
       TEBS = (TNone, TRec, TEnum, TBitSet);
@@ -73,6 +70,7 @@ type
     mInternal: Boolean;
     mNegBool: Boolean;
     mBitSetUnique: Boolean; // bitset can contain only one value
+    mAsMonsterId: Boolean; // special hack for triggers: monster record number+1 in binary (so 0 means "none")
     // default value
     mDefUnparsed: AnsiString;
     mDefSVal: AnsiString; // default string value
@@ -102,6 +100,7 @@ type
     class function getTypeName (t: TType): AnsiString;
 
     function definition (): AnsiString;
+    function pasdef (): AnsiString;
 
     function clone (newOwner: TDynRecord=nil): TDynField;
 
@@ -124,15 +123,12 @@ type
     property internal: Boolean read mInternal write mInternal;
     property ival: Integer read mIVal;
     property sval: AnsiString read mSVal;
-    //property list: TDynRecordArray read mRVal write mRVal;
-    property maxdim: Integer read mMaxDim; // for fixed-size arrays
-    property binOfs: Integer read mBinOfs; // offset in binary; <0 - none
-    property recOfs: Integer read mRecOfs; // offset in record; <0 - none
     property hasDefault: Boolean read mHasDefault;
     property defsval: AnsiString read mDefSVal;
     property ebs: TEBS read mEBS;
     property ebstype: TObject read mEBSType;
     property ebstypename: AnsiString read mEBSTypeName; // enum/bitset name
+    property list: TDynRecList read mRVal; // for list
 
     property x: Integer read mIVal;
     property w: Integer read mIVal;
@@ -176,6 +172,7 @@ type
     destructor Destroy (); override;
 
     function definition (): AnsiString;
+    function pasdef (): AnsiString;
 
     function clone (): TDynRecord;
 
@@ -185,7 +182,13 @@ type
     procedure parseBinValue (st: TStream; forceData: Boolean=false);
 
     procedure writeTo (wr: TTextWriter; putHeader: Boolean=true);
-    procedure writeBinTo (st: TStream; trigbufsz: Integer=-1);
+    procedure writeBinTo (st: TStream; trigbufsz: Integer=-1; onlyFields: Boolean=false);
+
+    // find field with `TriggerType` type
+    function trigTypeField (): TDynField;
+
+    // number of records of the given instance
+    function instanceCount (const typename: AnsiString): Integer;
 
   public
     property id: AnsiString read mId; // for map parser
@@ -197,9 +200,10 @@ type
     property field[const aname: AnsiString]: TDynField read getFieldByName;
     property isTrigData: Boolean read getIsTrigData;
     property isForTrig[const aname: AnsiString]: Boolean read getIsForTrig;
+    property headerType: TDynRecord read mHeaderRec;
+    property isHeader: Boolean read mHeader;
   end;
 
-
   TDynEBS = class
   private
     mOwner: TDynMapDef;
@@ -224,6 +228,7 @@ type
     destructor Destroy (); override;
 
     function definition (): AnsiString;
+    function pasdef (): AnsiString;
 
     // return empty string if not found
     function nameByValue (v: Integer): AnsiString;
@@ -255,6 +260,8 @@ type
     function findTrigFor (const aname: AnsiString): TDynRecord;
     function findEBSType (const aname: AnsiString): TDynEBS;
 
+    function pasdef (): AnsiString;
+
     // creates new header record
     function parseMap (pr: TTextParser): TDynRecord;
 
@@ -266,10 +273,20 @@ type
   end;
 
 
+{$IF DEFINED(D2D_DYNREC_PROFILER)}
+procedure xdynDumpProfiles ();
+{$ENDIF}
+
+
 implementation
 
 uses
-  SysUtils;
+  SysUtils, e_log, hashtable
+  {$IF DEFINED(D2D_DYNREC_PROFILER)},xprofiler{$ENDIF};
+
+
+// ////////////////////////////////////////////////////////////////////////// //
+function StrEqu (const a, b: AnsiString): Boolean; inline; begin result := (a = b); end;
 
 
 // ////////////////////////////////////////////////////////////////////////// //
@@ -326,6 +343,7 @@ begin
   mEBSTypeName := '';
   mEBSType := nil;
   mBitSetUnique := false;
+  mAsMonsterId := false;
   mNegBool := false;
   mRecRefId := '';
   if (mType = TType.TList) then mRVal := TDynRecList.Create();
@@ -366,6 +384,7 @@ begin
   result.mInternal := mInternal;
   result.mNegBool := mNegBool;
   result.mBitSetUnique := mBitSetUnique;
+  result.mAsMonsterId := mAsMonsterId;
   result.mDefUnparsed := mDefUnparsed;
   result.mDefSVal := mDefSVal;
   result.mDefIVal := mDefIVal;
@@ -525,6 +544,7 @@ begin
     TEBS.TEnum: result += ' enum '+mEBSTypeName;
     TEBS.TBitSet: begin result += ' bitset '; if mBitSetUnique then result += 'unique '; result += mEBSTypeName; end;
   end;
+  if mAsMonsterId then result += ' as monsterid';
   if mHasDefault and (Length(mDefUnparsed) > 0) then result += ' default '+mDefUnparsed;
   if mSepPosSize then
   begin
@@ -536,6 +556,34 @@ begin
 end;
 
 
+function TDynField.pasdef (): AnsiString;
+begin
+  result := mPasName+': ';
+  case mType of
+    TType.TBool: result += 'Boolean;';
+    TType.TChar: if (mMaxDim > 0) then result += formatstrf('Char%d;', [mMaxDim]) else result += 'Char;';
+    TType.TByte: result += 'ShortInt;';
+    TType.TUByte: result += 'Byte;';
+    TType.TShort: result += 'SmallInt;';
+    TType.TUShort: result += 'Word;';
+    TType.TInt: result += 'LongInt;';
+    TType.TUInt: result += 'LongWord;';
+    TType.TString: result += 'AnsiString;';
+    TType.TPoint:
+           if mAsT then result := 'tX, tY: Integer;'
+      else if mSepPosSize then result := 'X, Y: Integer;'
+      else result += 'TDFPoint;';
+    TType.TSize:
+           if mAsT then result := 'tWidth, tHeight: Word;'
+      else if mSepPosSize then result := 'Width, Height: Word;'
+      else result += 'TSize;';
+    TType.TList: assert(false);
+    TType.TTrigData: result += formatstrf('Byte%d;', [mMaxDim]);
+    else raise Exception.Create('ketmar forgot to handle some field type');
+  end;
+end;
+
+
 procedure TDynField.parseDef (pr: TTextParser);
 var
   fldname: AnsiString;
@@ -554,6 +602,7 @@ var
   lmaxdim: Integer;
   lebs: TDynField.TEBS;
   unique: Boolean;
+  asmonid: Boolean;
 begin
   fldpasname := '';
   fldname := '';
@@ -571,6 +620,7 @@ begin
   hasdefInt := false;
   hasdefId := false;
   unique := false;
+  asmonid := false;
   lmaxdim := -1;
   lebs := TDynField.TEBS.TNone;
 
@@ -606,6 +656,7 @@ begin
       else if pr.eatId('wh') then aswh := true
       else if pr.eatId('txy') then begin asxy := true; ast := true; end
       else if pr.eatId('twh') then begin aswh := true; ast := true; end
+      else if pr.eatId('monsterid') then begin asmonid := true; end
       else raise Exception.Create(Format('invalid field ''%s'' as what?', [fldname]));
       continue;
     end;
@@ -707,6 +758,7 @@ begin
   self.mEBS := lebs;
   self.mEBSTypeName := fldrecname;
   self.mBitSetUnique := unique;
+  self.mAsMonsterId := asmonid;
   self.mMaxDim := lmaxdim;
   self.mBinOfs := fldofs;
   self.mRecOfs := fldofs;
@@ -754,17 +806,6 @@ begin
           exit;
         end;
         // record reference
-        if (mRecRef = nil) then
-        begin
-          // no ref, write -1
-          case mType of
-            TType.TByte, TType.TUByte: writeInt(st, Byte(-1));
-            TType.TShort, TType.TUShort: writeInt(st, SmallInt(-1));
-            TType.TInt, TType.TUInt: writeInt(st, Integer(-1));
-            else raise Exception.Create(Format('record reference type ''%s'' in field ''%s'' cannot be written', [mEBSTypeName, mName]));
-          end;
-          exit;
-        end;
         case mType of
           TType.TByte: maxv := 127;
           TType.TUByte: maxv := 254;
@@ -775,13 +816,21 @@ begin
           else raise Exception.Create(Format('record reference type ''%s'' in field ''%s'' cannot be written', [mEBSTypeName, mName]));
         end;
         // find record number
-        f := mOwner.findRecordNumByType(mEBSTypeName, mRecRef);
-        if (f < 0) then raise Exception.Create(Format('record reference type ''%s'' in field ''%s'' not found in record list', [mEBSTypeName, mName]));
-        if (f > maxv) then raise Exception.Create(Format('record reference type ''%s'' in field ''%s'' has too big index', [mEBSTypeName, mName]));
+        if (mRecRef <> nil) then
+        begin
+          f := mOwner.findRecordNumByType(mEBSTypeName, mRecRef);
+          if (f < 0) then raise Exception.Create(Format('record reference type ''%s'' in field ''%s'' not found in record list', [mEBSTypeName, mName]));
+          if mAsMonsterId then Inc(f);
+          if (f > maxv) then raise Exception.Create(Format('record reference type ''%s'' in field ''%s'' has too big index', [mEBSTypeName, mName]));
+        end
+        else
+        begin
+          if mAsMonsterId then f := 0 else f := -1;
+        end;
         case mType of
           TType.TByte, TType.TUByte: writeInt(st, Byte(f));
           TType.TShort, TType.TUShort: writeInt(st, SmallInt(f));
-          TType.TInt, TType.TUInt: writeInt(st, Integer(f));
+          TType.TInt, TType.TUInt: writeInt(st, LongWord(f));
           else raise Exception.Create(Format('record reference type ''%s'' in field ''%s'' cannot be written', [mEBSTypeName, mName]));
         end;
         exit;
@@ -1020,7 +1069,6 @@ begin
   raise Exception.Create(Format('cannot parse field ''%s'' yet', [mName]));
 end;
 
-
 procedure TDynField.parseBinValue (st: TStream);
 var
   rec, rc: TDynRecord;
@@ -1040,9 +1088,8 @@ begin
           assert(mMaxDim > 0);
           rec := mOwner;
           // find trigger definition
-          tfld := rec.field['type'];
-          if (tfld = nil) then raise Exception.Create(Format('triggerdata value for field ''%s'' in record ''%s'' without ''type'' field', [mName, rec.mName]));
-          if (tfld.mEBS <> TEBS.TEnum) then raise Exception.Create(Format('triggerdata value for field ''%s'' in record ''%s'' with bad ''type'' field', [mName, rec.mName]));
+          tfld := rec.trigTypeField();
+          if (tfld = nil) then raise Exception.Create(Format('triggerdata value for field ''%s'' in record ''%s'' without TriggerType field', [mName, rec.mName]));
           rc := mOwner.mOwner.findTrigFor(tfld.mSVal); // find in mapdef
           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]));
           rc := rc.clone();
@@ -1069,6 +1116,7 @@ begin
             TType.TUInt: f := readLongWord(st);
             else raise Exception.Create(Format('invalid non-numeric type ''%s'' for field ''%s'' of record ''%s''', [getTypeName(mType), mName, mEBSTypeName]));
           end;
+          if mAsMonsterId then Dec(f);
           if (f < 0) then mRecRefId := '' else mRecRefId := Format('%s%d', [mEBSTypeName, f]);
         end;
         mDefined := true;
@@ -1221,6 +1269,7 @@ var
   es: TDynEBS = nil;
   tfld: TDynField;
   tk: AnsiString;
+  edim: AnsiChar;
 begin
   // if this field should contain struct, convert type and parse struct
   case mEBS of
@@ -1241,9 +1290,8 @@ begin
           begin
             rec := mOwner;
             // find trigger definition
-            tfld := rec.field['type'];
+            tfld := rec.trigTypeField();
             if (tfld = nil) then raise Exception.Create(Format('triggerdata value for field ''%s'' in record ''%s'' without ''type'' field', [mName, rec.mName]));
-            if (tfld.mEBS <> TEBS.TEnum) then raise Exception.Create(Format('triggerdata value for field ''%s'' in record ''%s'' with bad ''type'' field', [mName, rec.mName]));
             rc := mOwner.mOwner.findTrigFor(tfld.mSVal); // find in mapdef
             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]));
             rc := rc.clone();
@@ -1412,7 +1460,7 @@ begin
     TType.TPoint,
     TType.TSize:
       begin
-        pr.expectDelim('(');
+        if pr.eatDelim('[') then edim := ']' else begin pr.expectDelim('('); edim := ')'; end;
         mIVal := pr.expectInt();
         if (mType = TType.TSize) then
         begin
@@ -1424,7 +1472,7 @@ begin
           if (mIVal2 < 0) or (mIVal2 > 32767) then raise Exception.Create(Format('invalid %s value for field ''%s''', [getTypeName(mType), mName]));
         end;
         mDefined := true;
-        pr.expectDelim(')');
+        pr.expectDelim(edim);
         pr.expectTT(pr.TTSemi);
         exit;
       end;
@@ -1487,7 +1535,7 @@ begin
   result := 0;
   while (result < mFields.count) do
   begin
-    if (CompareText(aname, mFields[result].mName) = 0) then exit;
+    if StrEqu(aname, mFields[result].mName) then exit;
     Inc(result);
   end;
   result := -1;
@@ -1520,7 +1568,7 @@ var
   f: Integer;
 begin
   result := true;
-  for f := 0 to High(mTrigTypes) do if (CompareText(mTrigTypes[f], aname) = 0) then exit;
+  for f := 0 to High(mTrigTypes) do if StrEqu(mTrigTypes[f], aname) then exit;
   result := false;
 end;
 
@@ -1565,7 +1613,7 @@ begin
   begin
     for rec in fld.mRVal do
     begin
-      if (CompareText(rec.mId, aid) = 0) then begin result := rec; exit; end;
+      if StrEqu(rec.mId, aid) then begin result := rec; exit; end;
     end;
   end;
   // alas
@@ -1630,6 +1678,34 @@ begin
 end;
 
 
+function TDynRecord.trigTypeField (): TDynField;
+var
+  fld: TDynField;
+  es: TDynEBS = nil;
+begin
+  for fld in mFields do
+  begin
+    if (fld.mEBS <> TDynField.TEBS.TEnum) then continue;
+    if not (fld.mEBSType is TDynEBS) then continue;
+    es := (fld.mEBSType as TDynEBS);
+    assert(es <> nil);
+    if StrEqu(es.mName, 'TriggerType') then begin result := fld; exit; end;
+  end;
+  result := nil;
+end;
+
+
+// number of records of the given instance
+function TDynRecord.instanceCount (const typename: AnsiString): Integer;
+var
+  fld: TDynField;
+begin
+  result := 0;
+  fld := field[typename];
+  if (fld <> nil) and (fld.mType = fld.TType.TList) then result := fld.mRVal.count;
+end;
+
+
 procedure TDynRecord.parseDef (pr: TTextParser);
 var
   fld: TDynField;
@@ -1699,6 +1775,30 @@ begin
 end;
 
 
+function TDynRecord.pasdef (): AnsiString;
+var
+  fld: TDynField;
+begin
+  if isTrigData then
+  begin
+    assert(false);
+    result := '';
+  end
+  else
+  begin
+    // record
+    result := '  '+mPasName+' = packed record'#10;
+  end;
+  for fld in mFields do
+  begin
+    if fld.mInternal then continue;
+    if (fld.mBinOfs < 0) then continue;
+    result += '    '+fld.pasdef+#10;
+  end;
+  result += '  end;'#10;
+end;
+
+
 function TDynRecord.definition (): AnsiString;
 var
   f: Integer;
@@ -1768,7 +1868,11 @@ var
       if (Length(fld.mRecRefId) = 0) then continue;
       assert(fld.mEBSType <> nil);
       rt := findRecordByTypeId(fld.mEBSTypeName, fld.mRecRefId);
-      if (rt = nil) then raise Exception.Create(Format('record of type ''%s'' with id ''%s'' links to inexistant record of type ''%s'' with id ''%d''', [rec.mName, rec.mId, fld.mEBSTypeName, fld.mRecRefId]));
+      if (rt = nil) then
+      begin
+        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);
+        //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]));
+      end;
       //writeln(' ', rec.mName, '.', rec.mId, ':', fld.mName, ' -> ', rt.mName, '.', rt.mId, ' (', fld.mEBSTypeName, '.', fld.mRecRefId, ')');
       fld.mRecRefId := '';
       fld.mRecRef := rt;
@@ -1798,7 +1902,7 @@ begin
         if (btype = 0) then break; // no more blocks
         readLongWord(st); // reserved
         bsize := readLongInt(st);
-        writeln('btype=', btype, '; bsize=', bsize);
+        {$IF DEFINED(D2D_XDYN_DEBUG)}writeln('btype=', btype, '; bsize=', bsize);{$ENDIF}
         if (bsize < 0) or (bsize > $1fffffff) then raise Exception.Create(Format('block of type %d has invalid size %d', [btype, bsize]));
         if loaded[btype] then raise Exception.Create(Format('block of type %d already loaded', [btype]));
         loaded[btype] := true;
@@ -1806,7 +1910,7 @@ begin
         rect := nil;
         for rec in mOwner.recTypes do if (rec.mBinBlock = btype) then begin rect := rec; break; end;
         if (rect = nil) then raise Exception.Create(Format('block of type %d has no corresponding record', [btype]));
-        writeln('found type ''', rec.mName, ''' for block type ', btype);
+        //writeln('found type ''', rec.mName, ''' for block type ', btype);
         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]));
         // header?
         if (rect.mHeader) then
@@ -1853,7 +1957,7 @@ begin
     end;
 
     // read fields
-    if (CompareText(mName, 'TriggerData') = 0) then mSize := Integer(st.size-st.position);
+    if StrEqu(mName, 'TriggerData') then mSize := Integer(st.size-st.position);
     if (mSize < 1) then raise Exception.Create(Format('cannot read record of type ''%s'' with unknown size', [mName]));
     GetMem(buf, mSize);
     st.ReadBuffer(buf^, mSize);
@@ -1873,7 +1977,7 @@ begin
 end;
 
 
-procedure TDynRecord.writeBinTo (st: TStream; trigbufsz: Integer=-1);
+procedure TDynRecord.writeBinTo (st: TStream; trigbufsz: Integer=-1; onlyFields: Boolean=false);
 var
   fld: TDynField;
   rec, rv: TDynRecord;
@@ -1913,7 +2017,7 @@ begin
     end;
 
     // write block with normal fields
-    if mHeader then
+    if mHeader and not onlyFields then
     begin
       //writeln('writing header...');
       // signature and version
@@ -1928,7 +2032,7 @@ begin
     FreeMem(buf); buf := nil;
 
     // write other blocks, if any
-    if mHeader then
+    if mHeader and not onlyFields then
     begin
       // calculate blkmax
       blkmax := 0;
@@ -2031,13 +2135,47 @@ begin
 end;
 
 
+{$IF DEFINED(D2D_DYNREC_PROFILER)}
+var
+  profCloneRec: UInt64 = 0;
+  profFindRecType: UInt64 = 0;
+  profFieldSearching: UInt64 = 0;
+  profListDupChecking: UInt64 = 0;
+  profAddRecByType: UInt64 = 0;
+  profFieldValParsing: UInt64 = 0;
+  profFixDefaults: UInt64 = 0;
+  profRecValParse: UInt64 = 0;
+
+procedure xdynDumpProfiles ();
+begin
+  writeln('=== XDYNREC PROFILES ===');
+  writeln('record cloning: ', profCloneRec div 1000, '.', profCloneRec mod 1000, ' milliseconds');
+  writeln('findRecType   : ', profFindRecType div 1000, '.', profFindRecType mod 1000, ' milliseconds');
+  writeln('field[]       : ', profFieldSearching div 1000, '.', profFieldSearching mod 1000, ' milliseconds');
+  writeln('list dup check: ', profListDupChecking div 1000, '.', profListDupChecking mod 1000, ' milliseconds');
+  writeln('addRecByType  : ', profAddRecByType div 1000, '.', profAddRecByType mod 1000, ' milliseconds');
+  writeln('field valparse: ', profFieldValParsing div 1000, '.', profFieldValParsing mod 1000, ' milliseconds');
+  writeln('fix defaults  : ', profFixDefaults div 1000, '.', profFixDefaults mod 1000, ' milliseconds');
+  writeln('recvalparse   : ', profRecValParse div 1000, '.', profRecValParse mod 1000, ' milliseconds');
+end;
+{$ENDIF}
+
+
 procedure TDynRecord.parseValue (pr: TTextParser; beginEaten: Boolean=false);
 var
   fld: TDynField;
-  rec, trc, rv: TDynRecord;
+  rec: TDynRecord = nil;
+  trc{, rv}: TDynRecord;
+  {$IF DEFINED(D2D_DYNREC_PROFILER)}
+  stt, stall: UInt64;
+  {$ENDIF}
+  ids: THashStrInt = nil;
+  idtmp: AnsiString = '';
 begin
   if (mOwner = nil) then raise Exception.Create(Format('can''t parse record ''%s'' value without owner', [mName]));
 
+  {$IF DEFINED(D2D_DYNREC_PROFILER)}stall := curTimeMicro();{$ENDIF}
+
   // not a header?
   if not mHeader then
   begin
@@ -2047,65 +2185,98 @@ begin
   else
   begin
     assert(mHeaderRec = self);
+    ids := hashNewStrInt();
   end;
 
-  //writeln('parsing record <', mName, '>');
-  if not beginEaten then pr.expectTT(pr.TTBegin);
-  while (pr.tokType <> pr.TTEnd) do
-  begin
-    if (pr.tokType <> pr.TTId) then raise Exception.Create('identifier expected');
-    //writeln('<', mName, '.', pr.tokStr, '>');
-
-    // records
-    if mHeader then
+  try
+    //writeln('parsing record <', mName, '>');
+    if not beginEaten then pr.expectTT(pr.TTBegin);
+    while (pr.tokType <> pr.TTEnd) do
     begin
-      // add records with this type (if any)
-      trc := mOwner.findRecType(pr.tokStr);
-      if (trc <> nil) then
-      begin
-        rec := trc.clone();
-        rec.mHeaderRec := mHeaderRec;
-        try
-          pr.skipToken();
-          rec.parseValue(pr);
-          if (Length(rec.mId) > 0) then
-          begin
-            fld := field[pr.tokStr];
-            if (fld <> nil) and (fld.mRVal <> nil) then
+      if (pr.tokType <> pr.TTId) then raise Exception.Create('identifier expected');
+      //writeln('<', mName, '.', pr.tokStr, '>');
+
+      // records
+      if mHeader then
+      begin
+        // add records with this type (if any)
+        {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
+        trc := mOwner.findRecType(pr.tokStr);
+        {$IF DEFINED(D2D_DYNREC_PROFILER)}profFindRecType := curTimeMicro()-stt;{$ENDIF}
+        if (trc <> nil) then
+        begin
+          {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
+          rec := trc.clone();
+          {$IF DEFINED(D2D_DYNREC_PROFILER)}profCloneRec := curTimeMicro()-stt;{$ENDIF}
+          rec.mHeaderRec := mHeaderRec;
+          try
+            pr.skipToken();
+            rec.parseValue(pr);
+            if (Length(rec.mId) > 0) then
             begin
-              for rv in fld.mRVal do
-              begin
-                if (Length(rv.mId) > 0) and (CompareText(rv.mId, rec.mId) = 0) then raise Exception.Create(Format('duplicate thing ''%s'' in record ''%s''', [fld.mName, mName]));
-              end;
+              {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
+              fld := field[pr.tokStr];
+              {$IF DEFINED(D2D_DYNREC_PROFILER)}profFieldSearching := curTimeMicro()-stt;{$ENDIF}
+              {$iF FALSE}
+                if (fld <> nil) and (fld.mRVal <> nil) then
+                begin
+                  {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
+                  for rv in fld.mRVal do
+                  begin
+                    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]));
+                  end;
+                  {$IF DEFINED(D2D_DYNREC_PROFILER)}profListDupChecking := curTimeMicro()-stt;{$ENDIF}
+                end;
+              {$ELSE}
+                if (fld <> nil) and (fld.mRVal <> nil) then
+                begin
+                  {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
+                  idtmp := trc.mName+':'+rec.mId;
+                  if ids.put(idtmp, 1) then raise Exception.Create(Format('duplicate thing ''%s'' in record ''%s''', [fld.mName, mName]));
+                  {$IF DEFINED(D2D_DYNREC_PROFILER)}profListDupChecking := curTimeMicro()-stt;{$ENDIF}
+                end;
+              {$ENDIF}
             end;
+            {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
+            addRecordByType(rec.mName, rec);
+            {$IF DEFINED(D2D_DYNREC_PROFILER)}profAddRecByType := curTimeMicro()-stt;{$ENDIF}
+            rec := nil;
+          finally
+            rec.Free();
           end;
-          addRecordByType(rec.mName, rec);
-          rec := nil;
-        finally
-          rec.Free();
+          continue;
         end;
+      end;
+
+      // fields
+      {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
+      fld := field[pr.tokStr];
+      {$IF DEFINED(D2D_DYNREC_PROFILER)}profFieldSearching := curTimeMicro()-stt;{$ENDIF}
+      if (fld <> nil) then
+      begin
+        if fld.defined then raise Exception.Create(Format('duplicate field ''%s'' in record ''%s''', [fld.mName, mName]));
+        if fld.internal then raise Exception.Create(Format('internal field ''%s'' in record ''%s''', [fld.mName, mName]));
+        pr.skipToken();
+        {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
+        fld.parseValue(pr);
+        {$IF DEFINED(D2D_DYNREC_PROFILER)}profFieldValParsing := curTimeMicro()-stt;{$ENDIF}
         continue;
       end;
-    end;
 
-    // fields
-    fld := field[pr.tokStr];
-    if (fld <> nil) then
-    begin
-      if fld.defined then raise Exception.Create(Format('duplicate field ''%s'' in record ''%s''', [fld.mName, mName]));
-      if fld.internal then raise Exception.Create(Format('internal field ''%s'' in record ''%s''', [fld.mName, mName]));
-      pr.skipToken();
-      fld.parseValue(pr);
-      continue;
+      // something is wrong
+      raise Exception.Create(Format('unknown field ''%s'' in record ''%s''', [pr.tokStr, mName]));
     end;
-
-    // something is wrong
-    raise Exception.Create(Format('unknown field ''%s'' in record ''%s''', [pr.tokStr, mName]));
+    pr.expectTT(pr.TTEnd);
+    // fix field defaults
+    {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
+    for fld in mFields do fld.fixDefaultValue();
+    {$IF DEFINED(D2D_DYNREC_PROFILER)}profFixDefaults := curTimeMicro()-stt;{$ENDIF}
+    //writeln('done parsing record <', mName, '>');
+    //{$IF DEFINED(D2D_DYNREC_PROFILER)}writeln('stall: ', curTimeMicro()-stall);{$ENDIF}
+    {$IF DEFINED(D2D_DYNREC_PROFILER)}profRecValParse := curTimeMicro()-stall;{$ENDIF}
+  finally
+    ids.Free();
   end;
-  pr.expectTT(pr.TTEnd);
-  // fix field defaults
-  for fld in mFields do fld.fixDefaultValue();
-  //writeln('done parsing record <', mName, '>');
 end;
 
 
@@ -2140,7 +2311,7 @@ begin
   result := 0;
   while (result < Length(mIds)) do
   begin
-    if (CompareText(aname, mIds[result]) = 0) then exit;
+    if StrEqu(aname, mIds[result]) then exit;
     Inc(result);
   end;
   result := -1;
@@ -2193,6 +2364,19 @@ begin
 end;
 
 
+function TDynEBS.pasdef (): AnsiString;
+var
+  f: Integer;
+begin
+  result := '// '+mName+#10'const'#10;
+  // fields
+  for f := 0 to High(mIds) do
+  begin
+    result += formatstrf('  %s = %d;'#10, [mIds[f], mVals[f]]);
+  end;
+end;
+
+
 function TDynEBS.nameByValue (v: Integer): AnsiString;
 var
   f: Integer;
@@ -2225,9 +2409,9 @@ begin
     idname := pr.expectId();
     for f := 0 to High(mIds) do
     begin
-      if (CompareText(mIds[f], idname) = 0) then raise Exception.Create(Format('duplicate field ''%s'' in enum/bitset ''%s''', [idname, mName]));
+      if StrEqu(mIds[f], idname) then raise Exception.Create(Format('duplicate field ''%s'' in enum/bitset ''%s''', [idname, mName]));
     end;
-    if (CompareText(mMaxName, idname) = 0) then raise Exception.Create(Format('duplicate field ''%s'' in enum/bitset ''%s''', [idname, mName]));
+    if StrEqu(mMaxName, idname) then raise Exception.Create(Format('duplicate field ''%s'' in enum/bitset ''%s''', [idname, mName]));
     skipAdd := false;
     hasV := false;
     v := cv;
@@ -2322,7 +2506,7 @@ var
 begin
   for rec in recTypes do
   begin
-    if (CompareText(rec.name, aname) = 0) then begin result := rec; exit; end;
+    if StrEqu(rec.name, aname) then begin result := rec; exit; end;
   end;
   result := nil;
 end;
@@ -2346,7 +2530,7 @@ var
 begin
   for ebs in ebsTypes do
   begin
-    if (CompareText(ebs.name, aname) = 0) then begin result := ebs; exit; end;
+    if StrEqu(ebs.name, aname) then begin result := ebs; exit; end;
   end;
   result := nil;
 end;
@@ -2434,7 +2618,7 @@ begin
     rec := TDynRecord.Create(pr);
     //writeln(dr.definition); writeln;
     if (findRecType(rec.name) <> nil) then begin rec.Free(); raise Exception.Create(Format('duplicate record ''%s''', [rec.name])); end;
-    if (hdr <> nil) and (CompareText(rec.name, hdr.name) = 0) then begin rec.Free(); raise Exception.Create(Format('duplicate record ''%s''', [rec.name])); end;
+    if (hdr <> nil) and StrEqu(rec.name, hdr.name) then begin rec.Free(); raise Exception.Create(Format('duplicate record ''%s''', [rec.name])); end;
     rec.mOwner := self;
     if rec.mHeader then
     begin
@@ -2476,11 +2660,8 @@ begin
     res.parseValue(pr);
     result := res;
     res := nil;
-  except on E: Exception do
-    begin
-      res.Free();
-      raise;
-    end;
+  finally
+    res.Free();
   end;
 end;
 
@@ -2496,12 +2677,56 @@ begin
     res.parseBinValue(st);
     result := res;
     res := nil;
-  except on E: Exception do
+  finally
+    res.Free();
+  end;
+end;
+
+
+function TDynMapDef.pasdef (): AnsiString;
+var
+  ebs: TDynEBS;
+  rec: TDynRecord;
+  fld: TDynField;
+  needComma: Boolean;
+  tn: AnsiString;
+begin
+  result := '';
+  result += '// ////////////////////////////////////////////////////////////////////////// //'#10;
+  result += '// enums and bitsets'#10;
+  for ebs in ebsTypes do result += #10+ebs.pasdef();
+  result += #10#10'// ////////////////////////////////////////////////////////////////////////// //'#10;
+  result += '// records'#10'type'#10;
+  for rec in recTypes do
+  begin
+    if (rec.mSize < 1) then continue;
+    result += rec.pasdef();
+    result += #10;
+  end;
+  result += #10#10'// ////////////////////////////////////////////////////////////////////////// //'#10;
+  result += '// triggerdata'#10'type'#10;
+  result += '  TTriggerData = record'#10;
+  result += '    case Byte of'#10;
+  result += '      0: (Default: Byte128);'#10;
+  for rec in trigTypes do
+  begin
+    result += '      ';
+    needComma := false;
+    for tn in rec.mTrigTypes do
+    begin
+      if needComma then result += ', ' else needComma := true;
+      result += tn;
+    end;
+    result += ': ('#10;
+    for fld in rec.mFields do
     begin
-      res.Free();
-      raise;
+      if fld.mInternal then continue;
+      if (fld.mBinOfs < 0) then continue;
+      result += '        '+fld.pasdef+#10;
     end;
+    result += '      );'#10;
   end;
+  result += '  end;'#10;
 end;