DEADSOFTWARE

mapdef cleanup; renamed some fields; mapdef.txt is RC0 now
[d2df-sdl.git] / src / shared / xdynrec.pas
index 8ebac36188754ef6349bbb7bbc5147a1a0ccfe7a..3bce723917354b4ad0e119cf92edcba521f42e2d 100644 (file)
@@ -20,7 +20,7 @@ unit xdynrec;
 interface
 
 uses
-  Classes,
+  Variants, Classes,
   xparser, xstreams, utils, hashtable;
 
 
@@ -44,17 +44,16 @@ type
       // TSize: pair of UShorts
       // TList: actually, array of records
       // TTrigData: array of mMaxDim bytes, but internally a record (mRecRef)
-      // arrays of chars are pascal shortstrings (with counter in the first byte)
+      // in binary: arrays of chars are pascal shortstrings (with counter in the first byte)
 
   private
     type
       TEBS = (TNone, TRec, TEnum, TBitSet);
 
   private
-    mOwner: TDynRecord;
-    mPasName: AnsiString;
-    mName: AnsiString;
-    mType: TType;
+    mOwner: TDynRecord; // owner record
+    mName: AnsiString; // field name
+    mType: TType; // field type
     mIVal: Integer; // for all integer types
     mIVal2: Integer; // for point and size
     mSVal: AnsiString; // string; for byte and char arrays
@@ -67,7 +66,7 @@ type
     mAsT: Boolean; // for points and sizes, use separate fields, names starts with `t`
     mDefined: Boolean;
     mHasDefault: Boolean;
-    mOmitDef: Boolean;
+    mWriteDef: Boolean;
     mInternal: Boolean;
     mNegBool: Boolean;
     mBitSetUnique: Boolean; // bitset can contain only one value
@@ -88,6 +87,9 @@ type
     mTagInt: Integer;
     mTagPtr: Pointer;
 
+    // for pasgen
+    mAlias: AnsiString;
+
   private
     procedure cleanup ();
 
@@ -105,34 +107,27 @@ type
 
     procedure setIVal (v: Integer); inline;
 
+    function getVar (): Variant;
+    procedure setVar (val: Variant);
+
   protected
     // returns `true` for duplicate record id
     function addListItem (rec: TDynRecord): Boolean; inline;
 
-  public
-    type
-      TListEnumerator = record
-      private
-        mList: TDynRecList;
-        mCurIdx: Integer;
-      public
-        constructor Create (alist: TDynRecList);
-        function MoveNext (): Boolean; inline;
-        function getCurrent (): TDynRecord; inline;
-        property Current: TDynRecord read getCurrent;
-      end;
-
   public
     constructor Create (const aname: AnsiString; atype: TType);
     constructor Create (pr: TTextParser);
+    constructor Create (const aname: AnsiString; val: Variant);
     destructor Destroy (); override;
 
     class function getTypeName (t: TType): AnsiString;
 
+    // build "alias name" for pascal code
+    function palias (firstUp: Boolean=false): AnsiString;
+
     function definition (): AnsiString;
-    function pasdef (): AnsiString;
 
-    function clone (newOwner: TDynRecord=nil): TDynField;
+    function clone (newOwner: TDynRecord=nil; registerIn: TDynRecord=nil): TDynField;
 
     procedure parseValue (pr: TTextParser);
     procedure parseBinValue (st: TStream);
@@ -145,14 +140,13 @@ type
 
     procedure setValue (const s: AnsiString);
 
-    function GetEnumerator (): TListEnumerator;
+    function GetEnumerator (): TDynRecList.TEnumerator; inline;
 
   public
-    property pasname: AnsiString read mPasName;
     property name: AnsiString read mName;
     property baseType: TType read mType;
     property negbool: Boolean read mNegBool;
-    property defined: Boolean read mDefined write mDefined;
+    property defined: Boolean read mDefined;
     property internal: Boolean read mInternal write mInternal;
     property hasTPrefix: Boolean read mAsT;
     property separatePasFields: Boolean read mSepPosSize;
@@ -165,7 +159,7 @@ type
     property ebs: TEBS read mEBS;
     property ebstype: TObject read mEBSType;
     property ebstypename: AnsiString read mEBSTypeName; // enum/bitset name
-    property recref: TDynRecord read mRecRef write mRecRef; //FIXME: writing is a hack!
+    property recref: TDynRecord read mRecRef;
     property recrefIndex: Integer read getRecRefIndex; // search for this record in header; -1: not found
     // for lists
     property count: Integer read getListCount;
@@ -174,6 +168,8 @@ type
     // userdata
     property tagInt: Integer read mTagInt write mTagInt;
     property tagPtr: Pointer read mTagPtr write mTagPtr;
+    //
+    property varvalue: Variant read getVar write setVar;
   end;
 
 
@@ -182,7 +178,6 @@ type
   private
     mOwner: TDynMapDef;
     mId: AnsiString;
-    mPasName: AnsiString;
     mName: AnsiString;
     mSize: Integer;
     mFields: TDynFieldList;
@@ -198,6 +193,8 @@ type
     mTagInt: Integer;
     mTagPtr: Pointer;
 
+    mRec2Free: TDynRecList;
+
   private
     procedure parseDef (pr: TTextParser); // parse definition
 
@@ -213,6 +210,8 @@ type
     function getForTrigCount (): Integer; inline;
     function getForTrigAt (idx: Integer): AnsiString; inline;
 
