DEADSOFTWARE

slightly better `traceBox()`
[d2df-sdl.git] / src / shared / xdynrec.pas
index e1bd571235d2ba9be15abe8d1e8e1480df4d22a6..fe9c5616f346c30fe79c9ba24b19193763a9e9c2 100644 (file)
@@ -1420,13 +1420,9 @@ begin
           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(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
@@ -1597,6 +1593,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;
@@ -1623,13 +1621,9 @@ begin
             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
@@ -1645,9 +1639,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);
@@ -1867,7 +1869,14 @@ var
 begin
   if (mRec2Free <> nil) then
   begin
-    for rec in mRec2Free do if (rec <> self) then rec.Free();
+    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;
@@ -2625,6 +2634,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]));
 
@@ -2661,46 +2704,46 @@ begin
         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}
@@ -2711,6 +2754,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();
@@ -2921,6 +2975,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();
@@ -3092,6 +3147,7 @@ end;
 function TDynMapDef.parseMap (pr: TTextParser): TDynRecord;
 var
   res: TDynRecord = nil;
+  //fo: TextFile;
 begin
   result := nil;
   try
@@ -3103,6 +3159,17 @@ begin
     res := nil;
   finally
     res.Free();
+  {
+  except on e: Exception do
+    begin
+      //TMP:segfaults!
+      AssignFile(fo, 'z.log');
+      Rewrite(fo);
+      DumpExceptionBackTrace(fo);
+      CloseFile(fo);
+      res.Free();
+    end;
+  }
   end;
 end;