+    procedure regrec (rec: TDynRecord);
+
   protected
     function findRecordByTypeId (const atypename, aid: AnsiString): TDynRecord;
     function findRecordNumByType (const atypename: AnsiString; rc: TDynRecord): Integer;
@@ -227,9 +226,8 @@ type
     destructor Destroy (); override;
 
     function definition (): AnsiString;
-    function pasdef (): AnsiString;
 
-    function clone (): TDynRecord;
+    function clone (registerIn: TDynRecord): TDynRecord;
 
     function isSimpleEqu (rec: TDynRecord): Boolean;
 
@@ -245,9 +243,11 @@ type
     // number of records of the given instance
     function instanceCount (const typename: AnsiString): Integer;
 
+    function getUserVar (const aname: AnsiString): Variant;
+    procedure setUserVar (const aname: AnsiString; val: Variant);
+
   public
     property id: AnsiString read mId; // for map parser
-    property pasname: AnsiString read mPasName;
     property name: AnsiString read mName; // record name
     property size: Integer read mSize; // size in bytes
     //property fields: TDynFieldList read mFields;
@@ -264,6 +264,8 @@ type
     // userdata
     property tagInt: Integer read mTagInt write mTagInt;
     property tagPtr: Pointer read mTagPtr write mTagPtr;
+    // userfields
+    property user[const aname: AnsiString]: Variant read getUserVar write setUserVar;
   end;
 
   TDynEBS = class
@@ -325,7 +327,6 @@ type
     function findTrigFor (const aname: AnsiString): TDynRecord;
     function findEBSType (const aname: AnsiString): TDynEBS;
 
-    function pasdef (): AnsiString;
     function pasdefconst (): AnsiString;
 
     // creates new header record
@@ -358,29 +359,10 @@ function StrEqu (const a, b: AnsiString): Boolean; inline; begin result := (a =
 
 
 // ////////////////////////////////////////////////////////////////////////// //
-constructor TDynField.TListEnumerator.Create (alist: TDynRecList);
+function TDynField.GetEnumerator (): TDynRecList.TEnumerator; inline;
 begin
-  mList := alist;
-  mCurIdx := -1;
-end;
-
-
-function TDynField.TListEnumerator.MoveNext (): Boolean; inline;
-begin
-  Inc(mCurIdx);
-  result := (mList <> nil) and (mCurIdx < mList.count);
-end;
-
-
-function TDynField.TListEnumerator.getCurrent (): TDynRecord; inline;
-begin
-  result := mList[mCurIdx];
-end;
-
-
-function TDynField.GetEnumerator (): TListEnumerator;
-begin
-  result := TListEnumerator.Create(mRVal);
+  //result := TListEnumerator.Create(mRVal);
+  if (mRVal <> nil) then result := mRVal.GetEnumerator else result := TDynRecList.TEnumerator.Create(nil, 0);
 end;
 
 
@@ -408,6 +390,72 @@ begin
 end;
 
 
+constructor TDynField.Create (const aname: AnsiString; val: Variant);
+  procedure setInt32 (v: LongInt);
+  begin
+    case mType of
+      TType.TBool:
+             if (v = 0) then mIVal := 0
+        else if (v = 1) then mIVal := 1
+        else raise Exception.Create('cannot convert shortint variant to field value');
+      TType.TByte:
+             if (v >= -128) and (v <= 127) then mIVal := v
+        else raise Exception.Create('cannot convert shortint variant to field value');
+      TType.TUByte:
+             if (v >= 0) and (v <= 255) then mIVal := v
+        else raise Exception.Create('cannot convert shortint variant to field value');
+      TType.TShort:
+             if (v >= -32768) and (v <= 32767) then mIVal := v
+        else raise Exception.Create('cannot convert shortint variant to field value');
+      TType.TUShort:
+             if (v >= 0) and (v <= 65535) then mIVal := v
+        else raise Exception.Create('cannot convert shortint variant to field value');
+      TType.TInt:
+        mIVal := v;
+      TType.TUInt:
+        mIVal := v;
+      TType.TString:
+        mSVal := formatstrf('%s', [v]);
+      else
+        raise Exception.Create('cannot convert integral variant to field value');
+    end;
+  end;
+begin
+  mRVal := nil;
+  mRecRef := nil;
+  mRHash := nil;
+  cleanup();
+  mName := aname;
+  case varType(val) of
+    varEmpty: raise Exception.Create('cannot convert empty variant to field value');
+    varNull: raise Exception.Create('cannot convert null variant to field value');
+    varSingle: raise Exception.Create('cannot convert single variant to field value');
+    varDouble: raise Exception.Create('cannot convert double variant to field value');
+    varDecimal: raise Exception.Create('cannot convert decimal variant to field value');
+    varCurrency: raise Exception.Create('cannot convert currency variant to field value');
+    varDate: raise Exception.Create('cannot convert date variant to field value');
+    varOleStr: raise Exception.Create('cannot convert olestr variant to field value');
+    varStrArg: raise Exception.Create('cannot convert stdarg variant to field value');
+    varString: mType := TType.TString;
+    varDispatch: raise Exception.Create('cannot convert dispatch variant to field value');
+    varBoolean: mType := TType.TBool;
+    varVariant: raise Exception.Create('cannot convert variant variant to field value');
+    varUnknown: raise Exception.Create('cannot convert unknown variant to field value');
+    varByte: mType := TType.TUByte;
+    varWord: mType := TType.TUShort;
+    varShortInt: mType := TType.TByte;
+    varSmallint: mType := TType.TShort;
+    varInteger: mType := TType.TInt;
+    varInt64: raise Exception.Create('cannot convert int64 variant to field value');
+    varLongWord: raise Exception.Create('cannot convert longword variant to field value');
+    varQWord: raise Exception.Create('cannot convert uint64 variant to field value');
+    varError: raise Exception.Create('cannot convert error variant to field value');
+    else raise Exception.Create('cannot convert undetermined variant to field value');
+  end;
+  varvalue := val;
+end;
+
+
 destructor TDynField.Destroy ();
 begin
   cleanup();
@@ -433,7 +481,7 @@ begin
   mAsT := false;
   mHasDefault := false;
   mDefined := false;
-  mOmitDef := false;
+  mWriteDef := false;
   mInternal := true;
   mDefUnparsed := '';
   mDefSVal := '';
@@ -449,17 +497,17 @@ begin
   mRecRefId := '';
   mTagInt := 0;
   mTagPtr := nil;
+  mAlias := '';
 end;
 
 
-function TDynField.clone (newOwner: TDynRecord=nil): TDynField;
+function TDynField.clone (newOwner: TDynRecord=nil; registerIn: TDynRecord=nil): TDynField;
 var
   rec: TDynRecord;
 begin
   result := TDynField.Create(mName, mType);
   result.mOwner := mOwner;
   if (newOwner <> nil) then result.mOwner := newOwner else result.mOwner := mOwner;
-  result.mPasName := mPasName;
   result.mName := mName;
   result.mType := mType;
   result.mIVal := mIVal;
@@ -469,7 +517,7 @@ begin
   begin
     if (result.mRVal = nil) then result.mRVal := TDynRecList.Create(mRVal.count);
     if (result.mRHash = nil) then result.mRHash := hashNewStrInt();
-    for rec in mRVal do result.addListItem(rec.clone());
+    for rec in mRVal do result.addListItem(rec.clone(registerIn));
   end;
   result.mRecRef := mRecRef;
   result.mMaxDim := mMaxDim;
@@ -478,7 +526,7 @@ begin
   result.mAsT := mAsT;
   result.mDefined := mDefined;
   result.mHasDefault := mHasDefault;
-  result.mOmitDef := mOmitDef;
+  result.mWriteDef := mWriteDef;
   result.mInternal := mInternal;
   result.mNegBool := mNegBool;
   result.mBitSetUnique := mBitSetUnique;
@@ -494,6 +542,30 @@ begin
   result.mRecRefId := mRecRefId;
   result.mTagInt := mTagInt;
   result.mTagPtr := mTagPtr;
+  result.mAlias := mAlias;
+end;
+
+
+function TDynField.palias (firstUp: Boolean=false): AnsiString;
+var
+  nextUp: Boolean;
+  ch: AnsiChar;
+begin
+  if (Length(mAlias) > 0) then
+  begin
+    if firstUp then result := UpCase1251(mAlias[1])+Copy(mAlias, 2, Length(mAlias)-1) else result := mAlias;
+  end
+  else
+  begin
+    result := '';
+    nextUp := firstUp;
+    for ch in mName do
+    begin
+      if (ch = '_') then begin nextUp := true; continue; end;
+      if nextUp then result += UpCase1251(ch) else result += ch;
+      nextUp := false;
+    end;
+  end;
 end;
 
 
@@ -505,6 +577,118 @@ begin
 end;
 
 
+function TDynField.getVar (): Variant;
+begin
+  if (mEBS = TEBS.TRec) then begin result := LongInt(getRecRefIndex); exit; end;
+  case mType of
+    TType.TBool: result := (mIVal <> 0);
+    TType.TChar: result := mSVal;
+    TType.TByte: result := ShortInt(mIVal);
+    TType.TUByte: result := Byte(mIVal);
+    TType.TShort: result := SmallInt(mIVal);
+    TType.TUShort: result := Word(mIVal);
+    TType.TInt: result := LongInt(mIVal);
+    TType.TUInt: result := LongWord(mIVal);
+    TType.TString: result := mSVal;
+    TType.TPoint: raise Exception.Create('cannot convert point field to variant');
+    TType.TSize: raise Exception.Create('cannot convert size field to variant');
+    TType.TList: raise Exception.Create('cannot convert list field to variant');
+    TType.TTrigData: raise Exception.Create('cannot convert trigdata field to variant');
+    else result := Unassigned; raise Exception.Create('ketmar forgot to handle some field type');
+  end;
+end;
+
+
+procedure TDynField.setVar (val: Variant);
+  procedure setInt32 (v: LongInt);
+  begin
+    case mType of
+      TType.TBool:
+             if (v = 0) then mIVal := 0
+        else if (v = 1) then mIVal := 1
+        else raise Exception.Create('cannot convert shortint variant to field value');
+      TType.TByte:
+             if (v >= -128) and (v <= 127) then mIVal := v
+        else raise Exception.Create('cannot convert shortint variant to field value');
+      TType.TUByte:
+             if (v >= 0) and (v <= 255) then mIVal := v
+        else raise Exception.Create('cannot convert shortint variant to field value');
+      TType.TShort:
+             if (v >= -32768) and (v <= 32767) then mIVal := v
+        else raise Exception.Create('cannot convert shortint variant to field value');
+      TType.TUShort:
+             if (v >= 0) and (v <= 65535) then mIVal := v
+        else raise Exception.Create('cannot convert shortint variant to field value');
+      TType.TInt:
+        mIVal := v;
+      TType.TUInt:
+        mIVal := v;
+      TType.TString:
+        mSVal := formatstrf('%s', [v]);
+      else
+        raise Exception.Create('cannot convert integral variant to field value');
+    end;
+  end;
+begin
+  case varType(val) of
+    varEmpty: raise Exception.Create('cannot convert empty variant to field value');
+    varNull: raise Exception.Create('cannot convert null variant to field value');
+    varSingle: raise Exception.Create('cannot convert single variant to field value');
+    varDouble: raise Exception.Create('cannot convert double variant to field value');
+    varDecimal: raise Exception.Create('cannot convert decimal variant to field value');
+    varCurrency: raise Exception.Create('cannot convert currency variant to field value');
+    varDate: raise Exception.Create('cannot convert date variant to field value');
+    varOleStr: raise Exception.Create('cannot convert olestr variant to field value');
+    varStrArg: raise Exception.Create('cannot convert stdarg variant to field value');
+    varString:
+      if (mType = TType.TChar) or (mType = TType.TString) then
+      begin
+        mSVal := val;
+      end
+      else
+      begin
+        raise Exception.Create('cannot convert string variant to field value');
+      end;
+    varDispatch: raise Exception.Create('cannot convert dispatch variant to field value');
+    varBoolean:
+      case mType of
+        TType.TBool,
+        TType.TByte,
+        TType.TUByte,
+        TType.TShort,
+        TType.TUShort,
+        TType.TInt,
+        TType.TUInt:
+          if val then mIVal := 1 else mIVal := 0;
+        TType.TString:
+          if val then mSVal := 'true' else mSVal := 'false';
+        else
+          raise Exception.Create('cannot convert boolean variant to field value');
+      end;
+    varVariant: raise Exception.Create('cannot convert variant variant to field value');
+    varUnknown: raise Exception.Create('cannot convert unknown variant to field value');
+    varByte,
+    varWord,
+    varShortInt,
+    varSmallint,
+    varInteger:
+      setInt32(val);
+    varInt64:
+      if (val < Int64(LongInt($80000000))) or (val > LongInt($7FFFFFFF)) then
+        raise Exception.Create('cannot convert boolean variant to field value')
+      else
+        mIVal := LongInt(val);
+    varLongWord:
+      if (val > LongWord($7FFFFFFF)) then raise Exception.Create('cannot convert longword variant to field value')
+      else setInt32(Integer(val));
+    varQWord: raise Exception.Create('cannot convert uint64 variant to field value');
+    varError: raise Exception.Create('cannot convert error variant to field value');
+    else raise Exception.Create('cannot convert undetermined variant to field value');
+  end;
+  mDefined := true;
+end;
+
+
 // won't work for lists
 function TDynField.isSimpleEqu (fld: TDynField): Boolean;
 begin
@@ -673,8 +857,9 @@ end;
 
 function TDynField.definition (): AnsiString;
 begin
-  result := mPasName+' is '+quoteStr(mName)+' type ';
+  result := quoteStr(mName)+' type ';
   result += getTypeName(mType);
+  if (Length(mAlias) > 0) then result += ' alias '+mAlias;
   if (mMaxDim >= 0) then result += Format('[%d]', [mMaxDim]);
   if (mBinOfs >= 0) then result += Format(' offset %d', [mBinOfs]);
   case mEBS of
@@ -690,51 +875,22 @@ begin
          if (mType = TType.TPoint) then begin if (mAsT) then result += ' as txy' else result += ' as xy'; end
     else if (mType = TType.TSize) then begin if (mAsT) then result += ' as twh' else result += ' as wh'; end;
   end;
-  if mOmitDef then result += ' omitdefault';
+  if mWriteDef then result += ' writedefault';
   if mInternal then result += ' internal';
 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;
   fldtype: AnsiString;
   fldofs: Integer;
   fldrecname: AnsiString;
-  fldpasname: AnsiString;
   asxy, aswh, ast: Boolean;
   ainternal: Boolean;
-  omitdef: Boolean;
+  writedef: Boolean;
   defstr: AnsiString;
-  defint: Integer;
+  defint, defint2: Integer;
   hasdefStr: Boolean;
   hasdefInt: Boolean;
   hasdefId: Boolean;
@@ -742,8 +898,9 @@ var
   lebs: TDynField.TEBS;
   unique: Boolean;
   asmonid: Boolean;
+  defech: AnsiChar;
+  xalias: AnsiString;
 begin
-  fldpasname := '';
   fldname := '';
   fldtype := '';
   fldofs := -1;
@@ -752,9 +909,10 @@ begin
   aswh := false;
   ast := false;
   ainternal := false;
-  omitdef := false;
+  writedef := false;
   defstr := '';
   defint := 0;
+  defint2 := 0;
   hasdefStr := false;
   hasdefInt := false;
   hasdefId := false;
@@ -762,25 +920,36 @@ begin
   asmonid := false;
   lmaxdim := -1;
   lebs := TDynField.TEBS.TNone;
+  xalias := '';
 
-  fldpasname := pr.expectId(); // pascal field name
   // field name
-  pr.expectId('is');
-  fldname := pr.expectStr();
-  // field type
-  pr.expectId('type');
-  fldtype := pr.expectId();
-
-  // fixed-size array?
-  if pr.eatDelim('[') then
-  begin
-    lmaxdim := pr.expectInt();
-    if (lmaxdim < 1) then raise Exception.Create(Format('invalid field ''%s'' array size', [fldname]));
-    pr.expectDelim(']');
-  end;
+  fldname := pr.expectStrOrId();
 
   while (pr.tokType <> pr.TTSemi) do
   begin
+    if pr.eatId('type') then
+    begin
+      if (Length(fldtype) > 0) then raise Exception.Create(Format('duplicate type definition for field ''%s''', [fldname]));
+      // field type
+      fldtype := pr.expectId();
+      // fixed-size array?
+      if pr.eatDelim('[') then
+      begin
+        lmaxdim := pr.expectInt();
+        // arbitrary limits
+        if (lmaxdim < 1) or (lmaxdim > 32768) then raise Exception.Create(Format('invalid field ''%s'' array size', [fldname]));
+        pr.expectDelim(']');
+      end;
+      continue;
+    end;
+
+    if pr.eatId('alias') then
+    begin
+      if (Length(xalias) > 0) then raise Exception.Create(Format('duplicate alias definition for field ''%s''', [fldname]));
+      xalias := pr.expectId();
+      continue;
+    end;
+
     if pr.eatId('offset') then
     begin
       if (fldofs >= 0) then raise Exception.Create(Format('duplicate field ''%s'' offset', [fldname]));
@@ -836,15 +1005,23 @@ begin
             hasdefInt := true;
             defint := pr.expectInt();
           end;
+        pr.TTDelim:
+          begin
+            hasdefInt := true;
+            if pr.eatDelim('[') then defech := ']' else begin pr.expectDelim('('); defech := ')'; end;
+            defint := pr.expectInt();
+            defint2 := pr.expectInt();
+            pr.expectDelim(defech);
+          end;
         else
           raise Exception.Create(Format('field ''%s'' has invalid default', [fldname]));
       end;
       continue;
     end;
 
-    if pr.eatId('omitdefault') then
+    if pr.eatId('writedefault') then
     begin
-      omitdef := true;
+      writedef := true;
       continue;
     end;
 
@@ -854,6 +1031,7 @@ begin
       continue;
     end;
 
+    // record type, no special modifiers
     if (pr.tokType <> pr.TTId) then raise Exception.Create(Format('field ''%s'' has something unexpected in definition', [fldname]));
 
     if (Length(fldrecname) <> 0) then raise Exception.Create(Format('field ''%s'' already typed as ''%s''', [fldname, fldrecname]));
@@ -878,22 +1056,51 @@ begin
   else if (fldtype = 'point') then mType := TType.TPoint
   else if (fldtype = 'size') then mType := TType.TSize
   else if (fldtype = 'trigdata') then mType := TType.TTrigData
-  else raise Exception.Create(Format('field ''%s'' has invalid type ''%s''', [fldname, fldtype]));
+  else
+  begin
+    // record types defaults to int
+    if (Length(fldrecname) > 0) then
+    begin
+      mType := TType.TInt;
+    end
+    else
+    begin
+      if (Length(fldtype) = 0) then raise Exception.Create(Format('field ''%s'' has no type', [fldname]))
+      else raise Exception.Create(Format('field ''%s'' has invalid type ''%s''', [fldname, fldtype]));
+    end;
+  end;
 
+  // check for valid arrays
   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]));
+
+  // check for valid trigdata or record type
   if (mType = TType.TTrigData) then
   begin
-    if (lmaxdim < 1) then raise Exception.Create(Format('field ''%s'' of type ''%s'' cannot be array', [fldname, fldtype]));
-    if (Length(fldrecname) > 0) then raise Exception.Create(Format('field ''%s'' of type ''%s'' cannot have another type', [fldname, fldtype]));
+    // trigdata
+    if (lmaxdim < 1) then raise Exception.Create(Format('field ''%s'' of type ''%s'' cannot be non-array', [fldname, 'trigdata']));
+    if (Length(fldrecname) > 0) then raise Exception.Create(Format('field ''%s'' of type ''%s'' cannot have another type', [fldname, 'trigdata']));
     lebs := TDynField.TEBS.TRec;
+  end
+  else if (Length(fldrecname) > 0) then
+  begin
+    // record
+    if not (mType in [TType.TByte, TType.TUByte, TType.TShort, TType.TUShort, TType.TInt, TType.TUInt]) then
+    begin
+      raise Exception.Create(Format('field ''%s'' of record type ''%s'' cannot have type ''%s''', [fldname, fldrecname, fldtype]));
+    end;
   end;
 
+  // setup default value
        if hasdefStr then self.mDefUnparsed := quoteStr(defstr)
-  else if hasdefInt then self.mDefUnparsed := Format('%d', [defint])
-  else if hasdefId then self.mDefUnparsed := defstr;
+  else if hasdefId then self.mDefUnparsed := defstr
+  else if hasdefInt then
+  begin
+         if (mType = TType.TPoint) then self.mDefUnparsed := Format('(%d %d)', [defint, defint2])
+    else if (mType = TType.TSize) then self.mDefUnparsed := Format('[%d %d]', [defint, defint2])
+    else self.mDefUnparsed := Format('%d', [defint]);
+  end;
 
   self.mHasDefault := (hasdefStr or hasdefId or hasdefInt);
-  self.mPasName := fldpasname;
   self.mEBS := lebs;
   self.mEBSTypeName := fldrecname;
   self.mBitSetUnique := unique;
@@ -902,8 +1109,9 @@ begin
   self.mBinOfs := fldofs;
   self.mSepPosSize := (asxy or aswh);
   self.mAsT := ast;
-  self.mOmitDef := omitdef;
+  self.mWriteDef := writedef;
   self.mInternal := ainternal;
+  self.mAlias := xalias;
 end;
 
 
@@ -1238,15 +1446,11 @@ begin
           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();
+          rc := rc.clone(mOwner.mHeaderRec);
           rc.mHeaderRec := mOwner.mHeaderRec;
-          try
-            rc.parseBinValue(st, true);
-            mRecRef := rc;
-            rc := nil;
-          finally
-            rc.Free();
-          end;
+          // on error, it will be freed be memowner
+          rc.parseBinValue(st, true);
+          mRecRef := rc;
           mDefined := true;
           exit;
         end
@@ -1417,6 +1621,8 @@ var
   tk: AnsiString;
   edim: AnsiChar;
 begin
+  if (pr.tokType = pr.TTEOF) then raise Exception.Create('field value expected');
+  if (pr.tokType = pr.TTSemi) then raise Exception.Create('extra semicolon');
   // if this field should contain struct, convert type and parse struct
   case mEBS of
     TEBS.TNone: begin end;
@@ -1440,16 +1646,12 @@ begin
             if (tfld = nil) then raise Exception.Create(Format('triggerdata value for field ''%s'' in record ''%s'' without ''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();
+            rc := rc.clone(mOwner.mHeaderRec);
             rc.mHeaderRec := mOwner.mHeaderRec;
             //writeln(rc.definition);
-            try
-              rc.parseValue(pr, true);
-              mRecRef := rc;
-              rc := nil;
-            finally
-              rc.Free();
-            end;
+            // on error, it will be freed be memowner
+            rc.parseValue(pr, true);
+            mRecRef := rc;
           end;
           mDefined := true;
           pr.eatTT(pr.TTSemi); // hack: allow (but don't require) semicolon after inline records
@@ -1465,9 +1667,17 @@ begin
           else
           begin
             rec := mOwner.findRecordByTypeId(mEBSTypeName, pr.tokStr);
-            if (rec = nil) then raise Exception.Create(Format('record ''%s'' (%s) value for field ''%s'' not found', [pr.tokStr, mEBSTypeName, mName]));
+            if (rec = nil) then
+            begin
+              //raise Exception.Create(Format('record ''%s'' (%s) value for field ''%s'' not found', [pr.tokStr, mEBSTypeName, mName]));
+              mRecRefId := pr.tokStr;
+            end
+            else
+            begin
+              mRecRef := rec;
+              mRecRefId := '';
+            end;
             pr.expectId();
-            mRecRef := rec;
           end;
           mDefined := true;
           pr.expectTT(pr.TTSemi);
@@ -1479,7 +1689,7 @@ begin
           rec := nil;
           if (mEBSType <> nil) and (mEBSType is TDynRecord) then rec := (mEBSType as TDynRecord);
           if (rec = nil) then raise Exception.Create(Format('record type ''%s'' for field ''%s'' not found', [mEBSTypeName, mName]));
-          rc := rec.clone();
+          rc := rec.clone(mOwner.mHeaderRec);
           rc.mHeaderRec := mOwner.mHeaderRec;
           rc.parseValue(pr);
           mRecRef := rc;
@@ -1676,12 +1886,30 @@ begin
   mHeaderRec := nil;
   mTagInt := 0;
   mTagPtr := nil;
+  mRec2Free := nil;
 end;
 
 
 destructor TDynRecord.Destroy ();
+var
+  fld: TDynField;
+  rec: TDynRecord;
 begin
+  if (mRec2Free <> nil) then
+  begin
+    for rec in mRec2Free do
+    begin
+      if (rec <> self) then
+      begin
+        //writeln(formatstrf('freeing: 0x%08x; name=%s; id=%s', [Pointer(rec), rec.mName, rec.mId]));
+        rec.Free();
+      end;
+    end;
+    mRec2Free.Free();
+    mRec2Free := nil;
+  end;
   mName := '';
+  for fld in mFields do fld.Free();
   mFields.Free();
   mFields := nil;
   {$IF DEFINED(XDYNREC_USE_FIELDHASH)}
@@ -1696,6 +1924,16 @@ begin
 end;
 
 
+procedure TDynRecord.regrec (rec: TDynRecord);
+begin
+  if (rec <> nil) and (rec <> self) then
+  begin
+    if (mRec2Free = nil) then mRec2Free := TDynRecList.Create();
+    mRec2Free.append(rec);
+  end;
+end;
+
+
 procedure TDynRecord.addField (fld: TDynField); inline;
 begin
   if (fld = nil) then raise Exception.Create('cannot append nil field to record');
@@ -1791,7 +2029,7 @@ begin
 end;
 
 
-function TDynRecord.clone (): TDynRecord;
+function TDynRecord.clone (registerIn: TDynRecord): TDynRecord;
 var
   fld: TDynField;
   f: Integer;
@@ -1799,21 +2037,21 @@ begin
   result := TDynRecord.Create();
   result.mOwner := mOwner;
   result.mId := mId;
-  result.mPasName := mPasName;
   result.mName := mName;
   result.mSize := mSize;
+  result.mHeader := mHeader;
+  result.mBinBlock := mBinBlock;
+  result.mHeaderRec := mHeaderRec;
+  result.mTagInt := mTagInt;
+  result.mTagPtr := mTagPtr;
   if (mFields.count > 0) then
   begin
     result.mFields.capacity := mFields.count;
-    for fld in mFields do result.addField(fld.clone(result));
+    for fld in mFields do result.addField(fld.clone(result, registerIn));
   end;
   SetLength(result.mTrigTypes, Length(mTrigTypes));
   for f := 0 to High(mTrigTypes) do result.mTrigTypes[f] := mTrigTypes[f];
-  result.mHeader := mHeader;
-  result.mBinBlock := mBinBlock;
-  result.mHeaderRec := mHeaderRec;
-  result.mTagInt := mTagInt;
-  result.mTagPtr := mTagPtr;
+  if (registerIn <> nil) then registerIn.regrec(result);
 end;
 
 
@@ -1927,6 +2165,35 @@ begin
 end;
 
 
+function TDynRecord.getUserVar (const aname: AnsiString): Variant;
+var
+  fld: TDynField;
+begin
+  fld := getFieldByName(aname);
+  if (fld = nil) then result := Unassigned else result := fld.varvalue;
+end;
+
+
+procedure TDynRecord.setUserVar (const aname: AnsiString; val: Variant);
+var
+  fld: TDynField;
+begin
+  fld := getFieldByName(aname);
+  if (fld = nil) then
+  begin
+    if (Length(aname) = 0) then raise Exception.Create('cannot create nameless user field');
+    fld := TDynField.Create(aname, val);
+    fld.mOwner := self;
+    fld.mInternal := true;
+    addField(fld);
+  end
+  else
+  begin
+    fld.varvalue := val;
+  end;
+end;
+
+
 procedure TDynRecord.parseDef (pr: TTextParser);
 var
   fld: TDynField;
@@ -1957,9 +2224,7 @@ begin
   end
   else
   begin
-    mPasName := pr.expectId(); // pascal record name
-    pr.expectId('is');
-    mName := pr.expectStr();
+    mName := pr.expectStrOrId();
     while (pr.tokType <> pr.TTBegin) do
     begin
       if pr.eatId('header') then begin mHeader := true; continue; end;
@@ -2000,30 +2265,6 @@ 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;
@@ -2050,7 +2291,7 @@ begin
   else
   begin
     // record
-    result := mPasName+' is '+quoteStr(mName);
+    result := quoteStr(mName);
     if (mSize >= 0) then result += Format(' size %d bytes', [mSize]);
     if mHeader then result += ' header';
   end;
@@ -2159,7 +2400,7 @@ begin
             for f := 0 to (bsize div rec.mSize)-1 do
             begin
               mst.setup(buf+f*rec.mSize, rec.mSize);
-              rec := rect.clone();
+              rec := rect.clone(self);
               rec.mHeaderRec := self;
               rec.parseBinValue(mst);
               rec.mId := Format('%s%d', [rec.mName, f]);
@@ -2348,7 +2589,7 @@ begin
         continue;
       end;
       if fld.mInternal then continue;
-      if fld.mOmitDef and fld.isDefaultValue then continue;
+      if (not fld.mWriteDef) and fld.isDefaultValue then continue;
       wr.putIndent();
       fld.writeTo(wr);
     end;
@@ -2394,6 +2635,40 @@ var
   {$IF DEFINED(D2D_DYNREC_PROFILER)}
   stt, stall: UInt64;
   {$ENDIF}
+
+  procedure linkNames (rec: TDynRecord);
+  var
+    fld: TDynField;
+    rt: TDynRecord;
+  begin
+    //writeln('*** rec: ', rec.mName, '.', rec.mId, ' (', rec.mFields.count, ')');
+    for fld in rec.mFields do
+    begin
+      if (fld.mType = TDynField.TType.TTrigData) then
+      begin
+        if (fld.mRecRef <> nil) then linkNames(fld.mRecRef);
+        continue;
+      end;
+      if (Length(fld.mRecRefId) = 0) then continue;
+      assert(fld.mEBSType <> nil);
+      rt := findRecordByTypeId(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;
+      fld.mDefined := true;
+    end;
+    for fld in rec.mFields do
+    begin
+      //writeln('  ', fld.mName);
+      fld.fixDefaultValue(); // just in case
+    end;
+  end;
+
 begin
   if (mOwner = nil) then raise Exception.Create(Format('can''t parse record ''%s'' value without owner', [mName]));
 
@@ -2427,49 +2702,49 @@ begin
       if (trc <> nil) then
       begin
         {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
-        rec := trc.clone();
+        rec := trc.clone(mHeaderRec);
         {$IF DEFINED(D2D_DYNREC_PROFILER)}profCloneRec := curTimeMicro()-stt;{$ENDIF}
         rec.mHeaderRec := mHeaderRec;
-        try
-          pr.skipToken();
-          rec.parseValue(pr);
+        // on error, it will be freed be memowner
+        pr.skipToken();
+        rec.parseValue(pr);
+        (*
+        if (Length(rec.mId) > 0) then
+        begin
+          {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
+          fld := field[pr.tokStr];
+          {$IF DEFINED(D2D_DYNREC_PROFILER)}profFieldSearching := curTimeMicro()-stt;{$ENDIF}
           (*
-          if (Length(rec.mId) > 0) then
+          if (fld <> nil) and (fld.mRVal <> nil) then
           begin
             {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
-            fld := field[pr.tokStr];
-            {$IF DEFINED(D2D_DYNREC_PROFILER)}profFieldSearching := curTimeMicro()-stt;{$ENDIF}
-            (*
-            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 fld.mRHash.has(rec.mId) then raise Exception.Create(Format('duplicate thing ''%s'' in record ''%s''', [fld.mName, mName]));
-              {$IF DEFINED(D2D_DYNREC_PROFILER)}profListDupChecking := curTimeMicro()-stt;{$ENDIF}
-            end;
+            //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 fld.mRHash.has(rec.mId) then raise Exception.Create(Format('duplicate thing ''%s'' in record ''%s''', [fld.mName, mName]));
+            {$IF DEFINED(D2D_DYNREC_PROFILER)}profListDupChecking := curTimeMicro()-stt;{$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;
+        *)
+        {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
+        addRecordByType(rec.mName, rec);
+        {$IF DEFINED(D2D_DYNREC_PROFILER)}profAddRecByType := curTimeMicro()-stt;{$ENDIF}
         continue;
       end;
     end;
 
     // fields
     {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
+    //writeln('0: <', mName, '.', pr.tokStr, '>');
     fld := field[pr.tokStr];
+    //writeln('1: <', mName, '.', pr.tokStr, '>');
     {$IF DEFINED(D2D_DYNREC_PROFILER)}profFieldSearching := curTimeMicro()-stt;{$ENDIF}
     if (fld <> nil) then
     begin
+      //writeln('2: <', mName, '.', pr.tokStr, '>');
       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();
+      pr.skipToken(); // skip field name
+      //writeln('3: <', mName, '.', pr.tokStr, '>:', pr.tokType);
       {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
       fld.parseValue(pr);
       {$IF DEFINED(D2D_DYNREC_PROFILER)}profFieldValParsing := curTimeMicro()-stt;{$ENDIF}
@@ -2480,6 +2755,17 @@ begin
     raise Exception.Create(Format('unknown field ''%s'' in record ''%s''', [pr.tokStr, mName]));
   end;
   pr.expectTT(pr.TTEnd);
+
+  if mHeader then
+  begin
+    // link fields
+    for fld in mFields do
+    begin
+      if (fld.mType <> TDynField.TType.TList) then continue;
+      for rec in fld.mRVal do linkNames(rec);
+    end;
+  end;
+
   // fix field defaults
   {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
   for fld in mFields do fld.fixDefaultValue();
@@ -2690,6 +2976,7 @@ var
   rec: TDynRecord;
   ebs: TDynEBS;
 begin
+  //!!!FIXME!!! check who owns trigs and recs!
   for rec in recTypes do rec.Free();
   for rec in trigTypes do rec.Free();
   for ebs in ebsTypes do ebs.Free();
@@ -2792,37 +3079,41 @@ begin
   while true do
   begin
     if not pr.skipBlanks() then break;
-    if (pr.tokType <> pr.TTId) then raise Exception.Create('identifier expected');
 
-    if (pr.tokStr = 'enum') or (pr.tokStr = 'bitset') then
+    if (pr.tokType = pr.TTId) then
     begin
-      eb := TDynEBS.Create(pr);
-      if (findEBSType(eb.name) <> nil) then
+      // enum or bitset
+      if (pr.tokStr = 'enum') or (pr.tokStr = 'bitset') then
       begin
-        eb.Free();
-        raise Exception.Create(Format('duplicate enum/bitset ''%s''', [eb.name]));
+        eb := TDynEBS.Create(pr);
+        if (findEBSType(eb.name) <> nil) then
+        begin
+          eb.Free();
+          raise Exception.Create(Format('duplicate enum/bitset ''%s''', [eb.name]));
+        end;
+        eb.mOwner := self;
+        ebsTypes.append(eb);
+        //writeln(eb.definition); writeln;
+        continue;
       end;
-      eb.mOwner := self;
-      ebsTypes.append(eb);
-      //writeln(eb.definition); writeln;
-      continue;
-    end;
 
-    if (pr.tokStr = 'TriggerData') then
-    begin
-      rec := TDynRecord.Create(pr);
-      for f := 0 to High(rec.mTrigTypes) do
+      // triggerdata
+      if (pr.tokStr = 'TriggerData') then
       begin
-        if (findTrigFor(rec.mTrigTypes[f]) <> nil) then
+        rec := TDynRecord.Create(pr);
+        for f := 0 to High(rec.mTrigTypes) do
         begin
-          rec.Free();
-          raise Exception.Create(Format('duplicate trigdata ''%s''', [rec.mTrigTypes[f]]));
+          if (findTrigFor(rec.mTrigTypes[f]) <> nil) then
+          begin
+            rec.Free();
+            raise Exception.Create(Format('duplicate trigdata ''%s''', [rec.mTrigTypes[f]]));
+          end;
         end;
+        rec.mOwner := self;
+        trigTypes.append(rec);
+        //writeln(dr.definition); writeln;
+        continue;
       end;
-      rec.mOwner := self;
-      trigTypes.append(rec);
-      //writeln(dr.definition); writeln;
-      continue;
     end;
 
     rec := TDynRecord.Create(pr);
@@ -2865,7 +3156,7 @@ begin
   result := nil;
   try
     pr.expectId(headerType.name);
-    res := headerType.clone();
+    res := headerType.clone(nil);
     res.mHeaderRec := res;
     res.parseValue(pr);
     result := res;
@@ -2882,7 +3173,7 @@ var
 begin
   result := nil;
   try
-    res := headerType.clone();
+    res := headerType.clone(nil);
     res.mHeaderRec := res;
     res.parseBinValue(st);
     result := res;
@@ -2893,53 +3184,6 @@ begin
 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
-      if fld.mInternal then continue;
-      if (fld.mBinOfs < 0) then continue;
-      result += '        '+fld.pasdef+#10;
-    end;
-    result += '      );'#10;
-  end;
-  result += '  end;'#10;
-end;
-
-
 function TDynMapDef.pasdefconst (): AnsiString;
 var
   ebs: TDynEBS;